diff options
Diffstat (limited to 'unittests/Tooling/CompilationDatabaseTest.cpp')
-rw-r--r-- | unittests/Tooling/CompilationDatabaseTest.cpp | 94 |
1 files changed, 54 insertions, 40 deletions
diff --git a/unittests/Tooling/CompilationDatabaseTest.cpp b/unittests/Tooling/CompilationDatabaseTest.cpp index ffc1d5b3a4acb..949d6a3b73924 100644 --- a/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/unittests/Tooling/CompilationDatabaseTest.cpp @@ -14,11 +14,15 @@ #include "clang/Tooling/JSONCompilationDatabase.h" #include "clang/Tooling/Tooling.h" #include "llvm/Support/Path.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace clang { namespace tooling { +using testing::ElementsAre; +using testing::EndsWith; + static void expectFailure(StringRef JSONDatabase, StringRef Explanation) { std::string ErrorMessage; EXPECT_EQ(nullptr, @@ -467,21 +471,15 @@ TEST(unescapeJsonCommandLine, ParsesSingleQuotedString) { } TEST(FixedCompilationDatabase, ReturnsFixedCommandLine) { - std::vector<std::string> CommandLine; - CommandLine.push_back("one"); - CommandLine.push_back("two"); - FixedCompilationDatabase Database(".", CommandLine); + FixedCompilationDatabase Database(".", /*CommandLine*/ {"one", "two"}); StringRef FileName("source"); std::vector<CompileCommand> Result = Database.getCompileCommands(FileName); ASSERT_EQ(1ul, Result.size()); - std::vector<std::string> ExpectedCommandLine(1, "clang-tool"); - ExpectedCommandLine.insert(ExpectedCommandLine.end(), - CommandLine.begin(), CommandLine.end()); - ExpectedCommandLine.push_back("source"); EXPECT_EQ(".", Result[0].Directory); EXPECT_EQ(FileName, Result[0].Filename); - EXPECT_EQ(ExpectedCommandLine, Result[0].CommandLine); + EXPECT_THAT(Result[0].CommandLine, + ElementsAre(EndsWith("clang-tool"), "one", "two", "source")); } TEST(FixedCompilationDatabase, GetAllFiles) { @@ -537,12 +535,8 @@ TEST(ParseFixedCompilationDatabase, ReturnsArgumentsAfterDoubleDash) { Database->getCompileCommands("source"); ASSERT_EQ(1ul, Result.size()); ASSERT_EQ(".", Result[0].Directory); - std::vector<std::string> CommandLine; - CommandLine.push_back("clang-tool"); - CommandLine.push_back("-DDEF3"); - CommandLine.push_back("-DDEF4"); - CommandLine.push_back("source"); - ASSERT_EQ(CommandLine, Result[0].CommandLine); + ASSERT_THAT(Result[0].CommandLine, ElementsAre(EndsWith("clang-tool"), + "-DDEF3", "-DDEF4", "source")); EXPECT_EQ(2, Argc); } @@ -558,10 +552,8 @@ TEST(ParseFixedCompilationDatabase, ReturnsEmptyCommandLine) { Database->getCompileCommands("source"); ASSERT_EQ(1ul, Result.size()); ASSERT_EQ(".", Result[0].Directory); - std::vector<std::string> CommandLine; - CommandLine.push_back("clang-tool"); - CommandLine.push_back("source"); - ASSERT_EQ(CommandLine, Result[0].CommandLine); + ASSERT_THAT(Result[0].CommandLine, + ElementsAre(EndsWith("clang-tool"), "source")); EXPECT_EQ(2, Argc); } @@ -577,12 +569,8 @@ TEST(ParseFixedCompilationDatabase, HandlesPositionalArgs) { Database->getCompileCommands("source"); ASSERT_EQ(1ul, Result.size()); ASSERT_EQ(".", Result[0].Directory); - std::vector<std::string> Expected; - Expected.push_back("clang-tool"); - Expected.push_back("-c"); - Expected.push_back("-DDEF3"); - Expected.push_back("source"); - ASSERT_EQ(Expected, Result[0].CommandLine); + ASSERT_THAT(Result[0].CommandLine, + ElementsAre(EndsWith("clang-tool"), "-c", "-DDEF3", "source")); EXPECT_EQ(2, Argc); } @@ -599,12 +587,9 @@ TEST(ParseFixedCompilationDatabase, HandlesPositionalArgsSyntaxOnly) { std::vector<CompileCommand> Result = Database->getCompileCommands("source"); ASSERT_EQ(1ul, Result.size()); ASSERT_EQ(".", Result[0].Directory); - std::vector<std::string> Expected; - Expected.push_back("clang-tool"); - Expected.push_back("-fsyntax-only"); - Expected.push_back("-DDEF3"); - Expected.push_back("source"); - ASSERT_EQ(Expected, Result[0].CommandLine); + ASSERT_THAT( + Result[0].CommandLine, + ElementsAre(EndsWith("clang-tool"), "-fsyntax-only", "-DDEF3", "source")); } TEST(ParseFixedCompilationDatabase, HandlesArgv0) { @@ -620,9 +605,8 @@ TEST(ParseFixedCompilationDatabase, HandlesArgv0) { ASSERT_EQ(1ul, Result.size()); ASSERT_EQ(".", Result[0].Directory); std::vector<std::string> Expected; - Expected.push_back("clang-tool"); - Expected.push_back("source"); - ASSERT_EQ(Expected, Result[0].CommandLine); + ASSERT_THAT(Result[0].CommandLine, + ElementsAre(EndsWith("clang-tool"), "source")); EXPECT_EQ(2, Argc); } @@ -648,14 +632,17 @@ class InterpolateTest : public ::testing::Test { protected: // Adds an entry to the underlying compilation database. // A flag is injected: -D <File>, so the command used can be identified. - void add(llvm::StringRef File, llvm::StringRef Flags = "") { - llvm::SmallVector<StringRef, 8> Argv = {"clang", File, "-D", File}; + void add(StringRef File, StringRef Clang, StringRef Flags) { + SmallVector<StringRef, 8> Argv = {Clang, File, "-D", File}; llvm::SplitString(Flags, Argv); - llvm::SmallString<32> Dir; + + SmallString<32> Dir; llvm::sys::path::system_temp_directory(false, Dir); + Entries[path(File)].push_back( {Dir, path(File), {Argv.begin(), Argv.end()}, "foo.o"}); } + void add(StringRef File, StringRef Flags = "") { add(File, "clang", Flags); } // Turn a unix path fragment (foo/bar.h) into a native path (C:\tmp\foo\bar.h) std::string path(llvm::SmallString<32> File) { @@ -707,6 +694,7 @@ TEST_F(InterpolateTest, Nearby) { TEST_F(InterpolateTest, Language) { add("dir/foo.cpp", "-std=c++17"); + add("dir/bar.c", ""); add("dir/baz.cee", "-x c"); // .h is ambiguous, so we add explicit language flags @@ -716,9 +704,11 @@ TEST_F(InterpolateTest, Language) { EXPECT_EQ(getCommand("foo.hpp"), "clang -D dir/foo.cpp -std=c++17"); // respect -x if it's already there. EXPECT_EQ(getCommand("baz.h"), "clang -D dir/baz.cee -x c-header"); - // prefer a worse match with the right language - EXPECT_EQ(getCommand("foo.c"), "clang -D dir/baz.cee"); - Entries.erase(path(StringRef("dir/baz.cee"))); + // prefer a worse match with the right extension. + EXPECT_EQ(getCommand("foo.c"), "clang -D dir/bar.c"); + // make sure we don't crash on queries with invalid extensions. + EXPECT_EQ(getCommand("foo.cce"), "clang -D dir/foo.cpp"); + Entries.erase(path(StringRef("dir/bar.c"))); // Now we transfer across languages, so drop -std too. EXPECT_EQ(getCommand("foo.c"), "clang -D dir/foo.cpp"); } @@ -736,6 +726,30 @@ TEST_F(InterpolateTest, Case) { EXPECT_EQ(getCommand("foo/bar/baz/shout.C"), "clang -D FOO/BAR/BAZ/SHOUT.cc"); } +TEST_F(InterpolateTest, Aliasing) { + add("foo.cpp", "-faligned-new"); + + // The interpolated command should keep the given flag as written, even though + // the flag is internally represented as an alias. + EXPECT_EQ(getCommand("foo.hpp"), "clang -D foo.cpp -faligned-new"); +} + +TEST_F(InterpolateTest, ClangCL) { + add("foo.cpp", "clang-cl", "/W4"); + + // Language flags should be added with CL syntax. + EXPECT_EQ(getCommand("foo.h"), "clang-cl -D foo.cpp /W4 /TP"); +} + +TEST_F(InterpolateTest, DriverModes) { + add("foo.cpp", "clang-cl", "--driver-mode=gcc"); + add("bar.cpp", "clang", "--driver-mode=cl"); + + // --driver-mode overrides should be respected. + EXPECT_EQ(getCommand("foo.h"), "clang-cl -D foo.cpp --driver-mode=gcc -x c++-header"); + EXPECT_EQ(getCommand("bar.h"), "clang -D bar.cpp --driver-mode=cl /TP"); +} + TEST(CompileCommandTest, EqualityOperator) { CompileCommand CCRef("/foo/bar", "hello.c", {"a", "b"}, "hello.o"); CompileCommand CCTest = CCRef; |