[PATCH] D61633: [Tooling] Add -x flags when inferring compile commands for files with no/invalid extension.

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 7 07:33:45 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rC360153: [Tooling] Add -x flags when inferring compile commands for files with… (authored by sammccall, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D61633?vs=198413&id=198469#toc

Repository:
  rC Clang

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

https://reviews.llvm.org/D61633

Files:
  lib/Tooling/InterpolatingCompilationDatabase.cpp
  unittests/Tooling/CompilationDatabaseTest.cpp


Index: lib/Tooling/InterpolatingCompilationDatabase.cpp
===================================================================
--- lib/Tooling/InterpolatingCompilationDatabase.cpp
+++ lib/Tooling/InterpolatingCompilationDatabase.cpp
@@ -205,10 +205,13 @@
     bool TypeCertain;
     auto TargetType = guessType(Filename, &TypeCertain);
     // If the filename doesn't determine the language (.h), transfer with -x.
-    if (TargetType != types::TY_INVALID && !TypeCertain && Type) {
-      TargetType = types::onlyPrecompileType(TargetType) // header?
-                       ? types::lookupHeaderTypeForSourceType(*Type)
-                       : *Type;
+    if ((!TargetType || !TypeCertain) && Type) {
+      // Use *Type, or its header variant if the file is a header.
+      // Treat no/invalid extension as header (e.g. C++ standard library).
+      TargetType =
+          (!TargetType || types::onlyPrecompileType(TargetType)) // header?
+              ? types::lookupHeaderTypeForSourceType(*Type)
+              : *Type;
       if (ClangCLMode) {
         const StringRef Flag = toCLFlag(TargetType);
         if (!Flag.empty())
Index: unittests/Tooling/CompilationDatabaseTest.cpp
===================================================================
--- unittests/Tooling/CompilationDatabaseTest.cpp
+++ unittests/Tooling/CompilationDatabaseTest.cpp
@@ -723,14 +723,17 @@
   // .h is ambiguous, so we add explicit language flags
   EXPECT_EQ(getCommand("foo.h"),
             "clang -D dir/foo.cpp -x c++-header -std=c++17");
+  // Same thing if we have no extension. (again, we treat as header).
+  EXPECT_EQ(getCommand("foo"), "clang -D dir/foo.cpp -x c++-header -std=c++17");
+  // and invalid extensions.
+  EXPECT_EQ(getCommand("foo.cce"),
+            "clang -D dir/foo.cpp -x c++-header -std=c++17");
   // and don't add -x if the inferred language is correct.
   EXPECT_EQ(getCommand("foo.hpp"), "clang -D dir/foo.cpp -std=c++17");
   // respect -x if it's already there.
   EXPECT_EQ(getCommand("baz.h"), "clang -D dir/baz.cee -x c-header");
   // prefer a worse match with the right extension.
   EXPECT_EQ(getCommand("foo.c"), "clang -D dir/bar.c");
-  // make sure we don't crash on queries with invalid extensions.
-  EXPECT_EQ(getCommand("foo.cce"), "clang -D dir/foo.cpp");
   Entries.erase(path(StringRef("dir/bar.c")));
   // Now we transfer across languages, so drop -std too.
   EXPECT_EQ(getCommand("foo.c"), "clang -D dir/foo.cpp");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61633.198469.patch
Type: text/x-patch
Size: 2461 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190507/1fc2d5b4/attachment-0001.bin>


More information about the cfe-commits mailing list