Lines Matching +full:llvm +full:- +full:builddir

1 //===-- GlobalCompilationDatabaseTests.cpp ----------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
49 // .h files have unknown language, so they are parsed liberally as obj-c++.
51 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-xobjective-c++-header",
54 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-xobjective-c++-header",
58 static tooling::CompileCommand cmd(llvm::StringRef File, llvm::StringRef Arg) {
67 getCompileCommand(llvm::StringRef File) const override {
69 return cmd(File, "-DA=1");
74 getFallbackCommand(llvm::StringRef File) const override {
75 return cmd(File, "-DA=2");
90 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
91 AllOf(Contains(testPath("foo.cc")), Contains("-DA=1")));
94 auto Override = cmd(testPath("foo.cc"), "-DA=3");
97 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
98 Contains("-DA=3"));
102 EXPECT_THAT(CDB.getCompileCommand(testPath("missing.cc"))->CommandLine,
103 Contains("-DA=3"));
107 OverlayCDB CDB(Base.get(), {"-DA=4"});
109 ElementsAre("clang", "-DA=2", testPath("bar.cc"), "-DA=4"));
113 OverlayCDB CDB(nullptr, {"-DA=6"});
115 auto Override = cmd(testPath("bar.cc"), "-DA=5");
117 EXPECT_THAT(CDB.getCompileCommand(testPath("bar.cc"))->CommandLine,
118 Contains("-DA=5"));
121 ElementsAre("clang", testPath("foo.cc"), "-DA=6"));
142 OverlayCDB CDB(Base.get(), {"-DFallback"},
143 [](tooling::CompileCommand &Cmd, llvm::StringRef File) {
145 ("-DAdjust_" + llvm::sys::path::filename(File)).str());
149 EXPECT_THAT(Cmd.CommandLine, ElementsAre("clang", "-DA=1", testPath("foo.cc"),
150 "-DAdjust_foo.cc"));
155 BarCommand.CommandLine = {"clang++", "-DB=1", testPath("bar.cc")};
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"));
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"});
179 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
180 Contains("-Wall"));
216 llvm::formatv(CDBOuter, llvm::sys::path::convert_to_slash(testRoot()));
218 llvm::formatv(CDBInner, llvm::sys::path::convert_to_slash(testRoot()));
219 FS.Files[testPath("foo/compile_flags.txt")] = "-DFOO";
246 Opts.ContextProvider = [&](llvm::StringRef Path) {
272 EXPECT_THAT(Cmd->CommandLine, Contains("-DFOO")) << "a.cc uses foo/ CDB";
312 TEST(GlobalCompilationDatabaseTest, BuildDir) {
314 auto Command = [&](llvm::StringRef Relative) {
327 "command": "clang -DXYZZY {0}/x/foo.cc",
332 "command": "clang -DXYZZY {0}/bar.cc",
338 llvm::formatv(CDB, llvm::sys::path::convert_to_slash(testRoot()));
339 EXPECT_THAT(Command("x/foo.cc"), Contains("-DXYZZY"));
346 FS.Files[testPath("x/compile_flags.txt")] = "-DFOO";
350 EXPECT_THAT(Commands->CommandLine, Contains("-DFOO"));
352 EXPECT_EQ(testPath("x"), Commands->Directory);
360 if (!llvm::is_contained(arg->CommandLine, Flag)) {
361 *result_listener << "flags are " << printArgv(arg->CommandLine);
369 FS.Files[testPath("x/compile_flags.txt")] = "-DX";
370 FS.Files[testPath("x/y/z/compile_flags.txt")] = "-DZ";
374 Opts.ContextProvider = [&](llvm::StringRef Path) {
383 EXPECT_THAT(CDB.getCompileCommand(testPath("x/foo.cc")), hasArg("-DX"));
384 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/foo.cc")), hasArg("-DX"));
385 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/z/foo.cc")), hasArg("-DZ"));
401 EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc")), hasArg("-DZ"));
402 EXPECT_THAT(CDB.getCompileCommand(testPath("x/foo.cc")), hasArg("-DZ"));
403 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/foo.cc")), hasArg("-DZ"));
404 EXPECT_THAT(CDB.getCompileCommand(testPath("x/y/z/foo.cc")), hasArg("-DZ"));
415 llvm::SmallString<128> Root(testRoot());
416 llvm::sys::path::append(Root, "build", "..", "a.cc");
421 llvm::SmallString<128> File(testRoot());
422 llvm::sys::path::append(File, "blabla", "..", "a.cc");
433 EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot());
434 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot());
438 EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot());
439 EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot());
444 auto Command = [&](llvm::StringRef Relative) {
456 ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("args", "", FD, Path));
457 llvm::raw_fd_ostream OutStream(FD, true);
458 OutStream << "-DXYZZY";
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"));
488 llvm::StringRef Path,
494 return std::move(Result->CDB);
499 // Matches non-null CDBs which include the specified flag.
503 auto Cmds = arg->getCompileCommands(Path);
508 if (!llvm::is_contained(Cmds.front().CommandLine, Flag)) {
515 auto hasFlag(llvm::StringRef Flag) {
521 auto Stale = std::chrono::steady_clock::now() - std::chrono::minutes(1);
525 FS.Files["compile_flags.txt"] = "-DROOT";
527 EXPECT_THAT(Root, hasFlag("-DROOT"));
529 // Add a compilation database to a subdirectory - CDB loaded.
530 FS.Files["foo/compile_flags.txt"] = "-DFOO";
534 EXPECT_THAT(Foo, hasFlag("-DFOO")) << "new cdb loaded";
538 // Mtime changed, but no content change - CDB not reloaded.
542 // Content changed, but not size or mtime - CDB not reloaded.
543 FS.Files["foo/compile_flags.txt"] = "-DBAR";
546 // Mtime change forces a re-read, and we notice the different content.
549 EXPECT_THAT(Bar, hasFlag("-DBAR")) << "refreshed with mtime change";
551 // Size and content both change - CDB reloaded.
552 FS.Files["foo/compile_flags.txt"] = "-DFOOBAR";
556 EXPECT_THAT(FooBar, hasFlag("-DFOOBAR")) << "cdb reloaded";
560 llvm::formatv(R"json([{
562 "command": "clang -DBAZ mock_file.cc",
565 llvm::sys::path::convert_to_slash(testRoot()));
569 EXPECT_THAT(Baz, hasFlag("-DBAZ", testPath("foo/mock_file.cc")))
576 EXPECT_THAT(FoobarAgain, hasFlag("-DFOOBAR")) << "reloaded compile_flags";
580 // The parent CDB is *not* reloaded (we cache the CDB per-directory).