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(PathStorage.str());
91   llvm::sys::path::native("//net/dir/file2", PathStorage);
92   expected_files.push_back(PathStorage.str());
93   llvm::sys::path::native("//net/file1", PathStorage);
94   expected_files.push_back(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(findCompileArgsInJsonDatabase, FindsNothingIfEmpty) {
285   std::string ErrorMessage;
286   CompileCommand NotFound = findCompileArgsInJsonDatabase(
287     "a-file.cpp", "", ErrorMessage);
288   EXPECT_TRUE(NotFound.CommandLine.empty()) << ErrorMessage;
289   EXPECT_TRUE(NotFound.Directory.empty()) << ErrorMessage;
290 }
291 
292 TEST(findCompileArgsInJsonDatabase, ReadsSingleEntry) {
293   StringRef Directory("//net/some/directory");
294   StringRef FileName("//net/path/to/a-file.cpp");
295   StringRef Command("//net/path/to/compiler and some arguments");
296   std::string ErrorMessage;
297   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
298     FileName,
299     ("[{\"directory\":\"" + Directory + "\"," +
300        "\"command\":\"" + Command + "\","
301        "\"file\":\"" + FileName + "\"}]").str(),
302     ErrorMessage);
303   EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage;
304   ASSERT_EQ(4u, FoundCommand.CommandLine.size()) << ErrorMessage;
305   EXPECT_EQ("//net/path/to/compiler",
306             FoundCommand.CommandLine[0]) << ErrorMessage;
307   EXPECT_EQ("and", FoundCommand.CommandLine[1]) << ErrorMessage;
308   EXPECT_EQ("some", FoundCommand.CommandLine[2]) << ErrorMessage;
309   EXPECT_EQ("arguments", FoundCommand.CommandLine[3]) << ErrorMessage;
310 
311   CompileCommand NotFound = findCompileArgsInJsonDatabase(
312     "a-file.cpp",
313     ("[{\"directory\":\"" + Directory + "\"," +
314        "\"command\":\"" + Command + "\","
315        "\"file\":\"" + FileName + "\"}]").str(),
316     ErrorMessage);
317   EXPECT_TRUE(NotFound.Directory.empty()) << ErrorMessage;
318   EXPECT_TRUE(NotFound.CommandLine.empty()) << ErrorMessage;
319 }
320 
321 TEST(findCompileArgsInJsonDatabase, ReadsCompileCommandLinesWithSpaces) {
322   StringRef Directory("//net/some/directory");
323   StringRef FileName("//net/path/to/a-file.cpp");
324   StringRef Command("\\\"//net/path to compiler\\\" \\\"and an argument\\\"");
325   std::string ErrorMessage;
326   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
327     FileName,
328     ("[{\"directory\":\"" + Directory + "\"," +
329        "\"command\":\"" + Command + "\","
330        "\"file\":\"" + FileName + "\"}]").str(),
331     ErrorMessage);
332   ASSERT_EQ(2u, FoundCommand.CommandLine.size());
333   EXPECT_EQ("//net/path to compiler",
334             FoundCommand.CommandLine[0]) << ErrorMessage;
335   EXPECT_EQ("and an argument", FoundCommand.CommandLine[1]) << ErrorMessage;
336 }
337 
338 TEST(findCompileArgsInJsonDatabase, ReadsDirectoryWithSpaces) {
339   StringRef Directory("//net/some directory / with spaces");
340   StringRef FileName("//net/path/to/a-file.cpp");
341   StringRef Command("a command");
342   std::string ErrorMessage;
343   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
344     FileName,
345     ("[{\"directory\":\"" + Directory + "\"," +
346        "\"command\":\"" + Command + "\","
347        "\"file\":\"" + FileName + "\"}]").str(),
348     ErrorMessage);
349   EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage;
350 }
351 
352 TEST(findCompileArgsInJsonDatabase, FindsEntry) {
353   StringRef Directory("//net/directory");
354   StringRef FileName("file");
355   StringRef Command("command");
356   std::string JsonDatabase = "[";
357   for (int I = 0; I < 10; ++I) {
358     if (I > 0) JsonDatabase += ",";
359     JsonDatabase +=
360       ("{\"directory\":\"" + Directory + Twine(I) + "\"," +
361         "\"command\":\"" + Command + Twine(I) + "\","
362         "\"file\":\"" + FileName + Twine(I) + "\"}").str();
363   }
364   JsonDatabase += "]";
365   std::string ErrorMessage;
366   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
367     "//net/directory4/file4", JsonDatabase, ErrorMessage);
368   EXPECT_EQ("//net/directory4", FoundCommand.Directory) << ErrorMessage;
369   ASSERT_EQ(1u, FoundCommand.CommandLine.size()) << ErrorMessage;
370   EXPECT_EQ("command4", FoundCommand.CommandLine[0]) << ErrorMessage;
371 }
372 
373 static std::vector<std::string> unescapeJsonCommandLine(StringRef Command) {
374   std::string JsonDatabase =
375     ("[{\"directory\":\"//net/root\", \"file\":\"test\", \"command\": \"" +
376      Command + "\"}]").str();
377   std::string ErrorMessage;
378   CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
379     "//net/root/test", JsonDatabase, ErrorMessage);
380   EXPECT_TRUE(ErrorMessage.empty()) << ErrorMessage;
381   return FoundCommand.CommandLine;
382 }
383 
384 TEST(unescapeJsonCommandLine, ReturnsEmptyArrayOnEmptyString) {
385   std::vector<std::string> Result = unescapeJsonCommandLine("");
386   EXPECT_TRUE(Result.empty());
387 }
388 
389 TEST(unescapeJsonCommandLine, SplitsOnSpaces) {
390   std::vector<std::string> Result = unescapeJsonCommandLine("a b c");
391   ASSERT_EQ(3ul, Result.size());
392   EXPECT_EQ("a", Result[0]);
393   EXPECT_EQ("b", Result[1]);
394   EXPECT_EQ("c", Result[2]);
395 }
396 
397 TEST(unescapeJsonCommandLine, MungesMultipleSpaces) {
398   std::vector<std::string> Result = unescapeJsonCommandLine("   a   b   ");
399   ASSERT_EQ(2ul, Result.size());
400   EXPECT_EQ("a", Result[0]);
401   EXPECT_EQ("b", Result[1]);
402 }
403 
404 TEST(unescapeJsonCommandLine, UnescapesBackslashCharacters) {
405   std::vector<std::string> Backslash = unescapeJsonCommandLine("a\\\\\\\\");
406   ASSERT_EQ(1ul, Backslash.size());
407   EXPECT_EQ("a\\", Backslash[0]);
408   std::vector<std::string> Quote = unescapeJsonCommandLine("a\\\\\\\"");
409   ASSERT_EQ(1ul, Quote.size());
410   EXPECT_EQ("a\"", Quote[0]);
411 }
412 
413 TEST(unescapeJsonCommandLine, DoesNotMungeSpacesBetweenQuotes) {
414   std::vector<std::string> Result = unescapeJsonCommandLine("\\\"  a  b  \\\"");
415   ASSERT_EQ(1ul, Result.size());
416   EXPECT_EQ("  a  b  ", Result[0]);
417 }
418 
419 TEST(unescapeJsonCommandLine, AllowsMultipleQuotedArguments) {
420   std::vector<std::string> Result = unescapeJsonCommandLine(
421       "  \\\" a \\\"  \\\" b \\\"  ");
422   ASSERT_EQ(2ul, Result.size());
423   EXPECT_EQ(" a ", Result[0]);
424   EXPECT_EQ(" b ", Result[1]);
425 }
426 
427 TEST(unescapeJsonCommandLine, AllowsEmptyArgumentsInQuotes) {
428   std::vector<std::string> Result = unescapeJsonCommandLine(
429       "\\\"\\\"\\\"\\\"");
430   ASSERT_EQ(1ul, Result.size());
431   EXPECT_TRUE(Result[0].empty()) << Result[0];
432 }
433 
434 TEST(unescapeJsonCommandLine, ParsesEscapedQuotesInQuotedStrings) {
435   std::vector<std::string> Result = unescapeJsonCommandLine(
436       "\\\"\\\\\\\"\\\"");
437   ASSERT_EQ(1ul, Result.size());
438   EXPECT_EQ("\"", Result[0]);
439 }
440 
441 TEST(unescapeJsonCommandLine, ParsesMultipleArgumentsWithEscapedCharacters) {
442   std::vector<std::string> Result = unescapeJsonCommandLine(
443       "  \\\\\\\"  \\\"a \\\\\\\" b \\\"     \\\"and\\\\\\\\c\\\"   \\\\\\\"");
444   ASSERT_EQ(4ul, Result.size());
445   EXPECT_EQ("\"", Result[0]);
446   EXPECT_EQ("a \" b ", Result[1]);
447   EXPECT_EQ("and\\c", Result[2]);
448   EXPECT_EQ("\"", Result[3]);
449 }
450 
451 TEST(unescapeJsonCommandLine, ParsesStringsWithoutSpacesIntoSingleArgument) {
452   std::vector<std::string> QuotedNoSpaces = unescapeJsonCommandLine(
453       "\\\"a\\\"\\\"b\\\"");
454   ASSERT_EQ(1ul, QuotedNoSpaces.size());
455   EXPECT_EQ("ab", QuotedNoSpaces[0]);
456 
457   std::vector<std::string> MixedNoSpaces = unescapeJsonCommandLine(
458       "\\\"a\\\"bcd\\\"ef\\\"\\\"\\\"\\\"g\\\"");
459   ASSERT_EQ(1ul, MixedNoSpaces.size());
460   EXPECT_EQ("abcdefg", MixedNoSpaces[0]);
461 }
462 
463 TEST(unescapeJsonCommandLine, ParsesQuotedStringWithoutClosingQuote) {
464   std::vector<std::string> Unclosed = unescapeJsonCommandLine("\\\"abc");
465   ASSERT_EQ(1ul, Unclosed.size());
466   EXPECT_EQ("abc", Unclosed[0]);
467 
468   std::vector<std::string> Empty = unescapeJsonCommandLine("\\\"");
469   ASSERT_EQ(1ul, Empty.size());
470   EXPECT_EQ("", Empty[0]);
471 }
472 
473 TEST(unescapeJsonCommandLine, ParsesSingleQuotedString) {
474   std::vector<std::string> Args = unescapeJsonCommandLine("a'\\\\b \\\"c\\\"'");
475   ASSERT_EQ(1ul, Args.size());
476   EXPECT_EQ("a\\b \"c\"", Args[0]);
477 }
478 
479 TEST(FixedCompilationDatabase, ReturnsFixedCommandLine) {
480   FixedCompilationDatabase Database(".", /*CommandLine*/ {"one", "two"});
481   StringRef FileName("source");
482   std::vector<CompileCommand> Result =
483     Database.getCompileCommands(FileName);
484   ASSERT_EQ(1ul, Result.size());
485   EXPECT_EQ(".", Result[0].Directory);
486   EXPECT_EQ(FileName, Result[0].Filename);
487   EXPECT_THAT(Result[0].CommandLine,
488               ElementsAre(EndsWith("clang-tool"), "one", "two", "source"));
489 }
490 
491 TEST(FixedCompilationDatabase, GetAllFiles) {
492   std::vector<std::string> CommandLine;
493   CommandLine.push_back("one");
494   CommandLine.push_back("two");
495   FixedCompilationDatabase Database(".", CommandLine);
496 
497   EXPECT_EQ(0ul, Database.getAllFiles().size());
498 }
499 
500 TEST(FixedCompilationDatabase, GetAllCompileCommands) {
501   std::vector<std::string> CommandLine;
502   CommandLine.push_back("one");
503   CommandLine.push_back("two");
504   FixedCompilationDatabase Database(".", CommandLine);
505 
506   EXPECT_EQ(0ul, Database.getAllCompileCommands().size());
507 }
508 
509 TEST(ParseFixedCompilationDatabase, ReturnsNullOnEmptyArgumentList) {
510   int Argc = 0;
511   std::string ErrorMsg;
512   std::unique_ptr<FixedCompilationDatabase> Database =
513       FixedCompilationDatabase::loadFromCommandLine(Argc, nullptr, ErrorMsg);
514   EXPECT_FALSE(Database);
515   EXPECT_TRUE(ErrorMsg.empty());
516   EXPECT_EQ(0, Argc);
517 }
518 
519 TEST(ParseFixedCompilationDatabase, ReturnsNullWithoutDoubleDash) {
520   int Argc = 2;
521   const char *Argv[] = { "1", "2" };
522   std::string ErrorMsg;
523   std::unique_ptr<FixedCompilationDatabase> Database(
524       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg));
525   EXPECT_FALSE(Database);
526   EXPECT_TRUE(ErrorMsg.empty());
527   EXPECT_EQ(2, Argc);
528 }
529 
530 TEST(ParseFixedCompilationDatabase, ReturnsArgumentsAfterDoubleDash) {
531   int Argc = 5;
532   const char *Argv[] = {
533     "1", "2", "--\0no-constant-folding", "-DDEF3", "-DDEF4"
534   };
535   std::string ErrorMsg;
536   std::unique_ptr<FixedCompilationDatabase> Database(
537       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg));
538   ASSERT_TRUE((bool)Database);
539   ASSERT_TRUE(ErrorMsg.empty());
540   std::vector<CompileCommand> Result =
541     Database->getCompileCommands("source");
542   ASSERT_EQ(1ul, Result.size());
543   ASSERT_EQ(".", Result[0].Directory);
544   ASSERT_THAT(Result[0].CommandLine, ElementsAre(EndsWith("clang-tool"),
545                                                  "-DDEF3", "-DDEF4", "source"));
546   EXPECT_EQ(2, Argc);
547 }
548 
549 TEST(ParseFixedCompilationDatabase, ReturnsEmptyCommandLine) {
550   int Argc = 3;
551   const char *Argv[] = { "1", "2", "--\0no-constant-folding" };
552   std::string ErrorMsg;
553   std::unique_ptr<FixedCompilationDatabase> Database =
554       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMsg);
555   ASSERT_TRUE((bool)Database);
556   ASSERT_TRUE(ErrorMsg.empty());
557   std::vector<CompileCommand> Result =
558     Database->getCompileCommands("source");
559   ASSERT_EQ(1ul, Result.size());
560   ASSERT_EQ(".", Result[0].Directory);
561   ASSERT_THAT(Result[0].CommandLine,
562               ElementsAre(EndsWith("clang-tool"), "source"));
563   EXPECT_EQ(2, Argc);
564 }
565 
566 TEST(ParseFixedCompilationDatabase, HandlesPositionalArgs) {
567   const char *Argv[] = {"1", "2", "--", "-c", "somefile.cpp", "-DDEF3"};
568   int Argc = sizeof(Argv) / sizeof(char*);
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,
579               ElementsAre(EndsWith("clang-tool"), "-c", "-DDEF3", "source"));
580   EXPECT_EQ(2, Argc);
581 }
582 
583 TEST(ParseFixedCompilationDatabase, HandlesPositionalArgsSyntaxOnly) {
584   // Adjust the given command line arguments to ensure that any positional
585   // arguments in them are stripped.
586   const char *Argv[] = {"--", "somefile.cpp", "-fsyntax-only", "-DDEF3"};
587   int Argc = llvm::array_lengthof(Argv);
588   std::string ErrorMessage;
589   std::unique_ptr<CompilationDatabase> Database =
590       FixedCompilationDatabase::loadFromCommandLine(Argc, Argv, ErrorMessage);
591   ASSERT_TRUE((bool)Database);
592   ASSERT_TRUE(ErrorMessage.empty());
593   std::vector<CompileCommand> Result = Database->getCompileCommands("source");
594   ASSERT_EQ(1ul, Result.size());
595   ASSERT_EQ(".", Result[0].Directory);
596   ASSERT_THAT(
597       Result[0].CommandLine,
598       ElementsAre(EndsWith("clang-tool"), "-fsyntax-only", "-DDEF3", "source"));
599 }
600 
601 TEST(ParseFixedCompilationDatabase, HandlesArgv0) {
602   const char *Argv[] = {"1", "2", "--", "mytool", "somefile.cpp"};
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   std::vector<std::string> Expected;
614   ASSERT_THAT(Result[0].CommandLine,
615               ElementsAre(EndsWith("clang-tool"), "source"));
616   EXPECT_EQ(2, Argc);
617 }
618 
619 struct MemCDB : public CompilationDatabase {
620   using EntryMap = llvm::StringMap<SmallVector<CompileCommand, 1>>;
621   EntryMap Entries;
622   MemCDB(const EntryMap &E) : Entries(E) {}
623 
624   std::vector<CompileCommand> getCompileCommands(StringRef F) const override {
625     auto Ret = Entries.lookup(F);
626     return {Ret.begin(), Ret.end()};
627   }
628 
629   std::vector<std::string> getAllFiles() const override {
630     std::vector<std::string> Result;
631     for (const auto &Entry : Entries)
632       Result.push_back(Entry.first());
633     return Result;
634   }
635 };
636 
637 class MemDBTest : public ::testing::Test {
638 protected:
639   // Adds an entry to the underlying compilation database.
640   // A flag is injected: -D <File>, so the command used can be identified.
641   void add(StringRef File, StringRef Clang, StringRef Flags) {
642     SmallVector<StringRef, 8> Argv = {Clang, File, "-D", File};
643     llvm::SplitString(Flags, Argv);
644 
645     SmallString<32> Dir;
646     llvm::sys::path::system_temp_directory(false, Dir);
647 
648     Entries[path(File)].push_back(
649         {Dir, path(File), {Argv.begin(), Argv.end()}, "foo.o"});
650   }
651   void add(StringRef File, StringRef Flags = "") { add(File, "clang", Flags); }
652 
653   // Turn a unix path fragment (foo/bar.h) into a native path (C:\tmp\foo\bar.h)
654   std::string path(llvm::SmallString<32> File) {
655     llvm::SmallString<32> Dir;
656     llvm::sys::path::system_temp_directory(false, Dir);
657     llvm::sys::path::native(File);
658     llvm::SmallString<64> Result;
659     llvm::sys::path::append(Result, Dir, File);
660     return Result.str();
661   }
662 
663   MemCDB::EntryMap Entries;
664 };
665 
666 class InterpolateTest : public MemDBTest {
667 protected:
668   // Look up the command from a relative path, and return it in string form.
669   // The input file is not included in the returned command.
670   std::string getCommand(llvm::StringRef F) {
671     auto Results =
672         inferMissingCompileCommands(llvm::make_unique<MemCDB>(Entries))
673             ->getCompileCommands(path(F));
674     if (Results.empty())
675       return "none";
676     // drop the input file argument, so tests don't have to deal with path().
677     EXPECT_EQ(Results[0].CommandLine.back(), path(F))
678         << "Last arg should be the file";
679     Results[0].CommandLine.pop_back();
680     return llvm::join(Results[0].CommandLine, " ");
681   }
682 
683   // Parse the file whose command was used out of the Heuristic string.
684   std::string getProxy(llvm::StringRef F) {
685     auto Results =
686         inferMissingCompileCommands(llvm::make_unique<MemCDB>(Entries))
687             ->getCompileCommands(path(F));
688     if (Results.empty())
689       return "none";
690     StringRef Proxy = Results.front().Heuristic;
691     if (!Proxy.consume_front("inferred from "))
692       return "";
693     // We have a proxy file, convert back to a unix relative path.
694     // This is a bit messy, but we do need to test these strings somehow...
695     llvm::SmallString<32> TempDir;
696     llvm::sys::path::system_temp_directory(false, TempDir);
697     Proxy.consume_front(TempDir);
698     Proxy.consume_front(llvm::sys::path::get_separator());
699     llvm::SmallString<32> Result = Proxy;
700     llvm::sys::path::native(Result, llvm::sys::path::Style::posix);
701     return Result.str();
702   }
703 };
704 
705 TEST_F(InterpolateTest, Nearby) {
706   add("dir/foo.cpp");
707   add("dir/bar.cpp");
708   add("an/other/foo.cpp");
709 
710   // great: dir and name both match (prefix or full, case insensitive)
711   EXPECT_EQ(getProxy("dir/f.cpp"), "dir/foo.cpp");
712   EXPECT_EQ(getProxy("dir/FOO.cpp"), "dir/foo.cpp");
713   // no name match. prefer matching dir, break ties by alpha
714   EXPECT_EQ(getProxy("dir/a.cpp"), "dir/bar.cpp");
715   // an exact name match beats one segment of directory match
716   EXPECT_EQ(getProxy("some/other/bar.h"), "dir/bar.cpp");
717   // two segments of directory match beat a prefix name match
718   EXPECT_EQ(getProxy("an/other/b.cpp"), "an/other/foo.cpp");
719   // if nothing matches at all, we still get the closest alpha match
720   EXPECT_EQ(getProxy("below/some/obscure/path.cpp"), "an/other/foo.cpp");
721 }
722 
723 TEST_F(InterpolateTest, Language) {
724   add("dir/foo.cpp", "-std=c++17");
725   add("dir/bar.c", "");
726   add("dir/baz.cee", "-x c");
727 
728   // .h is ambiguous, so we add explicit language flags
729   EXPECT_EQ(getCommand("foo.h"),
730             "clang -D dir/foo.cpp -x c++-header -std=c++17");
731   // Same thing if we have no extension. (again, we treat as header).
732   EXPECT_EQ(getCommand("foo"), "clang -D dir/foo.cpp -x c++-header -std=c++17");
733   // and invalid extensions.
734   EXPECT_EQ(getCommand("foo.cce"),
735             "clang -D dir/foo.cpp -x c++-header -std=c++17");
736   // and don't add -x if the inferred language is correct.
737   EXPECT_EQ(getCommand("foo.hpp"), "clang -D dir/foo.cpp -std=c++17");
738   // respect -x if it's already there.
739   EXPECT_EQ(getCommand("baz.h"), "clang -D dir/baz.cee -x c-header");
740   // prefer a worse match with the right extension.
741   EXPECT_EQ(getCommand("foo.c"), "clang -D dir/bar.c");
742   Entries.erase(path(StringRef("dir/bar.c")));
743   // Now we transfer across languages, so drop -std too.
744   EXPECT_EQ(getCommand("foo.c"), "clang -D dir/foo.cpp");
745 }
746 
747 TEST_F(InterpolateTest, Strip) {
748   add("dir/foo.cpp", "-o foo.o -Wall");
749   // the -o option and the input file are removed, but -Wall is preserved.
750   EXPECT_EQ(getCommand("dir/bar.cpp"), "clang -D dir/foo.cpp -Wall");
751 }
752 
753 TEST_F(InterpolateTest, Case) {
754   add("FOO/BAR/BAZ/SHOUT.cc");
755   add("foo/bar/baz/quiet.cc");
756   // Case mismatches are completely ignored, so we choose the name match.
757   EXPECT_EQ(getProxy("foo/bar/baz/shout.C"), "FOO/BAR/BAZ/SHOUT.cc");
758 }
759 
760 TEST_F(InterpolateTest, Aliasing) {
761   add("foo.cpp", "-faligned-new");
762 
763   // The interpolated command should keep the given flag as written, even though
764   // the flag is internally represented as an alias.
765   EXPECT_EQ(getCommand("foo.hpp"), "clang -D foo.cpp -faligned-new");
766 }
767 
768 TEST_F(InterpolateTest, ClangCL) {
769   add("foo.cpp", "clang-cl", "/W4");
770 
771   // Language flags should be added with CL syntax.
772   EXPECT_EQ(getCommand("foo.h"), "clang-cl -D foo.cpp /W4 /TP");
773 }
774 
775 TEST_F(InterpolateTest, DriverModes) {
776   add("foo.cpp", "clang-cl", "--driver-mode=gcc");
777   add("bar.cpp", "clang", "--driver-mode=cl");
778 
779   // --driver-mode overrides should be respected.
780   EXPECT_EQ(getCommand("foo.h"), "clang-cl -D foo.cpp --driver-mode=gcc -x c++-header");
781   EXPECT_EQ(getCommand("bar.h"), "clang -D bar.cpp --driver-mode=cl /TP");
782 }
783 
784 TEST(CompileCommandTest, EqualityOperator) {
785   CompileCommand CCRef("/foo/bar", "hello.c", {"a", "b"}, "hello.o");
786   CompileCommand CCTest = CCRef;
787 
788   EXPECT_TRUE(CCRef == CCTest);
789   EXPECT_FALSE(CCRef != CCTest);
790 
791   CCTest = CCRef;
792   CCTest.Directory = "/foo/baz";
793   EXPECT_FALSE(CCRef == CCTest);
794   EXPECT_TRUE(CCRef != CCTest);
795 
796   CCTest = CCRef;
797   CCTest.Filename = "bonjour.c";
798   EXPECT_FALSE(CCRef == CCTest);
799   EXPECT_TRUE(CCRef != CCTest);
800 
801   CCTest = CCRef;
802   CCTest.CommandLine.push_back("c");
803   EXPECT_FALSE(CCRef == CCTest);
804   EXPECT_TRUE(CCRef != CCTest);
805 
806   CCTest = CCRef;
807   CCTest.Output = "bonjour.o";
808   EXPECT_FALSE(CCRef == CCTest);
809   EXPECT_TRUE(CCRef != CCTest);
810 }
811 
812 class TargetAndModeTest : public MemDBTest {
813 public:
814   TargetAndModeTest() { llvm::InitializeAllTargetInfos(); }
815 
816 protected:
817   // Look up the command from a relative path, and return it in string form.
818   std::string getCommand(llvm::StringRef F) {
819     auto Results = inferTargetAndDriverMode(llvm::make_unique<MemCDB>(Entries))
820                        ->getCompileCommands(path(F));
821     if (Results.empty())
822       return "none";
823     return llvm::join(Results[0].CommandLine, " ");
824   }
825 };
826 
827 TEST_F(TargetAndModeTest, TargetAndMode) {
828   add("foo.cpp", "clang-cl", "");
829   add("bar.cpp", "clang++", "");
830 
831   EXPECT_EQ(getCommand("foo.cpp"),
832             "clang-cl --driver-mode=cl foo.cpp -D foo.cpp");
833   EXPECT_EQ(getCommand("bar.cpp"),
834             "clang++ --driver-mode=g++ bar.cpp -D bar.cpp");
835 }
836 
837 } // end namespace tooling
838 } // end namespace clang
839