clang-tools 10.0.0

GlobalCompilationDatabaseTests.cpp
Go to the documentation of this file.
1 //===-- GlobalCompilationDatabaseTests.cpp ----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "GlobalCompilationDatabase.h"
10
11 #include "Path.h"
12 #include "TestFS.h"
13 #include "clang/Tooling/CompilationDatabase.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/FormatVariadic.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include <fstream>
26 #include <string>
27
28 namespace clang {
29 namespace clangd {
30 namespace {
31 using ::testing::AllOf;
32 using ::testing::Contains;
33 using ::testing::ElementsAre;
34 using ::testing::EndsWith;
35 using ::testing::HasSubstr;
36 using ::testing::IsEmpty;
37 using ::testing::Not;
38 using ::testing::StartsWith;
39 using ::testing::UnorderedElementsAre;
40
41 TEST(GlobalCompilationDatabaseTest, FallbackCommand) {
42 DirectoryBasedGlobalCompilationDatabase DB(None);
43 auto Cmd = DB.getFallbackCommand(testPath("foo/bar.cc"));
44 EXPECT_EQ(Cmd.Directory, testPath("foo"));
45 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", testPath("foo/bar.cc")));
46 EXPECT_EQ(Cmd.Output, "");
47
48 // .h files have unknown language, so they are parsed liberally as obj-c++.
49 Cmd = DB.getFallbackCommand(testPath("foo/bar.h"));
50 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-xobjective-c++-header",
51 testPath("foo/bar.h")));
52 Cmd = DB.getFallbackCommand(testPath("foo/bar"));
53 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-xobjective-c++-header",
54 testPath("foo/bar")));
55 }
56
57 static tooling::CompileCommand cmd(llvm::StringRef File, llvm::StringRef Arg) {
58 return tooling::CompileCommand(testRoot(), File, {"clang", Arg, File}, "");
59 }
60
61 class OverlayCDBTest : public ::testing::Test {
62 class BaseCDB : public GlobalCompilationDatabase {
63 public:
64 llvm::Optional<tooling::CompileCommand>
65 getCompileCommand(llvm::StringRef File) const override {
66 if (File == testPath("foo.cc"))
67 return cmd(File, "-DA=1");
68 return None;
69 }
70
71 tooling::CompileCommand
72 getFallbackCommand(llvm::StringRef File) const override {
73 return cmd(File, "-DA=2");
74 }
75
76 llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override {
77 return ProjectInfo{testRoot()};
78 }
79 };
80
81 protected:
82 OverlayCDBTest() : Base(std::make_unique<BaseCDB>()) {}
83 std::unique_ptr<GlobalCompilationDatabase> Base;
84 };
85
86 TEST_F(OverlayCDBTest, GetCompileCommand) {
87 OverlayCDB CDB(Base.get());
88 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
89 AllOf(Contains(testPath("foo.cc")), Contains("-DA=1")));
90 EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None);
91
92 auto Override = cmd(testPath("foo.cc"), "-DA=3");
93 CDB.setCompileCommand(testPath("foo.cc"), Override);
94 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
95 Contains("-DA=3"));
96 EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None);
97 CDB.setCompileCommand(testPath("missing.cc"), Override);
98 EXPECT_THAT(CDB.getCompileCommand(testPath("missing.cc"))->CommandLine,
99 Contains("-DA=3"));
100 }
101
102 TEST_F(OverlayCDBTest, GetFallbackCommand) {
103 OverlayCDB CDB(Base.get(), {"-DA=4"});
104 EXPECT_THAT(CDB.getFallbackCommand(testPath("bar.cc")).CommandLine,
105 ElementsAre("clang", "-DA=2", testPath("bar.cc"), "-DA=4"));
106 }
107
108 TEST_F(OverlayCDBTest, NoBase) {
109 OverlayCDB CDB(nullptr, {"-DA=6"});
110 EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), None);
111 auto Override = cmd(testPath("bar.cc"), "-DA=5");
112 CDB.setCompileCommand(testPath("bar.cc"), Override);
113 EXPECT_THAT(CDB.getCompileCommand(testPath("bar.cc"))->CommandLine,
114 Contains("-DA=5"));
115
116 EXPECT_THAT(CDB.getFallbackCommand(testPath("foo.cc")).CommandLine,
117 ElementsAre("clang", testPath("foo.cc"), "-DA=6"));
118 }
119
120 TEST_F(OverlayCDBTest, Watch) {
121 OverlayCDB Inner(nullptr);
122 OverlayCDB Outer(&Inner);
123
124 std::vector<std::vector<std::string>> Changes;
125 auto Sub = Outer.watch([&](const std::vector<std::string> &ChangedFiles) {
126 Changes.push_back(ChangedFiles);
127 });
128
129 Inner.setCompileCommand("A.cpp", tooling::CompileCommand());
130 Outer.setCompileCommand("B.cpp", tooling::CompileCommand());
131 Inner.setCompileCommand("A.cpp", llvm::None);
132 Outer.setCompileCommand("C.cpp", llvm::None);
133 EXPECT_THAT(Changes, ElementsAre(ElementsAre("A.cpp"), ElementsAre("B.cpp"),
134 ElementsAre("A.cpp"), ElementsAre("C.cpp")));
135 }
136
137 TEST_F(OverlayCDBTest, Adjustments) {
138 OverlayCDB CDB(Base.get(), {"-DFallback"},
139 [](const std::vector<std::string> &Cmd, llvm::StringRef File) {
140 auto Ret = Cmd;
141 Ret.push_back(
142 ("-DAdjust_" + llvm::sys::path::filename(File)).str());
143 return Ret;
144 });
145 // Command from underlying gets adjusted.
146 auto Cmd = CDB.getCompileCommand(testPath("foo.cc")).getValue();
147 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-DA=1", testPath("foo.cc"),
148 "-DAdjust_foo.cc"));
149
150 // Command from overlay gets adjusted.
151 tooling::CompileCommand BarCommand;
152 BarCommand.Filename = testPath("bar.cc");
153 BarCommand.CommandLine = {"clang++", "-DB=1", testPath("bar.cc")};
154 CDB.setCompileCommand(testPath("bar.cc"), BarCommand);
155 Cmd = CDB.getCompileCommand(testPath("bar.cc")).getValue();
156 EXPECT_THAT(
157 Cmd.CommandLine,
158 ElementsAre("clang++", "-DB=1", testPath("bar.cc"), "-DAdjust_bar.cc"));
159
160 // Fallback gets adjusted.
161 Cmd = CDB.getFallbackCommand("baz.cc");
162 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-DA=2", "baz.cc",
163 "-DFallback", "-DAdjust_baz.cc"));
164 }
165
166 TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
167 const char *const CDBOuter =
168 R"cdb(
169 [
170 {
171 "file": "a.cc",
172 "command": "",
173 "directory": "{0}",
174 },
175 {
176 "file": "build/gen.cc",
177 "command": "",
178 "directory": "{0}",
179 },
180 {
181 "file": "build/gen2.cc",
182 "command": "",
183 "directory": "{0}",
184 }
185 ]
186 )cdb";
187 const char *const CDBInner =
188 R"cdb(
189 [
190 {
191 "file": "gen.cc",
192 "command": "",
193 "directory": "{0}/build",
194 }
195 ]
196 )cdb";
197 class CleaningFS {
198 public:
199 llvm::SmallString<128> Root;
200
201 CleaningFS() {
202 EXPECT_FALSE(
203 llvm::sys::fs::createUniqueDirectory("clangd-cdb-test", Root))
204 << "Failed to create unique directory";
205 }
206
207 ~CleaningFS() {
208 EXPECT_FALSE(llvm::sys::fs::remove_directories(Root))
209 << "Failed to cleanup " << Root;
210 }
211
212 void registerFile(PathRef RelativePath, llvm::StringRef Contents) {
213 llvm::SmallString<128> AbsPath(Root);
214 llvm::sys::path::append(AbsPath, RelativePath);
215
216 EXPECT_FALSE(llvm::sys::fs::create_directories(
217 llvm::sys::path::parent_path(AbsPath)))
218 << "Failed to create directories for: " << AbsPath;
219
220 std::error_code EC;
221 llvm::raw_fd_ostream OS(AbsPath, EC);
222 EXPECT_FALSE(EC) << "Failed to open " << AbsPath << " for writing";
223 OS << llvm::formatv(Contents.data(),
224 llvm::sys::path::convert_to_slash(Root));
225 OS.close();
226
227 EXPECT_FALSE(OS.has_error());
228 }
229 };
230
231 CleaningFS FS;
232 FS.registerFile("compile_commands.json", CDBOuter);
233 FS.registerFile("build/compile_commands.json", CDBInner);
234 llvm::SmallString<128> File;
235
236 // Note that gen2.cc goes missing with our following model, not sure this
237 // happens in practice though.
238 {
239 DirectoryBasedGlobalCompilationDatabase DB(llvm::None);
240 std::vector<std::string> DiscoveredFiles;
241 auto Sub =
242 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
243 DiscoveredFiles = Changes;
244 });
245
246 File = FS.Root;
247 llvm::sys::path::append(File, "build", "..", "a.cc");
248 DB.getCompileCommand(File.str());
249 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(AllOf(
250 EndsWith("a.cc"), Not(HasSubstr("..")))));
251 DiscoveredFiles.clear();
252
253 File = FS.Root;
254 llvm::sys::path::append(File, "build", "gen.cc");
255 DB.getCompileCommand(File.str());
256 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(EndsWith("gen.cc")));
257 }
258
259 // With a custom compile commands dir.
260 {
261 DirectoryBasedGlobalCompilationDatabase DB(FS.Root.str().str());
262 std::vector<std::string> DiscoveredFiles;
263 auto Sub =
264 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
265 DiscoveredFiles = Changes;
266 });
267
268 File = FS.Root;
269 llvm::sys::path::append(File, "a.cc");
270 DB.getCompileCommand(File.str());
271 EXPECT_THAT(DiscoveredFiles,
272 UnorderedElementsAre(EndsWith("a.cc"), EndsWith("gen.cc"),
273 EndsWith("gen2.cc")));
274 DiscoveredFiles.clear();
275
276 File = FS.Root;
277 llvm::sys::path::append(File, "build", "gen.cc");
278 DB.getCompileCommand(File.str());
279 EXPECT_THAT(DiscoveredFiles, IsEmpty());
280 }
281 }
282
283 TEST(GlobalCompilationDatabaseTest, NonCanonicalFilenames) {
284 OverlayCDB DB(nullptr);
285 std::vector<std::string> DiscoveredFiles;
286 auto Sub =
287 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
288 DiscoveredFiles = Changes;
289 });
290
291 llvm::SmallString<128> Root(testRoot());
292 llvm::sys::path::append(Root, "build", "..", "a.cc");
293 DB.setCompileCommand(Root.str(), tooling::CompileCommand());
294 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(testPath("a.cc")));
295 DiscoveredFiles.clear();
296
297 llvm::SmallString<128> File(testRoot());
298 llvm::sys::path::append(File, "blabla", "..", "a.cc");
299
300 EXPECT_TRUE(DB.getCompileCommand(File));
301 EXPECT_TRUE(DB.getProjectInfo(File));
302 }
303
304 } // namespace
305 } // namespace clangd
306 } // namespace clang
Contents
llvm::StringRef Contents
Definition: CodeComplete.cpp:1021
Changes
tooling::Replacements Changes
Definition: Format.cpp:108
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
FS
MockFSProvider FS
Definition: HeadersTests.cpp:119
clang::clangd::TextDocumentSyncKind::None
Documents should not be synced at all.
TestFS.h
GlobalCompilationDatabase.h
clang::clangd::TEST_F
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
Definition: BackgroundIndexTests.cpp:88
clang::clangd::TEST
TEST(BackgroundQueueTest, Priority)
Definition: BackgroundIndexTests.cpp:647
clang::clangd::testPath
std::string testPath(PathRef File)
Definition: TestFS.cpp:82
clang::clangd::testRoot
const char * testRoot()
Definition: TestFS.cpp:74
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Path.h
Base
std::unique_ptr< GlobalCompilationDatabase > Base
Definition: GlobalCompilationDatabaseTests.cpp:83
clang::clangd::CompletionItemKind::File