zturner updated this revision to Diff 67915.
zturner added a comment.

Update with changes needed to make fixed compilation database work (which also 
breaks many other tests)


https://reviews.llvm.org/D23455

Files:
  include/clang/Tooling/CompilationDatabase.h
  lib/Tooling/CommonOptionsParser.cpp
  lib/Tooling/CompilationDatabase.cpp
  lib/Tooling/JSONCompilationDatabase.cpp

Index: lib/Tooling/JSONCompilationDatabase.cpp
===================================================================
--- lib/Tooling/JSONCompilationDatabase.cpp
+++ lib/Tooling/JSONCompilationDatabase.cpp
@@ -16,7 +16,10 @@
 #include "clang/Tooling/CompilationDatabasePluginRegistry.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Allocator.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Support/StringSaver.h"
 #include <system_error>
 
 namespace clang {
@@ -113,8 +116,17 @@
 
 std::vector<std::string> unescapeCommandLine(
     StringRef EscapedCommandLine) {
+#if defined(LLVM_ON_WIN32)
+  llvm::BumpPtrAllocator Alloc;
+  llvm::StringSaver Saver(Alloc);
+  llvm::SmallVector<const char *, 64> T;
+  llvm::cl::TokenizeWindowsCommandLine(EscapedCommandLine, Saver, T);
+  std::vector<std::string> Result(T.begin(), T.end());
+  return Result;
+#else
   CommandLineArgumentParser parser(EscapedCommandLine);
   return parser.parse();
+#endif
 }
 
 class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin {
Index: lib/Tooling/CompilationDatabase.cpp
===================================================================
--- lib/Tooling/CompilationDatabase.cpp
+++ lib/Tooling/CompilationDatabase.cpp
@@ -205,25 +205,27 @@
 ///          \li true if successful.
 ///          \li false if \c Args cannot be used for compilation jobs (e.g.
 ///          contains an option like -E or -version).
-static bool stripPositionalArgs(std::vector<const char *> Args,
+static bool stripPositionalArgs(std::vector<const char *> Args, std::string &ProgName,
                                 std::vector<std::string> &Result) {
+  ProgName = "clang-tool";
+  Result.clear();
+  if (Args.empty())
+    return true;
+
   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
   UnusedInputDiagConsumer DiagClient;
   DiagnosticsEngine Diagnostics(
       IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()),
       &*DiagOpts, &DiagClient, false);
 
-  // The clang executable path isn't required since the jobs the driver builds
-  // will not be executed.
+  // Although the jobs the driver builds will not be executed, the clang
+  // executable path is still required in order to determine the correct
+  // driver mode (CL, GCC, G++, etc).
+  ProgName = Args[0];
   std::unique_ptr<driver::Driver> NewDriver(new driver::Driver(
-      /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(),
-      Diagnostics));
+      ProgName, llvm::sys::getDefaultTargetTriple(), 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");
-
   // 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
   // would have been used for linking. If the user provided a compiler name as
@@ -290,14 +292,15 @@
   Argc = DoubleDash - Argv;
 
   std::vector<std::string> StrippedArgs;
-  if (!stripPositionalArgs(CommandLine, StrippedArgs))
+  std::string ToolName;
+  if (!stripPositionalArgs(CommandLine, ToolName, StrippedArgs))
     return nullptr;
-  return new FixedCompilationDatabase(Directory, StrippedArgs);
+  return new FixedCompilationDatabase(Directory, ToolName, StrippedArgs);
 }
 
-FixedCompilationDatabase::
-FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) {
-  std::vector<std::string> ToolCommandLine(1, "clang-tool");
+FixedCompilationDatabase::FixedCompilationDatabase(
+    Twine Directory, StringRef ProgName, ArrayRef<std::string> CommandLine) {
+  std::vector<std::string> ToolCommandLine(1, ProgName);
   ToolCommandLine.insert(ToolCommandLine.end(),
                          CommandLine.begin(), CommandLine.end());
   CompileCommands.emplace_back(Directory, StringRef(),
Index: lib/Tooling/CommonOptionsParser.cpp
===================================================================
--- lib/Tooling/CommonOptionsParser.cpp
+++ lib/Tooling/CommonOptionsParser.cpp
@@ -136,8 +136,8 @@
     if (!Compilations) {
       llvm::errs() << "Error while trying to load a compilation database:\n"
                    << ErrorMessage << "Running without flags.\n";
-      Compilations.reset(
-          new FixedCompilationDatabase(".", std::vector<std::string>()));
+      Compilations.reset(new FixedCompilationDatabase(
+          ".", "clang-tool", std::vector<std::string>()));
     }
   }
   auto AdjustingCompilations =
Index: include/clang/Tooling/CompilationDatabase.h
===================================================================
--- include/clang/Tooling/CompilationDatabase.h
+++ include/clang/Tooling/CompilationDatabase.h
@@ -189,7 +189,8 @@
 
   /// \brief Constructs a compilation data base from a specified directory
   /// and command line.
-  FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine);
+  FixedCompilationDatabase(Twine Directory, StringRef ProgName,
+                           ArrayRef<std::string> CommandLine);
 
   /// \brief Returns the given compile command.
   ///
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to