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       {"ccache gcc foo.c", "gcc foo.c"},
387       {"ccache.exe gcc foo.c", "gcc foo.c"},
388       {"ccache g++.exe foo.c", "g++.exe foo.c"},
389       {"ccache distcc gcc foo.c", "gcc foo.c"},
390 
391       {"distcc foo.c", "distcc foo.c"},
392       {"distcc -I/foo/bar foo.c", "distcc -I/foo/bar foo.c"},
393   };
394   std::string ErrorMessage;
395 
396   for (const auto &Case : Cases) {
397     std::string DB =
398         R"([{"directory":"//net/dir", "file":"//net/dir/foo.c", "command":")" +
399         Case.first + "\"}]";
400     CompileCommand FoundCommand =
401         findCompileArgsInJsonDatabase("//net/dir/foo.c", DB, ErrorMessage);
402     EXPECT_EQ(Case.second, llvm::join(FoundCommand.CommandLine, " "))
403         << Case.first;
404   }
405 }
406 
407 static std::vector<std::string> unescapeJsonCommandLine(StringRef Command) {
408   std::string JsonDatabase =
409     ("[{\"directory\":\"//net/root\", \"file\":\"test\", \"command\": \"" +
410      Command + "\"}]").str();
411   std::string ErrorMessage;
412   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
413     "//net/root/test", JsonDatabase, ErrorMessage);
414   EXPECT_TRUE(ErrorMessage.empty()) << ErrorMessage;
415   return FoundCommand.CommandLine;
416 }
417 
418 TEST(unescapeJsonCommandLine, ReturnsEmptyArrayOnEmptyString) {
419   std::vector<std::string> Result = unescapeJsonCommandLine("");
420   EXPECT_TRUE(Result.empty());
421 }
422 
423 TEST(unescapeJsonCommandLine, SplitsOnSpaces) {
424   std::vector<std::string> Result = unescapeJsonCommandLine("a b c");
425   ASSERT_EQ(3ul, Result.size());
426   EXPECT_EQ("a", Result[0]);
427   EXPECT_EQ("b", Result[1]);
428   EXPECT_EQ("c", Result[2]);
429 }
430 
431 TEST(unescapeJsonCommandLine, MungesMultipleSpaces) {
432   std::vector<std::string> Result = unescapeJsonCommandLine("   a   b   ");
433   ASSERT_EQ(2ul, Result.size());
434   EXPECT_EQ("a", Result[0]);
435   EXPECT_EQ("b", Result[1]);
436 }
437 
438 TEST(unescapeJsonCommandLine, UnescapesBackslashCharacters) {
439   std::vector<std::string> Backslash = unescapeJsonCommandLine("a\\\\\\\\");
440   ASSERT_EQ(1ul, Backslash.size());
441   EXPECT_EQ("a\\", Backslash[0]);
442   std::vector<std::string> Quote = unescapeJsonCommandLine("a\\\\\\\"");
443   ASSERT_EQ(1ul, Quote.size());
444   EXPECT_EQ("a\"", Quote[0]);
445 }
446 
447 TEST(unescapeJsonCommandLine, DoesNotMungeSpacesBetweenQuotes) {
448   std::vector<std::string> Result = unescapeJsonCommandLine("\\\"  a  b  \\\"");
449   ASSERT_EQ(1ul, Result.size());
450   EXPECT_EQ("  a  b  ", Result[0]);
451 }
452 
453 TEST(unescapeJsonCommandLine, AllowsMultipleQuotedArguments) {
454   std::vector<std::string> Result = unescapeJsonCommandLine(
455       "  \\\" a \\\"  \\\" b \\\"  ");
456   ASSERT_EQ(2ul, Result.size());
457   EXPECT_EQ(" a ", Result[0]);
458   EXPECT_EQ(" b ", Result[1]);
459 }
460 
461 TEST(unescapeJsonCommandLine, AllowsEmptyArgumentsInQuotes) {
462   std::vector<std::string> Result = unescapeJsonCommandLine(
463       "\\\"\\\"\\\"\\\"");
464   ASSERT_EQ(1ul, Result.size());
465   EXPECT_TRUE(Result[0].empty()) << Result[0];
466 }
467 
468 TEST(unescapeJsonCommandLine, ParsesEscapedQuotesInQuotedStrings) {
469   std::vector<std::string> Result = unescapeJsonCommandLine(
470       "\\\"\\\\\\\"\\\"");
471   ASSERT_EQ(1ul, Result.size());
472   EXPECT_EQ("\"", Result[0]);
473 }
474 
475 TEST(unescapeJsonCommandLine, ParsesMultipleArgumentsWithEscapedCharacters) {
476   std::vector<std::string> Result = unescapeJsonCommandLine(
477       "  \\\\\\\"  \\\"a \\\\\\\" b \\\"     \\\"and\\\\\\\\c\\\"   \\\\\\\"");
478   ASSERT_EQ(4ul, Result.size());
479   EXPECT_EQ("\"", Result[0]);
480   EXPECT_EQ("a \" b ", Result[1]);
481   EXPECT_EQ("and\\c", Result[2]);
482   EXPECT_EQ("\"", Result[3]);
483 }
484 
485 TEST(unescapeJsonCommandLine, ParsesStringsWithoutSpacesIntoSingleArgument) {
486   std::vector<std::string> QuotedNoSpaces = unescapeJsonCommandLine(
487       "\\\"a\\\"\\\"b\\\"");
488   ASSERT_EQ(1ul, QuotedNoSpaces.size());
489   EXPECT_EQ("ab", QuotedNoSpaces[0]);
490 
491   std::vector<std::string> MixedNoSpaces = unescapeJsonCommandLine(
492       "\\\"a\\\"bcd\\\"ef\\\"\\\"\\\"\\\"g\\\"");
493   ASSERT_EQ(1ul, MixedNoSpaces.size());
494   EXPECT_EQ("abcdefg", MixedNoSpaces[0]);
495 }
496 
497 TEST(unescapeJsonCommandLine, ParsesQuotedStringWithoutClosingQuote) {
498   std::vector<std::string> Unclosed = unescapeJsonCommandLine("\\\"abc");
499   ASSERT_EQ(1ul, Unclosed.size());
500   EXPECT_EQ("abc", Unclosed[0]);
501 
502   std::vector<std::string> Empty = unescapeJsonCommandLine("\\\"");
503   ASSERT_EQ(1ul, Empty.size());
504   EXPECT_EQ("", Empty[0]);
505 }
506 
507 TEST(unescapeJsonCommandLine, ParsesSingleQuotedString) {
508   std::vector<std::string> Args = unescapeJsonCommandLine("a'\\\\b \\\"c\\\"'");
509   ASSERT_EQ(1ul, Args.size());
510   EXPECT_EQ("a\\b \"c\"", Args[0]);
511 }
512 
513 TEST(FixedCompilationDatabase, ReturnsFixedCommandLine) {
514   FixedCompilationDatabase Database(".", /*CommandLine*/ {"one", "two"});
515   StringRef FileName("source");
516   std::vector<CompileCommand> Result =
517     Database.getCompileCommands(FileName);
518   ASSERT_EQ(1ul, Result.size());
519   EXPECT_EQ(".", Result[0].Directory);
520   EXPECT_EQ(FileName, Result[0].Filename);
521   EXPECT_THAT(Result[0].CommandLine,
522               ElementsAre(EndsWith("clang-tool"), "one", "two", "source"));
523 }
524 
525 TEST(FixedCompilationDatabase, GetAllFiles) {
526   std::vector<std::string> CommandLine;
527   CommandLine.push_back("one");
528   CommandLine.push_back("two");
529   FixedCompilationDatabase Database(".", CommandLine);
530 
531   EXPECT_EQ(0ul, Database.getAllFiles().size());
532 }
533 
534 TEST(FixedCompilationDatabase, GetAllCompileCommands) {
535   std::vector<std::string> CommandLine;
536   CommandLine.push_back("one");
537   CommandLine.push_back("two");
538   FixedCompilationDatabase Database(".", CommandLine);
539 
540   EXPECT_EQ(0ul, Database.getAllCompileCommands().size());
541 }
542 
543 TEST(ParseFixedCompilationDatabase, ReturnsNullOnEmptyArgumentList) {
544   int Argc = 0;
545   std::string ErrorMsg;
546   std::unique_ptr<FixedCompilationDatabase> Database =
547       FixedCompilationDatabase::loadFromCommandLine(Argc, nullptr, ErrorMsg);
548   EXPECT_FALSE(Database);
549   EXPECT_TRUE(ErrorMsg.empty());
550   EXPECT_EQ(0, Argc);
551 }
552 
553 TEST(ParseFixedCompilationDatabase, ReturnsNullWithoutDoubleDash) {
554   int Argc = 2;
555   const char *Argv[] = { "1", "2" };
556   std::string ErrorMsg;
557   std::unique_ptr<FixedCompilationDatabase> Database(
558       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg));
559   EXPECT_FALSE(Database);
560   EXPECT_TRUE(ErrorMsg.empty());
561   EXPECT_EQ(2, Argc);
562 }
563 
564 TEST(ParseFixedCompilationDatabase, ReturnsArgumentsAfterDoubleDash) {
565   int Argc = 5;
566   const char *Argv[] = {
567     "1", "2", "--\0no-constant-folding", "-DDEF3", "-DDEF4"
568   };
569   std::string ErrorMsg;
570   std::unique_ptr<FixedCompilationDatabase> Database(
571       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg));
572   ASSERT_TRUE((bool)Database);
573   ASSERT_TRUE(ErrorMsg.empty());
574   std::vector<CompileCommand> Result =
575     Database->getCompileCommands("source");
576   ASSERT_EQ(1ul, Result.size());
577   ASSERT_EQ(".", Result[0].Directory);
578   ASSERT_THAT(Result[0].CommandLine, ElementsAre(EndsWith("clang-tool"),
579                                                  "-DDEF3", "-DDEF4", "source"));
580   EXPECT_EQ(2, Argc);
581 }
582 
583 TEST(ParseFixedCompilationDatabase, ReturnsEmptyCommandLine) {
584   int Argc = 3;
585   const char *Argv[] = { "1", "2", "--\0no-constant-folding" };
586   std::string ErrorMsg;
587   std::unique_ptr<FixedCompilationDatabase> Database =
588       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
589   ASSERT_TRUE((bool)Database);
590   ASSERT_TRUE(ErrorMsg.empty());
591   std::vector<CompileCommand> Result =
592     Database->getCompileCommands("source");
593   ASSERT_EQ(1ul, Result.size());
594   ASSERT_EQ(".", Result[0].Directory);
595   ASSERT_THAT(Result[0].CommandLine,
596               ElementsAre(EndsWith("clang-tool"), "source"));
597   EXPECT_EQ(2, Argc);
598 }
599 
600 TEST(ParseFixedCompilationDatabase, HandlesPositionalArgs) {
601   const char *Argv[] = {"1", "2", "--", "-c", "somefile.cpp", "-DDEF3"};
602   int Argc = sizeof(Argv) / sizeof(char*);
603   std::string ErrorMsg;
604   std::unique_ptr<FixedCompilationDatabase> Database =
605       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
606   ASSERT_TRUE((bool)Database);
607   ASSERT_TRUE(ErrorMsg.empty());
608   std::vector<CompileCommand> Result =
609     Database->getCompileCommands("source");
610   ASSERT_EQ(1ul, Result.size());
611   ASSERT_EQ(".", Result[0].Directory);
612   ASSERT_THAT(Result[0].CommandLine,
613               ElementsAre(EndsWith("clang-tool"), "-c", "-DDEF3", "source"));
614   EXPECT_EQ(2, Argc);
615 }
616 
617 TEST(ParseFixedCompilationDatabase, HandlesPositionalArgsSyntaxOnly) {
618   // Adjust the given command line arguments to ensure that any positional
619   // arguments in them are stripped.
620   const char *Argv[] = {"--", "somefile.cpp", "-fsyntax-only", "-DDEF3"};
621   int Argc = llvm::array_lengthof(Argv);
622   std::string ErrorMessage;
623   std::unique_ptr<CompilationDatabase> Database =
624       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMessage);
625   ASSERT_TRUE((bool)Database);
626   ASSERT_TRUE(ErrorMessage.empty());
627   std::vector<CompileCommand> Result = Database->getCompileCommands("source");
628   ASSERT_EQ(1ul, Result.size());
629   ASSERT_EQ(".", Result[0].Directory);
630   ASSERT_THAT(
631       Result[0].CommandLine,
632       ElementsAre(EndsWith("clang-tool"), "-fsyntax-only", "-DDEF3", "source"));
633 }
634 
635 TEST(ParseFixedCompilationDatabase, HandlesArgv0) {
636   const char *Argv[] = {"1", "2", "--", "mytool", "somefile.cpp"};
637   int Argc = sizeof(Argv) / sizeof(char*);
638   std::string ErrorMsg;
639   std::unique_ptr<FixedCompilationDatabase> Database =
640       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
641   ASSERT_TRUE((bool)Database);
642   ASSERT_TRUE(ErrorMsg.empty());
643   std::vector<CompileCommand> Result =
644     Database->getCompileCommands("source");
645   ASSERT_EQ(1ul, Result.size());
646   ASSERT_EQ(".", Result[0].Directory);
647   std::vector<std::string> Expected;
648   ASSERT_THAT(Result[0].CommandLine,
649               ElementsAre(EndsWith("clang-tool"), "source"));
650   EXPECT_EQ(2, Argc);
651 }
652 
653 struct MemCDB : public CompilationDatabase {
654   using EntryMap = llvm::StringMap<SmallVector<CompileCommand, 1>>;
655   EntryMap Entries;
656   MemCDB(const EntryMap &E) : Entries(E) {}
657 
658   std::vector<CompileCommand> getCompileCommands(StringRef F) const override {
659     auto Ret = Entries.lookup(F);
660     return {Ret.begin(), Ret.end()};
661   }
662 
663   std::vector<std::string> getAllFiles() const override {
664     std::vector<std::string> Result;
665     for (const auto &Entry : Entries)
666       Result.push_back(std::string(Entry.first()));
667     return Result;
668   }
669 };
670 
671 class MemDBTest : public ::testing::Test {
672 protected:
673   // Adds an entry to the underlying compilation database.
674   // A flag is injected: -D <File>, so the command used can be identified.
675   void add(StringRef File, StringRef Clang, StringRef Flags) {
676     SmallVector<StringRef, 8> Argv = {Clang, File, "-D", File};
677     llvm::SplitString(Flags, Argv);
678 
679     SmallString<32> Dir;
680     llvm::sys::path::system_temp_directory(false, Dir);
681 
682     Entries[path(File)].push_back(
683         {Dir, path(File), {Argv.begin(), Argv.end()}, "foo.o"});
684   }
685   void add(StringRef File, StringRef Flags = "") { add(File, "clang", Flags); }
686 
687   // Turn a unix path fragment (foo/bar.h) into a native path (C:\tmp\foo\bar.h)
688   std::string path(llvm::SmallString<32> File) {
689     llvm::SmallString<32> Dir;
690     llvm::sys::path::system_temp_directory(false, Dir);
691     llvm::sys::path::native(File);
692     llvm::SmallString<64> Result;
693     llvm::sys::path::append(Result, Dir, File);
694     return std::string(Result.str());
695   }
696 
697   MemCDB::EntryMap Entries;
698 };
699 
700 class InterpolateTest : public MemDBTest {
701 protected:
702   // Look up the command from a relative path, and return it in string form.
703   // The input file is not included in the returned command.
704   std::string getCommand(llvm::StringRef F) {
705     auto Results =
706         inferMissingCompileCommands(std::make_unique<MemCDB>(Entries))
707             ->getCompileCommands(path(F));
708     if (Results.empty())
709       return "none";
710     // drop the input file argument, so tests don't have to deal with path().
711     EXPECT_EQ(Results[0].CommandLine.back(), path(F))
712         << "Last arg should be the file";
713     Results[0].CommandLine.pop_back();
714     return llvm::join(Results[0].CommandLine, " ");
715   }
716 
717   // Parse the file whose command was used out of the Heuristic string.
718   std::string getProxy(llvm::StringRef F) {
719     auto Results =
720         inferMissingCompileCommands(std::make_unique<MemCDB>(Entries))
721             ->getCompileCommands(path(F));
722     if (Results.empty())
723       return "none";
724     StringRef Proxy = Results.front().Heuristic;
725     if (!Proxy.consume_front("inferred from "))
726       return "";
727     // We have a proxy file, convert back to a unix relative path.
728     // This is a bit messy, but we do need to test these strings somehow...
729     llvm::SmallString<32> TempDir;
730     llvm::sys::path::system_temp_directory(false, TempDir);
731     Proxy.consume_front(TempDir);
732     Proxy.consume_front(llvm::sys::path::get_separator());
733     llvm::SmallString<32> Result = Proxy;
734     llvm::sys::path::native(Result, llvm::sys::path::Style::posix);
735     return std::string(Result.str());
736   }
737 };
738 
739 TEST_F(InterpolateTest, Nearby) {
740   add("dir/foo.cpp");
741   add("dir/bar.cpp");
742   add("an/other/foo.cpp");
743 
744   // great: dir and name both match (prefix or full, case insensitive)
745   EXPECT_EQ(getProxy("dir/f.cpp"), "dir/foo.cpp");
746   EXPECT_EQ(getProxy("dir/FOO.cpp"), "dir/foo.cpp");
747   // no name match. prefer matching dir, break ties by alpha
748   EXPECT_EQ(getProxy("dir/a.cpp"), "dir/bar.cpp");
749   // an exact name match beats one segment of directory match
750   EXPECT_EQ(getProxy("some/other/bar.h"), "dir/bar.cpp");
751   // two segments of directory match beat a prefix name match
752   EXPECT_EQ(getProxy("an/other/b.cpp"), "an/other/foo.cpp");
753   // if nothing matches at all, we still get the closest alpha match
754   EXPECT_EQ(getProxy("below/some/obscure/path.cpp"), "an/other/foo.cpp");
755 }
756 
757 TEST_F(InterpolateTest, Language) {
758   add("dir/foo.cpp", "-std=c++17");
759   add("dir/bar.c", "");
760   add("dir/baz.cee", "-x c");
761   add("dir/aux.cpp", "-std=c++17 -x objective-c++");
762 
763   // .h is ambiguous, so we add explicit language flags
764   EXPECT_EQ(getCommand("foo.h"),
765             "clang -D dir/foo.cpp -x c++-header -std=c++17");
766   // Same thing if we have no extension. (again, we treat as header).
767   EXPECT_EQ(getCommand("foo"), "clang -D dir/foo.cpp -x c++-header -std=c++17");
768   // and invalid extensions.
769   EXPECT_EQ(getCommand("foo.cce"),
770             "clang -D dir/foo.cpp -x c++-header -std=c++17");
771   // and don't add -x if the inferred language is correct.
772   EXPECT_EQ(getCommand("foo.hpp"), "clang -D dir/foo.cpp -std=c++17");
773   // respect -x if it's already there.
774   EXPECT_EQ(getCommand("baz.h"), "clang -D dir/baz.cee -x c-header");
775   // prefer a worse match with the right extension.
776   EXPECT_EQ(getCommand("foo.c"), "clang -D dir/bar.c");
777   Entries.erase(path(StringRef("dir/bar.c")));
778   // Now we transfer across languages, so drop -std too.
779   EXPECT_EQ(getCommand("foo.c"), "clang -D dir/foo.cpp");
780   // Prefer -x over -std when overriding language.
781   EXPECT_EQ(getCommand("aux.h"),
782             "clang -D dir/aux.cpp -x objective-c++-header -std=c++17");
783 }
784 
785 TEST_F(InterpolateTest, Strip) {
786   add("dir/foo.cpp", "-o foo.o -Wall");
787   // the -o option and the input file are removed, but -Wall is preserved.
788   EXPECT_EQ(getCommand("dir/bar.cpp"), "clang -D dir/foo.cpp -Wall");
789 }
790 
791 TEST_F(InterpolateTest, Case) {
792   add("FOO/BAR/BAZ/SHOUT.cc");
793   add("foo/bar/baz/quiet.cc");
794   // Case mismatches are completely ignored, so we choose the name match.
795   EXPECT_EQ(getProxy("foo/bar/baz/shout.C"), "FOO/BAR/BAZ/SHOUT.cc");
796 }
797 
798 TEST_F(InterpolateTest, Aliasing) {
799   add("foo.cpp", "-faligned-new");
800 
801   // The interpolated command should keep the given flag as written, even though
802   // the flag is internally represented as an alias.
803   EXPECT_EQ(getCommand("foo.hpp"), "clang -D foo.cpp -faligned-new");
804 }
805 
806 TEST_F(InterpolateTest, ClangCL) {
807   add("foo.cpp", "clang-cl", "/W4");
808 
809   // Language flags should be added with CL syntax.
810   EXPECT_EQ(getCommand("foo.h"), "clang-cl -D foo.cpp /W4 /TP");
811 }
812 
813 TEST_F(InterpolateTest, DriverModes) {
814   add("foo.cpp", "clang-cl", "--driver-mode=gcc");
815   add("bar.cpp", "clang", "--driver-mode=cl");
816 
817   // --driver-mode overrides should be respected.
818   EXPECT_EQ(getCommand("foo.h"), "clang-cl -D foo.cpp --driver-mode=gcc -x c++-header");
819   EXPECT_EQ(getCommand("bar.h"), "clang -D bar.cpp --driver-mode=cl /TP");
820 }
821 
822 TEST(CompileCommandTest, EqualityOperator) {
823   CompileCommand CCRef("/foo/bar", "hello.c", {"a", "b"}, "hello.o");
824   CompileCommand CCTest = CCRef;
825 
826   EXPECT_TRUE(CCRef == CCTest);
827   EXPECT_FALSE(CCRef != CCTest);
828 
829   CCTest = CCRef;
830   CCTest.Directory = "/foo/baz";
831   EXPECT_FALSE(CCRef == CCTest);
832   EXPECT_TRUE(CCRef != CCTest);
833 
834   CCTest = CCRef;
835   CCTest.Filename = "bonjour.c";
836   EXPECT_FALSE(CCRef == CCTest);
837   EXPECT_TRUE(CCRef != CCTest);
838 
839   CCTest = CCRef;
840   CCTest.CommandLine.push_back("c");
841   EXPECT_FALSE(CCRef == CCTest);
842   EXPECT_TRUE(CCRef != CCTest);
843 
844   CCTest = CCRef;
845   CCTest.Output = "bonjour.o";
846   EXPECT_FALSE(CCRef == CCTest);
847   EXPECT_TRUE(CCRef != CCTest);
848 }
849 
850 class TargetAndModeTest : public MemDBTest {
851 public:
852   TargetAndModeTest() { llvm::InitializeAllTargetInfos(); }
853 
854 protected:
855   // Look up the command from a relative path, and return it in string form.
856   std::string getCommand(llvm::StringRef F) {
857     auto Results = inferTargetAndDriverMode(std::make_unique<MemCDB>(Entries))
858                        ->getCompileCommands(path(F));
859     if (Results.empty())
860       return "none";
861     return llvm::join(Results[0].CommandLine, " ");
862   }
863 };
864 
865 TEST_F(TargetAndModeTest, TargetAndMode) {
866   add("foo.cpp", "clang-cl", "");
867   add("bar.cpp", "clang++", "");
868 
869   EXPECT_EQ(getCommand("foo.cpp"),
870             "clang-cl --driver-mode=cl foo.cpp -D foo.cpp");
871   EXPECT_EQ(getCommand("bar.cpp"),
872             "clang++ --driver-mode=g++ bar.cpp -D bar.cpp");
873 }
874 
875 class ExpandResponseFilesTest : public MemDBTest {
876 public:
877   ExpandResponseFilesTest() : FS(new llvm::vfs::InMemoryFileSystem) {}
878 
879 protected:
880   void addFile(StringRef File, StringRef Content) {
881     ASSERT_TRUE(
882         FS->addFile(File, 0, llvm::MemoryBuffer::getMemBufferCopy(Content)));
883   }
884 
885   std::string getCommand(llvm::StringRef F) {
886     auto Results = expandResponseFiles(std::make_unique<MemCDB>(Entries), FS)
887                        ->getCompileCommands(path(F));
888     if (Results.empty())
889       return "none";
890     return llvm::join(Results[0].CommandLine, " ");
891   }
892 
893   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS;
894 };
895 
896 TEST_F(ExpandResponseFilesTest, ExpandResponseFiles) {
897   addFile(path(StringRef("rsp1.rsp")), "-Dflag");
898 
899   add("foo.cpp", "clang", "@rsp1.rsp");
900   add("bar.cpp", "clang", "-Dflag");
901   EXPECT_EQ(getCommand("foo.cpp"), "clang foo.cpp -D foo.cpp -Dflag");
902   EXPECT_EQ(getCommand("bar.cpp"), "clang bar.cpp -D bar.cpp -Dflag");
903 }
904 
905 } // end namespace tooling
906 } // end namespace clang
907