CompileCommandsTests.cpp
13.2 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//===-- CompileCommandsTests.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "CompileCommands.h"
#include "Config.h"
#include "TestFS.h"
#include "support/Context.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
namespace {
using ::testing::_;
using ::testing::Contains;
using ::testing::ElementsAre;
using ::testing::HasSubstr;
using ::testing::Not;
// Sadly, CommandMangler::detect(), which contains much of the logic, is
// a bunch of untested integration glue. We test the string manipulation here
// assuming its results are correct.
// Make use of all features and assert the exact command we get out.
// Other tests just verify presence/absence of certain args.
TEST(CommandMangler, Everything) {
auto Mangler = CommandMangler::forTests();
Mangler.ClangPath = testPath("fake/clang");
Mangler.ResourceDir = testPath("fake/resources");
Mangler.Sysroot = testPath("fake/sysroot");
std::vector<std::string> Cmd = {"clang++", "-Xclang", "-load", "-Xclang",
"plugin", "-MF", "dep", "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"), "foo.cc",
"-fsyntax-only",
"-resource-dir=" + testPath("fake/resources"),
"-isysroot", testPath("fake/sysroot")));
}
TEST(CommandMangler, ResourceDir) {
auto Mangler = CommandMangler::forTests();
Mangler.ResourceDir = testPath("fake/resources");
std::vector<std::string> Cmd = {"clang++", "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_THAT(Cmd, Contains("-resource-dir=" + testPath("fake/resources")));
}
TEST(CommandMangler, Sysroot) {
auto Mangler = CommandMangler::forTests();
Mangler.Sysroot = testPath("fake/sysroot");
std::vector<std::string> Cmd = {"clang++", "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_THAT(llvm::join(Cmd, " "),
HasSubstr("-isysroot " + testPath("fake/sysroot")));
}
TEST(CommandMangler, StripPlugins) {
auto Mangler = CommandMangler::forTests();
std::vector<std::string> Cmd = {"clang++", "-Xclang", "-load",
"-Xclang", "plugin", "foo.cc"};
Mangler.adjust(Cmd);
for (const char* Stripped : {"-Xclang", "-load", "plugin"})
EXPECT_THAT(Cmd, Not(Contains(Stripped)));
}
TEST(CommandMangler, StripOutput) {
auto Mangler = CommandMangler::forTests();
std::vector<std::string> Cmd = {"clang++", "-MF", "dependency", "-c",
"foo.cc"};
Mangler.adjust(Cmd);
for (const char* Stripped : {"-MF", "dependency"})
EXPECT_THAT(Cmd, Not(Contains(Stripped)));
}
TEST(CommandMangler, StripShowIncludes) {
auto Mangler = CommandMangler::forTests();
std::vector<std::string> Cmd = {"clang-cl", "/showIncludes", "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_THAT(Cmd, Not(Contains("/showIncludes")));
}
TEST(CommandMangler, StripShowIncludesUser) {
auto Mangler = CommandMangler::forTests();
std::vector<std::string> Cmd = {"clang-cl", "/showIncludes:user", "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_THAT(Cmd, Not(Contains("/showIncludes:user")));
}
TEST(CommandMangler, ClangPath) {
auto Mangler = CommandMangler::forTests();
Mangler.ClangPath = testPath("fake/clang");
std::vector<std::string> Cmd = {"clang++", "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_EQ(testPath("fake/clang++"), Cmd.front());
Cmd = {"unknown-binary", "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_EQ(testPath("fake/unknown-binary"), Cmd.front());
Cmd = {testPath("path/clang++"), "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_EQ(testPath("path/clang++"), Cmd.front());
Cmd = {"foo/unknown-binary", "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_EQ("foo/unknown-binary", Cmd.front());
}
// Only run the PATH/symlink resolving test on unix, we need to fiddle
// with permissions and environment variables...
#ifdef LLVM_ON_UNIX
MATCHER(Ok, "") {
if (arg) {
*result_listener << arg.message();
return false;
}
return true;
}
TEST(CommandMangler, ClangPathResolve) {
// Set up filesystem:
// /temp/
// bin/
// foo -> temp/lib/bar
// lib/
// bar
llvm::SmallString<256> TempDir;
ASSERT_THAT(llvm::sys::fs::createUniqueDirectory("ClangPathResolve", TempDir),
Ok());
// /var/tmp is a symlink on Mac. Resolve it so we're asserting the right path.
ASSERT_THAT(llvm::sys::fs::real_path(TempDir.str(), TempDir), Ok());
auto CleanDir = llvm::make_scope_exit(
[&] { llvm::sys::fs::remove_directories(TempDir); });
ASSERT_THAT(llvm::sys::fs::create_directory(TempDir + "/bin"), Ok());
ASSERT_THAT(llvm::sys::fs::create_directory(TempDir + "/lib"), Ok());
int FD;
ASSERT_THAT(llvm::sys::fs::openFileForWrite(TempDir + "/lib/bar", FD), Ok());
ASSERT_THAT(llvm::sys::Process::SafelyCloseFileDescriptor(FD), Ok());
::chmod((TempDir + "/lib/bar").str().c_str(), 0755); // executable
ASSERT_THAT(
llvm::sys::fs::create_link(TempDir + "/lib/bar", TempDir + "/bin/foo"),
Ok());
// Test the case where the driver is an absolute path to a symlink.
auto Mangler = CommandMangler::forTests();
Mangler.ClangPath = testPath("fake/clang");
std::vector<std::string> Cmd = {(TempDir + "/bin/foo").str(), "foo.cc"};
Mangler.adjust(Cmd);
// Directory based on resolved symlink, basename preserved.
EXPECT_EQ((TempDir + "/lib/foo").str(), Cmd.front());
// Set PATH to point to temp/bin so we can find 'foo' on it.
ASSERT_TRUE(::getenv("PATH"));
auto RestorePath =
llvm::make_scope_exit([OldPath = std::string(::getenv("PATH"))] {
::setenv("PATH", OldPath.c_str(), 1);
});
::setenv("PATH", (TempDir + "/bin").str().c_str(), /*overwrite=*/1);
// Test the case where the driver is a $PATH-relative path to a symlink.
Mangler = CommandMangler::forTests();
Mangler.ClangPath = testPath("fake/clang");
// Driver found on PATH.
Cmd = {"foo", "foo.cc"};
Mangler.adjust(Cmd);
// Found the symlink and resolved the path as above.
EXPECT_EQ((TempDir + "/lib/foo").str(), Cmd.front());
// Symlink not resolved with -no-canonical-prefixes.
Cmd = {"foo", "-no-canonical-prefixes", "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_EQ((TempDir + "/bin/foo").str(), Cmd.front());
}
#endif
TEST(CommandMangler, ConfigEdits) {
auto Mangler = CommandMangler::forTests();
std::vector<std::string> Cmd = {"clang++", "foo.cc"};
{
Config Cfg;
Cfg.CompileFlags.Edits.push_back([](std::vector<std::string> &Argv) {
for (auto &Arg : Argv)
for (char &C : Arg)
C = llvm::toUpper(C);
});
Cfg.CompileFlags.Edits.push_back(
[](std::vector<std::string> &Argv) { Argv.push_back("--hello"); });
WithContextValue WithConfig(Config::Key, std::move(Cfg));
Mangler.adjust(Cmd);
}
// Edits are applied in given order and before other mangling.
EXPECT_THAT(Cmd, ElementsAre(_, "FOO.CC", "--hello", "-fsyntax-only"));
}
static std::string strip(llvm::StringRef Arg, llvm::StringRef Argv) {
llvm::SmallVector<llvm::StringRef, 8> Parts;
llvm::SplitString(Argv, Parts);
std::vector<std::string> Args = {Parts.begin(), Parts.end()};
ArgStripper S;
S.strip(Arg);
S.process(Args);
return llvm::join(Args, " ");
}
TEST(ArgStripperTest, Spellings) {
// May use alternate prefixes.
EXPECT_EQ(strip("-pedantic", "clang -pedantic foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("-pedantic", "clang --pedantic foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("--pedantic", "clang -pedantic foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("--pedantic", "clang --pedantic foo.cc"), "clang foo.cc");
// May use alternate names.
EXPECT_EQ(strip("-x", "clang -x c++ foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("-x", "clang --language=c++ foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("--language=", "clang -x c++ foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("--language=", "clang --language=c++ foo.cc"),
"clang foo.cc");
}
TEST(ArgStripperTest, UnknownFlag) {
EXPECT_EQ(strip("-xyzzy", "clang -xyzzy foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("-xyz*", "clang -xyzzy foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("-xyzzy", "clang -Xclang -xyzzy foo.cc"), "clang foo.cc");
}
TEST(ArgStripperTest, Xclang) {
// Flags may be -Xclang escaped.
EXPECT_EQ(strip("-ast-dump", "clang -Xclang -ast-dump foo.cc"),
"clang foo.cc");
// Args may be -Xclang escaped.
EXPECT_EQ(strip("-add-plugin", "clang -Xclang -add-plugin -Xclang z foo.cc"),
"clang foo.cc");
}
TEST(ArgStripperTest, ClangCL) {
// /I is a synonym for -I in clang-cl mode only.
// Not stripped by default.
EXPECT_EQ(strip("-I", "clang -I /usr/inc /Interesting/file.cc"),
"clang /Interesting/file.cc");
// Stripped when invoked as clang-cl.
EXPECT_EQ(strip("-I", "clang-cl -I /usr/inc /Interesting/file.cc"),
"clang-cl");
// Stripped when invoked as CL.EXE
EXPECT_EQ(strip("-I", "CL.EXE -I /usr/inc /Interesting/file.cc"), "CL.EXE");
// Stripped when passed --driver-mode=cl.
EXPECT_EQ(strip("-I", "cc -I /usr/inc /Interesting/file.cc --driver-mode=cl"),
"cc --driver-mode=cl");
}
TEST(ArgStripperTest, ArgStyles) {
// Flag
EXPECT_EQ(strip("-Qn", "clang -Qn foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("-Qn", "clang -QnZ foo.cc"), "clang -QnZ foo.cc");
// Joined
EXPECT_EQ(strip("-std=", "clang -std= foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("-std=", "clang -std=c++11 foo.cc"), "clang foo.cc");
// Separate
EXPECT_EQ(strip("-mllvm", "clang -mllvm X foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("-mllvm", "clang -mllvmX foo.cc"), "clang -mllvmX foo.cc");
// RemainingArgsJoined
EXPECT_EQ(strip("/link", "clang-cl /link b c d foo.cc"), "clang-cl");
EXPECT_EQ(strip("/link", "clang-cl /linka b c d foo.cc"), "clang-cl");
// CommaJoined
EXPECT_EQ(strip("-Wl,", "clang -Wl,x,y foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("-Wl,", "clang -Wl, foo.cc"), "clang foo.cc");
// MultiArg
EXPECT_EQ(strip("-segaddr", "clang -segaddr a b foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("-segaddr", "clang -segaddra b foo.cc"),
"clang -segaddra b foo.cc");
// JoinedOrSeparate
EXPECT_EQ(strip("-G", "clang -GX foo.cc"), "clang foo.cc");
EXPECT_EQ(strip("-G", "clang -G X foo.cc"), "clang foo.cc");
// JoinedAndSeparate
EXPECT_EQ(strip("-plugin-arg-", "clang -cc1 -plugin-arg-X Y foo.cc"),
"clang -cc1 foo.cc");
EXPECT_EQ(strip("-plugin-arg-", "clang -cc1 -plugin-arg- Y foo.cc"),
"clang -cc1 foo.cc");
}
TEST(ArgStripperTest, EndOfList) {
// When we hit the end-of-args prematurely, we don't crash.
// We consume the incomplete args if we've matched the target option.
EXPECT_EQ(strip("-I", "clang -Xclang"), "clang -Xclang");
EXPECT_EQ(strip("-I", "clang -Xclang -I"), "clang");
EXPECT_EQ(strip("-I", "clang -I -Xclang"), "clang");
EXPECT_EQ(strip("-I", "clang -I"), "clang");
}
TEST(ArgStripperTest, Multiple) {
ArgStripper S;
S.strip("-o");
S.strip("-c");
std::vector<std::string> Args = {"clang", "-o", "foo.o", "foo.cc", "-c"};
S.process(Args);
EXPECT_THAT(Args, ElementsAre("clang", "foo.cc"));
}
TEST(ArgStripperTest, Warning) {
{
// -W is a flag name
ArgStripper S;
S.strip("-W");
std::vector<std::string> Args = {"clang", "-Wfoo", "-Wno-bar", "-Werror",
"foo.cc"};
S.process(Args);
EXPECT_THAT(Args, ElementsAre("clang", "foo.cc"));
}
{
// -Wfoo is not a flag name, matched literally.
ArgStripper S;
S.strip("-Wunused");
std::vector<std::string> Args = {"clang", "-Wunused", "-Wno-unused",
"foo.cc"};
S.process(Args);
EXPECT_THAT(Args, ElementsAre("clang", "-Wno-unused", "foo.cc"));
}
}
TEST(ArgStripperTest, Define) {
{
// -D is a flag name
ArgStripper S;
S.strip("-D");
std::vector<std::string> Args = {"clang", "-Dfoo", "-Dbar=baz", "foo.cc"};
S.process(Args);
EXPECT_THAT(Args, ElementsAre("clang", "foo.cc"));
}
{
// -Dbar is not: matched literally
ArgStripper S;
S.strip("-Dbar");
std::vector<std::string> Args = {"clang", "-Dfoo", "-Dbar=baz", "foo.cc"};
S.process(Args);
EXPECT_THAT(Args, ElementsAre("clang", "-Dfoo", "-Dbar=baz", "foo.cc"));
S.strip("-Dfoo");
S.process(Args);
EXPECT_THAT(Args, ElementsAre("clang", "-Dbar=baz", "foo.cc"));
S.strip("-Dbar=*");
S.process(Args);
EXPECT_THAT(Args, ElementsAre("clang", "foo.cc"));
}
}
TEST(ArgStripperTest, OrderDependent) {
ArgStripper S;
// If -include is stripped first, we see -pch as its arg and foo.pch remains.
// To get this case right, we must process -include-pch first.
S.strip("-include");
S.strip("-include-pch");
std::vector<std::string> Args = {"clang", "-include-pch", "foo.pch",
"foo.cc"};
S.process(Args);
EXPECT_THAT(Args, ElementsAre("clang", "foo.cc"));
}
} // namespace
} // namespace clangd
} // namespace clang