blob: 8866e751fcfe5a7058c19f7c557335f8c9515bff [file] [log] [blame]
Manuel Klimekcb971c62012-04-04 12:07:46 +00001//===- unittest/Tooling/CompilationDatabaseTest.cpp -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/AST/ASTConsumer.h"
11#include "clang/AST/DeclCXX.h"
12#include "clang/AST/DeclGroup.h"
13#include "clang/Frontend/FrontendAction.h"
Daniel Jasperd3420c92012-10-08 16:08:15 +000014#include "clang/Tooling/FileMatchTrie.h"
Daniel Jasper7fd90b02012-08-24 05:50:27 +000015#include "clang/Tooling/JSONCompilationDatabase.h"
Manuel Klimekcb971c62012-04-04 12:07:46 +000016#include "clang/Tooling/Tooling.h"
Rafael Espindola8229d222013-06-11 22:15:02 +000017#include "llvm/Support/Path.h"
Manuel Klimekcb971c62012-04-04 12:07:46 +000018#include "gtest/gtest.h"
19
20namespace clang {
21namespace tooling {
22
Manuel Klimek1a8d6862012-05-15 11:46:07 +000023static void expectFailure(StringRef JSONDatabase, StringRef Explanation) {
24 std::string ErrorMessage;
Stephen Hinesc568f1e2014-07-21 00:47:37 -070025 EXPECT_EQ(nullptr, JSONCompilationDatabase::loadFromBuffer(JSONDatabase,
26 ErrorMessage))
Stephen Hines651f13c2014-04-23 16:59:28 -070027 << "Expected an error because of: " << Explanation.str();
Manuel Klimek1a8d6862012-05-15 11:46:07 +000028}
29
30TEST(JSONCompilationDatabase, ErrsOnInvalidFormat) {
31 expectFailure("", "Empty database");
32 expectFailure("{", "Invalid JSON");
33 expectFailure("[[]]", "Array instead of object");
34 expectFailure("[{\"a\":[]}]", "Array instead of value");
35 expectFailure("[{\"a\":\"b\"}]", "Unknown key");
36 expectFailure("[{[]:\"\"}]", "Incorrectly typed entry");
37 expectFailure("[{}]", "Empty entry");
38 expectFailure("[{\"directory\":\"\",\"command\":\"\"}]", "Missing file");
39 expectFailure("[{\"directory\":\"\",\"file\":\"\"}]", "Missing command");
40 expectFailure("[{\"command\":\"\",\"file\":\"\"}]", "Missing directory");
41}
42
Manuel Klimeka3c70962012-07-13 12:31:45 +000043static std::vector<std::string> getAllFiles(StringRef JSONDatabase,
44 std::string &ErrorMessage) {
Stephen Hines651f13c2014-04-23 16:59:28 -070045 std::unique_ptr<CompilationDatabase> Database(
Manuel Klimeka3c70962012-07-13 12:31:45 +000046 JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage));
47 if (!Database) {
48 ADD_FAILURE() << ErrorMessage;
49 return std::vector<std::string>();
50 }
51 return Database->getAllFiles();
52}
53
Argyrios Kyrtzidis7e96bfb2012-12-04 07:26:44 +000054static std::vector<CompileCommand> getAllCompileCommands(StringRef JSONDatabase,
55 std::string &ErrorMessage) {
Stephen Hines651f13c2014-04-23 16:59:28 -070056 std::unique_ptr<CompilationDatabase> Database(
Argyrios Kyrtzidis7e96bfb2012-12-04 07:26:44 +000057 JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage));
58 if (!Database) {
59 ADD_FAILURE() << ErrorMessage;
60 return std::vector<CompileCommand>();
61 }
62 return Database->getAllCompileCommands();
63}
64
Manuel Klimeka3c70962012-07-13 12:31:45 +000065TEST(JSONCompilationDatabase, GetAllFiles) {
66 std::string ErrorMessage;
67 EXPECT_EQ(std::vector<std::string>(),
68 getAllFiles("[]", ErrorMessage)) << ErrorMessage;
69
70 std::vector<std::string> expected_files;
Daniel Jasper3201da32012-10-08 20:32:51 +000071 SmallString<16> PathStorage;
72 llvm::sys::path::native("//net/dir/file1", PathStorage);
73 expected_files.push_back(PathStorage.str());
74 llvm::sys::path::native("//net/dir/file2", PathStorage);
75 expected_files.push_back(PathStorage.str());
Manuel Klimeka3c70962012-07-13 12:31:45 +000076 EXPECT_EQ(expected_files, getAllFiles(
Daniel Jasper8a5e8c32012-10-08 20:08:25 +000077 "[{\"directory\":\"//net/dir\","
Manuel Klimeka3c70962012-07-13 12:31:45 +000078 "\"command\":\"command\","
79 "\"file\":\"file1\"},"
Daniel Jasper8a5e8c32012-10-08 20:08:25 +000080 " {\"directory\":\"//net/dir\","
Manuel Klimeka3c70962012-07-13 12:31:45 +000081 "\"command\":\"command\","
82 "\"file\":\"file2\"}]",
83 ErrorMessage)) << ErrorMessage;
84}
85
Argyrios Kyrtzidis7e96bfb2012-12-04 07:26:44 +000086TEST(JSONCompilationDatabase, GetAllCompileCommands) {
87 std::string ErrorMessage;
88 EXPECT_EQ(0u,
89 getAllCompileCommands("[]", ErrorMessage).size()) << ErrorMessage;
90
91 StringRef Directory1("//net/dir1");
92 StringRef FileName1("file1");
93 StringRef Command1("command1");
94 StringRef Directory2("//net/dir2");
95 StringRef FileName2("file1");
96 StringRef Command2("command1");
97
98 std::vector<CompileCommand> Commands = getAllCompileCommands(
99 ("[{\"directory\":\"" + Directory1 + "\"," +
100 "\"command\":\"" + Command1 + "\","
101 "\"file\":\"" + FileName1 + "\"},"
102 " {\"directory\":\"" + Directory2 + "\"," +
103 "\"command\":\"" + Command2 + "\","
104 "\"file\":\"" + FileName2 + "\"}]").str(),
105 ErrorMessage);
106 EXPECT_EQ(2U, Commands.size()) << ErrorMessage;
107 EXPECT_EQ(Directory1, Commands[0].Directory) << ErrorMessage;
108 ASSERT_EQ(1u, Commands[0].CommandLine.size());
109 EXPECT_EQ(Command1, Commands[0].CommandLine[0]) << ErrorMessage;
110 EXPECT_EQ(Directory2, Commands[1].Directory) << ErrorMessage;
111 ASSERT_EQ(1u, Commands[1].CommandLine.size());
112 EXPECT_EQ(Command2, Commands[1].CommandLine[0]) << ErrorMessage;
113}
114
Manuel Klimekcb971c62012-04-04 12:07:46 +0000115static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName,
116 StringRef JSONDatabase,
117 std::string &ErrorMessage) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700118 std::unique_ptr<CompilationDatabase> Database(
Manuel Klimekcb971c62012-04-04 12:07:46 +0000119 JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage));
120 if (!Database)
121 return CompileCommand();
122 std::vector<CompileCommand> Commands = Database->getCompileCommands(FileName);
123 EXPECT_LE(Commands.size(), 1u);
124 if (Commands.empty())
125 return CompileCommand();
126 return Commands[0];
127}
128
Daniel Jasperd3420c92012-10-08 16:08:15 +0000129struct FakeComparator : public PathComparator {
130 virtual ~FakeComparator() {}
Daniel Jasper2dbe2fa2012-10-08 18:31:54 +0000131 virtual bool equivalent(StringRef FileA, StringRef FileB) const {
132 return FileA.equals_lower(FileB);
Daniel Jasperd3420c92012-10-08 16:08:15 +0000133 }
134};
135
136class FileMatchTrieTest : public ::testing::Test {
137protected:
138 FileMatchTrieTest() : Trie(new FakeComparator()) {}
139
140 StringRef find(StringRef Path) {
141 llvm::raw_string_ostream ES(Error);
142 return Trie.findEquivalent(Path, ES);
143 }
144
145 FileMatchTrie Trie;
146 std::string Error;
147};
148
149TEST_F(FileMatchTrieTest, InsertingRelativePath) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000150 Trie.insert("//net/path/file.cc");
Daniel Jasperd3420c92012-10-08 16:08:15 +0000151 Trie.insert("file.cc");
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000152 EXPECT_EQ("//net/path/file.cc", find("//net/path/file.cc"));
Daniel Jasperd3420c92012-10-08 16:08:15 +0000153}
154
155TEST_F(FileMatchTrieTest, MatchingRelativePath) {
156 EXPECT_EQ("", find("file.cc"));
157}
158
159TEST_F(FileMatchTrieTest, ReturnsBestResults) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000160 Trie.insert("//net/d/c/b.cc");
161 Trie.insert("//net/d/b/b.cc");
162 EXPECT_EQ("//net/d/b/b.cc", find("//net/d/b/b.cc"));
Daniel Jasperd3420c92012-10-08 16:08:15 +0000163}
164
165TEST_F(FileMatchTrieTest, HandlesSymlinks) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000166 Trie.insert("//net/AA/file.cc");
167 EXPECT_EQ("//net/AA/file.cc", find("//net/aa/file.cc"));
Daniel Jasperd3420c92012-10-08 16:08:15 +0000168}
169
170TEST_F(FileMatchTrieTest, ReportsSymlinkAmbiguity) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000171 Trie.insert("//net/Aa/file.cc");
172 Trie.insert("//net/aA/file.cc");
173 EXPECT_TRUE(find("//net/aa/file.cc").empty());
Daniel Jasperd3420c92012-10-08 16:08:15 +0000174 EXPECT_EQ("Path is ambiguous", Error);
175}
176
177TEST_F(FileMatchTrieTest, LongerMatchingSuffixPreferred) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000178 Trie.insert("//net/src/Aa/file.cc");
179 Trie.insert("//net/src/aA/file.cc");
180 Trie.insert("//net/SRC/aa/file.cc");
181 EXPECT_EQ("//net/SRC/aa/file.cc", find("//net/src/aa/file.cc"));
Daniel Jasperd3420c92012-10-08 16:08:15 +0000182}
183
184TEST_F(FileMatchTrieTest, EmptyTrie) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000185 EXPECT_TRUE(find("//net/some/path").empty());
Daniel Jasperd3420c92012-10-08 16:08:15 +0000186}
187
188TEST_F(FileMatchTrieTest, NoResult) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000189 Trie.insert("//net/somepath/otherfile.cc");
190 Trie.insert("//net/otherpath/somefile.cc");
191 EXPECT_EQ("", find("//net/somepath/somefile.cc"));
Daniel Jasperd3420c92012-10-08 16:08:15 +0000192}
193
194TEST_F(FileMatchTrieTest, RootElementDifferent) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000195 Trie.insert("//net/path/file.cc");
196 Trie.insert("//net/otherpath/file.cc");
197 EXPECT_EQ("//net/path/file.cc", find("//net/path/file.cc"));
Daniel Jasperd3420c92012-10-08 16:08:15 +0000198}
199
200TEST_F(FileMatchTrieTest, CannotResolveRelativePath) {
201 EXPECT_EQ("", find("relative-path.cc"));
202 EXPECT_EQ("Cannot resolve relative paths", Error);
203}
204
Manuel Klimekcb971c62012-04-04 12:07:46 +0000205TEST(findCompileArgsInJsonDatabase, FindsNothingIfEmpty) {
206 std::string ErrorMessage;
207 CompileCommand NotFound = findCompileArgsInJsonDatabase(
208 "a-file.cpp", "", ErrorMessage);
209 EXPECT_TRUE(NotFound.CommandLine.empty()) << ErrorMessage;
210 EXPECT_TRUE(NotFound.Directory.empty()) << ErrorMessage;
211}
212
213TEST(findCompileArgsInJsonDatabase, ReadsSingleEntry) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000214 StringRef Directory("//net/some/directory");
215 StringRef FileName("//net/path/to/a-file.cpp");
216 StringRef Command("//net/path/to/compiler and some arguments");
Manuel Klimekcb971c62012-04-04 12:07:46 +0000217 std::string ErrorMessage;
218 CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
219 FileName,
220 ("[{\"directory\":\"" + Directory + "\"," +
221 "\"command\":\"" + Command + "\","
222 "\"file\":\"" + FileName + "\"}]").str(),
223 ErrorMessage);
224 EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage;
225 ASSERT_EQ(4u, FoundCommand.CommandLine.size()) << ErrorMessage;
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000226 EXPECT_EQ("//net/path/to/compiler",
227 FoundCommand.CommandLine[0]) << ErrorMessage;
Manuel Klimekcb971c62012-04-04 12:07:46 +0000228 EXPECT_EQ("and", FoundCommand.CommandLine[1]) << ErrorMessage;
229 EXPECT_EQ("some", FoundCommand.CommandLine[2]) << ErrorMessage;
230 EXPECT_EQ("arguments", FoundCommand.CommandLine[3]) << ErrorMessage;
231
232 CompileCommand NotFound = findCompileArgsInJsonDatabase(
233 "a-file.cpp",
234 ("[{\"directory\":\"" + Directory + "\"," +
235 "\"command\":\"" + Command + "\","
236 "\"file\":\"" + FileName + "\"}]").str(),
237 ErrorMessage);
238 EXPECT_TRUE(NotFound.Directory.empty()) << ErrorMessage;
239 EXPECT_TRUE(NotFound.CommandLine.empty()) << ErrorMessage;
240}
241
242TEST(findCompileArgsInJsonDatabase, ReadsCompileCommandLinesWithSpaces) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000243 StringRef Directory("//net/some/directory");
244 StringRef FileName("//net/path/to/a-file.cpp");
245 StringRef Command("\\\"//net/path to compiler\\\" \\\"and an argument\\\"");
Manuel Klimekcb971c62012-04-04 12:07:46 +0000246 std::string ErrorMessage;
247 CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
248 FileName,
249 ("[{\"directory\":\"" + Directory + "\"," +
250 "\"command\":\"" + Command + "\","
251 "\"file\":\"" + FileName + "\"}]").str(),
252 ErrorMessage);
253 ASSERT_EQ(2u, FoundCommand.CommandLine.size());
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000254 EXPECT_EQ("//net/path to compiler",
255 FoundCommand.CommandLine[0]) << ErrorMessage;
Manuel Klimekcb971c62012-04-04 12:07:46 +0000256 EXPECT_EQ("and an argument", FoundCommand.CommandLine[1]) << ErrorMessage;
257}
258
259TEST(findCompileArgsInJsonDatabase, ReadsDirectoryWithSpaces) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000260 StringRef Directory("//net/some directory / with spaces");
261 StringRef FileName("//net/path/to/a-file.cpp");
Manuel Klimekcb971c62012-04-04 12:07:46 +0000262 StringRef Command("a command");
263 std::string ErrorMessage;
264 CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
265 FileName,
266 ("[{\"directory\":\"" + Directory + "\"," +
267 "\"command\":\"" + Command + "\","
268 "\"file\":\"" + FileName + "\"}]").str(),
269 ErrorMessage);
270 EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage;
271}
272
273TEST(findCompileArgsInJsonDatabase, FindsEntry) {
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000274 StringRef Directory("//net/directory");
Manuel Klimekcb971c62012-04-04 12:07:46 +0000275 StringRef FileName("file");
276 StringRef Command("command");
277 std::string JsonDatabase = "[";
278 for (int I = 0; I < 10; ++I) {
279 if (I > 0) JsonDatabase += ",";
280 JsonDatabase +=
281 ("{\"directory\":\"" + Directory + Twine(I) + "\"," +
282 "\"command\":\"" + Command + Twine(I) + "\","
283 "\"file\":\"" + FileName + Twine(I) + "\"}").str();
284 }
285 JsonDatabase += "]";
286 std::string ErrorMessage;
287 CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000288 "//net/directory4/file4", JsonDatabase, ErrorMessage);
289 EXPECT_EQ("//net/directory4", FoundCommand.Directory) << ErrorMessage;
Manuel Klimekcb971c62012-04-04 12:07:46 +0000290 ASSERT_EQ(1u, FoundCommand.CommandLine.size()) << ErrorMessage;
291 EXPECT_EQ("command4", FoundCommand.CommandLine[0]) << ErrorMessage;
292}
293
294static std::vector<std::string> unescapeJsonCommandLine(StringRef Command) {
295 std::string JsonDatabase =
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000296 ("[{\"directory\":\"//net/root\", \"file\":\"test\", \"command\": \"" +
Manuel Klimekcb971c62012-04-04 12:07:46 +0000297 Command + "\"}]").str();
298 std::string ErrorMessage;
299 CompileCommand FoundCommand = findCompileArgsInJsonDatabase(
Daniel Jasper8a5e8c32012-10-08 20:08:25 +0000300 "//net/root/test", JsonDatabase, ErrorMessage);
Manuel Klimekcb971c62012-04-04 12:07:46 +0000301 EXPECT_TRUE(ErrorMessage.empty()) << ErrorMessage;
302 return FoundCommand.CommandLine;
303}
304
305TEST(unescapeJsonCommandLine, ReturnsEmptyArrayOnEmptyString) {
306 std::vector<std::string> Result = unescapeJsonCommandLine("");
307 EXPECT_TRUE(Result.empty());
308}
309
310TEST(unescapeJsonCommandLine, SplitsOnSpaces) {
311 std::vector<std::string> Result = unescapeJsonCommandLine("a b c");
312 ASSERT_EQ(3ul, Result.size());
313 EXPECT_EQ("a", Result[0]);
314 EXPECT_EQ("b", Result[1]);
315 EXPECT_EQ("c", Result[2]);
316}
317
318TEST(unescapeJsonCommandLine, MungesMultipleSpaces) {
319 std::vector<std::string> Result = unescapeJsonCommandLine(" a b ");
320 ASSERT_EQ(2ul, Result.size());
321 EXPECT_EQ("a", Result[0]);
322 EXPECT_EQ("b", Result[1]);
323}
324
325TEST(unescapeJsonCommandLine, UnescapesBackslashCharacters) {
326 std::vector<std::string> Backslash = unescapeJsonCommandLine("a\\\\\\\\");
327 ASSERT_EQ(1ul, Backslash.size());
328 EXPECT_EQ("a\\", Backslash[0]);
329 std::vector<std::string> Quote = unescapeJsonCommandLine("a\\\\\\\"");
330 ASSERT_EQ(1ul, Quote.size());
331 EXPECT_EQ("a\"", Quote[0]);
332}
333
334TEST(unescapeJsonCommandLine, DoesNotMungeSpacesBetweenQuotes) {
335 std::vector<std::string> Result = unescapeJsonCommandLine("\\\" a b \\\"");
336 ASSERT_EQ(1ul, Result.size());
337 EXPECT_EQ(" a b ", Result[0]);
338}
339
340TEST(unescapeJsonCommandLine, AllowsMultipleQuotedArguments) {
341 std::vector<std::string> Result = unescapeJsonCommandLine(
342 " \\\" a \\\" \\\" b \\\" ");
343 ASSERT_EQ(2ul, Result.size());
344 EXPECT_EQ(" a ", Result[0]);
345 EXPECT_EQ(" b ", Result[1]);
346}
347
348TEST(unescapeJsonCommandLine, AllowsEmptyArgumentsInQuotes) {
349 std::vector<std::string> Result = unescapeJsonCommandLine(
350 "\\\"\\\"\\\"\\\"");
351 ASSERT_EQ(1ul, Result.size());
352 EXPECT_TRUE(Result[0].empty()) << Result[0];
353}
354
355TEST(unescapeJsonCommandLine, ParsesEscapedQuotesInQuotedStrings) {
356 std::vector<std::string> Result = unescapeJsonCommandLine(
357 "\\\"\\\\\\\"\\\"");
358 ASSERT_EQ(1ul, Result.size());
359 EXPECT_EQ("\"", Result[0]);
360}
361
362TEST(unescapeJsonCommandLine, ParsesMultipleArgumentsWithEscapedCharacters) {
363 std::vector<std::string> Result = unescapeJsonCommandLine(
364 " \\\\\\\" \\\"a \\\\\\\" b \\\" \\\"and\\\\\\\\c\\\" \\\\\\\"");
365 ASSERT_EQ(4ul, Result.size());
366 EXPECT_EQ("\"", Result[0]);
367 EXPECT_EQ("a \" b ", Result[1]);
368 EXPECT_EQ("and\\c", Result[2]);
369 EXPECT_EQ("\"", Result[3]);
370}
371
372TEST(unescapeJsonCommandLine, ParsesStringsWithoutSpacesIntoSingleArgument) {
373 std::vector<std::string> QuotedNoSpaces = unescapeJsonCommandLine(
374 "\\\"a\\\"\\\"b\\\"");
375 ASSERT_EQ(1ul, QuotedNoSpaces.size());
376 EXPECT_EQ("ab", QuotedNoSpaces[0]);
377
378 std::vector<std::string> MixedNoSpaces = unescapeJsonCommandLine(
379 "\\\"a\\\"bcd\\\"ef\\\"\\\"\\\"\\\"g\\\"");
380 ASSERT_EQ(1ul, MixedNoSpaces.size());
381 EXPECT_EQ("abcdefg", MixedNoSpaces[0]);
382}
383
384TEST(unescapeJsonCommandLine, ParsesQuotedStringWithoutClosingQuote) {
385 std::vector<std::string> Unclosed = unescapeJsonCommandLine("\\\"abc");
386 ASSERT_EQ(1ul, Unclosed.size());
387 EXPECT_EQ("abc", Unclosed[0]);
388
389 std::vector<std::string> Empty = unescapeJsonCommandLine("\\\"");
390 ASSERT_EQ(1ul, Empty.size());
391 EXPECT_EQ("", Empty[0]);
392}
393
Peter Collingbourneb88d9482013-03-02 06:00:16 +0000394TEST(unescapeJsonCommandLine, ParsesSingleQuotedString) {
395 std::vector<std::string> Args = unescapeJsonCommandLine("a'\\\\b \\\"c\\\"'");
396 ASSERT_EQ(1ul, Args.size());
397 EXPECT_EQ("a\\b \"c\"", Args[0]);
398}
399
Manuel Klimek30318e62012-04-18 07:41:50 +0000400TEST(FixedCompilationDatabase, ReturnsFixedCommandLine) {
401 std::vector<std::string> CommandLine;
402 CommandLine.push_back("one");
403 CommandLine.push_back("two");
404 FixedCompilationDatabase Database(".", CommandLine);
405 std::vector<CompileCommand> Result =
406 Database.getCompileCommands("source");
407 ASSERT_EQ(1ul, Result.size());
408 std::vector<std::string> ExpectedCommandLine(1, "clang-tool");
409 ExpectedCommandLine.insert(ExpectedCommandLine.end(),
410 CommandLine.begin(), CommandLine.end());
411 ExpectedCommandLine.push_back("source");
412 EXPECT_EQ(".", Result[0].Directory);
413 EXPECT_EQ(ExpectedCommandLine, Result[0].CommandLine);
414}
415
Manuel Klimeka3c70962012-07-13 12:31:45 +0000416TEST(FixedCompilationDatabase, GetAllFiles) {
417 std::vector<std::string> CommandLine;
418 CommandLine.push_back("one");
419 CommandLine.push_back("two");
420 FixedCompilationDatabase Database(".", CommandLine);
421
422 EXPECT_EQ(0ul, Database.getAllFiles().size());
423}
424
Argyrios Kyrtzidis7e96bfb2012-12-04 07:26:44 +0000425TEST(FixedCompilationDatabase, GetAllCompileCommands) {
426 std::vector<std::string> CommandLine;
427 CommandLine.push_back("one");
428 CommandLine.push_back("two");
429 FixedCompilationDatabase Database(".", CommandLine);
430
431 EXPECT_EQ(0ul, Database.getAllCompileCommands().size());
432}
433
Manuel Klimek30318e62012-04-18 07:41:50 +0000434TEST(ParseFixedCompilationDatabase, ReturnsNullOnEmptyArgumentList) {
435 int Argc = 0;
Stephen Hines651f13c2014-04-23 16:59:28 -0700436 std::unique_ptr<FixedCompilationDatabase> Database(
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700437 FixedCompilationDatabase::loadFromCommandLine(Argc, nullptr));
Manuel Klimek30318e62012-04-18 07:41:50 +0000438 EXPECT_FALSE(Database);
439 EXPECT_EQ(0, Argc);
440}
441
442TEST(ParseFixedCompilationDatabase, ReturnsNullWithoutDoubleDash) {
443 int Argc = 2;
444 const char *Argv[] = { "1", "2" };
Stephen Hines651f13c2014-04-23 16:59:28 -0700445 std::unique_ptr<FixedCompilationDatabase> Database(
Manuel Klimek30318e62012-04-18 07:41:50 +0000446 FixedCompilationDatabase::loadFromCommandLine(Argc, Argv));
447 EXPECT_FALSE(Database);
448 EXPECT_EQ(2, Argc);
449}
450
451TEST(ParseFixedCompilationDatabase, ReturnsArgumentsAfterDoubleDash) {
452 int Argc = 5;
Edwin Vanec8f03422013-11-17 16:08:04 +0000453 const char *Argv[] = {
454 "1", "2", "--\0no-constant-folding", "-DDEF3", "-DDEF4"
455 };
Stephen Hines651f13c2014-04-23 16:59:28 -0700456 std::unique_ptr<FixedCompilationDatabase> Database(
Manuel Klimek30318e62012-04-18 07:41:50 +0000457 FixedCompilationDatabase::loadFromCommandLine(Argc, Argv));
Stephen Hines651f13c2014-04-23 16:59:28 -0700458 ASSERT_TRUE((bool)Database);
Manuel Klimek30318e62012-04-18 07:41:50 +0000459 std::vector<CompileCommand> Result =
460 Database->getCompileCommands("source");
461 ASSERT_EQ(1ul, Result.size());
462 ASSERT_EQ(".", Result[0].Directory);
463 std::vector<std::string> CommandLine;
464 CommandLine.push_back("clang-tool");
Edwin Vanec8f03422013-11-17 16:08:04 +0000465 CommandLine.push_back("-DDEF3");
466 CommandLine.push_back("-DDEF4");
Manuel Klimek30318e62012-04-18 07:41:50 +0000467 CommandLine.push_back("source");
468 ASSERT_EQ(CommandLine, Result[0].CommandLine);
469 EXPECT_EQ(2, Argc);
470}
471
472TEST(ParseFixedCompilationDatabase, ReturnsEmptyCommandLine) {
473 int Argc = 3;
474 const char *Argv[] = { "1", "2", "--\0no-constant-folding" };
Stephen Hines651f13c2014-04-23 16:59:28 -0700475 std::unique_ptr<FixedCompilationDatabase> Database(
Manuel Klimek30318e62012-04-18 07:41:50 +0000476 FixedCompilationDatabase::loadFromCommandLine(Argc, Argv));
Stephen Hines651f13c2014-04-23 16:59:28 -0700477 ASSERT_TRUE((bool)Database);
Manuel Klimek30318e62012-04-18 07:41:50 +0000478 std::vector<CompileCommand> Result =
479 Database->getCompileCommands("source");
480 ASSERT_EQ(1ul, Result.size());
481 ASSERT_EQ(".", Result[0].Directory);
482 std::vector<std::string> CommandLine;
483 CommandLine.push_back("clang-tool");
484 CommandLine.push_back("source");
485 ASSERT_EQ(CommandLine, Result[0].CommandLine);
486 EXPECT_EQ(2, Argc);
487}
488
Edwin Vanec8f03422013-11-17 16:08:04 +0000489TEST(ParseFixedCompilationDatabase, HandlesPositionalArgs) {
490 const char *Argv[] = {"1", "2", "--", "-c", "somefile.cpp", "-DDEF3"};
491 int Argc = sizeof(Argv) / sizeof(char*);
Stephen Hines651f13c2014-04-23 16:59:28 -0700492 std::unique_ptr<FixedCompilationDatabase> Database(
Edwin Vanec8f03422013-11-17 16:08:04 +0000493 FixedCompilationDatabase::loadFromCommandLine(Argc, Argv));
Stephen Hines651f13c2014-04-23 16:59:28 -0700494 ASSERT_TRUE((bool)Database);
Edwin Vanec8f03422013-11-17 16:08:04 +0000495 std::vector<CompileCommand> Result =
496 Database->getCompileCommands("source");
497 ASSERT_EQ(1ul, Result.size());
498 ASSERT_EQ(".", Result[0].Directory);
499 std::vector<std::string> Expected;
500 Expected.push_back("clang-tool");
501 Expected.push_back("-c");
502 Expected.push_back("-DDEF3");
503 Expected.push_back("source");
504 ASSERT_EQ(Expected, Result[0].CommandLine);
505 EXPECT_EQ(2, Argc);
506}
507
508TEST(ParseFixedCompilationDatabase, HandlesArgv0) {
509 const char *Argv[] = {"1", "2", "--", "mytool", "somefile.cpp"};
510 int Argc = sizeof(Argv) / sizeof(char*);
Stephen Hines651f13c2014-04-23 16:59:28 -0700511 std::unique_ptr<FixedCompilationDatabase> Database(
Edwin Vanec8f03422013-11-17 16:08:04 +0000512 FixedCompilationDatabase::loadFromCommandLine(Argc, Argv));
Stephen Hines651f13c2014-04-23 16:59:28 -0700513 ASSERT_TRUE((bool)Database);
Edwin Vanec8f03422013-11-17 16:08:04 +0000514 std::vector<CompileCommand> Result =
515 Database->getCompileCommands("source");
516 ASSERT_EQ(1ul, Result.size());
517 ASSERT_EQ(".", Result[0].Directory);
518 std::vector<std::string> Expected;
519 Expected.push_back("clang-tool");
520 Expected.push_back("source");
521 ASSERT_EQ(Expected, Result[0].CommandLine);
522 EXPECT_EQ(2, Argc);
523}
524
Manuel Klimekcb971c62012-04-04 12:07:46 +0000525} // end namespace tooling
526} // end namespace clang