[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clangd / unittests / GlobalCompilationDatabaseTests.cpp
blob38bf66a1e25eb57025d765eddd0d518c593e0e0b
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 //===----------------------------------------------------------------------===//
9 #include "GlobalCompilationDatabase.h"
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/STLExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/FormatVariadic.h"
21 #include "llvm/Support/Path.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include <chrono>
25 #include <fstream>
26 #include <optional>
27 #include <string>
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;
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, "");
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")));
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)}, "");
63 class OverlayCDBTest : public ::testing::Test {
64 class BaseCDB : public GlobalCompilationDatabase {
65 public:
66 std::optional<tooling::CompileCommand>
67 getCompileCommand(llvm::StringRef File) const override {
68 if (File == testPath("foo.cc"))
69 return cmd(File, "-DA=1");
70 return std::nullopt;
73 tooling::CompileCommand
74 getFallbackCommand(llvm::StringRef File) const override {
75 return cmd(File, "-DA=2");
78 std::optional<ProjectInfo> getProjectInfo(PathRef File) const override {
79 return ProjectInfo{testRoot()};
83 protected:
84 OverlayCDBTest() : Base(std::make_unique<BaseCDB>()) {}
85 std::unique_ptr<GlobalCompilationDatabase> Base;
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")), std::nullopt);
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")), std::nullopt);
99 CDB.setCompileCommand(testPath("missing.cc"), Override);
100 EXPECT_THAT(CDB.getCompileCommand(testPath("missing.cc"))->CommandLine,
101 Contains("-DA=3"));
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"));
110 TEST_F(OverlayCDBTest, NoBase) {
111 OverlayCDB CDB(nullptr, {"-DA=6"});
112 EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), std::nullopt);
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"));
118 EXPECT_THAT(CDB.getFallbackCommand(testPath("foo.cc")).CommandLine,
119 ElementsAre("clang", testPath("foo.cc"), "-DA=6"));
122 TEST_F(OverlayCDBTest, Watch) {
123 OverlayCDB Inner(nullptr);
124 OverlayCDB Outer(&Inner);
126 std::vector<std::vector<std::string>> Changes;
127 auto Sub = Outer.watch([&](const std::vector<std::string> &ChangedFiles) {
128 Changes.push_back(ChangedFiles);
131 Inner.setCompileCommand("A.cpp", tooling::CompileCommand());
132 Outer.setCompileCommand("B.cpp", tooling::CompileCommand());
133 Inner.setCompileCommand("A.cpp", std::nullopt);
134 Outer.setCompileCommand("C.cpp", std::nullopt);
135 EXPECT_THAT(Changes, ElementsAre(ElementsAre("A.cpp"), ElementsAre("B.cpp"),
136 ElementsAre("A.cpp"), ElementsAre("C.cpp")));
139 TEST_F(OverlayCDBTest, Adjustments) {
140 OverlayCDB CDB(Base.get(), {"-DFallback"},
141 [](tooling::CompileCommand &Cmd, llvm::StringRef File) {
142 Cmd.CommandLine.push_back(
143 ("-DAdjust_" + llvm::sys::path::filename(File)).str());
145 // Command from underlying gets adjusted.
146 auto Cmd = *CDB.getCompileCommand(testPath("foo.cc"));
147 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-DA=1", testPath("foo.cc"),
148 "-DAdjust_foo.cc"));
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"));
156 EXPECT_THAT(
157 Cmd.CommandLine,
158 ElementsAre("clang++", "-DB=1", testPath("bar.cc"), "-DAdjust_bar.cc"));
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"));
166 TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
167 const char *const CDBOuter =
168 R"cdb(
171 "file": "a.cc",
172 "command": "",
173 "directory": "{0}",
176 "file": "build/gen.cc",
177 "command": "",
178 "directory": "{0}",
181 "file": "build/gen2.cc",
182 "command": "",
183 "directory": "{0}",
186 )cdb";
187 const char *const CDBInner =
188 R"cdb(
191 "file": "gen.cc",
192 "command": "",
193 "directory": "{0}/build",
196 )cdb";
197 MockFS FS;
198 FS.Files[testPath("compile_commands.json")] =
199 llvm::formatv(CDBOuter, llvm::sys::path::convert_to_slash(testRoot()));
200 FS.Files[testPath("build/compile_commands.json")] =
201 llvm::formatv(CDBInner, llvm::sys::path::convert_to_slash(testRoot()));
202 FS.Files[testPath("foo/compile_flags.txt")] = "-DFOO";
204 // Note that gen2.cc goes missing with our following model, not sure this
205 // happens in practice though.
207 SCOPED_TRACE("Default ancestor scanning");
208 DirectoryBasedGlobalCompilationDatabase DB(FS);
209 std::vector<std::string> DiscoveredFiles;
210 auto Sub =
211 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
212 DiscoveredFiles = Changes;
215 DB.getCompileCommand(testPath("build/../a.cc"));
216 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
217 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(AllOf(
218 EndsWith("a.cc"), Not(HasSubstr("..")))));
219 DiscoveredFiles.clear();
221 DB.getCompileCommand(testPath("build/gen.cc"));
222 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
223 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(EndsWith("gen.cc")));
227 SCOPED_TRACE("With config");
228 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
229 Opts.ContextProvider = [&](llvm::StringRef Path) {
230 Config Cfg;
231 if (Path.endswith("a.cc")) {
232 // a.cc uses another directory's CDB, so it won't be discovered.
233 Cfg.CompileFlags.CDBSearch.Policy = Config::CDBSearchSpec::FixedDir;
234 Cfg.CompileFlags.CDBSearch.FixedCDBPath = testPath("foo");
235 } else if (Path.endswith("gen.cc")) {
236 // gen.cc has CDB search disabled, so it won't be discovered.
237 Cfg.CompileFlags.CDBSearch.Policy = Config::CDBSearchSpec::NoCDBSearch;
238 } else if (Path.endswith("gen2.cc")) {
239 // gen2.cc explicitly lists this directory, so it will be discovered.
240 Cfg.CompileFlags.CDBSearch.Policy = Config::CDBSearchSpec::FixedDir;
241 Cfg.CompileFlags.CDBSearch.FixedCDBPath = testRoot();
243 return Context::current().derive(Config::Key, std::move(Cfg));
245 DirectoryBasedGlobalCompilationDatabase DB(Opts);
246 std::vector<std::string> DiscoveredFiles;
247 auto Sub =
248 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
249 DiscoveredFiles = Changes;
252 // Does not use the root CDB, so no broadcast.
253 auto Cmd = DB.getCompileCommand(testPath("build/../a.cc"));
254 ASSERT_TRUE(Cmd);
255 EXPECT_THAT(Cmd->CommandLine, Contains("-DFOO")) << "a.cc uses foo/ CDB";
256 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
257 EXPECT_THAT(DiscoveredFiles, IsEmpty()) << "Root CDB not discovered yet";
259 // No special config for b.cc, so we trigger broadcast of the root CDB.
260 DB.getCompileCommand(testPath("b.cc"));
261 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
262 EXPECT_THAT(DiscoveredFiles, ElementsAre(testPath("build/gen2.cc")));
263 DiscoveredFiles.clear();
265 // No CDB search so no discovery/broadcast triggered for build/ CDB.
266 DB.getCompileCommand(testPath("build/gen.cc"));
267 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
268 EXPECT_THAT(DiscoveredFiles, IsEmpty());
272 SCOPED_TRACE("With custom compile commands dir");
273 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
274 Opts.CompileCommandsDir = testRoot();
275 DirectoryBasedGlobalCompilationDatabase DB(Opts);
276 std::vector<std::string> DiscoveredFiles;
277 auto Sub =
278 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
279 DiscoveredFiles = Changes;
282 DB.getCompileCommand(testPath("a.cc"));
283 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
284 EXPECT_THAT(DiscoveredFiles,
285 UnorderedElementsAre(EndsWith("a.cc"), EndsWith("gen.cc"),
286 EndsWith("gen2.cc")));
287 DiscoveredFiles.clear();
289 DB.getCompileCommand(testPath("build/gen.cc"));
290 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
291 EXPECT_THAT(DiscoveredFiles, IsEmpty());
295 TEST(GlobalCompilationDatabaseTest, BuildDir) {
296 MockFS FS;
297 auto Command = [&](llvm::StringRef Relative) {
298 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
299 return DirectoryBasedGlobalCompilationDatabase(Opts)
300 .getCompileCommand(testPath(Relative))
301 .value_or(tooling::CompileCommand())
302 .CommandLine;
304 EXPECT_THAT(Command("x/foo.cc"), IsEmpty());
305 const char *const CDB =
306 R"cdb(
309 "file": "{0}/x/foo.cc",
310 "command": "clang -DXYZZY {0}/x/foo.cc",
311 "directory": "{0}",
314 "file": "{0}/bar.cc",
315 "command": "clang -DXYZZY {0}/bar.cc",
316 "directory": "{0}",
319 )cdb";
320 FS.Files[testPath("x/build/compile_commands.json")] =
321 llvm::formatv(CDB, llvm::sys::path::convert_to_slash(testRoot()));
322 EXPECT_THAT(Command("x/foo.cc"), Contains("-DXYZZY"));
323 EXPECT_THAT(Command("bar.cc"), IsEmpty())
324 << "x/build/compile_flags.json only applicable to x/";
327 TEST(GlobalCompilationDatabaseTest, CompileFlagsDirectory) {
328 MockFS FS;
329 FS.Files[testPath("x/compile_flags.txt")] = "-DFOO";
330 DirectoryBasedGlobalCompilationDatabase CDB(FS);
331 auto Commands = CDB.getCompileCommand(testPath("x/y.cpp"));
332 ASSERT_TRUE(Commands.has_value());
333 EXPECT_THAT(Commands->CommandLine, Contains("-DFOO"));
334 // Make sure we pick the right working directory.
335 EXPECT_EQ(testPath("x"), Commands->Directory);
338 MATCHER_P(hasArg, Flag, "") {
339 if (!arg) {
340 *result_listener << "command is null";
341 return false;
343 if (!llvm::is_contained(arg->CommandLine, Flag)) {
344 *result_listener << "flags are " << printArgv(arg->CommandLine);
345 return false;
347 return true;
350 TEST(GlobalCompilationDatabaseTest, Config) {
351 MockFS FS;
352 FS.Files[testPath("x/compile_flags.txt")] = "-DX";
353 FS.Files[testPath("x/y/z/compile_flags.txt")] = "-DZ";
355 Config::CDBSearchSpec Spec;
356 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
357 Opts.ContextProvider = [&](llvm::StringRef Path) {
358 Config C;
359 C.CompileFlags.CDBSearch = Spec;
360 return Context::current().derive(Config::Key, std::move(C));
362 DirectoryBasedGlobalCompilationDatabase CDB(Opts);
364 // Default ancestor behavior.
365 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
366 EXPECT_THAT(CDB.getCompileCommand(testPath("x/foo.cc")), hasArg("-DX"));
367 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/foo.cc")), hasArg("-DX"));
368 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/z/foo.cc")), hasArg("-DZ"));
370 Spec.Policy = Config::CDBSearchSpec::NoCDBSearch;
371 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
372 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/foo.cc")));
373 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/foo.cc")));
374 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/z/foo.cc")));
376 Spec.Policy = Config::CDBSearchSpec::FixedDir;
377 Spec.FixedCDBPath = testPath("w"); // doesn't exist
378 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
379 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/foo.cc")));
380 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/foo.cc")));
381 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/z/foo.cc")));
383 Spec.FixedCDBPath = testPath("x/y/z");
384 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc")), hasArg("-DZ"));
385 EXPECT_THAT(CDB.getCompileCommand(testPath("x/foo.cc")), hasArg("-DZ"));
386 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/foo.cc")), hasArg("-DZ"));
387 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/z/foo.cc")), hasArg("-DZ"));
390 TEST(GlobalCompilationDatabaseTest, NonCanonicalFilenames) {
391 OverlayCDB DB(nullptr);
392 std::vector<std::string> DiscoveredFiles;
393 auto Sub =
394 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
395 DiscoveredFiles = Changes;
398 llvm::SmallString<128> Root(testRoot());
399 llvm::sys::path::append(Root, "build", "..", "a.cc");
400 DB.setCompileCommand(Root.str(), tooling::CompileCommand());
401 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(testPath("a.cc")));
402 DiscoveredFiles.clear();
404 llvm::SmallString<128> File(testRoot());
405 llvm::sys::path::append(File, "blabla", "..", "a.cc");
407 EXPECT_TRUE(DB.getCompileCommand(File));
408 EXPECT_FALSE(DB.getProjectInfo(File));
411 TEST_F(OverlayCDBTest, GetProjectInfo) {
412 OverlayCDB DB(Base.get());
413 Path File = testPath("foo.cc");
414 Path Header = testPath("foo.h");
416 EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot());
417 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot());
419 // Shouldn't change after an override.
420 DB.setCompileCommand(File, tooling::CompileCommand());
421 EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot());
422 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot());
424 } // namespace
426 // Friend test has access to internals.
427 class DirectoryBasedGlobalCompilationDatabaseCacheTest
428 : public ::testing::Test {
429 protected:
430 std::shared_ptr<const tooling::CompilationDatabase>
431 lookupCDB(const DirectoryBasedGlobalCompilationDatabase &GDB,
432 llvm::StringRef Path,
433 std::chrono::steady_clock::time_point FreshTime) {
434 DirectoryBasedGlobalCompilationDatabase::CDBLookupRequest Req;
435 Req.FileName = Path;
436 Req.FreshTime = Req.FreshTimeMissing = FreshTime;
437 if (auto Result = GDB.lookupCDB(Req))
438 return std::move(Result->CDB);
439 return nullptr;
443 // Matches non-null CDBs which include the specified flag.
444 MATCHER_P2(hasFlag, Flag, Path, "") {
445 if (arg == nullptr)
446 return false;
447 auto Cmds = arg->getCompileCommands(Path);
448 if (Cmds.empty()) {
449 *result_listener << "yields no commands";
450 return false;
452 if (!llvm::is_contained(Cmds.front().CommandLine, Flag)) {
453 *result_listener << "flags are: " << printArgv(Cmds.front().CommandLine);
454 return false;
456 return true;
459 auto hasFlag(llvm::StringRef Flag) {
460 return hasFlag(Flag, "mock_file_name.cc");
463 TEST_F(DirectoryBasedGlobalCompilationDatabaseCacheTest, Cacheable) {
464 MockFS FS;
465 auto Stale = std::chrono::steady_clock::now() - std::chrono::minutes(1);
466 auto Fresh = std::chrono::steady_clock::now() + std::chrono::hours(24);
468 DirectoryBasedGlobalCompilationDatabase GDB(FS);
469 FS.Files["compile_flags.txt"] = "-DROOT";
470 auto Root = lookupCDB(GDB, testPath("foo/test.cc"), Stale);
471 EXPECT_THAT(Root, hasFlag("-DROOT"));
473 // Add a compilation database to a subdirectory - CDB loaded.
474 FS.Files["foo/compile_flags.txt"] = "-DFOO";
475 EXPECT_EQ(Root, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
476 << "cache still valid";
477 auto Foo = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
478 EXPECT_THAT(Foo, hasFlag("-DFOO")) << "new cdb loaded";
479 EXPECT_EQ(Foo, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
480 << "new cdb in cache";
482 // Mtime changed, but no content change - CDB not reloaded.
483 ++FS.Timestamps["foo/compile_flags.txt"];
484 auto FooAgain = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
485 EXPECT_EQ(Foo, FooAgain) << "Same content, read but not reloaded";
486 // Content changed, but not size or mtime - CDB not reloaded.
487 FS.Files["foo/compile_flags.txt"] = "-DBAR";
488 auto FooAgain2 = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
489 EXPECT_EQ(Foo, FooAgain2) << "Same filesize, change not detected";
490 // Mtime change forces a re-read, and we notice the different content.
491 ++FS.Timestamps["foo/compile_flags.txt"];
492 auto Bar = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
493 EXPECT_THAT(Bar, hasFlag("-DBAR")) << "refreshed with mtime change";
495 // Size and content both change - CDB reloaded.
496 FS.Files["foo/compile_flags.txt"] = "-DFOOBAR";
497 EXPECT_EQ(Bar, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
498 << "cache still valid";
499 auto FooBar = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
500 EXPECT_THAT(FooBar, hasFlag("-DFOOBAR")) << "cdb reloaded";
502 // compile_commands.json takes precedence over compile_flags.txt.
503 FS.Files["foo/compile_commands.json"] =
504 llvm::formatv(R"json([{
505 "file": "{0}/foo/mock_file.cc",
506 "command": "clang -DBAZ mock_file.cc",
507 "directory": "{0}/foo",
508 }])json",
509 llvm::sys::path::convert_to_slash(testRoot()));
510 EXPECT_EQ(FooBar, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
511 << "cache still valid";
512 auto Baz = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
513 EXPECT_THAT(Baz, hasFlag("-DBAZ", testPath("foo/mock_file.cc")))
514 << "compile_commands overrides compile_flags";
516 // Removing compile_commands.json reveals compile_flags.txt again.
517 // However this *does* cause a CDB reload (we cache only one CDB per dir).
518 FS.Files.erase("foo/compile_commands.json");
519 auto FoobarAgain = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
520 EXPECT_THAT(FoobarAgain, hasFlag("-DFOOBAR")) << "reloaded compile_flags";
521 EXPECT_NE(FoobarAgain, FooBar) << "CDB discarded (shadowed within directory)";
523 // Removing the directory's CDB leaves the parent CDB active.
524 // The parent CDB is *not* reloaded (we cache the CDB per-directory).
525 FS.Files.erase("foo/compile_flags.txt");
526 EXPECT_EQ(Root, lookupCDB(GDB, testPath("foo/test.cc"), Fresh))
527 << "CDB retained (shadowed by another directory)";
530 } // namespace clangd
531 } // namespace clang