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")));
94 auto Override = cmd(
testPath(
"foo.cc"),
"-DA=3");
95 EXPECT_TRUE(CDB.setCompileCommand(
testPath(
"foo.cc"), Override));
96 EXPECT_FALSE(CDB.setCompileCommand(
testPath(
"foo.cc"), Override));
100 EXPECT_TRUE(CDB.setCompileCommand(
testPath(
"missing.cc"), Override));
101 EXPECT_FALSE(CDB.setCompileCommand(
testPath(
"missing.cc"), Override));
106TEST_F(OverlayCDBTest, GetFallbackCommand) {
107 OverlayCDB CDB(Base.get(), {
"-DA=4"});
109 ElementsAre(
"clang",
"-DA=2",
testPath(
"bar.cc"),
"-DA=4"));
112TEST_F(OverlayCDBTest, NoBase) {
113 OverlayCDB CDB(
nullptr, {
"-DA=6"});
115 auto Override = cmd(
testPath(
"bar.cc"),
"-DA=5");
116 EXPECT_TRUE(CDB.setCompileCommand(
testPath(
"bar.cc"), Override));
121 ElementsAre(
"clang",
testPath(
"foo.cc"),
"-DA=6"));
124TEST_F(OverlayCDBTest, Watch) {
125 OverlayCDB
Inner(
nullptr);
126 OverlayCDB Outer(&
Inner);
128 std::vector<std::vector<std::string>>
Changes;
129 auto Sub = Outer.watch([&](
const std::vector<std::string> &ChangedFiles) {
130 Changes.push_back(ChangedFiles);
133 EXPECT_TRUE(
Inner.setCompileCommand(
"A.cpp", tooling::CompileCommand()));
134 EXPECT_TRUE(Outer.setCompileCommand(
"B.cpp", tooling::CompileCommand()));
135 EXPECT_TRUE(
Inner.setCompileCommand(
"A.cpp", std::nullopt));
136 EXPECT_TRUE(Outer.setCompileCommand(
"C.cpp", std::nullopt));
137 EXPECT_THAT(
Changes, ElementsAre(ElementsAre(
"A.cpp"), ElementsAre(
"B.cpp"),
138 ElementsAre(
"A.cpp"), ElementsAre(
"C.cpp")));
141TEST_F(OverlayCDBTest, Adjustments) {
142 OverlayCDB CDB(Base.get(), {
"-DFallback"},
143 [](tooling::CompileCommand &Cmd, llvm::StringRef
File) {
144 Cmd.CommandLine.push_back(
145 (
"-DAdjust_" + llvm::sys::path::filename(File)).str());
149 EXPECT_THAT(Cmd.CommandLine, ElementsAre(
"clang",
"-DA=1",
testPath(
"foo.cc"),
153 tooling::CompileCommand BarCommand;
154 BarCommand.Filename =
testPath(
"bar.cc");
155 BarCommand.CommandLine = {
"clang++",
"-DB=1",
testPath(
"bar.cc")};
156 EXPECT_TRUE(CDB.setCompileCommand(
testPath(
"bar.cc"), BarCommand));
160 ElementsAre(
"clang++",
"-DB=1",
testPath(
"bar.cc"),
"-DAdjust_bar.cc"));
164 EXPECT_THAT(Cmd.CommandLine, ElementsAre(
"clang",
"-DA=2",
"baz.cc",
165 "-DFallback",
"-DAdjust_baz.cc"));
168TEST_F(OverlayCDBTest, ExpandedResponseFiles) {
169 SmallString<1024>
Path;
171 ASSERT_FALSE(llvm::sys::fs::createTemporaryFile(
"args",
"", FD,
Path));
172 llvm::raw_fd_ostream OutStream(FD,
true);
173 OutStream <<
"-Wall";
176 OverlayCDB CDB(Base.get(), {
"-DFallback"});
177 auto Override = cmd(
testPath(
"foo.cc"), (
"@" +
Path).str());
178 CDB.setCompileCommand(
testPath(
"foo.cc"), Override);
183TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
184 const char *
const CDBOuter =
193 "file": "build/gen.cc",
198 "file": "build/gen2.cc",
204 const char *
const CDBInner =
210 "directory": "{0}/build",
215 FS.Files[testPath("compile_commands.json")] =
216 llvm::formatv(CDBOuter, llvm::sys::path::convert_to_slash(
testRoot()));
217 FS.Files[
testPath(
"build/compile_commands.json")] =
218 llvm::formatv(CDBInner, llvm::sys::path::convert_to_slash(
testRoot()));
219 FS.Files[
testPath(
"foo/compile_flags.txt")] =
"-DFOO";
224 SCOPED_TRACE(
"Default ancestor scanning");
225 DirectoryBasedGlobalCompilationDatabase DB(FS);
226 std::vector<std::string> DiscoveredFiles;
228 DB.watch([&DiscoveredFiles](
const std::vector<std::string>
Changes) {
232 DB.getCompileCommand(
testPath(
"build/../a.cc"));
234 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(AllOf(
235 EndsWith(
"a.cc"), Not(HasSubstr(
"..")))));
236 DiscoveredFiles.clear();
238 DB.getCompileCommand(
testPath(
"build/gen.cc"));
240 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(EndsWith(
"gen.cc")));
244 SCOPED_TRACE(
"With config");
245 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
246 Opts.ContextProvider = [&](llvm::StringRef
Path) {
248 if (
Path.ends_with(
"a.cc")) {
252 }
else if (
Path.ends_with(
"gen.cc")) {
255 }
else if (
Path.ends_with(
"gen2.cc")) {
262 DirectoryBasedGlobalCompilationDatabase DB(Opts);
263 std::vector<std::string> DiscoveredFiles;
265 DB.watch([&DiscoveredFiles](
const std::vector<std::string>
Changes) {
270 auto Cmd = DB.getCompileCommand(
testPath(
"build/../a.cc"));
272 EXPECT_THAT(Cmd->CommandLine, Contains(
"-DFOO")) <<
"a.cc uses foo/ CDB";
274 EXPECT_THAT(DiscoveredFiles, IsEmpty()) <<
"Root CDB not discovered yet";
277 DB.getCompileCommand(
testPath(
"b.cc"));
279 EXPECT_THAT(DiscoveredFiles, ElementsAre(
testPath(
"build/gen2.cc")));
280 DiscoveredFiles.clear();
283 DB.getCompileCommand(
testPath(
"build/gen.cc"));
285 EXPECT_THAT(DiscoveredFiles, IsEmpty());
289 SCOPED_TRACE(
"With custom compile commands dir");
290 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
291 Opts.CompileCommandsDir =
testRoot();
292 DirectoryBasedGlobalCompilationDatabase DB(Opts);
293 std::vector<std::string> DiscoveredFiles;
295 DB.watch([&DiscoveredFiles](
const std::vector<std::string>
Changes) {
299 DB.getCompileCommand(
testPath(
"a.cc"));
301 EXPECT_THAT(DiscoveredFiles,
302 UnorderedElementsAre(EndsWith(
"a.cc"), EndsWith(
"gen.cc"),
303 EndsWith(
"gen2.cc")));
304 DiscoveredFiles.clear();
306 DB.getCompileCommand(
testPath(
"build/gen.cc"));
308 EXPECT_THAT(DiscoveredFiles, IsEmpty());
314 auto Command = [&](llvm::StringRef Relative) {
315 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
316 return DirectoryBasedGlobalCompilationDatabase(Opts)
317 .getCompileCommand(
testPath(Relative))
318 .value_or(tooling::CompileCommand())
321 EXPECT_THAT(Command(
"x/foo.cc"), IsEmpty());
322 const char *
const CDB =
326 "file": "{0}/x/foo.cc",
327 "command": "clang -DXYZZY {0}/x/foo.cc",
331 "file": "{0}/bar.cc",
332 "command": "clang -DXYZZY {0}/bar.cc",
337 FS.Files[testPath("x/build/compile_commands.json")] =
338 llvm::formatv(CDB, llvm::sys::path::convert_to_slash(
testRoot()));
339 EXPECT_THAT(Command(
"x/foo.cc"), Contains(
"-DXYZZY"));
340 EXPECT_THAT(Command(
"bar.cc"), IsEmpty())
341 <<
"x/build/compile_flags.json only applicable to x/";
344TEST(GlobalCompilationDatabaseTest, CompileFlagsDirectory) {
346 FS.Files[
testPath(
"x/compile_flags.txt")] =
"-DFOO";
347 DirectoryBasedGlobalCompilationDatabase CDB(FS);
350 EXPECT_THAT(
Commands->CommandLine, Contains(
"-DFOO"));
357 *result_listener <<
"command is null";
360 if (!llvm::is_contained(arg->CommandLine, Flag)) {
361 *result_listener <<
"flags are " <<
printArgv(arg->CommandLine);
367TEST(GlobalCompilationDatabaseTest, Config) {
369 FS.Files[
testPath(
"x/compile_flags.txt")] =
"-DX";
370 FS.Files[
testPath(
"x/y/z/compile_flags.txt")] =
"-DZ";
372 Config::CDBSearchSpec Spec;
373 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
374 Opts.ContextProvider = [&](llvm::StringRef
Path) {
376 C.CompileFlags.CDBSearch = Spec;
379 DirectoryBasedGlobalCompilationDatabase CDB(Opts);
400 Spec.FixedCDBPath =
testPath(
"x/y/z");
407TEST(GlobalCompilationDatabaseTest, NonCanonicalFilenames) {
408 OverlayCDB DB(
nullptr);
409 std::vector<std::string> DiscoveredFiles;
411 DB.watch([&DiscoveredFiles](
const std::vector<std::string>
Changes) {
416 llvm::sys::path::append(
Root,
"build",
"..",
"a.cc");
417 EXPECT_TRUE(DB.setCompileCommand(
Root.str(), tooling::CompileCommand()));
418 EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(
testPath(
"a.cc")));
419 DiscoveredFiles.clear();
422 llvm::sys::path::append(
File,
"blabla",
"..",
"a.cc");
424 EXPECT_TRUE(DB.getCompileCommand(
File));
425 EXPECT_FALSE(DB.getProjectInfo(
File));
428TEST_F(OverlayCDBTest, GetProjectInfo) {
429 OverlayCDB DB(Base.get());
433 EXPECT_EQ(DB.getProjectInfo(
File)->SourceRoot,
testRoot());
434 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot,
testRoot());
437 EXPECT_TRUE(DB.setCompileCommand(
File, tooling::CompileCommand()));
438 EXPECT_EQ(DB.getProjectInfo(
File)->SourceRoot,
testRoot());
439 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot,
testRoot());
442TEST(GlobalCompilationDatabaseTest, InferenceWithResponseFile) {
444 auto Command = [&](llvm::StringRef Relative) {
445 DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
446 return DirectoryBasedGlobalCompilationDatabase(Opts)
447 .getCompileCommand(
testPath(Relative))
448 .value_or(tooling::CompileCommand())
451 EXPECT_THAT(Command(
"foo.cc"), IsEmpty());
454 SmallString<1024>
Path;
456 ASSERT_FALSE(llvm::sys::fs::createTemporaryFile(
"args",
"", FD,
Path));
457 llvm::raw_fd_ostream OutStream(FD,
true);
458 OutStream <<
"-DXYZZY";
461 const char *
const CDB =
465 "file": "{0}/foo.cc",
466 "command": "clang @{1} {0}/foo.cc",
471 FS.Files[testPath("compile_commands.json")] =
472 llvm::formatv(CDB, llvm::sys::path::convert_to_slash(
testRoot()),
473 llvm::sys::path::convert_to_slash(
Path));
476 EXPECT_THAT(Command(
"foo.cc"), Contains(
"-DXYZZY"));
478 EXPECT_THAT(Command(
"foo.h"), Contains(
"-DXYZZY"));
484 :
public ::testing::Test {
486 std::shared_ptr<const tooling::CompilationDatabase>
488 llvm::StringRef
Path,
489 std::chrono::steady_clock::time_point FreshTime) {
490 DirectoryBasedGlobalCompilationDatabase::CDBLookupRequest Req;
492 Req.FreshTime = Req.FreshTimeMissing = FreshTime;
493 if (
auto Result = GDB.lookupCDB(Req))
494 return std::move(Result->CDB);
503 auto Cmds = arg->getCompileCommands(
Path);
505 *result_listener <<
"yields no commands";
508 if (!llvm::is_contained(Cmds.front().CommandLine, Flag)) {
509 *result_listener <<
"flags are: " <<
printArgv(Cmds.front().CommandLine);
516 return hasFlag(Flag,
"mock_file_name.cc");
521 auto Stale = std::chrono::steady_clock::now() - std::chrono::minutes(1);
522 auto Fresh = std::chrono::steady_clock::now() + std::chrono::hours(24);
525 FS.Files[
"compile_flags.txt"] =
"-DROOT";
526 auto Root = lookupCDB(GDB,
testPath(
"foo/test.cc"), Stale);
530 FS.Files[
"foo/compile_flags.txt"] =
"-DFOO";
531 EXPECT_EQ(
Root, lookupCDB(GDB,
testPath(
"foo/test.cc"), Stale))
532 <<
"cache still valid";
533 auto Foo = lookupCDB(GDB,
testPath(
"foo/test.cc"), Fresh);
534 EXPECT_THAT(Foo,
hasFlag(
"-DFOO")) <<
"new cdb loaded";
535 EXPECT_EQ(Foo, lookupCDB(GDB,
testPath(
"foo/test.cc"), Stale))
536 <<
"new cdb in cache";
539 ++FS.Timestamps[
"foo/compile_flags.txt"];
540 auto FooAgain = lookupCDB(GDB,
testPath(
"foo/test.cc"), Fresh);
541 EXPECT_EQ(Foo, FooAgain) <<
"Same content, read but not reloaded";
543 FS.Files[
"foo/compile_flags.txt"] =
"-DBAR";
544 auto FooAgain2 = lookupCDB(GDB,
testPath(
"foo/test.cc"), Fresh);
545 EXPECT_EQ(Foo, FooAgain2) <<
"Same filesize, change not detected";
547 ++FS.Timestamps[
"foo/compile_flags.txt"];
548 auto Bar = lookupCDB(GDB,
testPath(
"foo/test.cc"), Fresh);
549 EXPECT_THAT(
Bar,
hasFlag(
"-DBAR")) <<
"refreshed with mtime change";
552 FS.Files[
"foo/compile_flags.txt"] =
"-DFOOBAR";
553 EXPECT_EQ(
Bar, lookupCDB(GDB,
testPath(
"foo/test.cc"), Stale))
554 <<
"cache still valid";
555 auto FooBar = lookupCDB(GDB,
testPath(
"foo/test.cc"), Fresh);
556 EXPECT_THAT(FooBar,
hasFlag(
"-DFOOBAR")) <<
"cdb reloaded";
559 FS.Files[
"foo/compile_commands.json"] =
560 llvm::formatv(R
"json([{
561 "file": "{0}/foo/mock_file.cc",
562 "command": "clang -DBAZ mock_file.cc",
563 "directory": "{0}/foo",
565 llvm::sys::path::convert_to_slash(testRoot()));
566 EXPECT_EQ(FooBar, lookupCDB(GDB, testPath("foo/test.cc"), Stale))
567 <<
"cache still valid";
568 auto Baz = lookupCDB(GDB,
testPath(
"foo/test.cc"), Fresh);
570 <<
"compile_commands overrides compile_flags";
574 FS.Files.erase(
"foo/compile_commands.json");
575 auto FoobarAgain = lookupCDB(GDB,
testPath(
"foo/test.cc"), Fresh);
576 EXPECT_THAT(FoobarAgain,
hasFlag(
"-DFOOBAR")) <<
"reloaded compile_flags";
577 EXPECT_NE(FoobarAgain, FooBar) <<
"CDB discarded (shadowed within directory)";
581 FS.Files.erase(
"foo/compile_flags.txt");
582 EXPECT_EQ(
Root, lookupCDB(GDB,
testPath(
"foo/test.cc"), Fresh))
583 <<
"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.
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.
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.