From f2e25e708c49fa79a2dbe8b68f3ce4873a489739 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Wed, 16 Jan 2019 09:41:26 +0000 Subject: Reapply [Tooling] Make clang-tool find libc++ dir on mac when running on a file without compilation database. This reverts commit r351282, and re-lands r351222 and r351229 with the use-after-free fixed. llvm-svn: 351316 --- clang/lib/Tooling/CompilationDatabase.cpp | 19 ++++++-- ...clang-check-mac-libcxx-fixed-compilation-db.cpp | 16 +++++++ .../unittests/Tooling/CompilationDatabaseTest.cpp | 52 ++++++++-------------- 3 files changed, 49 insertions(+), 38 deletions(-) create mode 100644 clang/test/Tooling/clang-check-mac-libcxx-fixed-compilation-db.cpp (limited to 'clang') diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp index 246d3c0a78f1..cce8e1f1df24 100644 --- a/clang/lib/Tooling/CompilationDatabase.cpp +++ b/clang/lib/Tooling/CompilationDatabase.cpp @@ -227,6 +227,16 @@ struct FilterUnusedFlags { } }; +std::string GetClangToolCommand() { + static int Dummy; + std::string ClangExecutable = + llvm::sys::fs::getMainExecutable("clang", (void *)&Dummy); + SmallString<128> ClangToolPath; + ClangToolPath = llvm::sys::path::parent_path(ClangExecutable); + llvm::sys::path::append(ClangToolPath, "clang-tool"); + return ClangToolPath.str(); +} + } // namespace /// Strips any positional args and possible argv[0] from a command-line @@ -266,9 +276,10 @@ static bool stripPositionalArgs(std::vector Args, Diagnostics)); NewDriver->setCheckInputsExist(false); - // This becomes the new argv[0]. The value is actually not important as it - // isn't used for invoking Tools. - Args.insert(Args.begin(), "clang-tool"); + // This becomes the new argv[0]. The value is used to detect libc++ include + // dirs on Mac, it isn't used for other platforms. + std::string Argv0 = GetClangToolCommand(); + Args.insert(Args.begin(), Argv0.c_str()); // By adding -c, we force the driver to treat compilation as the last phase. // It will then issue warnings via Diagnostics about un-used options that @@ -366,7 +377,7 @@ FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) { FixedCompilationDatabase:: FixedCompilationDatabase(Twine Directory, ArrayRef CommandLine) { - std::vector ToolCommandLine(1, "clang-tool"); + std::vector ToolCommandLine(1, GetClangToolCommand()); ToolCommandLine.insert(ToolCommandLine.end(), CommandLine.begin(), CommandLine.end()); CompileCommands.emplace_back(Directory, StringRef(), diff --git a/clang/test/Tooling/clang-check-mac-libcxx-fixed-compilation-db.cpp b/clang/test/Tooling/clang-check-mac-libcxx-fixed-compilation-db.cpp new file mode 100644 index 000000000000..035155c96c92 --- /dev/null +++ b/clang/test/Tooling/clang-check-mac-libcxx-fixed-compilation-db.cpp @@ -0,0 +1,16 @@ +// Clang on MacOS can find libc++ living beside the installed compiler. +// This test makes sure our libTooling-based tools emulate this properly with +// fixed compilation database. +// +// RUN: rm -rf %t +// RUN: mkdir %t +// +// Install the mock libc++ (simulates the libc++ directory structure). +// RUN: cp -r %S/Inputs/mock-libcxx %t/ +// +// RUN: cp $(which clang-check) %t/mock-libcxx/bin/ +// RUN: cp "%s" "%t/test.cpp" +// RUN: %t/mock-libcxx/bin/clang-check -p "%t" "%t/test.cpp" -- -stdlib=libc++ + +#include +vector v; diff --git a/clang/unittests/Tooling/CompilationDatabaseTest.cpp b/clang/unittests/Tooling/CompilationDatabaseTest.cpp index 125ba4896ef6..949d6a3b7392 100644 --- a/clang/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/clang/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 CommandLine; - CommandLine.push_back("one"); - CommandLine.push_back("two"); - FixedCompilationDatabase Database(".", CommandLine); + FixedCompilationDatabase Database(".", /*CommandLine*/ {"one", "two"}); StringRef FileName("source"); std::vector Result = Database.getCompileCommands(FileName); ASSERT_EQ(1ul, Result.size()); - std::vector 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 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 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 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 Result = Database->getCompileCommands("source"); ASSERT_EQ(1ul, Result.size()); ASSERT_EQ(".", Result[0].Directory); - std::vector 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 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); } -- cgit v1.2.3