[PATCH] D64489: [clangd][QueryDriver] Use language from underlying database if possible

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 10 07:04:14 PDT 2019


kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added subscribers: cfe-commits, arphaman, jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64489

Files:
  clang-tools-extra/clangd/QueryDriverDatabase.cpp


Index: clang-tools-extra/clangd/QueryDriverDatabase.cpp
===================================================================
--- clang-tools-extra/clangd/QueryDriverDatabase.cpp
+++ clang-tools-extra/clangd/QueryDriverDatabase.cpp
@@ -83,11 +83,11 @@
 }
 
 std::vector<std::string> extractSystemIncludes(PathRef Driver,
-                                               llvm::StringRef Ext,
+                                               llvm::StringRef Lang,
                                                llvm::Regex &QueryDriverRegex) {
   trace::Span Tracer("Extract system includes");
   SPAN_ATTACH(Tracer, "driver", Driver);
-  SPAN_ATTACH(Tracer, "ext", Ext);
+  SPAN_ATTACH(Tracer, "lang", Lang);
 
   if (!QueryDriverRegex.match(Driver)) {
     vlog("System include extraction: not whitelisted driver {0}", Driver);
@@ -117,14 +117,8 @@
   llvm::Optional<llvm::StringRef> Redirects[] = {
       {""}, {""}, llvm::StringRef(StdErrPath)};
 
-  auto Type = driver::types::lookupTypeForExtension(Ext);
-  if (Type == driver::types::TY_INVALID) {
-    elog("System include extraction: invalid file type for {0}", Ext);
-    return {};
-  }
   // Should we also preserve flags like "-sysroot", "-nostdinc" ?
-  const llvm::StringRef Args[] = {
-      Driver, "-E", "-x", driver::types::getTypeName(Type), "-", "-v"};
+  const llvm::StringRef Args[] = {Driver, "-E", "-x", Lang, "-", "-v"};
 
   if (int RC = llvm::sys::ExecuteAndWait(Driver, Args, /*Env=*/llvm::None,
                                          Redirects)) {
@@ -220,10 +214,27 @@
     if (!Cmd || Cmd->CommandLine.empty())
       return Cmd;
 
+    llvm::StringRef Lang;
+    for (size_t I = 0, E = Cmd->CommandLine.size(); I < E; ++I) {
+      llvm::StringRef Arg = Cmd->CommandLine[I];
+      if (Arg == "-x" && I + 1 < E)
+        Lang = Cmd->CommandLine[I + 1];
+      else if (Arg.startswith("-x"))
+        Lang = Arg.drop_front(2).trim();
+    }
+    if (Lang.empty()) {
+      llvm::StringRef Ext = llvm::sys::path::extension(File).trim('.');
+      auto Type = driver::types::lookupTypeForExtension(Ext);
+      if (Type == driver::types::TY_INVALID) {
+        elog("System include extraction: invalid file type for {0}", Ext);
+        return {};
+      }
+      Lang = driver::types::getTypeName(Type);
+    }
+
     llvm::SmallString<128> Driver(Cmd->CommandLine.front());
     llvm::sys::fs::make_absolute(Cmd->Directory, Driver);
-    llvm::StringRef Ext = llvm::sys::path::extension(File).trim('.');
-    auto Key = std::make_pair(Driver.str(), Ext);
+    auto Key = std::make_pair(Driver.str(), Lang);
 
     std::vector<std::string> SystemIncludes;
     {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64489.208949.patch
Type: text/x-patch
Size: 2643 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190710/21ac360c/attachment-0001.bin>


More information about the cfe-commits mailing list