xref: /llvm-project/clang/unittests/Tooling/CompilationDatabaseTest.cpp (revision 2756e2ee0bceaf532d4990cbb4f2a84fe62b6b84)
1 //===- unittest/Tooling/CompilationDatabaseTest.cpp -----------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/AST/DeclCXX.h"
10 #include "clang/AST/DeclGroup.h"
11 #include "clang/Frontend/FrontendAction.h"
12 #include "clang/Tooling/CompilationDatabase.h"
13 #include "clang/Tooling/FileMatchTrie.h"
14 #include "clang/Tooling/JSONCompilationDatabase.h"
15 #include "clang/Tooling/Tooling.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/TargetSelect.h"
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20 
21 namespace clang {
22 namespace tooling {
23 
24 using testing::ElementsAre;
25 using testing::EndsWith;
26 
27 static void expectFailure(StringRef JSONDatabase, StringRef Explanation) {
28   std::string ErrorMessage;
29   EXPECT_EQ(nullptr,
30             JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage,
31                                                     JSONCommandLineSyntax::Gnu))
32       << "Expected an error because of: " << Explanation.str();
33 }
34 
35 TEST(JSONCompilationDatabase, ErrsOnInvalidFormat) {
36   expectFailure("", "Empty database");
37   expectFailure("{", "Invalid JSON");
38   expectFailure("[[]]", "Array instead of object");
39   expectFailure("[{\"a\":[]}]", "Array instead of value");
40   expectFailure("[{\"a\":\"b\"}]", "Unknown key");
41   expectFailure("[{[]:\"\"}]", "Incorrectly typed entry");
42   expectFailure("[{}]", "Empty entry");
43   expectFailure("[{\"directory\":\"\",\"command\":\"\"}]", "Missing file");
44   expectFailure("[{\"directory\":\"\",\"file\":\"\"}]", "Missing command or arguments");
45   expectFailure("[{\"command\":\"\",\"file\":\"\"}]", "Missing directory");
46   expectFailure("[{\"directory\":\"\",\"arguments\":[]}]", "Missing file");
47   expectFailure("[{\"arguments\":\"\",\"file\":\"\"}]", "Missing directory");
48   expectFailure("[{\"directory\":\"\",\"arguments\":\"\",\"file\":\"\"}]", "Arguments not array");
49   expectFailure("[{\"directory\":\"\",\"command\":[],\"file\":\"\"}]", "Command not string");
50   expectFailure("[{\"directory\":\"\",\"arguments\":[[]],\"file\":\"\"}]",
51                 "Arguments contain non-string");
52   expectFailure("[{\"output\":[]}]", "Expected strings as value.");
53 }
54 
55 static std::vector<std::string> getAllFiles(StringRef JSONDatabase,
56                                             std::string &ErrorMessage,
57                                             JSONCommandLineSyntax Syntax) {
58   std::unique_ptr<CompilationDatabase> Database(
59       JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage,
60                                               Syntax));
61   if (!Database) {
62     ADD_FAILURE() << ErrorMessage;
63     return std::vector<std::string>();
64   }
65   return Database->getAllFiles();
66 }
67 
68 static std::vector<CompileCommand>
69 getAllCompileCommands(JSONCommandLineSyntax Syntax, StringRef JSONDatabase,
70                       std::string &ErrorMessage) {
71   std::unique_ptr<CompilationDatabase> Database(
72       JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage,
73                                               Syntax));
74   if (!Database) {
75     ADD_FAILURE() << ErrorMessage;
76     return std::vector<CompileCommand>();
77   }
78   return Database->getAllCompileCommands();
79 }
80 
81 TEST(JSONCompilationDatabase, GetAllFiles) {
82   std::string ErrorMessage;
83   EXPECT_EQ(std::vector<std::string>(),
84             getAllFiles("[]", ErrorMessage, JSONCommandLineSyntax::Gnu))
85       << ErrorMessage;
86 
87   std::vector<std::string> expected_files;
88   SmallString<16> PathStorage;
89   llvm::sys::path::native("//net/dir/file1", PathStorage);
90   expected_files.push_back(std::string(PathStorage.str()));
91   llvm::sys::path::native("//net/dir/file2", PathStorage);
92   expected_files.push_back(std::string(PathStorage.str()));
93   llvm::sys::path::native("//net/file1", PathStorage);
94   expected_files.push_back(std::string(PathStorage.str()));
95   EXPECT_EQ(expected_files,
96             getAllFiles("[{\"directory\":\"//net/dir\","
97                         "\"command\":\"command\","
98                         "\"file\":\"file1\"},"
99                         " {\"directory\":\"//net/dir\","
100                         "\"command\":\"command\","
101                         "\"file\":\"../file1\"},"
102                         " {\"directory\":\"//net/dir\","
103                         "\"command\":\"command\","
104                         "\"file\":\"file2\"}]",
105                         ErrorMessage, JSONCommandLineSyntax::Gnu))
106       << ErrorMessage;
107 }
108 
109 TEST(JSONCompilationDatabase, GetAllCompileCommands) {
110   std::string ErrorMessage;
111   EXPECT_EQ(
112       0u, getAllCompileCommands(JSONCommandLineSyntax::Gnu, "[]", ErrorMessage)
113               .size())
114       << ErrorMessage;
115 
116   StringRef Directory1("//net/dir1");
117   StringRef FileName1("file1");
118   StringRef Command1("command1");
119   StringRef Output1("file1.o");
120   StringRef Directory2("//net/dir2");
121   StringRef FileName2("file2");
122   StringRef Command2("command2");
123   StringRef Output2("");
124 
125   std::vector<CompileCommand> Commands = getAllCompileCommands(
126       JSONCommandLineSyntax::Gnu,
127       ("[{\"directory\":\"" + Directory1 + "\"," + "\"command\":\"" + Command1 +
128        "\","
129        "\"file\":\"" +
130        FileName1 + "\", \"output\":\"" +
131        Output1 + "\"},"
132                    " {\"directory\":\"" +
133        Directory2 + "\"," + "\"command\":\"" + Command2 + "\","
134                                                           "\"file\":\"" +
135        FileName2 + "\"}]")
136           .str(),
137       ErrorMessage);
138   EXPECT_EQ(2U, Commands.size()) << ErrorMessage;
139   EXPECT_EQ(Directory1, Commands[0].Directory) << ErrorMessage;
140   EXPECT_EQ(FileName1, Commands[0].Filename) << ErrorMessage;
141   EXPECT_EQ(Output1, Commands[0].Output) << ErrorMessage;
142   ASSERT_EQ(1u, Commands[0].CommandLine.size());
143   EXPECT_EQ(Command1, Commands[0].CommandLine[0]) << ErrorMessage;
144   EXPECT_EQ(Directory2, Commands[1].Directory) << ErrorMessage;
145   EXPECT_EQ(FileName2, Commands[1].Filename) << ErrorMessage;
146   EXPECT_EQ(Output2, Commands[1].Output) << ErrorMessage;
147   ASSERT_EQ(1u, Commands[1].CommandLine.size());
148   EXPECT_EQ(Command2, Commands[1].CommandLine[0]) << ErrorMessage;
149 
150   // Check that order is preserved.
151   Commands = getAllCompileCommands(
152       JSONCommandLineSyntax::Gnu,
153       ("[{\"directory\":\"" + Directory2 + "\"," + "\"command\":\"" + Command2 +
154        "\","
155        "\"file\":\"" +
156        FileName2 + "\"},"
157                    " {\"directory\":\"" +
158        Directory1 + "\"," + "\"command\":\"" + Command1 + "\","
159                                                           "\"file\":\"" +
160        FileName1 + "\"}]")
161           .str(),
162       ErrorMessage);
163   EXPECT_EQ(2U, Commands.size()) << ErrorMessage;
164   EXPECT_EQ(Directory2, Commands[0].Directory) << ErrorMessage;
165   EXPECT_EQ(FileName2, Commands[0].Filename) << ErrorMessage;
166   ASSERT_EQ(1u, Commands[0].CommandLine.size());
167   EXPECT_EQ(Command2, Commands[0].CommandLine[0]) << ErrorMessage;
168   EXPECT_EQ(Directory1, Commands[1].Directory) << ErrorMessage;
169   EXPECT_EQ(FileName1, Commands[1].Filename) << ErrorMessage;
170   ASSERT_EQ(1u, Commands[1].CommandLine.size());
171   EXPECT_EQ(Command1, Commands[1].CommandLine[0]) << ErrorMessage;
172 }
173 
174 static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName,
175                                                     StringRef JSONDatabase,
176                                                     std::string &ErrorMessage) {
177   std::unique_ptr<CompilationDatabase> Database(
178       JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage,
179                                               JSONCommandLineSyntax::Gnu));
180   if (!Database)
181     return CompileCommand();
182   std::vector<CompileCommand> Commands = Database->getCompileCommands(FileName);
183   EXPECT_LE(Commands.size(), 1u);
184   if (Commands.empty())
185     return CompileCommand();
186   return Commands[0];
187 }
188 
189 TEST(JSONCompilationDatabase, ArgumentsPreferredOverCommand) {
190    StringRef Directory("//net/dir");
191    StringRef FileName("//net/dir/filename");
192    StringRef Command("command");
193    StringRef Arguments = "arguments";
194    Twine ArgumentsAccumulate;
195    std::string ErrorMessage;
196    CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
197       FileName,
198       ("[{\"directory\":\"" + Directory + "\","
199          "\"arguments\":[\"" + Arguments + "\"],"
200          "\"command\":\"" + Command + "\","
201          "\"file\":\"" + FileName + "\"}]").str(),
202       ErrorMessage);
203    EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage;
204    EXPECT_EQ(1u, FoundCommand.CommandLine.size()) << ErrorMessage;
205    EXPECT_EQ(Arguments, FoundCommand.CommandLine[0]) << ErrorMessage;
206 }
207 
208 struct FakeComparator : public PathComparator {
209   ~FakeComparator() override {}
210   bool equivalent(StringRef FileA, StringRef FileB) const override {
211     return FileA.equals_lower(FileB);
212   }
213 };
214 
215 class FileMatchTrieTest : public ::testing::Test {
216 protected:
217   FileMatchTrieTest() : Trie(new FakeComparator()) {}
218 
219   StringRef find(StringRef Path) {
220     llvm::raw_string_ostream ES(Error);
221     return Trie.findEquivalent(Path, ES);
222   }
223 
224   FileMatchTrie Trie;
225   std::string Error;
226 };
227 
228 TEST_F(FileMatchTrieTest, InsertingRelativePath) {
229   Trie.insert("//net/path/file.cc");
230   Trie.insert("file.cc");
231   EXPECT_EQ("//net/path/file.cc", find("//net/path/file.cc"));
232 }
233 
234 TEST_F(FileMatchTrieTest, MatchingRelativePath) {
235   EXPECT_EQ("", find("file.cc"));
236 }
237 
238 TEST_F(FileMatchTrieTest, ReturnsBestResults) {
239   Trie.insert("//net/d/c/b.cc");
240   Trie.insert("//net/d/b/b.cc");
241   EXPECT_EQ("//net/d/b/b.cc", find("//net/d/b/b.cc"));
242 }
243 
244 TEST_F(FileMatchTrieTest, HandlesSymlinks) {
245   Trie.insert("//net/AA/file.cc");
246   EXPECT_EQ("//net/AA/file.cc", find("//net/aa/file.cc"));
247 }
248 
249 TEST_F(FileMatchTrieTest, ReportsSymlinkAmbiguity) {
250   Trie.insert("//net/Aa/file.cc");
251   Trie.insert("//net/aA/file.cc");
252   EXPECT_TRUE(find("//net/aa/file.cc").empty());
253   EXPECT_EQ("Path is ambiguous", Error);
254 }
255 
256 TEST_F(FileMatchTrieTest, LongerMatchingSuffixPreferred) {
257   Trie.insert("//net/src/Aa/file.cc");
258   Trie.insert("//net/src/aA/file.cc");
259   Trie.insert("//net/SRC/aa/file.cc");
260   EXPECT_EQ("//net/SRC/aa/file.cc", find("//net/src/aa/file.cc"));
261 }
262 
263 TEST_F(FileMatchTrieTest, EmptyTrie) {
264   EXPECT_TRUE(find("//net/some/path").empty());
265 }
266 
267 TEST_F(FileMatchTrieTest, NoResult) {
268   Trie.insert("//net/somepath/otherfile.cc");
269   Trie.insert("//net/otherpath/somefile.cc");
270   EXPECT_EQ("", find("//net/somepath/somefile.cc"));
271 }
272 
273 TEST_F(FileMatchTrieTest, RootElementDifferent) {
274   Trie.insert("//net/path/file.cc");
275   Trie.insert("//net/otherpath/file.cc");
276   EXPECT_EQ("//net/path/file.cc", find("//net/path/file.cc"));
277 }
278 
279 TEST_F(FileMatchTrieTest, CannotResolveRelativePath) {
280   EXPECT_EQ("", find("relative-path.cc"));
281   EXPECT_EQ("Cannot resolve relative paths", Error);
282 }
283 
284 TEST_F(FileMatchTrieTest, SingleFile) {
285   Trie.insert("/root/RootFile.cc");
286   EXPECT_EQ("", find("/root/rootfile.cc"));
287   // Add subpath to avoid `if (Children.empty())` special case
288   // which we hit at previous `find()`.
289   Trie.insert("/root/otherpath/OtherFile.cc");
290   EXPECT_EQ("", find("/root/rootfile.cc"));
291 }
292 
293 TEST(findCompileArgsInJsonDatabase, FindsNothingIfEmpty) {
294   std::string ErrorMessage;
295   CompileCommand NotFound = findCompileArgsInJsonDatabase(
296     "a-file.cpp", "", ErrorMessage);
297   EXPECT_TRUE(NotFound.CommandLine.empty()) << ErrorMessage;
298   EXPECT_TRUE(NotFound.Directory.empty()) << ErrorMessage;
299 }
300 
301 TEST(findCompileArgsInJsonDatabase, ReadsSingleEntry) {
302   StringRef Directory("//net/some/directory");
303   StringRef FileName("//net/path/to/a-file.cpp");
304   StringRef Command("//net/path/to/compiler and some arguments");
305   std::string ErrorMessage;
306   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
307     FileName,
308     ("[{\"directory\":\"" + Directory + "\"," +
309        "\"command\":\"" + Command + "\","
310        "\"file\":\"" + FileName + "\"}]").str(),
311     ErrorMessage);
312   EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage;
313   ASSERT_EQ(4u, FoundCommand.CommandLine.size()) << ErrorMessage;
314   EXPECT_EQ("//net/path/to/compiler",
315             FoundCommand.CommandLine[0]) << ErrorMessage;
316   EXPECT_EQ("and", FoundCommand.CommandLine[1]) << ErrorMessage;
317   EXPECT_EQ("some", FoundCommand.CommandLine[2]) << ErrorMessage;
318   EXPECT_EQ("arguments", FoundCommand.CommandLine[3]) << ErrorMessage;
319 
320   CompileCommand NotFound = findCompileArgsInJsonDatabase(
321     "a-file.cpp",
322     ("[{\"directory\":\"" + Directory + "\"," +
323        "\"command\":\"" + Command + "\","
324        "\"file\":\"" + FileName + "\"}]").str(),
325     ErrorMessage);
326   EXPECT_TRUE(NotFound.Directory.empty()) << ErrorMessage;
327   EXPECT_TRUE(NotFound.CommandLine.empty()) << ErrorMessage;
328 }
329 
330 TEST(findCompileArgsInJsonDatabase, ReadsCompileCommandLinesWithSpaces) {
331   StringRef Directory("//net/some/directory");
332   StringRef FileName("//net/path/to/a-file.cpp");
333   StringRef Command("\\\"//net/path to compiler\\\" \\\"and an argument\\\"");
334   std::string ErrorMessage;
335   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
336     FileName,
337     ("[{\"directory\":\"" + Directory + "\"," +
338        "\"command\":\"" + Command + "\","
339        "\"file\":\"" + FileName + "\"}]").str(),
340     ErrorMessage);
341   ASSERT_EQ(2u, FoundCommand.CommandLine.size());
342   EXPECT_EQ("//net/path to compiler",
343             FoundCommand.CommandLine[0]) << ErrorMessage;
344   EXPECT_EQ("and an argument", FoundCommand.CommandLine[1]) << ErrorMessage;
345 }
346 
347 TEST(findCompileArgsInJsonDatabase, ReadsDirectoryWithSpaces) {
348   StringRef Directory("//net/some directory / with spaces");
349   StringRef FileName("//net/path/to/a-file.cpp");
350   StringRef Command("a command");
351   std::string ErrorMessage;
352   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
353     FileName,
354     ("[{\"directory\":\"" + Directory + "\"," +
355        "\"command\":\"" + Command + "\","
356        "\"file\":\"" + FileName + "\"}]").str(),
357     ErrorMessage);
358   EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage;
359 }
360 
361 TEST(findCompileArgsInJsonDatabase, FindsEntry) {
362   StringRef Directory("//net/directory");
363   StringRef FileName("file");
364   StringRef Command("command");
365   std::string JsonDatabase = "[";
366   for (int I = 0; I < 10; ++I) {
367     if (I > 0) JsonDatabase += ",";
368     JsonDatabase +=
369       ("{\"directory\":\"" + Directory + Twine(I) + "\"," +
370         "\"command\":\"" + Command + Twine(I) + "\","
371         "\"file\":\"" + FileName + Twine(I) + "\"}").str();
372   }
373   JsonDatabase += "]";
374   std::string ErrorMessage;
375   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
376     "//net/directory4/file4", JsonDatabase, ErrorMessage);
377   EXPECT_EQ("//net/directory4", FoundCommand.Directory) << ErrorMessage;
378   ASSERT_EQ(1u, FoundCommand.CommandLine.size()) << ErrorMessage;
379   EXPECT_EQ("command4", FoundCommand.CommandLine[0]) << ErrorMessage;
380 }
381 
382 TEST(findCompileArgsInJsonDatabase, ParsesCompilerWrappers) {
383   std::vector<std::pair<std::string, std::string>> Cases = {
384       {"distcc gcc foo.c", "gcc foo.c"},
385       {"gomacc clang++ foo.c", "clang++ foo.c"},
386       {"sccache clang++ foo.c", "clang++ foo.c"},
387       {"ccache gcc foo.c", "gcc foo.c"},
388       {"ccache.exe gcc foo.c", "gcc foo.c"},
389       {"ccache g++.exe foo.c", "g++.exe foo.c"},
390       {"ccache distcc gcc foo.c", "gcc foo.c"},
391 
392       {"distcc foo.c", "distcc foo.c"},
393       {"distcc -I/foo/bar foo.c", "distcc -I/foo/bar foo.c"},
394   };
395   std::string ErrorMessage;
396 
397   for (const auto &Case : Cases) {
398     std::string DB =
399         R"([{"directory":"//net/dir", "file":"//net/dir/foo.c", "command":")" +
400         Case.first + "\"}]";
401     CompileCommand FoundCommand =
402         findCompileArgsInJsonDatabase("//net/dir/foo.c", DB, ErrorMessage);
403     EXPECT_EQ(Case.second, llvm::join(FoundCommand.CommandLine, " "))
404         << Case.first;
405   }
406 }
407 
408 static std::vector<std::string> unescapeJsonCommandLine(StringRef Command) {
409   std::string JsonDatabase =
410     ("[{\"directory\":\"//net/root\", \"file\":\"test\", \"command\": \"" +
411      Command + "\"}]").str();
412   std::string ErrorMessage;
413   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
414     "//net/root/test", JsonDatabase, ErrorMessage);
415   EXPECT_TRUE(ErrorMessage.empty()) << ErrorMessage;
416   return FoundCommand.CommandLine;
417 }
418 
419 TEST(unescapeJsonCommandLine, ReturnsEmptyArrayOnEmptyString) {
420   std::vector<std::string> Result = unescapeJsonCommandLine("");
421   EXPECT_TRUE(Result.empty());
422 }
423 
424 TEST(unescapeJsonCommandLine, SplitsOnSpaces) {
425   std::vector<std::string> Result = unescapeJsonCommandLine("a b c");
426   ASSERT_EQ(3ul, Result.size());
427   EXPECT_EQ("a", Result[0]);
428   EXPECT_EQ("b", Result[1]);
429   EXPECT_EQ("c", Result[2]);
430 }
431 
432 TEST(unescapeJsonCommandLine, MungesMultipleSpaces) {
433   std::vector<std::string> Result = unescapeJsonCommandLine("   a   b   ");
434   ASSERT_EQ(2ul, Result.size());
435   EXPECT_EQ("a", Result[0]);
436   EXPECT_EQ("b", Result[1]);
437 }
438 
439 TEST(unescapeJsonCommandLine, UnescapesBackslashCharacters) {
440   std::vector<std::string> Backslash = unescapeJsonCommandLine("a\\\\\\\\");
441   ASSERT_EQ(1ul, Backslash.size());
442   EXPECT_EQ("a\\", Backslash[0]);
443   std::vector<std::string> Quote = unescapeJsonCommandLine("a\\\\\\\"");
444   ASSERT_EQ(1ul, Quote.size());
445   EXPECT_EQ("a\"", Quote[0]);
446 }
447 
448 TEST(unescapeJsonCommandLine, DoesNotMungeSpacesBetweenQuotes) {
449   std::vector<std::string> Result = unescapeJsonCommandLine("\\\"  a  b  \\\"");
450   ASSERT_EQ(1ul, Result.size());
451   EXPECT_EQ("  a  b  ", Result[0]);
452 }
453 
454 TEST(unescapeJsonCommandLine, AllowsMultipleQuotedArguments) {
455   std::vector<std::string> Result = unescapeJsonCommandLine(
456       "  \\\" a \\\"  \\\" b \\\"  ");
457   ASSERT_EQ(2ul, Result.size());
458   EXPECT_EQ(" a ", Result[0]);
459   EXPECT_EQ(" b ", Result[1]);
460 }
461 
462 TEST(unescapeJsonCommandLine, AllowsEmptyArgumentsInQuotes) {
463   std::vector<std::string> Result = unescapeJsonCommandLine(
464       "\\\"\\\"\\\"\\\"");
465   ASSERT_EQ(1ul, Result.size());
466   EXPECT_TRUE(Result[0].empty()) << Result[0];
467 }
468 
469 TEST(unescapeJsonCommandLine, ParsesEscapedQuotesInQuotedStrings) {
470   std::vector<std::string> Result = unescapeJsonCommandLine(
471       "\\\"\\\\\\\"\\\"");
472   ASSERT_EQ(1ul, Result.size());
473   EXPECT_EQ("\"", Result[0]);
474 }
475 
476 TEST(unescapeJsonCommandLine, ParsesMultipleArgumentsWithEscapedCharacters) {
477   std::vector<std::string> Result = unescapeJsonCommandLine(
478       "  \\\\\\\"  \\\"a \\\\\\\" b \\\"     \\\"and\\\\\\\\c\\\"   \\\\\\\"");
479   ASSERT_EQ(4ul, Result.size());
480   EXPECT_EQ("\"", Result[0]);
481   EXPECT_EQ("a \" b ", Result[1]);
482   EXPECT_EQ("and\\c", Result[2]);
483   EXPECT_EQ("\"", Result[3]);
484 }
485 
486 TEST(unescapeJsonCommandLine, ParsesStringsWithoutSpacesIntoSingleArgument) {
487   std::vector<std::string> QuotedNoSpaces = unescapeJsonCommandLine(
488       "\\\"a\\\"\\\"b\\\"");
489   ASSERT_EQ(1ul, QuotedNoSpaces.size());
490   EXPECT_EQ("ab", QuotedNoSpaces[0]);
491 
492   std::vector<std::string> MixedNoSpaces = unescapeJsonCommandLine(
493       "\\\"a\\\"bcd\\\"ef\\\"\\\"\\\"\\\"g\\\"");
494   ASSERT_EQ(1ul, MixedNoSpaces.size());
495   EXPECT_EQ("abcdefg", MixedNoSpaces[0]);
496 }
497 
498 TEST(unescapeJsonCommandLine, ParsesQuotedStringWithoutClosingQuote) {
499   std::vector<std::string> Unclosed = unescapeJsonCommandLine("\\\"abc");
500   ASSERT_EQ(1ul, Unclosed.size());
501   EXPECT_EQ("abc", Unclosed[0]);
502 
503   std::vector<std::string> Empty = unescapeJsonCommandLine("\\\"");
504   ASSERT_EQ(1ul, Empty.size());
505   EXPECT_EQ("", Empty[0]);
506 }
507 
508 TEST(unescapeJsonCommandLine, ParsesSingleQuotedString) {
509   std::vector<std::string> Args = unescapeJsonCommandLine("a'\\\\b \\\"c\\\"'");
510   ASSERT_EQ(1ul, Args.size());
511   EXPECT_EQ("a\\b \"c\"", Args[0]);
512 }
513 
514 TEST(FixedCompilationDatabase, ReturnsFixedCommandLine) {
515   FixedCompilationDatabase Database(".", /*CommandLine*/ {"one", "two"});
516   StringRef FileName("source");
517   std::vector<CompileCommand> Result =
518     Database.getCompileCommands(FileName);
519   ASSERT_EQ(1ul, Result.size());
520   EXPECT_EQ(".", Result[0].Directory);
521   EXPECT_EQ(FileName, Result[0].Filename);
522   EXPECT_THAT(Result[0].CommandLine,
523               ElementsAre(EndsWith("clang-tool"), "one", "two", "source"));
524 }
525 
526 TEST(FixedCompilationDatabase, GetAllFiles) {
527   std::vector<std::string> CommandLine;
528   CommandLine.push_back("one");
529   CommandLine.push_back("two");
530   FixedCompilationDatabase Database(".", CommandLine);
531 
532   EXPECT_EQ(0ul, Database.getAllFiles().size());
533 }
534 
535 TEST(FixedCompilationDatabase, GetAllCompileCommands) {
536   std::vector<std::string> CommandLine;
537   CommandLine.push_back("one");
538   CommandLine.push_back("two");
539   FixedCompilationDatabase Database(".", CommandLine);
540 
541   EXPECT_EQ(0ul, Database.getAllCompileCommands().size());
542 }
543 
544 TEST(ParseFixedCompilationDatabase, ReturnsNullOnEmptyArgumentList) {
545   int Argc = 0;
546   std::string ErrorMsg;
547   std::unique_ptr<FixedCompilationDatabase> Database =
548       FixedCompilationDatabase::loadFromCommandLine(Argc, nullptr, ErrorMsg);
549   EXPECT_FALSE(Database);
550   EXPECT_TRUE(ErrorMsg.empty());
551   EXPECT_EQ(0, Argc);
552 }
553 
554 TEST(ParseFixedCompilationDatabase, ReturnsNullWithoutDoubleDash) {
555   int Argc = 2;
556   const char *Argv[] = { "1", "2" };
557   std::string ErrorMsg;
558   std::unique_ptr<FixedCompilationDatabase> Database(
559       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg));
560   EXPECT_FALSE(Database);
561   EXPECT_TRUE(ErrorMsg.empty());
562   EXPECT_EQ(2, Argc);
563 }
564 
565 TEST(ParseFixedCompilationDatabase, ReturnsArgumentsAfterDoubleDash) {
566   int Argc = 5;
567   const char *Argv[] = {
568     "1", "2", "--\0no-constant-folding", "-DDEF3", "-DDEF4"
569   };
570   std::string ErrorMsg;
571   std::unique_ptr<FixedCompilationDatabase> Database(
572       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg));
573   ASSERT_TRUE((bool)Database);
574   ASSERT_TRUE(ErrorMsg.empty());
575   std::vector<CompileCommand> Result =
576     Database->getCompileCommands("source");
577   ASSERT_EQ(1ul, Result.size());
578   ASSERT_EQ(".", Result[0].Directory);
579   ASSERT_THAT(Result[0].CommandLine, ElementsAre(EndsWith("clang-tool"),
580                                                  "-DDEF3", "-DDEF4", "source"));
581   EXPECT_EQ(2, Argc);
582 }
583 
584 TEST(ParseFixedCompilationDatabase, ReturnsEmptyCommandLine) {
585   int Argc = 3;
586   const char *Argv[] = { "1", "2", "--\0no-constant-folding" };
587   std::string ErrorMsg;
588   std::unique_ptr<FixedCompilationDatabase> Database =
589       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
590   ASSERT_TRUE((bool)Database);
591   ASSERT_TRUE(ErrorMsg.empty());
592   std::vector<CompileCommand> Result =
593     Database->getCompileCommands("source");
594   ASSERT_EQ(1ul, Result.size());
595   ASSERT_EQ(".", Result[0].Directory);
596   ASSERT_THAT(Result[0].CommandLine,
597               ElementsAre(EndsWith("clang-tool"), "source"));
598   EXPECT_EQ(2, Argc);
599 }
600 
601 TEST(ParseFixedCompilationDatabase, HandlesPositionalArgs) {
602   const char *Argv[] = {"1", "2", "--", "-c", "somefile.cpp", "-DDEF3"};
603   int Argc = sizeof(Argv) / sizeof(char*);
604   std::string ErrorMsg;
605   std::unique_ptr<FixedCompilationDatabase> Database =
606       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
607   ASSERT_TRUE((bool)Database);
608   ASSERT_TRUE(ErrorMsg.empty());
609   std::vector<CompileCommand> Result =
610     Database->getCompileCommands("source");
611   ASSERT_EQ(1ul, Result.size());
612   ASSERT_EQ(".", Result[0].Directory);
613   ASSERT_THAT(Result[0].CommandLine,
614               ElementsAre(EndsWith("clang-tool"), "-c", "-DDEF3", "source"));
615   EXPECT_EQ(2, Argc);
616 }
617 
618 TEST(ParseFixedCompilationDatabase, HandlesPositionalArgsSyntaxOnly) {
619   // Adjust the given command line arguments to ensure that any positional
620   // arguments in them are stripped.
621   const char *Argv[] = {"--", "somefile.cpp", "-fsyntax-only", "-DDEF3"};
622   int Argc = llvm::array_lengthof(Argv);
623   std::string ErrorMessage;
624   std::unique_ptr<CompilationDatabase> Database =
625       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMessage);
626   ASSERT_TRUE((bool)Database);
627   ASSERT_TRUE(ErrorMessage.empty());
628   std::vector<CompileCommand> Result = Database->getCompileCommands("source");
629   ASSERT_EQ(1ul, Result.size());
630   ASSERT_EQ(".", Result[0].Directory);
631   ASSERT_THAT(
632       Result[0].CommandLine,
633       ElementsAre(EndsWith("clang-tool"), "-fsyntax-only", "-DDEF3", "source"));
634 }
635 
636 TEST(ParseFixedCompilationDatabase, HandlesArgv0) {
637   const char *Argv[] = {"1", "2", "--", "mytool", "somefile.cpp"};
638   int Argc = sizeof(Argv) / sizeof(char*);
639   std::string ErrorMsg;
640   std::unique_ptr<FixedCompilationDatabase> Database =
641       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
642   ASSERT_TRUE((bool)Database);
643   ASSERT_TRUE(ErrorMsg.empty());
644   std::vector<CompileCommand> Result =
645     Database->getCompileCommands("source");
646   ASSERT_EQ(1ul, Result.size());
647   ASSERT_EQ(".", Result[0].Directory);
648   std::vector<std::string> Expected;
649   ASSERT_THAT(Result[0].CommandLine,
650               ElementsAre(EndsWith("clang-tool"), "source"));
651   EXPECT_EQ(2, Argc);
652 }
653 
654 struct MemCDB : public CompilationDatabase {
655   using EntryMap = llvm::StringMap<SmallVector<CompileCommand, 1>>;
656   EntryMap Entries;
657   MemCDB(const EntryMap &E) : Entries(E) {}
658 
659   std::vector<CompileCommand> getCompileCommands(StringRef F) const override {
660     auto Ret = Entries.lookup(F);
661     return {Ret.begin(), Ret.end()};
662   }
663 
664   std::vector<std::string> getAllFiles() const override {
665     std::vector<std::string> Result;
666     for (const auto &Entry : Entries)
667       Result.push_back(std::string(Entry.first()));
668     return Result;
669   }
670 };
671 
672 class MemDBTest : public ::testing::Test {
673 protected:
674   // Adds an entry to the underlying compilation database.
675   // A flag is injected: -D <File>, so the command used can be identified.
676   void add(StringRef File, StringRef Clang, StringRef Flags) {
677     SmallVector<StringRef, 8> Argv = {Clang, File, "-D", File};
678     llvm::SplitString(Flags, Argv);
679 
680     SmallString<32> Dir;
681     llvm::sys::path::system_temp_directory(false, Dir);
682 
683     Entries[path(File)].push_back(
684         {Dir, path(File), {Argv.begin(), Argv.end()}, "foo.o"});
685   }
686   void add(StringRef File, StringRef Flags = "") { add(File, "clang", Flags); }
687 
688   // Turn a unix path fragment (foo/bar.h) into a native path (C:\tmp\foo\bar.h)
689   std::string path(llvm::SmallString<32> File) {
690     llvm::SmallString<32> Dir;
691     llvm::sys::path::system_temp_directory(false, Dir);
692     llvm::sys::path::native(File);
693     llvm::SmallString<64> Result;
694     llvm::sys::path::append(Result, Dir, File);
695     return std::string(Result.str());
696   }
697 
698   MemCDB::EntryMap Entries;
699 };
700 
701 class InterpolateTest : public MemDBTest {
702 protected:
703   // Look up the command from a relative path, and return it in string form.
704   // The input file is not included in the returned command.
705   std::string getCommand(llvm::StringRef F) {
706     auto Results =
707         inferMissingCompileCommands(std::make_unique<MemCDB>(Entries))
708             ->getCompileCommands(path(F));
709     if (Results.empty())
710       return "none";
711     // drop the input file argument, so tests don't have to deal with path().
712     EXPECT_EQ(Results[0].CommandLine.back(), path(F))
713         << "Last arg should be the file";
714     Results[0].CommandLine.pop_back();
715     return llvm::join(Results[0].CommandLine, " ");
716   }
717 
718   // Parse the file whose command was used out of the Heuristic string.
719   std::string getProxy(llvm::StringRef F) {
720     auto Results =
721         inferMissingCompileCommands(std::make_unique<MemCDB>(Entries))
722             ->getCompileCommands(path(F));
723     if (Results.empty())
724       return "none";
725     StringRef Proxy = Results.front().Heuristic;
726     if (!Proxy.consume_front("inferred from "))
727       return "";
728     // We have a proxy file, convert back to a unix relative path.
729     // This is a bit messy, but we do need to test these strings somehow...
730     llvm::SmallString<32> TempDir;
731     llvm::sys::path::system_temp_directory(false, TempDir);
732     Proxy.consume_front(TempDir);
733     Proxy.consume_front(llvm::sys::path::get_separator());
734     llvm::SmallString<32> Result = Proxy;
735     llvm::sys::path::native(Result, llvm::sys::path::Style::posix);
736     return std::string(Result.str());
737   }
738 };
739 
740 TEST_F(InterpolateTest, Nearby) {
741   add("dir/foo.cpp");
742   add("dir/bar.cpp");
743   add("an/other/foo.cpp");
744 
745   // great: dir and name both match (prefix or full, case insensitive)
746   EXPECT_EQ(getProxy("dir/f.cpp"), "dir/foo.cpp");
747   EXPECT_EQ(getProxy("dir/FOO.cpp"), "dir/foo.cpp");
748   // no name match. prefer matching dir, break ties by alpha
749   EXPECT_EQ(getProxy("dir/a.cpp"), "dir/bar.cpp");
750   // an exact name match beats one segment of directory match
751   EXPECT_EQ(getProxy("some/other/bar.h"), "dir/bar.cpp");
752   // two segments of directory match beat a prefix name match
753   EXPECT_EQ(getProxy("an/other/b.cpp"), "an/other/foo.cpp");
754   // if nothing matches at all, we still get the closest alpha match
755   EXPECT_EQ(getProxy("below/some/obscure/path.cpp"), "an/other/foo.cpp");
756 }
757 
758 TEST_F(InterpolateTest, Language) {
759   add("dir/foo.cpp", "-std=c++17");
760   add("dir/bar.c", "");
761   add("dir/baz.cee", "-x c");
762   add("dir/aux.cpp", "-std=c++17 -x objective-c++");
763 
764   // .h is ambiguous, so we add explicit language flags
765   EXPECT_EQ(getCommand("foo.h"),
766             "clang -D dir/foo.cpp -x c++-header -std=c++17");
767   // Same thing if we have no extension. (again, we treat as header).
768   EXPECT_EQ(getCommand("foo"), "clang -D dir/foo.cpp -x c++-header -std=c++17");
769   // and invalid extensions.
770   EXPECT_EQ(getCommand("foo.cce"),
771             "clang -D dir/foo.cpp -x c++-header -std=c++17");
772   // and don't add -x if the inferred language is correct.
773   EXPECT_EQ(getCommand("foo.hpp"), "clang -D dir/foo.cpp -std=c++17");
774   // respect -x if it's already there.
775   EXPECT_EQ(getCommand("baz.h"), "clang -D dir/baz.cee -x c-header");
776   // prefer a worse match with the right extension.
777   EXPECT_EQ(getCommand("foo.c"), "clang -D dir/bar.c");
778   Entries.erase(path(StringRef("dir/bar.c")));
779   // Now we transfer across languages, so drop -std too.
780   EXPECT_EQ(getCommand("foo.c"), "clang -D dir/foo.cpp");
781   // Prefer -x over -std when overriding language.
782   EXPECT_EQ(getCommand("aux.h"),
783             "clang -D dir/aux.cpp -x objective-c++-header -std=c++17");
784 }
785 
786 TEST_F(InterpolateTest, Strip) {
787   add("dir/foo.cpp", "-o foo.o -Wall");
788   // the -o option and the input file are removed, but -Wall is preserved.
789   EXPECT_EQ(getCommand("dir/bar.cpp"), "clang -D dir/foo.cpp -Wall");
790 }
791 
792 TEST_F(InterpolateTest, Case) {
793   add("FOO/BAR/BAZ/SHOUT.cc");
794   add("foo/bar/baz/quiet.cc");
795   // Case mismatches are completely ignored, so we choose the name match.
796   EXPECT_EQ(getProxy("foo/bar/baz/shout.C"), "FOO/BAR/BAZ/SHOUT.cc");
797 }
798 
799 TEST_F(InterpolateTest, Aliasing) {
800   add("foo.cpp", "-faligned-new");
801 
802   // The interpolated command should keep the given flag as written, even though
803   // the flag is internally represented as an alias.
804   EXPECT_EQ(getCommand("foo.hpp"), "clang -D foo.cpp -faligned-new");
805 }
806 
807 TEST_F(InterpolateTest, ClangCL) {
808   add("foo.cpp", "clang-cl", "/W4");
809 
810   // Language flags should be added with CL syntax.
811   EXPECT_EQ(getCommand("foo.h"), "clang-cl -D foo.cpp /W4 /TP");
812 }
813 
814 TEST_F(InterpolateTest, DriverModes) {
815   add("foo.cpp", "clang-cl", "--driver-mode=gcc");
816   add("bar.cpp", "clang", "--driver-mode=cl");
817 
818   // --driver-mode overrides should be respected.
819   EXPECT_EQ(getCommand("foo.h"), "clang-cl -D foo.cpp --driver-mode=gcc -x c++-header");
820   EXPECT_EQ(getCommand("bar.h"), "clang -D bar.cpp --driver-mode=cl /TP");
821 }
822 
823 TEST(CompileCommandTest, EqualityOperator) {
824   CompileCommand CCRef("/foo/bar", "hello.c", {"a", "b"}, "hello.o");
825   CompileCommand CCTest = CCRef;
826 
827   EXPECT_TRUE(CCRef == CCTest);
828   EXPECT_FALSE(CCRef != CCTest);
829 
830   CCTest = CCRef;
831   CCTest.Directory = "/foo/baz";
832   EXPECT_FALSE(CCRef == CCTest);
833   EXPECT_TRUE(CCRef != CCTest);
834 
835   CCTest = CCRef;
836   CCTest.Filename = "bonjour.c";
837   EXPECT_FALSE(CCRef == CCTest);
838   EXPECT_TRUE(CCRef != CCTest);
839 
840   CCTest = CCRef;
841   CCTest.CommandLine.push_back("c");
842   EXPECT_FALSE(CCRef == CCTest);
843   EXPECT_TRUE(CCRef != CCTest);
844 
845   CCTest = CCRef;
846   CCTest.Output = "bonjour.o";
847   EXPECT_FALSE(CCRef == CCTest);
848   EXPECT_TRUE(CCRef != CCTest);
849 }
850 
851 class TargetAndModeTest : public MemDBTest {
852 public:
853   TargetAndModeTest() { llvm::InitializeAllTargetInfos(); }
854 
855 protected:
856   // Look up the command from a relative path, and return it in string form.
857   std::string getCommand(llvm::StringRef F) {
858     auto Results = inferTargetAndDriverMode(std::make_unique<MemCDB>(Entries))
859                        ->getCompileCommands(path(F));
860     if (Results.empty())
861       return "none";
862     return llvm::join(Results[0].CommandLine, " ");
863   }
864 };
865 
866 TEST_F(TargetAndModeTest, TargetAndMode) {
867   add("foo.cpp", "clang-cl", "");
868   add("bar.cpp", "clang++", "");
869 
870   EXPECT_EQ(getCommand("foo.cpp"),
871             "clang-cl --driver-mode=cl foo.cpp -D foo.cpp");
872   EXPECT_EQ(getCommand("bar.cpp"),
873             "clang++ --driver-mode=g++ bar.cpp -D bar.cpp");
874 }
875 
876 class ExpandResponseFilesTest : public MemDBTest {
877 public:
878   ExpandResponseFilesTest() : FS(new llvm::vfs::InMemoryFileSystem) {}
879 
880 protected:
881   void addFile(StringRef File, StringRef Content) {
882     ASSERT_TRUE(
883         FS->addFile(File, 0, llvm::MemoryBuffer::getMemBufferCopy(Content)));
884   }
885 
886   std::string getCommand(llvm::StringRef F) {
887     auto Results = expandResponseFiles(std::make_unique<MemCDB>(Entries), FS)
888                        ->getCompileCommands(path(F));
889     if (Results.empty())
890       return "none";
891     return llvm::join(Results[0].CommandLine, " ");
892   }
893 
894   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS;
895 };
896 
897 TEST_F(ExpandResponseFilesTest, ExpandResponseFiles) {
898   addFile(path(StringRef("rsp1.rsp")), "-Dflag");
899 
900   add("foo.cpp", "clang", "@rsp1.rsp");
901   add("bar.cpp", "clang", "-Dflag");
902   EXPECT_EQ(getCommand("foo.cpp"), "clang foo.cpp -D foo.cpp -Dflag");
903   EXPECT_EQ(getCommand("bar.cpp"), "clang bar.cpp -D bar.cpp -Dflag");
904 }
905 
906 } // end namespace tooling
907 } // end namespace clang
908