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"
32using ::testing::AllOf;
33using ::testing::Contains;
34using ::testing::ElementsAre;
35using ::testing::EndsWith;
36using ::testing::HasSubstr;
37using ::testing::IsEmpty;
39using ::testing::UnorderedElementsAre;
41TEST(GlobalCompilationDatabaseTest, FallbackCommand) {
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,
"");
50 Cmd = DB.getFallbackCommand(
testPath(
"foo/bar.h"));
51 EXPECT_THAT(Cmd.CommandLine, ElementsAre(
"clang",
"-xobjective-c++-header",
53 Cmd = DB.getFallbackCommand(
testPath(
"foo/bar"));
54 EXPECT_THAT(Cmd.CommandLine, ElementsAre(
"clang",
"-xobjective-c++-header",
58static tooling::CompileCommand cmd(llvm::StringRef
File, llvm::StringRef Arg) {
59 return tooling::CompileCommand(
63class OverlayCDBTest :
public ::testing::Test {
64 class BaseCDB :
public GlobalCompilationDatabase {
66 std::optional<tooling::CompileCommand>
67 getCompileCommand(llvm::StringRef
File)
const override {
69 return cmd(
File,
"-DA=1");
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 {
84 OverlayCDBTest() : Base(std::make_unique<BaseCDB>()) {}
85 std::unique_ptr<GlobalCompilationDatabase> Base;
88TEST_F(OverlayCDBTest, GetCompileCommand) {
89 OverlayCDB CDB(Base.get());
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);
98 EXPECT_EQ(CDB.getCompileCommand(
testPath(
"missing.cc")), std::nullopt);
99 CDB.setCompileCommand(
testPath(
"missing.cc"), Override);
104TEST_F(OverlayCDBTest, GetFallbackCommand) {
105 OverlayCDB CDB(Base.get(), {
"-DA=4"});
107 ElementsAre(
"clang",
"-DA=2",
testPath(
"bar.cc"),
"-DA=4"));
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);
119 ElementsAre(
"clang",
testPath(
"foo.cc"),
"-DA=6"));
122TEST_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")));
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());
146 auto Cmd = *CDB.getCompileCommand(
testPath(
"foo.cc"));
147 EXPECT_THAT(Cmd.CommandLine, ElementsAre(
"clang",
"-DA=1",
testPath(
"foo.cc"),
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"));
158 ElementsAre(
"clang++",
"-DB=1",
testPath(
"bar.cc"),
"-DAdjust_bar.cc"));
161 Cmd = CDB.getFallbackCommand(
"baz.cc");
162 EXPECT_THAT(Cmd.CommandLine, ElementsAre(
"clang",
"-DA=2",
"baz.cc",
163 "-DFallback",
"-DAdjust_baz.cc"));
166TEST_F(OverlayCDBTest, ExpandedResponseFiles) {
167 SmallString<1024>
Path;
169 ASSERT_FALSE(llvm::sys::fs::createTemporaryFile(
"args",
"", FD,
Path));
170 llvm::raw_fd_ostream OutStream(FD,
true);
171 OutStream <<
"-Wall";
174 OverlayCDB CDB(Base.get(), {
"-DFallback"});
175 auto Override = cmd(
testPath(
"foo.cc"), (
"@" +
Path).str());
176 CDB.setCompileCommand(
testPath(
"foo.cc"), Override);
181TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
182 const char *
const CDBOuter =
191 "file": "build/gen.cc",
196 "file": "build/gen2.cc",
202 const char *
const CDBInner =
208 "directory": "{0}/build",
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";
222 SCOPED_TRACE(
"Default ancestor scanning");
223 DirectoryBasedGlobalCompilationDatabase DB(FS);
224 std::vector<std::string> DiscoveredFiles;
226 DB.watch([&DiscoveredFiles](
const std::vector<std::string>
Changes) {
230 DB.getCompileCommand(
testPath(
"build/../a.cc"));
232 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(AllOf(
233 EndsWith(
"a.cc"), Not(HasSubstr(
"..")))));
234 DiscoveredFiles.clear();
236 DB.getCompileCommand(
testPath(
"build/gen.cc"));
238 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(EndsWith(
"gen.cc")));
242 SCOPED_TRACE(
"With config");
243 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
244 Opts.ContextProvider = [&](llvm::StringRef
Path) {
246 if (
Path.ends_with(
"a.cc")) {
250 }
else if (
Path.ends_with(
"gen.cc")) {
253 }
else if (
Path.ends_with(
"gen2.cc")) {
260 DirectoryBasedGlobalCompilationDatabase DB(Opts);
261 std::vector<std::string> DiscoveredFiles;
263 DB.watch([&DiscoveredFiles](
const std::vector<std::string>
Changes) {
268 auto Cmd = DB.getCompileCommand(
testPath(
"build/../a.cc"));
270 EXPECT_THAT(Cmd->CommandLine, Contains(
"-DFOO")) <<
"a.cc uses foo/ CDB";
272 EXPECT_THAT(DiscoveredFiles, IsEmpty()) <<
"Root CDB not discovered yet";
275 DB.getCompileCommand(
testPath(
"b.cc"));
277 EXPECT_THAT(DiscoveredFiles, ElementsAre(
testPath(
"build/gen2.cc")));
278 DiscoveredFiles.clear();
281 DB.getCompileCommand(
testPath(
"build/gen.cc"));
283 EXPECT_THAT(DiscoveredFiles, IsEmpty());
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;
293 DB.watch([&DiscoveredFiles](
const std::vector<std::string>
Changes) {
297 DB.getCompileCommand(
testPath(
"a.cc"));
299 EXPECT_THAT(DiscoveredFiles,
300 UnorderedElementsAre(EndsWith(
"a.cc"), EndsWith(
"gen.cc"),
301 EndsWith(
"gen2.cc")));
302 DiscoveredFiles.clear();
304 DB.getCompileCommand(
testPath(
"build/gen.cc"));
306 EXPECT_THAT(DiscoveredFiles, IsEmpty());
312 auto Command = [&](llvm::StringRef Relative) {
313 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
314 return DirectoryBasedGlobalCompilationDatabase(Opts)
315 .getCompileCommand(
testPath(Relative))
316 .value_or(tooling::CompileCommand())
319 EXPECT_THAT(Command(
"x/foo.cc"), IsEmpty());
320 const char *
const CDB =
324 "file": "{0}/x/foo.cc",
325 "command": "clang -DXYZZY {0}/x/foo.cc",
329 "file": "{0}/bar.cc",
330 "command": "clang -DXYZZY {0}/bar.cc",
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/";
342TEST(GlobalCompilationDatabaseTest, CompileFlagsDirectory) {
344 FS.Files[
testPath(
"x/compile_flags.txt")] =
"-DFOO";
345 DirectoryBasedGlobalCompilationDatabase CDB(FS);
348 EXPECT_THAT(
Commands->CommandLine, Contains(
"-DFOO"));
355 *result_listener <<
"command is null";
358 if (!llvm::is_contained(arg->CommandLine, Flag)) {
359 *result_listener <<
"flags are " <<
printArgv(arg->CommandLine);
365TEST(GlobalCompilationDatabaseTest, Config) {
367 FS.Files[
testPath(
"x/compile_flags.txt")] =
"-DX";
368 FS.Files[
testPath(
"x/y/z/compile_flags.txt")] =
"-DZ";
370 Config::CDBSearchSpec Spec;
371 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
372 Opts.ContextProvider = [&](llvm::StringRef
Path) {
374 C.CompileFlags.CDBSearch = Spec;
377 DirectoryBasedGlobalCompilationDatabase CDB(Opts);
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"));
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")));
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")));
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"));
405TEST(GlobalCompilationDatabaseTest, NonCanonicalFilenames) {
406 OverlayCDB DB(
nullptr);
407 std::vector<std::string> DiscoveredFiles;
409 DB.watch([&DiscoveredFiles](
const std::vector<std::string>
Changes) {
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();
420 llvm::sys::path::append(
File,
"blabla",
"..",
"a.cc");
422 EXPECT_TRUE(DB.getCompileCommand(
File));
423 EXPECT_FALSE(DB.getProjectInfo(
File));
426TEST_F(OverlayCDBTest, GetProjectInfo) {
427 OverlayCDB DB(Base.get());
431 EXPECT_EQ(DB.getProjectInfo(
File)->SourceRoot,
testRoot());
432 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot,
testRoot());
435 DB.setCompileCommand(
File, tooling::CompileCommand());
436 EXPECT_EQ(DB.getProjectInfo(
File)->SourceRoot,
testRoot());
437 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot,
testRoot());
440TEST(GlobalCompilationDatabaseTest, InferenceWithResponseFile) {
442 auto Command = [&](llvm::StringRef Relative) {
443 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
444 return DirectoryBasedGlobalCompilationDatabase(Opts)
445 .getCompileCommand(
testPath(Relative))
446 .value_or(tooling::CompileCommand())
449 EXPECT_THAT(Command(
"foo.cc"), IsEmpty());
452 SmallString<1024>
Path;
454 ASSERT_FALSE(llvm::sys::fs::createTemporaryFile(
"args",
"", FD,
Path));
455 llvm::raw_fd_ostream OutStream(FD,
true);
456 OutStream <<
"-DXYZZY";
459 const char *
const CDB =
463 "file": "{0}/foo.cc",
464 "command": "clang @{1} {0}/foo.cc",
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));
474 EXPECT_THAT(Command(
"foo.cc"), Contains(
"-DXYZZY"));
476 EXPECT_THAT(Command(
"foo.h"), Contains(
"-DXYZZY"));
482 :
public ::testing::Test {
484 std::shared_ptr<const tooling::CompilationDatabase>
486 llvm::StringRef
Path,
487 std::chrono::steady_clock::time_point FreshTime) {
488 DirectoryBasedGlobalCompilationDatabase::CDBLookupRequest Req;
490 Req.FreshTime = Req.FreshTimeMissing = FreshTime;
491 if (
auto Result = GDB.lookupCDB(Req))
492 return std::move(Result->CDB);
501 auto Cmds = arg->getCompileCommands(
Path);
503 *result_listener <<
"yields no commands";
506 if (!llvm::is_contained(Cmds.front().CommandLine, Flag)) {
507 *result_listener <<
"flags are: " <<
printArgv(Cmds.front().CommandLine);
514 return hasFlag(Flag,
"mock_file_name.cc");
519 auto Stale = std::chrono::steady_clock::now() - std::chrono::minutes(1);
520 auto Fresh = std::chrono::steady_clock::now() + std::chrono::hours(24);
523 FS.Files[
"compile_flags.txt"] =
"-DROOT";
524 auto Root = lookupCDB(GDB,
testPath(
"foo/test.cc"), Stale);
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";
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";
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";
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";
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";
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",
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);
568 <<
"compile_commands overrides compile_flags";
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)";
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)";
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))
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().
static const Context & current()
Returns the context for the current thread, creating it if needed.
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.
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
std::string Path
A typedef to represent a file path.
MATCHER_P2(hasFlag, Flag, Path, "")
std::string testPath(PathRef File, llvm::sys::path::Style Style)
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.
llvm::StringRef PathRef
A typedef to represent a ref to file path.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
enum clang::clangd::Config::CDBSearchSpec::@10 Policy
std::optional< std::string > FixedCDBPath
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
CDBSearchSpec CDBSearch
Where to search for compilation databases for this file's flags.
struct clang::clangd::Config::@2 CompileFlags
Controls how the compile command for the current file is determined.