[clang-tools-extra] r269758 - [include-fixer] Make the "extend to the right" hack support typos without nested names in the front.

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Tue May 17 05:35:19 PDT 2016


Author: d0k
Date: Tue May 17 07:35:18 2016
New Revision: 269758

URL: http://llvm.org/viewvc/llvm-project?rev=269758&view=rev
Log:
[include-fixer] Make the "extend to the right" hack support typos without nested names in the front.

This handles cases where the initial namespace is unknown.

Modified:
    clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
    clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp

Modified: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp?rev=269758&r1=269757&r2=269758&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp Tue May 17 07:35:18 2016
@@ -129,11 +129,7 @@ public:
       }
     }
 
-    /// If we have a scope specification, use that to get more precise results.
-    std::string QueryString;
-    if (SS && SS->getRange().isValid()) {
-      auto Range = CharSourceRange::getTokenRange(SS->getRange().getBegin(),
-                                                  Typo.getLoc());
+    auto ExtendNestedNameSpecifier = [this](CharSourceRange Range) {
       StringRef Source =
           Lexer::getSourceText(Range, getCompilerInstance().getSourceManager(),
                                getCompilerInstance().getLangOpts());
@@ -158,7 +154,21 @@ public:
       while (isIdentifierBody(*End) || *End == ':')
         ++End;
 
-      QueryString = std::string(Source.begin(), End);
+      return std::string(Source.begin(), End);
+    };
+
+    /// If we have a scope specification, use that to get more precise results.
+    std::string QueryString;
+    if (SS && SS->getRange().isValid()) {
+      auto Range = CharSourceRange::getTokenRange(SS->getRange().getBegin(),
+                                                  Typo.getLoc());
+
+      QueryString = ExtendNestedNameSpecifier(Range);
+    } else if (Typo.getName().isIdentifier() && !Typo.getLoc().isMacroID()) {
+      auto Range =
+          CharSourceRange::getTokenRange(Typo.getBeginLoc(), Typo.getEndLoc());
+
+      QueryString = ExtendNestedNameSpecifier(Range);
     } else {
       QueryString = Typo.getAsString();
     }

Modified: clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp?rev=269758&r1=269757&r2=269758&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp Tue May 17 07:35:18 2016
@@ -126,10 +126,20 @@ TEST(IncludeFixer, MinimizeInclude) {
             runIncludeFixer("a::b::foo bar;\n", IncludePath));
 }
 
-#if 0
+#ifndef _WIN32
 // It doesn't pass for targeting win32. Investigating.
 TEST(IncludeFixer, NestedName) {
   EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n"
+            "int x = a::b::foo(0);\n",
+            runIncludeFixer("int x = a::b::foo(0);\n"));
+
+  // FIXME: Handle simple macros.
+  EXPECT_EQ("#define FOO a::b::foo\nint x = FOO;\n",
+            runIncludeFixer("#define FOO a::b::foo\nint x = FOO;\n"));
+  EXPECT_EQ("#define FOO(x) a::##x\nint x = FOO(b::foo);\n",
+            runIncludeFixer("#define FOO(x) a::##x\nint x = FOO(b::foo);\n"));
+
+  EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n"
             "namespace a {}\nint a = a::b::foo(0);\n",
             runIncludeFixer("namespace a {}\nint a = a::b::foo(0);\n"));
 }




More information about the cfe-commits mailing list