clang-tools 22.0.0git
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
10
11#include "CompileCommands.h"
12#include "Config.h"
13#include "TestFS.h"
14#include "support/Path.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>
28
29namespace clang {
30namespace clangd {
31namespace {
32using ::testing::AllOf;
33using ::testing::Contains;
34using ::testing::ElementsAre;
35using ::testing::EndsWith;
36using ::testing::HasSubstr;
37using ::testing::IsEmpty;
38using ::testing::Not;
39using ::testing::UnorderedElementsAre;
40
41TEST(GlobalCompilationDatabaseTest, FallbackCommand) {
42 MockFS 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
58TEST(GlobalCompilationDatabaseTest, FallbackWorkingDirectory) {
59 MockFS TFS;
61 CDBOpts.applyFallbackWorkingDirectory(testPath("foo"));
62 EXPECT_EQ(CDBOpts.FallbackWorkingDirectory, testPath("foo"));
63
65 auto Cmd = DB.getFallbackCommand(testPath("foo/src/bar.cc"));
66 EXPECT_EQ(Cmd.Directory, testPath("foo"));
67 EXPECT_THAT(Cmd.CommandLine,
68 ElementsAre("clang", testPath("foo/src/bar.cc")));
69 EXPECT_EQ(Cmd.Output, "");
70}
71
72static tooling::CompileCommand cmd(llvm::StringRef File, llvm::StringRef Arg) {
73 return tooling::CompileCommand(
74 testRoot(), File, {"clang", std::string(Arg), std::string(File)}, "");
75}
76
77class OverlayCDBTest : public ::testing::Test {
78 class BaseCDB : public GlobalCompilationDatabase {
79 public:
80 std::optional<tooling::CompileCommand>
81 getCompileCommand(llvm::StringRef File) const override {
82 if (File == testPath("foo.cc"))
83 return cmd(File, "-DA=1");
84 return std::nullopt;
85 }
86
87 tooling::CompileCommand
88 getFallbackCommand(llvm::StringRef File) const override {
89 return cmd(File, "-DA=2");
90 }
91
92 std::optional<ProjectInfo> getProjectInfo(PathRef File) const override {
93 return ProjectInfo{testRoot()};
94 }
95 };
96
97protected:
98 OverlayCDBTest() : Base(std::make_unique<BaseCDB>()) {}
99 std::unique_ptr<GlobalCompilationDatabase> Base;
100};
101
102TEST_F(OverlayCDBTest, GetCompileCommand) {
103 OverlayCDB CDB(Base.get());
104 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
105 AllOf(Contains(testPath("foo.cc")), Contains("-DA=1")));
106 EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), std::nullopt);
107
108 auto Override = cmd(testPath("foo.cc"), "-DA=3");
109 EXPECT_TRUE(CDB.setCompileCommand(testPath("foo.cc"), Override));
110 EXPECT_FALSE(CDB.setCompileCommand(testPath("foo.cc"), Override));
111 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
112 Contains("-DA=3"));
113 EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), std::nullopt);
114 EXPECT_TRUE(CDB.setCompileCommand(testPath("missing.cc"), Override));
115 EXPECT_FALSE(CDB.setCompileCommand(testPath("missing.cc"), Override));
116 EXPECT_THAT(CDB.getCompileCommand(testPath("missing.cc"))->CommandLine,
117 Contains("-DA=3"));
118}
119
120TEST_F(OverlayCDBTest, GetFallbackCommand) {
121 OverlayCDB CDB(Base.get(), {"-DA=4"});
122 EXPECT_THAT(CDB.getFallbackCommand(testPath("bar.cc")).CommandLine,
123 ElementsAre("clang", "-DA=2", testPath("bar.cc"), "-DA=4"));
124}
125
126TEST_F(OverlayCDBTest, NoBase) {
127 OverlayCDB CDB(nullptr, {"-DA=6"});
128 EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), std::nullopt);
129 auto Override = cmd(testPath("bar.cc"), "-DA=5");
130 EXPECT_TRUE(CDB.setCompileCommand(testPath("bar.cc"), Override));
131 EXPECT_THAT(CDB.getCompileCommand(testPath("bar.cc"))->CommandLine,
132 Contains("-DA=5"));
133
134 EXPECT_THAT(CDB.getFallbackCommand(testPath("foo.cc")).CommandLine,
135 ElementsAre("clang", testPath("foo.cc"), "-DA=6"));
136}
137
138TEST_F(OverlayCDBTest, Watch) {
139 OverlayCDB Inner(nullptr);
140 OverlayCDB Outer(&Inner);
141
142 std::vector<std::vector<std::string>> Changes;
143 auto Sub = Outer.watch([&](const std::vector<std::string> &ChangedFiles) {
144 Changes.push_back(ChangedFiles);
145 });
146
147 EXPECT_TRUE(Inner.setCompileCommand("A.cpp", tooling::CompileCommand()));
148 EXPECT_TRUE(Outer.setCompileCommand("B.cpp", tooling::CompileCommand()));
149 EXPECT_TRUE(Inner.setCompileCommand("A.cpp", std::nullopt));
150 EXPECT_TRUE(Outer.setCompileCommand("C.cpp", std::nullopt));
151 EXPECT_THAT(Changes, ElementsAre(ElementsAre("A.cpp"), ElementsAre("B.cpp"),
152 ElementsAre("A.cpp"), ElementsAre("C.cpp")));
153}
154
155TEST_F(OverlayCDBTest, Adjustments) {
156 OverlayCDB CDB(Base.get(), {"-DFallback"},
157 [](tooling::CompileCommand &Cmd, llvm::StringRef File) {
158 Cmd.CommandLine.push_back(
159 ("-DAdjust_" + llvm::sys::path::filename(File)).str());
160 });
161 // Command from underlying gets adjusted.
162 auto Cmd = *CDB.getCompileCommand(testPath("foo.cc"));
163 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-DA=1", testPath("foo.cc"),
164 "-DAdjust_foo.cc"));
165
166 // Command from overlay gets adjusted.
167 tooling::CompileCommand BarCommand;
168 BarCommand.Filename = testPath("bar.cc");
169 BarCommand.CommandLine = {"clang++", "-DB=1", testPath("bar.cc")};
170 EXPECT_TRUE(CDB.setCompileCommand(testPath("bar.cc"), BarCommand));
171 Cmd = *CDB.getCompileCommand(testPath("bar.cc"));
172 EXPECT_THAT(
173 Cmd.CommandLine,
174 ElementsAre("clang++", "-DB=1", testPath("bar.cc"), "-DAdjust_bar.cc"));
175
176 // Fallback gets adjusted.
177 Cmd = CDB.getFallbackCommand("baz.cc");
178 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-DA=2", "baz.cc",
179 "-DFallback", "-DAdjust_baz.cc"));
180}
181
182TEST_F(OverlayCDBTest, ExpandedResponseFiles) {
183 SmallString<1024> Path;
184 int FD;
185 ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("args", "", FD, Path));
186 llvm::raw_fd_ostream OutStream(FD, true);
187 OutStream << "-Wall";
188 OutStream.close();
189
190 OverlayCDB CDB(Base.get(), {"-DFallback"});
191 auto Override = cmd(testPath("foo.cc"), ("@" + Path).str());
192 CDB.setCompileCommand(testPath("foo.cc"), Override);
193 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
194 Contains("-Wall"));
195}
196
197TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
198 const char *const CDBOuter =
199 R"cdb(
200 [
201 {
202 "file": "a.cc",
203 "command": "",
204 "directory": "{0}",
205 },
206 {
207 "file": "build/gen.cc",
208 "command": "",
209 "directory": "{0}",
210 },
211 {
212 "file": "build/gen2.cc",
213 "command": "",
214 "directory": "{0}",
215 }
216 ]
217 )cdb";
218 const char *const CDBInner =
219 R"cdb(
220 [
221 {
222 "file": "gen.cc",
223 "command": "",
224 "directory": "{0}/build",
225 }
226 ]
227 )cdb";
228 MockFS FS;
229 FS.Files[testPath("compile_commands.json")] =
230 llvm::formatv(CDBOuter, llvm::sys::path::convert_to_slash(testRoot()));
231 FS.Files[testPath("build/compile_commands.json")] =
232 llvm::formatv(CDBInner, llvm::sys::path::convert_to_slash(testRoot()));
233 FS.Files[testPath("foo/compile_flags.txt")] = "-DFOO";
234
235 // Note that gen2.cc goes missing with our following model, not sure this
236 // happens in practice though.
237 {
238 SCOPED_TRACE("Default ancestor scanning");
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 DB.getCompileCommand(testPath("build/../a.cc"));
247 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
248 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(AllOf(
249 EndsWith("a.cc"), Not(HasSubstr("..")))));
250 DiscoveredFiles.clear();
251
252 DB.getCompileCommand(testPath("build/gen.cc"));
253 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
254 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(EndsWith("gen.cc")));
255 }
256
257 {
258 SCOPED_TRACE("With config");
260 Opts.ContextProvider = [&](llvm::StringRef Path) {
261 Config Cfg;
262 if (Path.ends_with("a.cc")) {
263 // a.cc uses another directory's CDB, so it won't be discovered.
264 Cfg.CompileFlags.CDBSearch.Policy = Config::CDBSearchSpec::FixedDir;
265 Cfg.CompileFlags.CDBSearch.FixedCDBPath = testPath("foo");
266 } else if (Path.ends_with("gen.cc")) {
267 // gen.cc has CDB search disabled, so it won't be discovered.
268 Cfg.CompileFlags.CDBSearch.Policy = Config::CDBSearchSpec::NoCDBSearch;
269 } else if (Path.ends_with("gen2.cc")) {
270 // gen2.cc explicitly lists this directory, so it will be discovered.
271 Cfg.CompileFlags.CDBSearch.Policy = Config::CDBSearchSpec::FixedDir;
272 Cfg.CompileFlags.CDBSearch.FixedCDBPath = testRoot();
273 }
274 return Context::current().derive(Config::Key, std::move(Cfg));
275 };
277 std::vector<std::string> DiscoveredFiles;
278 auto Sub =
279 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
280 DiscoveredFiles = Changes;
281 });
282
283 // Does not use the root CDB, so no broadcast.
284 auto Cmd = DB.getCompileCommand(testPath("build/../a.cc"));
285 ASSERT_TRUE(Cmd);
286 EXPECT_THAT(Cmd->CommandLine, Contains("-DFOO")) << "a.cc uses foo/ CDB";
287 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
288 EXPECT_THAT(DiscoveredFiles, IsEmpty()) << "Root CDB not discovered yet";
289
290 // No special config for b.cc, so we trigger broadcast of the root CDB.
291 DB.getCompileCommand(testPath("b.cc"));
292 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
293 EXPECT_THAT(DiscoveredFiles, ElementsAre(testPath("build/gen2.cc")));
294 DiscoveredFiles.clear();
295
296 // No CDB search so no discovery/broadcast triggered for build/ CDB.
297 DB.getCompileCommand(testPath("build/gen.cc"));
298 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
299 EXPECT_THAT(DiscoveredFiles, IsEmpty());
300 }
301
302 {
303 SCOPED_TRACE("With custom compile commands dir");
305 Opts.CompileCommandsDir = testRoot();
307 std::vector<std::string> DiscoveredFiles;
308 auto Sub =
309 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
310 DiscoveredFiles = Changes;
311 });
312
313 DB.getCompileCommand(testPath("a.cc"));
314 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
315 EXPECT_THAT(DiscoveredFiles,
316 UnorderedElementsAre(EndsWith("a.cc"), EndsWith("gen.cc"),
317 EndsWith("gen2.cc")));
318 DiscoveredFiles.clear();
319
320 DB.getCompileCommand(testPath("build/gen.cc"));
321 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
322 EXPECT_THAT(DiscoveredFiles, IsEmpty());
323 }
324}
325
326TEST(GlobalCompilationDatabaseTest, BuildDir) {
327 MockFS FS;
328 auto Command = [&](llvm::StringRef Relative) {
331 .getCompileCommand(testPath(Relative))
332 .value_or(tooling::CompileCommand())
333 .CommandLine;
334 };
335 EXPECT_THAT(Command("x/foo.cc"), IsEmpty());
336 const char *const CDB =
337 R"cdb(
338 [
339 {
340 "file": "{0}/x/foo.cc",
341 "command": "clang -DXYZZY {0}/x/foo.cc",
342 "directory": "{0}",
343 },
344 {
345 "file": "{0}/bar.cc",
346 "command": "clang -DXYZZY {0}/bar.cc",
347 "directory": "{0}",
348 }
349 ]
350 )cdb";
351 FS.Files[testPath("x/build/compile_commands.json")] =
352 llvm::formatv(CDB, llvm::sys::path::convert_to_slash(testRoot()));
353 EXPECT_THAT(Command("x/foo.cc"), Contains("-DXYZZY"));
354 EXPECT_THAT(Command("bar.cc"), IsEmpty())
355 << "x/build/compile_flags.json only applicable to x/";
356}
357
358TEST(GlobalCompilationDatabaseTest, CompileFlagsDirectory) {
359 MockFS FS;
360 FS.Files[testPath("x/compile_flags.txt")] = "-DFOO";
362 auto Commands = CDB.getCompileCommand(testPath("x/y.cpp"));
363 ASSERT_TRUE(Commands.has_value());
364 EXPECT_THAT(Commands->CommandLine, Contains("-DFOO"));
365 // Make sure we pick the right working directory.
366 EXPECT_EQ(testPath("x"), Commands->Directory);
367}
368
369MATCHER_P(hasArg, Flag, "") {
370 if (!arg) {
371 *result_listener << "command is null";
372 return false;
373 }
374 if (!llvm::is_contained(arg->CommandLine, Flag)) {
375 *result_listener << "flags are " << printArgv(arg->CommandLine);
376 return false;
377 }
378 return true;
379}
380
381TEST(GlobalCompilationDatabaseTest, Config) {
382 MockFS FS;
383 FS.Files[testPath("x/compile_flags.txt")] = "-DX";
384 FS.Files[testPath("x/y/z/compile_flags.txt")] = "-DZ";
385
388 Opts.ContextProvider = [&](llvm::StringRef Path) {
389 Config C;
390 C.CompileFlags.CDBSearch = Spec;
391 return Context::current().derive(Config::Key, std::move(C));
392 };
394
395 // Default ancestor behavior.
396 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
397 EXPECT_THAT(CDB.getCompileCommand(testPath("x/foo.cc")), hasArg("-DX"));
398 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/foo.cc")), hasArg("-DX"));
399 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/z/foo.cc")), hasArg("-DZ"));
400
402 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
403 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/foo.cc")));
404 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/foo.cc")));
405 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/z/foo.cc")));
406
408 Spec.FixedCDBPath = testPath("w"); // doesn't exist
409 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
410 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/foo.cc")));
411 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/foo.cc")));
412 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/z/foo.cc")));
413
414 Spec.FixedCDBPath = testPath("x/y/z");
415 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc")), hasArg("-DZ"));
416 EXPECT_THAT(CDB.getCompileCommand(testPath("x/foo.cc")), hasArg("-DZ"));
417 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/foo.cc")), hasArg("-DZ"));
418 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/z/foo.cc")), hasArg("-DZ"));
419}
420
421TEST(GlobalCompilationDatabaseTest, NonCanonicalFilenames) {
422 OverlayCDB DB(nullptr);
423 std::vector<std::string> DiscoveredFiles;
424 auto Sub =
425 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
426 DiscoveredFiles = Changes;
427 });
428
429 llvm::SmallString<128> Root(testRoot());
430 llvm::sys::path::append(Root, "build", "..", "a.cc");
431 EXPECT_TRUE(DB.setCompileCommand(Root.str(), tooling::CompileCommand()));
432 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(testPath("a.cc")));
433 DiscoveredFiles.clear();
434
435 llvm::SmallString<128> File(testRoot());
436 llvm::sys::path::append(File, "blabla", "..", "a.cc");
437
438 EXPECT_TRUE(DB.getCompileCommand(File));
439 EXPECT_FALSE(DB.getProjectInfo(File));
440}
441
442TEST_F(OverlayCDBTest, GetProjectInfo) {
443 OverlayCDB DB(Base.get());
444 Path File = testPath("foo.cc");
445 Path Header = testPath("foo.h");
446
447 EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot());
448 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot());
449
450 // Shouldn't change after an override.
451 EXPECT_TRUE(DB.setCompileCommand(File, tooling::CompileCommand()));
452 EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot());
453 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot());
454}
455
456TEST(GlobalCompilationDatabaseTest, InferenceWithResponseFile) {
457 MockFS FS;
458 auto Command = [&](llvm::StringRef Relative) {
461 .getCompileCommand(testPath(Relative))
462 .value_or(tooling::CompileCommand())
463 .CommandLine;
464 };
465 EXPECT_THAT(Command("foo.cc"), IsEmpty());
466
467 // Have to use real FS for response file.
468 SmallString<1024> Path;
469 int FD;
470 ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("args", "", FD, Path));
471 llvm::raw_fd_ostream OutStream(FD, true);
472 OutStream << "-DXYZZY";
473 OutStream.close();
474
475 const char *const CDB =
476 R"cdb(
477 [
478 {
479 "file": "{0}/foo.cc",
480 "command": "clang @{1} {0}/foo.cc",
481 "directory": "{0}",
482 }
483 ]
484 )cdb";
485 FS.Files[testPath("compile_commands.json")] =
486 llvm::formatv(CDB, llvm::sys::path::convert_to_slash(testRoot()),
487 llvm::sys::path::convert_to_slash(Path));
488
489 // File from CDB.
490 EXPECT_THAT(Command("foo.cc"), Contains("-DXYZZY"));
491 // File not in CDB, use inference.
492 EXPECT_THAT(Command("foo.h"), Contains("-DXYZZY"));
493}
494} // namespace
495
496// Friend test has access to internals.
498 : public ::testing::Test {
499protected:
500 std::shared_ptr<const tooling::CompilationDatabase>
502 llvm::StringRef Path,
503 std::chrono::steady_clock::time_point FreshTime) {
504 DirectoryBasedGlobalCompilationDatabase::CDBLookupRequest Req;
505 Req.FileName = Path;
506 Req.FreshTime = Req.FreshTimeMissing = FreshTime;
507 if (auto Result = GDB.lookupCDB(Req))
508 return std::move(Result->CDB);
509 return nullptr;
510 }
511};
512
513// Matches non-null CDBs which include the specified flag.
515 if (arg == nullptr)
516 return false;
517 auto Cmds = arg->getCompileCommands(Path);
518 if (Cmds.empty()) {
519 *result_listener << "yields no commands";
520 return false;
521 }
522 if (!llvm::is_contained(Cmds.front().CommandLine, Flag)) {
523 *result_listener << "flags are: " << printArgv(Cmds.front().CommandLine);
524 return false;
525 }
526 return true;
527}
528
529auto hasFlag(llvm::StringRef Flag) {
530 return hasFlag(Flag, "mock_file_name.cc");
531}
532
534 MockFS FS;
535 auto Stale = std::chrono::steady_clock::now() - std::chrono::minutes(1);
536 auto Fresh = std::chrono::steady_clock::now() + std::chrono::hours(24);
537
539 FS.Files["compile_flags.txt"] = "-DROOT";
540 auto Root = lookupCDB(GDB, testPath("foo/test.cc"), Stale);
541 EXPECT_THAT(Root, hasFlag("-DROOT"));
542
543 // Add a compilation database to a subdirectory - CDB loaded.
544 FS.Files["foo/compile_flags.txt"] = "-DFOO";
545 EXPECT_EQ(Root, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
546 << "cache still valid";
547 auto Foo = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
548 EXPECT_THAT(Foo, hasFlag("-DFOO")) << "new cdb loaded";
549 EXPECT_EQ(Foo, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
550 << "new cdb in cache";
551
552 // Mtime changed, but no content change - CDB not reloaded.
553 ++FS.Timestamps["foo/compile_flags.txt"];
554 auto FooAgain = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
555 EXPECT_EQ(Foo, FooAgain) << "Same content, read but not reloaded";
556 // Content changed, but not size or mtime - CDB not reloaded.
557 FS.Files["foo/compile_flags.txt"] = "-DBAR";
558 auto FooAgain2 = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
559 EXPECT_EQ(Foo, FooAgain2) << "Same filesize, change not detected";
560 // Mtime change forces a re-read, and we notice the different content.
561 ++FS.Timestamps["foo/compile_flags.txt"];
562 auto Bar = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
563 EXPECT_THAT(Bar, hasFlag("-DBAR")) << "refreshed with mtime change";
564
565 // Size and content both change - CDB reloaded.
566 FS.Files["foo/compile_flags.txt"] = "-DFOOBAR";
567 EXPECT_EQ(Bar, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
568 << "cache still valid";
569 auto FooBar = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
570 EXPECT_THAT(FooBar, hasFlag("-DFOOBAR")) << "cdb reloaded";
571
572 // compile_commands.json takes precedence over compile_flags.txt.
573 FS.Files["foo/compile_commands.json"] =
574 llvm::formatv(R"json([{
575 "file": "{0}/foo/mock_file.cc",
576 "command": "clang -DBAZ mock_file.cc",
577 "directory": "{0}/foo",
578 }])json",
579 llvm::sys::path::convert_to_slash(testRoot()));
580 EXPECT_EQ(FooBar, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
581 << "cache still valid";
582 auto Baz = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
583 EXPECT_THAT(Baz, hasFlag("-DBAZ", testPath("foo/mock_file.cc")))
584 << "compile_commands overrides compile_flags";
585
586 // Removing compile_commands.json reveals compile_flags.txt again.
587 // However this *does* cause a CDB reload (we cache only one CDB per dir).
588 FS.Files.erase("foo/compile_commands.json");
589 auto FoobarAgain = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
590 EXPECT_THAT(FoobarAgain, hasFlag("-DFOOBAR")) << "reloaded compile_flags";
591 EXPECT_NE(FoobarAgain, FooBar) << "CDB discarded (shadowed within directory)";
592
593 // Removing the directory's CDB leaves the parent CDB active.
594 // The parent CDB is *not* reloaded (we cache the CDB per-directory).
595 FS.Files.erase("foo/compile_flags.txt");
596 EXPECT_EQ(Root, lookupCDB(GDB, testPath("foo/test.cc"), Fresh))
597 << "CDB retained (shadowed by another directory)";
598}
599
600} // namespace clangd
601} // namespace clang
static cl::list< std::string > Commands("c", cl::desc("Specify command to run"), cl::value_desc("command"), cl::cat(ClangQueryCategory))
std::string CommandLine
Context derive(const Key< Type > &Key, std::decay_t< Type > Value) const &
Derives a child context It is safe to move or destroy a parent context after calling derive().
Definition Context.h:119
static const Context & current()
Returns the context for the current thread, creating it if needed.
Definition Context.cpp:27
std::shared_ptr< const tooling::CompilationDatabase > lookupCDB(const DirectoryBasedGlobalCompilationDatabase &GDB, llvm::StringRef Path, std::chrono::steady_clock::time_point FreshTime)
Gets compile args from tooling::CompilationDatabases built for parent directories.
std::optional< tooling::CompileCommand > getCompileCommand(PathRef File) const override
Scans File's parents looking for compilation databases.
llvm::StringMap< std::string > Files
Definition TestFS.h:45
llvm::StringMap< time_t > Timestamps
Definition TestFS.h:46
Wraps another compilation database, and supports overriding the commands using an in-memory mapping.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:45
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
MATCHER_P2(hasFlag, Flag, Path, "")
MATCHER_P(named, N, "")
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition TestFS.cpp:93
TEST(BackgroundQueueTest, Priority)
auto hasFlag(llvm::StringRef Flag)
std::string printArgv(llvm::ArrayRef< llvm::StringRef > Args)
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition Path.h:29
std::string Path
A typedef to represent a file path.
Definition Path.h:26
Deadline timeoutSeconds(std::optional< double > Seconds)
Makes a deadline from a timeout in seconds. std::nullopt means wait forever.
const char * testRoot()
Definition TestFS.cpp:85
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition sample.cpp:5
Settings that express user/project preferences and control clangd behavior.
Definition Config.h:44
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
Definition Config.h:48