]> granicus.if.org Git - clang/commitdiff
[Tooling] Parse compilation database command lines on Windows.
authorZachary Turner <zturner@google.com>
Wed, 17 Aug 2016 20:04:35 +0000 (20:04 +0000)
committerZachary Turner <zturner@google.com>
Wed, 17 Aug 2016 20:04:35 +0000 (20:04 +0000)
When a compilation database is used on Windows, the command lines cannot
be parsed using the standard GNU style syntax. LLVM provides functions for
parsing Windows style command lines, so use them where appropriate.

After this patch, clang-tidy runs correctly on Windows.

Reviewed by: alexfh
Differential Revision: https://reviews.llvm.org/D23455

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@278964 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Tooling/JSONCompilationDatabase.cpp

index 299fbdc149bfb2fefe538ee64c9062b3913d282b..5f192f793a499a8c258ff4d331a51fd284013147 100644 (file)
 #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,6 +116,21 @@ class CommandLineArgumentParser {
 
 std::vector<std::string> unescapeCommandLine(
     StringRef EscapedCommandLine) {
+  llvm::Triple Triple(llvm::sys::getProcessTriple());
+  if (Triple.getOS() == llvm::Triple::OSType::Win32) {
+    // Assume Windows command line parsing on Win32 unless the triple explicitly
+    // tells us otherwise.
+    if (!Triple.hasEnvironment() ||
+        Triple.getEnvironment() == llvm::Triple::EnvironmentType::MSVC) {
+      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;
+    }
+  }
+
   CommandLineArgumentParser parser(EscapedCommandLine);
   return parser.parse();
 }