[PATCH] D56680: [Tooling] Make clang-tool find libc++ dir on mac when running on a file without compilation database.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 15 11:09:43 PST 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rC351222: [Tooling] Make clang-tool find libc++ dir on mac when running on a file without… (authored by hokein, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D56680?vs=181637&id=181827#toc

Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D56680/new/

https://reviews.llvm.org/D56680

Files:
  lib/Tooling/CompilationDatabase.cpp
  test/Tooling/clang-check-mac-libcxx-fixed-compilation-db.cpp


Index: test/Tooling/clang-check-mac-libcxx-fixed-compilation-db.cpp
===================================================================
--- test/Tooling/clang-check-mac-libcxx-fixed-compilation-db.cpp
+++ 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 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 <mock_vector>
+vector v;
Index: lib/Tooling/CompilationDatabase.cpp
===================================================================
--- lib/Tooling/CompilationDatabase.cpp
+++ lib/Tooling/CompilationDatabase.cpp
@@ -227,6 +227,16 @@
   }
 };
 
+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,9 @@
       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.
+  Args.insert(Args.begin(), GetClangToolCommand().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 +376,7 @@
 
 FixedCompilationDatabase::
 FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) {
-  std::vector<std::string> ToolCommandLine(1, "clang-tool");
+  std::vector<std::string> ToolCommandLine(1, GetClangToolCommand());
   ToolCommandLine.insert(ToolCommandLine.end(),
                          CommandLine.begin(), CommandLine.end());
   CompileCommands.emplace_back(Directory, StringRef(),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56680.181827.patch
Type: text/x-patch
Size: 2519 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190115/a6199cea/attachment.bin>


More information about the cfe-commits mailing list