1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
//===-- GlobalCompilationDatabaseTests.cpp ----------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "GlobalCompilationDatabase.h"
#include "TestFS.h"
#include "llvm/ADT/StringExtras.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
namespace {
using ::testing::ElementsAre;
using ::testing::EndsWith;
TEST(GlobalCompilationDatabaseTest, FallbackCommand) {
DirectoryBasedGlobalCompilationDatabase DB(None);
auto Cmd = DB.getFallbackCommand(testPath("foo/bar.cc"));
EXPECT_EQ(Cmd.Directory, testPath("foo"));
EXPECT_THAT(Cmd.CommandLine, ElementsAre(
EndsWith("clang"), testPath("foo/bar.cc")));
EXPECT_EQ(Cmd.Output, "");
// .h files have unknown language, so they are parsed liberally as obj-c++.
Cmd = DB.getFallbackCommand(testPath("foo/bar.h"));
EXPECT_THAT(Cmd.CommandLine,
ElementsAre(EndsWith("clang"), "-xobjective-c++-header",
testPath("foo/bar.h")));
}
static tooling::CompileCommand cmd(llvm::StringRef File, llvm::StringRef Arg) {
return tooling::CompileCommand(testRoot(), File, {"clang", Arg, File}, "");
}
class OverlayCDBTest : public ::testing::Test {
class BaseCDB : public GlobalCompilationDatabase {
public:
llvm::Optional<tooling::CompileCommand>
getCompileCommand(llvm::StringRef File,
ProjectInfo *Project) const override {
if (File == testPath("foo.cc")) {
if (Project)
Project->SourceRoot = testRoot();
return cmd(File, "-DA=1");
}
return None;
}
tooling::CompileCommand
getFallbackCommand(llvm::StringRef File) const override {
return cmd(File, "-DA=2");
}
};
protected:
OverlayCDBTest() : Base(llvm::make_unique<BaseCDB>()) {}
std::unique_ptr<GlobalCompilationDatabase> Base;
};
TEST_F(OverlayCDBTest, GetCompileCommand) {
OverlayCDB CDB(Base.get());
EXPECT_EQ(CDB.getCompileCommand(testPath("foo.cc")),
Base->getCompileCommand(testPath("foo.cc")));
EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None);
auto Override = cmd(testPath("foo.cc"), "-DA=3");
CDB.setCompileCommand(testPath("foo.cc"), Override);
EXPECT_EQ(CDB.getCompileCommand(testPath("foo.cc")), Override);
EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None);
CDB.setCompileCommand(testPath("missing.cc"), Override);
EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), Override);
}
TEST_F(OverlayCDBTest, GetFallbackCommand) {
OverlayCDB CDB(Base.get(), {"-DA=4"});
EXPECT_THAT(CDB.getFallbackCommand(testPath("bar.cc")).CommandLine,
ElementsAre("clang", "-DA=2", testPath("bar.cc"), "-DA=4"));
}
TEST_F(OverlayCDBTest, NoBase) {
OverlayCDB CDB(nullptr, {"-DA=6"});
EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), None);
auto Override = cmd(testPath("bar.cc"), "-DA=5");
CDB.setCompileCommand(testPath("bar.cc"), Override);
EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), Override);
EXPECT_THAT(CDB.getFallbackCommand(testPath("foo.cc")).CommandLine,
ElementsAre(EndsWith("clang"), testPath("foo.cc"), "-DA=6"));
}
TEST_F(OverlayCDBTest, Watch) {
OverlayCDB Inner(nullptr);
OverlayCDB Outer(&Inner);
std::vector<std::vector<std::string>> Changes;
auto Sub = Outer.watch([&](const std::vector<std::string> &ChangedFiles) {
Changes.push_back(ChangedFiles);
});
Inner.setCompileCommand("A.cpp", tooling::CompileCommand());
Outer.setCompileCommand("B.cpp", tooling::CompileCommand());
Inner.setCompileCommand("A.cpp", llvm::None);
Outer.setCompileCommand("C.cpp", llvm::None);
EXPECT_THAT(Changes, ElementsAre(ElementsAre("A.cpp"), ElementsAre("B.cpp"),
ElementsAre("A.cpp"), ElementsAre("C.cpp")));
}
} // namespace
} // namespace clangd
} // namespace clang
|