clang-tools 19.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;
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
58static 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
63class 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;
71 }
72
73 tooling::CompileCommand
74 getFallbackCommand(llvm::StringRef File) const override {
75 return cmd(File, "-DA=2");
76 }
77
78 std::optional<ProjectInfo> getProjectInfo(PathRef File) const override {
79 return ProjectInfo{testRoot()};
80 }
81 };
82
83protected:
84 OverlayCDBTest() : Base(std::make_unique<BaseCDB>()) {}
85 std::unique_ptr<GlobalCompilationDatabase> Base;
86};
87
88TEST_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);
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")), std::nullopt);
99 CDB.setCompileCommand(testPath("missing.cc"), Override);
100 EXPECT_THAT(CDB.getCompileCommand(testPath("missing.cc"))->CommandLine,
101 Contains("-DA=3"));
102}
103
104TEST_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
110TEST_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"));
117
118 EXPECT_THAT(CDB.getFallbackCommand(testPath("foo.cc")).CommandLine,
119 ElementsAre("clang", testPath("foo.cc"), "-DA=6"));
120}
121
122TEST_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", 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")));
137}
138
139TEST_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());
144 });
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"));
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"));
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
166TEST_F(OverlayCDBTest, ExpandedResponseFiles) {
167 SmallString<1024> Path;
168 int FD;
169 ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("args", "", FD, Path));
170 llvm::raw_fd_ostream OutStream(FD, true);
171 OutStream << "-Wall";
172 OutStream.close();
173
174 OverlayCDB CDB(Base.get(), {"-DFallback"});
175 auto Override = cmd(testPath("foo.cc"), ("@" + Path).str());
176 CDB.setCompileCommand(testPath("foo.cc"), Override);
177 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
178 Contains("-Wall"));
179}
180
181TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
182 const char *const CDBOuter =
183 R"cdb(
184 [
185 {
186 "file": "a.cc",
187 "command": "",
188 "directory": "{0}",
189 },
190 {
191 "file": "build/gen.cc",
192 "command": "",
193 "directory": "{0}",
194 },
195 {
196 "file": "build/gen2.cc",
197 "command": "",
198 "directory": "{0}",
199 }
200 ]
201 )cdb";
202 const char *const CDBInner =
203 R"cdb(
204 [
205 {
206 "file": "gen.cc",
207 "command": "",
208 "directory": "{0}/build",
209 }
210 ]
211 )cdb";
212 MockFS FS;
213 FS.Files[testPath("compile_commands.json")] =
214 llvm::formatv(CDBOuter, llvm::sys::path::convert_to_slash(testRoot()));
215 FS.Files[testPath("build/compile_commands.json")] =
216 llvm::formatv(CDBInner, llvm::sys::path::convert_to_slash(testRoot()));
217 FS.Files[testPath("foo/compile_flags.txt")] = "-DFOO";
218
219 // Note that gen2.cc goes missing with our following model, not sure this
220 // happens in practice though.
221 {
222 SCOPED_TRACE("Default ancestor scanning");
223 DirectoryBasedGlobalCompilationDatabase DB(FS);
224 std::vector<std::string> DiscoveredFiles;
225 auto Sub =
226 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
227 DiscoveredFiles = Changes;
228 });
229
230 DB.getCompileCommand(testPath("build/../a.cc"));
231 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
232 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(AllOf(
233 EndsWith("a.cc"), Not(HasSubstr("..")))));
234 DiscoveredFiles.clear();
235
236 DB.getCompileCommand(testPath("build/gen.cc"));
237 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
238 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(EndsWith("gen.cc")));
239 }
240
241 {
242 SCOPED_TRACE("With config");
243 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
244 Opts.ContextProvider = [&](llvm::StringRef Path) {
245 Config Cfg;
246 if (Path.ends_with("a.cc")) {
247 // a.cc uses another directory's CDB, so it won't be discovered.
250 } else if (Path.ends_with("gen.cc")) {
251 // gen.cc has CDB search disabled, so it won't be discovered.
253 } else if (Path.ends_with("gen2.cc")) {
254 // gen2.cc explicitly lists this directory, so it will be discovered.
257 }
258 return Context::current().derive(Config::Key, std::move(Cfg));
259 };
260 DirectoryBasedGlobalCompilationDatabase DB(Opts);
261 std::vector<std::string> DiscoveredFiles;
262 auto Sub =
263 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
264 DiscoveredFiles = Changes;
265 });
266
267 // Does not use the root CDB, so no broadcast.
268 auto Cmd = DB.getCompileCommand(testPath("build/../a.cc"));
269 ASSERT_TRUE(Cmd);
270 EXPECT_THAT(Cmd->CommandLine, Contains("-DFOO")) << "a.cc uses foo/ CDB";
271 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
272 EXPECT_THAT(DiscoveredFiles, IsEmpty()) << "Root CDB not discovered yet";
273
274 // No special config for b.cc, so we trigger broadcast of the root CDB.
275 DB.getCompileCommand(testPath("b.cc"));
276 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
277 EXPECT_THAT(DiscoveredFiles, ElementsAre(testPath("build/gen2.cc")));
278 DiscoveredFiles.clear();
279
280 // No CDB search so no discovery/broadcast triggered for build/ CDB.
281 DB.getCompileCommand(testPath("build/gen.cc"));
282 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
283 EXPECT_THAT(DiscoveredFiles, IsEmpty());
284 }
285
286 {
287 SCOPED_TRACE("With custom compile commands dir");
288 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
289 Opts.CompileCommandsDir = testRoot();
290 DirectoryBasedGlobalCompilationDatabase DB(Opts);
291 std::vector<std::string> DiscoveredFiles;
292 auto Sub =
293 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
294 DiscoveredFiles = Changes;
295 });
296
297 DB.getCompileCommand(testPath("a.cc"));
298 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
299 EXPECT_THAT(DiscoveredFiles,
300 UnorderedElementsAre(EndsWith("a.cc"), EndsWith("gen.cc"),
301 EndsWith("gen2.cc")));
302 DiscoveredFiles.clear();
303
304 DB.getCompileCommand(testPath("build/gen.cc"));
305 ASSERT_TRUE(DB.blockUntilIdle(timeoutSeconds(10)));
306 EXPECT_THAT(DiscoveredFiles, IsEmpty());
307 }
308}
309
310TEST(GlobalCompilationDatabaseTest, BuildDir) {
311 MockFS FS;
312 auto Command = [&](llvm::StringRef Relative) {
313 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
314 return DirectoryBasedGlobalCompilationDatabase(Opts)
315 .getCompileCommand(testPath(Relative))
316 .value_or(tooling::CompileCommand())
317 .CommandLine;
318 };
319 EXPECT_THAT(Command("x/foo.cc"), IsEmpty());
320 const char *const CDB =
321 R"cdb(
322 [
323 {
324 "file": "{0}/x/foo.cc",
325 "command": "clang -DXYZZY {0}/x/foo.cc",
326 "directory": "{0}",
327 },
328 {
329 "file": "{0}/bar.cc",
330 "command": "clang -DXYZZY {0}/bar.cc",
331 "directory": "{0}",
332 }
333 ]
334 )cdb";
335 FS.Files[testPath("x/build/compile_commands.json")] =
336 llvm::formatv(CDB, llvm::sys::path::convert_to_slash(testRoot()));
337 EXPECT_THAT(Command("x/foo.cc"), Contains("-DXYZZY"));
338 EXPECT_THAT(Command("bar.cc"), IsEmpty())
339 << "x/build/compile_flags.json only applicable to x/";
340}
341
342TEST(GlobalCompilationDatabaseTest, CompileFlagsDirectory) {
343 MockFS FS;
344 FS.Files[testPath("x/compile_flags.txt")] = "-DFOO";
345 DirectoryBasedGlobalCompilationDatabase CDB(FS);
346 auto Commands = CDB.getCompileCommand(testPath("x/y.cpp"));
347 ASSERT_TRUE(Commands.has_value());
348 EXPECT_THAT(Commands->CommandLine, Contains("-DFOO"));
349 // Make sure we pick the right working directory.
350 EXPECT_EQ(testPath("x"), Commands->Directory);
351}
352
353MATCHER_P(hasArg, Flag, "") {
354 if (!arg) {
355 *result_listener << "command is null";
356 return false;
357 }
358 if (!llvm::is_contained(arg->CommandLine, Flag)) {
359 *result_listener << "flags are " << printArgv(arg->CommandLine);
360 return false;
361 }
362 return true;
363}
364
365TEST(GlobalCompilationDatabaseTest, Config) {
366 MockFS FS;
367 FS.Files[testPath("x/compile_flags.txt")] = "-DX";
368 FS.Files[testPath("x/y/z/compile_flags.txt")] = "-DZ";
369
370 Config::CDBSearchSpec Spec;
371 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
372 Opts.ContextProvider = [&](llvm::StringRef Path) {
373 Config C;
374 C.CompileFlags.CDBSearch = Spec;
375 return Context::current().derive(Config::Key, std::move(C));
376 };
377 DirectoryBasedGlobalCompilationDatabase CDB(Opts);
378
379 // Default ancestor behavior.
380 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
381 EXPECT_THAT(CDB.getCompileCommand(testPath("x/foo.cc")), hasArg("-DX"));
382 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/foo.cc")), hasArg("-DX"));
383 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/z/foo.cc")), hasArg("-DZ"));
384
386 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
387 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/foo.cc")));
388 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/foo.cc")));
389 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/z/foo.cc")));
390
392 Spec.FixedCDBPath = testPath("w"); // doesn't exist
393 EXPECT_FALSE(CDB.getCompileCommand(testPath("foo.cc")));
394 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/foo.cc")));
395 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/foo.cc")));
396 EXPECT_FALSE(CDB.getCompileCommand(testPath("x/y/z/foo.cc")));
397
398 Spec.FixedCDBPath = testPath("x/y/z");
399 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc")), hasArg("-DZ"));
400 EXPECT_THAT(CDB.getCompileCommand(testPath("x/foo.cc")), hasArg("-DZ"));
401 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/foo.cc")), hasArg("-DZ"));
402 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/z/foo.cc")), hasArg("-DZ"));
403}
404
405TEST(GlobalCompilationDatabaseTest, NonCanonicalFilenames) {
406 OverlayCDB DB(nullptr);
407 std::vector<std::string> DiscoveredFiles;
408 auto Sub =
409 DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
410 DiscoveredFiles = Changes;
411 });
412
413 llvm::SmallString<128> Root(testRoot());
414 llvm::sys::path::append(Root, "build", "..", "a.cc");
415 DB.setCompileCommand(Root.str(), tooling::CompileCommand());
416 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(testPath("a.cc")));
417 DiscoveredFiles.clear();
418
419 llvm::SmallString<128> File(testRoot());
420 llvm::sys::path::append(File, "blabla", "..", "a.cc");
421
422 EXPECT_TRUE(DB.getCompileCommand(File));
423 EXPECT_FALSE(DB.getProjectInfo(File));
424}
425
426TEST_F(OverlayCDBTest, GetProjectInfo) {
427 OverlayCDB DB(Base.get());
428 Path File = testPath("foo.cc");
429 Path Header = testPath("foo.h");
430
431 EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot());
432 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot());
433
434 // Shouldn't change after an override.
435 DB.setCompileCommand(File, tooling::CompileCommand());
436 EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot());
437 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot());
438}
439
440TEST(GlobalCompilationDatabaseTest, InferenceWithResponseFile) {
441 MockFS FS;
442 auto Command = [&](llvm::StringRef Relative) {
443 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
444 return DirectoryBasedGlobalCompilationDatabase(Opts)
445 .getCompileCommand(testPath(Relative))
446 .value_or(tooling::CompileCommand())
447 .CommandLine;
448 };
449 EXPECT_THAT(Command("foo.cc"), IsEmpty());
450
451 // Have to use real FS for response file.
452 SmallString<1024> Path;
453 int FD;
454 ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("args", "", FD, Path));
455 llvm::raw_fd_ostream OutStream(FD, true);
456 OutStream << "-DXYZZY";
457 OutStream.close();
458
459 const char *const CDB =
460 R"cdb(
461 [
462 {
463 "file": "{0}/foo.cc",
464 "command": "clang @{1} {0}/foo.cc",
465 "directory": "{0}",
466 }
467 ]
468 )cdb";
469 FS.Files[testPath("compile_commands.json")] =
470 llvm::formatv(CDB, llvm::sys::path::convert_to_slash(testRoot()),
471 llvm::sys::path::convert_to_slash(Path));
472
473 // File from CDB.
474 EXPECT_THAT(Command("foo.cc"), Contains("-DXYZZY"));
475 // File not in CDB, use inference.
476 EXPECT_THAT(Command("foo.h"), Contains("-DXYZZY"));
477}
478} // namespace
479
480// Friend test has access to internals.
482 : public ::testing::Test {
483protected:
484 std::shared_ptr<const tooling::CompilationDatabase>
486 llvm::StringRef Path,
487 std::chrono::steady_clock::time_point FreshTime) {
488 DirectoryBasedGlobalCompilationDatabase::CDBLookupRequest Req;
489 Req.FileName = Path;
490 Req.FreshTime = Req.FreshTimeMissing = FreshTime;
491 if (auto Result = GDB.lookupCDB(Req))
492 return std::move(Result->CDB);
493 return nullptr;
494 }
495};
496
497// Matches non-null CDBs which include the specified flag.
499 if (arg == nullptr)
500 return false;
501 auto Cmds = arg->getCompileCommands(Path);
502 if (Cmds.empty()) {
503 *result_listener << "yields no commands";
504 return false;
505 }
506 if (!llvm::is_contained(Cmds.front().CommandLine, Flag)) {
507 *result_listener << "flags are: " << printArgv(Cmds.front().CommandLine);
508 return false;
509 }
510 return true;
511}
512
513auto hasFlag(llvm::StringRef Flag) {
514 return hasFlag(Flag, "mock_file_name.cc");
515}
516
518 MockFS FS;
519 auto Stale = std::chrono::steady_clock::now() - std::chrono::minutes(1);
520 auto Fresh = std::chrono::steady_clock::now() + std::chrono::hours(24);
521
523 FS.Files["compile_flags.txt"] = "-DROOT";
524 auto Root = lookupCDB(GDB, testPath("foo/test.cc"), Stale);
525 EXPECT_THAT(Root, hasFlag("-DROOT"));
526
527 // Add a compilation database to a subdirectory - CDB loaded.
528 FS.Files["foo/compile_flags.txt"] = "-DFOO";
529 EXPECT_EQ(Root, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
530 << "cache still valid";
531 auto Foo = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
532 EXPECT_THAT(Foo, hasFlag("-DFOO")) << "new cdb loaded";
533 EXPECT_EQ(Foo, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
534 << "new cdb in cache";
535
536 // Mtime changed, but no content change - CDB not reloaded.
537 ++FS.Timestamps["foo/compile_flags.txt"];
538 auto FooAgain = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
539 EXPECT_EQ(Foo, FooAgain) << "Same content, read but not reloaded";
540 // Content changed, but not size or mtime - CDB not reloaded.
541 FS.Files["foo/compile_flags.txt"] = "-DBAR";
542 auto FooAgain2 = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
543 EXPECT_EQ(Foo, FooAgain2) << "Same filesize, change not detected";
544 // Mtime change forces a re-read, and we notice the different content.
545 ++FS.Timestamps["foo/compile_flags.txt"];
546 auto Bar = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
547 EXPECT_THAT(Bar, hasFlag("-DBAR")) << "refreshed with mtime change";
548
549 // Size and content both change - CDB reloaded.
550 FS.Files["foo/compile_flags.txt"] = "-DFOOBAR";
551 EXPECT_EQ(Bar, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
552 << "cache still valid";
553 auto FooBar = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
554 EXPECT_THAT(FooBar, hasFlag("-DFOOBAR")) << "cdb reloaded";
555
556 // compile_commands.json takes precedence over compile_flags.txt.
557 FS.Files["foo/compile_commands.json"] =
558 llvm::formatv(R"json([{
559 "file": "{0}/foo/mock_file.cc",
560 "command": "clang -DBAZ mock_file.cc",
561 "directory": "{0}/foo",
562 }])json",
563 llvm::sys::path::convert_to_slash(testRoot()));
564 EXPECT_EQ(FooBar, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
565 << "cache still valid";
566 auto Baz = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
567 EXPECT_THAT(Baz, hasFlag("-DBAZ", testPath("foo/mock_file.cc")))
568 << "compile_commands overrides compile_flags";
569
570 // Removing compile_commands.json reveals compile_flags.txt again.
571 // However this *does* cause a CDB reload (we cache only one CDB per dir).
572 FS.Files.erase("foo/compile_commands.json");
573 auto FoobarAgain = lookupCDB(GDB, testPath("foo/test.cc"), Fresh);
574 EXPECT_THAT(FoobarAgain, hasFlag("-DFOOBAR")) << "reloaded compile_flags";
575 EXPECT_NE(FoobarAgain, FooBar) << "CDB discarded (shadowed within directory)";
576
577 // Removing the directory's CDB leaves the parent CDB active.
578 // The parent CDB is *not* reloaded (we cache the CDB per-directory).
579 FS.Files.erase("foo/compile_flags.txt");
580 EXPECT_EQ(Root, lookupCDB(GDB, testPath("foo/test.cc"), Fresh))
581 << "CDB retained (shadowed by another directory)";
582}
583
584} // namespace clangd
585} // namespace clang
std::pair< Context, Canceler > Inner
static cl::list< std::string > Commands("c", cl::desc("Specify command to run"), cl::value_desc("command"), cl::cat(ClangQueryCategory))
StringRef BuildDir
Definition: ClangTidy.cpp:305
ASTNode Root
Definition: DumpAST.cpp:342
tooling::Replacements Changes
Definition: Format.cpp:109
const Criteria C
std::vector< llvm::StringRef > 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.
virtual tooling::CompileCommand getFallbackCommand(PathRef File) const
Makes a guess at how to build a file.
virtual std::optional< tooling::CompileCommand > getCompileCommand(PathRef File) const =0
If there are any known-good commands for building this file, returns one.
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
std::string Path
A typedef to represent a file path.
Definition: Path.h:26
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)
Deadline timeoutSeconds(std::optional< double > Seconds)
Makes a deadline from a timeout in seconds. std::nullopt means wait forever.
Definition: Threading.cpp:113
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
const char * testRoot()
Definition: TestFS.cpp:85
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: sample.cpp:5
enum clang::clangd::Config::CDBSearchSpec::@10 Policy
std::optional< std::string > FixedCDBPath
Definition: Config.h:59
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
Definition: Config.h:48
CDBSearchSpec CDBSearch
Where to search for compilation databases for this file's flags.
Definition: Config.h:68
struct clang::clangd::Config::@2 CompileFlags
Controls how the compile command for the current file is determined.