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 "CompileCommands.h"
12 #include "Config.h"
13 #include "TestFS.h"
14 #include "support/Path.h"
15 #include "support/ThreadsafeFS.h"
16 #include "clang/Tooling/CompilationDatabase.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/FormatVariadic.h"
22 #include "llvm/Support/Path.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include <chrono>
26 #include <fstream>
27 #include <string>
28
29 namespace clang {
30 namespace clangd {
31 namespace {
32 using ::testing::AllOf;
33 using ::testing::Contains;
34 using ::testing::ElementsAre;
35 using ::testing::EndsWith;
36 using ::testing::HasSubstr;
37 using ::testing::IsEmpty;
38 using ::testing::Not;
39 using ::testing::UnorderedElementsAre;
40
TEST(GlobalCompilationDatabaseTest,FallbackCommand)41 TEST(GlobalCompilationDatabaseTest, FallbackCommand) {
42 MockFS TFS;
43 DirectoryBasedGlobalCompilationDatabase DB(TFS);
44 auto Cmd = DB.getFallbackCommand(testPath("foo/bar.cc"));
45 EXPECT_EQ(Cmd.Directory, testPath("foo"));
46 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", testPath("foo/bar.cc")));
47 EXPECT_EQ(Cmd.Output, "");
48
49 // .h files have unknown language, so they are parsed liberally as obj-c++.
50 Cmd = DB.getFallbackCommand(testPath("foo/bar.h"));
51 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-xobjective-c++-header",
52 testPath("foo/bar.h")));
53 Cmd = DB.getFallbackCommand(testPath("foo/bar"));
54 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-xobjective-c++-header",
55 testPath("foo/bar")));
56 }
57
cmd(llvm::StringRef File,llvm::StringRef Arg)58 static tooling::CompileCommand cmd(llvm::StringRef File, llvm::StringRef Arg) {
59 return tooling::CompileCommand(
60 testRoot(), File, {"clang", std::string(Arg), std::string(File)}, "");
61 }
62
63 class OverlayCDBTest : public ::testing::Test {
64 class BaseCDB : public GlobalCompilationDatabase {
65 public:
66 llvm::Optional<tooling::CompileCommand>
getCompileCommand(llvm::StringRef File) const67 getCompileCommand(llvm::StringRef File) const override {
68 if (File == testPath("foo.cc"))
69 return cmd(File, "-DA=1");
70 return None;
71 }
72
73 tooling::CompileCommand
getFallbackCommand(llvm::StringRef File) const74 getFallbackCommand(llvm::StringRef File) const override {
75 return cmd(File, "-DA=2");
76 }
77
getProjectInfo(PathRef File) const78 llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override {
79 return ProjectInfo{testRoot()};
80 }
81 };
82
83 protected:
OverlayCDBTest()84 OverlayCDBTest() : Base(std::make_unique<BaseCDB>()) {}
85 std::unique_ptr<GlobalCompilationDatabase> Base;
86 };
87
TEST_F(OverlayCDBTest,GetCompileCommand)88 TEST_F(OverlayCDBTest, GetCompileCommand) {
89 OverlayCDB CDB(Base.get());
90 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
91 AllOf(Contains(testPath("foo.cc")), Contains("-DA=1")));
92 EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None);
93
94 auto Override = cmd(testPath("foo.cc"), "-DA=3");
95 CDB.setCompileCommand(testPath("foo.cc"), Override);
96 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
97 Contains("-DA=3"));
98 EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None);
99 CDB.setCompileCommand(testPath("missing.cc"), Override);
100 EXPECT_THAT(CDB.getCompileCommand(testPath("missing.cc"))->CommandLine,
101 Contains("-DA=3"));
102 }
103
TEST_F(OverlayCDBTest,GetFallbackCommand)104 TEST_F(OverlayCDBTest, GetFallbackCommand) {
105 OverlayCDB CDB(Base.get(), {"-DA=4"});
106 EXPECT_THAT(CDB.getFallbackCommand(testPath("bar.cc")).CommandLine,
107 ElementsAre("clang", "-DA=2", testPath("bar.cc"), "-DA=4"));
108 }
109
TEST_F(OverlayCDBTest,NoBase)110 TEST_F(OverlayCDBTest, NoBase) {
111 OverlayCDB CDB(nullptr, {"-DA=6"});
112 EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), None);
113 auto Override = cmd(testPath("bar.cc"), "-DA=5");
114 CDB.setCompileCommand(testPath("bar.cc"), Override);
115 EXPECT_THAT(CDB.getCompileCommand(testPath("bar.cc"))->CommandLine,
116 Contains("-DA=5"));
117
118 EXPECT_THAT(CDB.getFallbackCommand(testPath("foo.cc")).CommandLine,
119 ElementsAre("clang", testPath("foo.cc"), "-DA=6"));
120 }
121
TEST_F(OverlayCDBTest,Watch)122 TEST_F(OverlayCDBTest, Watch) {
123 OverlayCDB Inner(nullptr);
124 OverlayCDB Outer(&Inner);
125
126 std::vector<std::vector<std::string>> Changes;
127 auto Sub = Outer.watch([&](const std::vector<std::string> &ChangedFiles) {
128 Changes.push_back(ChangedFiles);
129 });
130
131 Inner.setCompileCommand("A.cpp", tooling::CompileCommand());
132 Outer.setCompileCommand("B.cpp", tooling::CompileCommand());
133 Inner.setCompileCommand("A.cpp", llvm::None);
134 Outer.setCompileCommand("C.cpp", llvm::None);
135 EXPECT_THAT(Changes, ElementsAre(ElementsAre("A.cpp"), ElementsAre("B.cpp"),
136 ElementsAre("A.cpp"), ElementsAre("C.cpp")));
137 }
138
TEST_F(OverlayCDBTest,Adjustments)139 TEST_F(OverlayCDBTest, Adjustments) {
140 OverlayCDB CDB(Base.get(), {"-DFallback"},
141 [](const std::vector<std::string> &Cmd, llvm::StringRef File) {
142 auto Ret = Cmd;
143 Ret.push_back(
144 ("-DAdjust_" + llvm::sys::path::filename(File)).str());
145 return Ret;
146 });
147 // Command from underlying gets adjusted.
148 auto Cmd = *CDB.getCompileCommand(testPath("foo.cc"));
149 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-DA=1", testPath("foo.cc"),
150 "-DAdjust_foo.cc"));
151
152 // Command from overlay gets adjusted.
153 tooling::CompileCommand BarCommand;
154 BarCommand.Filename = testPath("bar.cc");
155 BarCommand.CommandLine = {"clang++", "-DB=1", testPath("bar.cc")};
156 CDB.setCompileCommand(testPath("bar.cc"), BarCommand);
157 Cmd = *CDB.getCompileCommand(testPath("bar.cc"));
158 EXPECT_THAT(
159 Cmd.CommandLine,
160 ElementsAre("clang++", "-DB=1", testPath("bar.cc"), "-DAdjust_bar.cc"));
161
162 // Fallback gets adjusted.
163 Cmd = CDB.getFallbackCommand("baz.cc");
164 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-DA=2", "baz.cc",
165 "-DFallback", "-DAdjust_baz.cc"));
166 }
167
TEST(GlobalCompilationDatabaseTest,DiscoveryWithNestedCDBs)168 TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
169 const char *const CDBOuter =
170 R"cdb(
171 [
172 {
173 "file": "a.cc",
174 "command": "",
175 "directory": "{0}",
176 },
177 {
178 "file": "build/gen.cc",
179 "command": "",
180 "directory": "{0}",
181 },
182 {
183 "file": "build/gen2.cc",
184 "command": "",
185 "directory": "{0}",
186 }
187 ]
188 )cdb";
189 const char *const CDBInner =
190 R"cdb(
191 [
192 {
193 "file": "gen.cc",
194 "command": "",
195 "directory": "{0}/build",
196 }
197 ]
198 )cdb";
199 MockFS FS;
200 FS.Files[testPath("compile_commands.json")] =
201 llvm::formatv(CDBOuter, llvm::sys::path::convert_to_slash(testRoot()));
202 FS.Files[testPath("build/compile_commands.json")] =
203 llvm::formatv(CDBInner, llvm::sys::path::convert_to_slash(testRoot()));
204 FS.Files[testPath("foo/compile_flags.txt")] = "-DFOO";
205
206 // Note that gen2.cc goes missing with our following model, not sure this
207 // happens in practice though.
208 {
209 SCOPED_TRACE("Default ancestor scanning");
210 DirectoryBasedGlobalCompilationDatabase DB(FS);
211 std::vector<std::string> DiscoveredFiles;
212 auto Sub =
213 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
214 DiscoveredFiles = Changes;
215 });
216
217 DB.getCompileCommand(testPath("build/../a.cc"));
218 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
219 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(AllOf(
220 EndsWith("a.cc"), Not(HasSubstr("..")))));
221 DiscoveredFiles.clear();
222
223 DB.getCompileCommand(testPath("build/gen.cc"));
224 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
225 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(EndsWith("gen.cc")));
226 }
227
228 {
229 SCOPED_TRACE("With config");
230 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
231 Opts.ContextProvider = [&](llvm::StringRef Path) {
232 Config Cfg;
233 if (Path.endswith("a.cc")) {
234 // a.cc uses another directory's CDB, so it won't be discovered.
235 Cfg.CompileFlags.CDBSearch.Policy = Config::CDBSearchSpec::FixedDir;
236 Cfg.CompileFlags.CDBSearch.FixedCDBPath = testPath("foo");
237 } else if (Path.endswith("gen.cc")) {
238 // gen.cc has CDB search disabled, so it won't be discovered.
239 Cfg.CompileFlags.CDBSearch.Policy = Config::CDBSearchSpec::NoCDBSearch;
240 } else if (Path.endswith("gen2.cc")) {
241 // gen2.cc explicitly lists this directory, so it will be discovered.
242 Cfg.CompileFlags.CDBSearch.Policy = Config::CDBSearchSpec::FixedDir;
243 Cfg.CompileFlags.CDBSearch.FixedCDBPath = testRoot();
244 }
245 return Context::current().derive(Config::Key, std::move(Cfg));
246 };
247 DirectoryBasedGlobalCompilationDatabase DB(Opts);
248 std::vector<std::string> DiscoveredFiles;
249 auto Sub =
250 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
251 DiscoveredFiles = Changes;
252 });
253
254 // Does not use the root CDB, so no broadcast.
255 auto Cmd = DB.getCompileCommand(testPath("build/../a.cc"));
256 ASSERT_TRUE(Cmd);
257 EXPECT_THAT(Cmd->CommandLine, Contains("-DFOO")) << "a.cc uses foo/ CDB";
258 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
259 EXPECT_THAT(DiscoveredFiles, IsEmpty()) << "Root CDB not discovered yet";
260
261 // No special config for b.cc, so we trigger broadcast of the root CDB.
262 DB.getCompileCommand(testPath("b.cc"));
263 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
264 EXPECT_THAT(DiscoveredFiles, ElementsAre(testPath("build/gen2.cc")));
265 DiscoveredFiles.clear();
266
267 // No CDB search so no discovery/broadcast triggered for build/ CDB.
268 DB.getCompileCommand(testPath("build/gen.cc"));
269 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
270 EXPECT_THAT(DiscoveredFiles, IsEmpty());
271 }
272
273 {
274 SCOPED_TRACE("With custom compile commands dir");
275 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
276 Opts.CompileCommandsDir = testRoot();
277 DirectoryBasedGlobalCompilationDatabase DB(Opts);
278 std::vector<std::string> DiscoveredFiles;
279 auto Sub =
280 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
281 DiscoveredFiles = Changes;
282 });
283
284 DB.getCompileCommand(testPath("a.cc"));
285 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
286 EXPECT_THAT(DiscoveredFiles,
287 UnorderedElementsAre(EndsWith("a.cc"), EndsWith("gen.cc"),
288 EndsWith("gen2.cc")));
289 DiscoveredFiles.clear();
290
291 DB.getCompileCommand(testPath("build/gen.cc"));
292 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
293 EXPECT_THAT(DiscoveredFiles, IsEmpty());
294 }
295 }
296
TEST(GlobalCompilationDatabaseTest,BuildDir)297 TEST(GlobalCompilationDatabaseTest, BuildDir) {
298 MockFS FS;
299 auto Command = [&](llvm::StringRef Relative) {
300 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
301 return DirectoryBasedGlobalCompilationDatabase(Opts)
302 .getCompileCommand(testPath(Relative))
303 .value_or(tooling::CompileCommand())
304 .CommandLine;
305 };
306 EXPECT_THAT(Command("x/foo.cc"), IsEmpty());
307 const char *const CDB =
308 R"cdb(
309 [
310 {
311 "file": "{0}/x/foo.cc",
312 "command": "clang -DXYZZY {0}/x/foo.cc",
313 "directory": "{0}",
314 },
315 {
316 "file": "{0}/bar.cc",
317 "command": "clang -DXYZZY {0}/bar.cc",
318 "directory": "{0}",
319 }
320 ]
321 )cdb";
322 FS.Files[testPath("x/build/compile_commands.json")] =
323 llvm::formatv(CDB, llvm::sys::path::convert_to_slash(testRoot()));
324 EXPECT_THAT(Command("x/foo.cc"), Contains("-DXYZZY"));
325 EXPECT_THAT(Command("bar.cc"), IsEmpty())
326 << "x/build/compile_flags.json only applicable to x/";
327 }
328
TEST(GlobalCompilationDatabaseTest,CompileFlagsDirectory)329 TEST(GlobalCompilationDatabaseTest, CompileFlagsDirectory) {
330 MockFS FS;
331 FS.Files[testPath("x/compile_flags.txt")] = "-DFOO";
332 DirectoryBasedGlobalCompilationDatabase CDB(FS);
333 auto Commands = CDB.getCompileCommand(testPath("x/y.cpp"));
334 ASSERT_TRUE(Commands.has_value());
335 EXPECT_THAT(Commands.value().CommandLine, Contains("-DFOO"));
336 // Make sure we pick the right working directory.
337 EXPECT_EQ(testPath("x"), Commands.value().Directory);
338 }
339
340 MATCHER_P(hasArg, Flag, "") {
341 if (!arg) {
342 *result_listener << "command is null";
343 return false;
344 }
345 if (!llvm::is_contained(arg->CommandLine, Flag)) {
346 *result_listener << "flags are " << printArgv(arg->CommandLine);
347 return false;
348 }
349 return true;
350 }
351
TEST(GlobalCompilationDatabaseTest,Config)352 TEST(GlobalCompilationDatabaseTest, Config) {
353 MockFS FS;
354 FS.Files[testPath("x/compile_flags.txt")] = "-DX";
355 FS.Files[testPath("x/y/z/compile_flags.txt")] = "-DZ";
356
357 Config::CDBSearchSpec Spec;
358 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
359 Opts.ContextProvider = [&](llvm::StringRef Path) {
360 Config C;
361 C.CompileFlags.CDBSearch = Spec;
362 return Context::current().derive(Config::Key, std::move(C));
363 };
364 DirectoryBasedGlobalCompilationDatabase CDB(Opts);
365
366 // Default ancestor behavior.
367 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
368 EXPECT_THAT(CDB.getCompileCommand(testPath("x/foo.cc")), hasArg("-DX"));
369 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/foo.cc")), hasArg("-DX"));
370 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/z/foo.cc")), hasArg("-DZ"));
371
372 Spec.Policy = Config::CDBSearchSpec::NoCDBSearch;
373 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
374 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/foo.cc")));
375 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/foo.cc")));
376 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/z/foo.cc")));
377
378 Spec.Policy = Config::CDBSearchSpec::FixedDir;
379 Spec.FixedCDBPath = testPath("w"); // doesn't exist
380 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
381 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/foo.cc")));
382 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/foo.cc")));
383 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/z/foo.cc")));
384
385 Spec.FixedCDBPath = testPath("x/y/z");
386 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc")), hasArg("-DZ"));
387 EXPECT_THAT(CDB.getCompileCommand(testPath("x/foo.cc")), hasArg("-DZ"));
388 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/foo.cc")), hasArg("-DZ"));
389 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/z/foo.cc")), hasArg("-DZ"));
390 }
391
TEST(GlobalCompilationDatabaseTest,NonCanonicalFilenames)392 TEST(GlobalCompilationDatabaseTest, NonCanonicalFilenames) {
393 OverlayCDB DB(nullptr);
394 std::vector<std::string> DiscoveredFiles;
395 auto Sub =
396 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
397 DiscoveredFiles = Changes;
398 });
399
400 llvm::SmallString<128> Root(testRoot());
401 llvm::sys::path::append(Root, "build", "..", "a.cc");
402 DB.setCompileCommand(Root.str(), tooling::CompileCommand());
403 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(testPath("a.cc")));
404 DiscoveredFiles.clear();
405
406 llvm::SmallString<128> File(testRoot());
407 llvm::sys::path::append(File, "blabla", "..", "a.cc");
408
409 EXPECT_TRUE(DB.getCompileCommand(File));
410 EXPECT_FALSE(DB.getProjectInfo(File));
411 }
412
TEST_F(OverlayCDBTest,GetProjectInfo)413 TEST_F(OverlayCDBTest, GetProjectInfo) {
414 OverlayCDB DB(Base.get());
415 Path File = testPath("foo.cc");
416 Path Header = testPath("foo.h");
417
418 EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot());
419 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot());
420
421 // Shouldn't change after an override.
422 DB.setCompileCommand(File, tooling::CompileCommand());
423 EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot());
424 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot());
425 }
426 } // namespace
427
428 // Friend test has access to internals.
429 class DirectoryBasedGlobalCompilationDatabaseCacheTest
430 : public ::testing::Test {
431 protected:
432 std::shared_ptr<const tooling::CompilationDatabase>
lookupCDB(const DirectoryBasedGlobalCompilationDatabase & GDB,llvm::StringRef Path,std::chrono::steady_clock::time_point FreshTime)433 lookupCDB(const DirectoryBasedGlobalCompilationDatabase &GDB,
434 llvm::StringRef Path,
435 std::chrono::steady_clock::time_point FreshTime) {
436 DirectoryBasedGlobalCompilationDatabase::CDBLookupRequest Req;
437 Req.FileName = Path;
438 Req.FreshTime = Req.FreshTimeMissing = FreshTime;
439 if (auto Result = GDB.lookupCDB(Req))
440 return std::move(Result->CDB);
441 return nullptr;
442 }
443 };
444
445 // Matches non-null CDBs which include the specified flag.
446 MATCHER_P2(hasFlag, Flag, Path, "") {
447 if (arg == nullptr)
448 return false;
449 auto Cmds = arg->getCompileCommands(Path);
450 if (Cmds.empty()) {
451 *result_listener << "yields no commands";
452 return false;
453 }
454 if (!llvm::is_contained(Cmds.front().CommandLine, Flag)) {
455 *result_listener << "flags are: " << printArgv(Cmds.front().CommandLine);
456 return false;
457 }
458 return true;
459 }
460
hasFlag(llvm::StringRef Flag)461 auto hasFlag(llvm::StringRef Flag) {
462 return hasFlag(Flag, "mock_file_name.cc");
463 }
464
TEST_F(DirectoryBasedGlobalCompilationDatabaseCacheTest,Cacheable)465 TEST_F(DirectoryBasedGlobalCompilationDatabaseCacheTest, Cacheable) {
466 MockFS FS;
467 auto Stale = std::chrono::steady_clock::now() - std::chrono::minutes(1);
468 auto Fresh = std::chrono::steady_clock::now() + std::chrono::hours(24);
469
470 DirectoryBasedGlobalCompilationDatabase GDB(FS);
471 FS.Files["compile_flags.txt"] = "-DROOT";
472 auto Root = lookupCDB(GDB, testPath("foo/test.cc"), Stale);
473 EXPECT_THAT(Root, hasFlag("-DROOT"));
474
475 // Add a compilation database to a subdirectory - CDB loaded.
476 FS.Files["foo/compile_flags.txt"] = "-DFOO";
477 EXPECT_EQ(Root, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
478 << "cache still valid";
479 auto Foo = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
480 EXPECT_THAT(Foo, hasFlag("-DFOO")) << "new cdb loaded";
481 EXPECT_EQ(Foo, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
482 << "new cdb in cache";
483
484 // Mtime changed, but no content change - CDB not reloaded.
485 ++FS.Timestamps["foo/compile_flags.txt"];
486 auto FooAgain = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
487 EXPECT_EQ(Foo, FooAgain) << "Same content, read but not reloaded";
488 // Content changed, but not size or mtime - CDB not reloaded.
489 FS.Files["foo/compile_flags.txt"] = "-DBAR";
490 auto FooAgain2 = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
491 EXPECT_EQ(Foo, FooAgain2) << "Same filesize, change not detected";
492 // Mtime change forces a re-read, and we notice the different content.
493 ++FS.Timestamps["foo/compile_flags.txt"];
494 auto Bar = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
495 EXPECT_THAT(Bar, hasFlag("-DBAR")) << "refreshed with mtime change";
496
497 // Size and content both change - CDB reloaded.
498 FS.Files["foo/compile_flags.txt"] = "-DFOOBAR";
499 EXPECT_EQ(Bar, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
500 << "cache still valid";
501 auto FooBar = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
502 EXPECT_THAT(FooBar, hasFlag("-DFOOBAR")) << "cdb reloaded";
503
504 // compile_commands.json takes precedence over compile_flags.txt.
505 FS.Files["foo/compile_commands.json"] =
506 llvm::formatv(R"json([{
507 "file": "{0}/foo/mock_file.cc",
508 "command": "clang -DBAZ mock_file.cc",
509 "directory": "{0}/foo",
510 }])json",
511 llvm::sys::path::convert_to_slash(testRoot()));
512 EXPECT_EQ(FooBar, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
513 << "cache still valid";
514 auto Baz = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
515 EXPECT_THAT(Baz, hasFlag("-DBAZ", testPath("foo/mock_file.cc")))
516 << "compile_commands overrides compile_flags";
517
518 // Removing compile_commands.json reveals compile_flags.txt again.
519 // However this *does* cause a CDB reload (we cache only one CDB per dir).
520 FS.Files.erase("foo/compile_commands.json");
521 auto FoobarAgain = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
522 EXPECT_THAT(FoobarAgain, hasFlag("-DFOOBAR")) << "reloaded compile_flags";
523 EXPECT_NE(FoobarAgain, FooBar) << "CDB discarded (shadowed within directory)";
524
525 // Removing the directory's CDB leaves the parent CDB active.
526 // The parent CDB is *not* reloaded (we cache the CDB per-directory).
527 FS.Files.erase("foo/compile_flags.txt");
528 EXPECT_EQ(Root, lookupCDB(GDB, testPath("foo/test.cc"), Fresh))
529 << "CDB retained (shadowed by another directory)";
530 }
531
532 } // namespace clangd
533 } // namespace clang
534