hokein created this revision.
hokein added a reviewer: bkramer.
hokein added a subscriber: cfe-commits.

The judgement that checks whether the fully-qualified name has scoped qualifiers
prefix is incorrect. Should always check whether the first matched postion is 
the
beginning position.

http://reviews.llvm.org/D22343

Files:
  include-fixer/IncludeFixerContext.cpp
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===================================================================
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -63,6 +63,9 @@
       SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar.h\"", 1,
                  {{SymbolInfo::ContextType::Namespace, "b"},
                   {SymbolInfo::ContextType::Namespace, "a"}}),
+      SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar2.h\"", 1,
+                 {{SymbolInfo::ContextType::Namespace, "c"},
+                  {SymbolInfo::ContextType::Namespace, "a"}}),
       SymbolInfo("Green", SymbolInfo::SymbolKind::Class, "\"color.h\"", 1,
                  {{SymbolInfo::ContextType::EnumDecl, "Color"},
                   {SymbolInfo::ContextType::Namespace, "b"},
@@ -237,11 +240,15 @@
             runIncludeFixer("namespace a {\nnamespace b{\nbar b;\n}\n}\n"));
   EXPECT_EQ("c::b::bar b;\n",
             runIncludeFixer("c::b::bar b;\n"));
-  EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar b;\n}\n",
+  EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar b;\n}\n",
+            runIncludeFixer("namespace d {\nbar b;\n}\n"));
+  EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar b;\n}\n",
             runIncludeFixer("namespace c {\nbar b;\n}\n"));
 
   // Test nested classes.
-  EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar::t b;\n}\n",
+  EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar::t b;\n}\n",
+            runIncludeFixer("namespace d {\nbar::t b;\n}\n"));
+  EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar::t b;\n}\n",
             runIncludeFixer("namespace c {\nbar::t b;\n}\n"));
   EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar::t b;\n}\n",
             runIncludeFixer("namespace a {\nbar::t b;\n}\n"));
Index: include-fixer/IncludeFixerContext.cpp
===================================================================
--- include-fixer/IncludeFixerContext.cpp
+++ include-fixer/IncludeFixerContext.cpp
@@ -42,9 +42,12 @@
   }
   // Append the missing stripped qualifiers.
   std::string FullyQualifiedName = QualifiedName + StrippedQualifiers;
-  auto pos = FullyQualifiedName.find(SymbolScopedQualifiers);
-  return FullyQualifiedName.substr(
-      pos == std::string::npos ? 0 : SymbolScopedQualifiers.size());
+  bool HasScopedQualifiersPrefix =
+      FullyQualifiedName.find(SymbolScopedQualifiers) == 0;
+  if (!HasScopedQualifiersPrefix)
+    return FullyQualifiedName;
+  // Skips symbol scoped qualifiers.
+  return FullyQualifiedName.substr(SymbolScopedQualifiers.size());
 }
 
 } // anonymous namespace


Index: unittests/include-fixer/IncludeFixerTest.cpp
===================================================================
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -63,6 +63,9 @@
       SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar.h\"", 1,
                  {{SymbolInfo::ContextType::Namespace, "b"},
                   {SymbolInfo::ContextType::Namespace, "a"}}),
+      SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar2.h\"", 1,
+                 {{SymbolInfo::ContextType::Namespace, "c"},
+                  {SymbolInfo::ContextType::Namespace, "a"}}),
       SymbolInfo("Green", SymbolInfo::SymbolKind::Class, "\"color.h\"", 1,
                  {{SymbolInfo::ContextType::EnumDecl, "Color"},
                   {SymbolInfo::ContextType::Namespace, "b"},
@@ -237,11 +240,15 @@
             runIncludeFixer("namespace a {\nnamespace b{\nbar b;\n}\n}\n"));
   EXPECT_EQ("c::b::bar b;\n",
             runIncludeFixer("c::b::bar b;\n"));
-  EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar b;\n}\n",
+  EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar b;\n}\n",
+            runIncludeFixer("namespace d {\nbar b;\n}\n"));
+  EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar b;\n}\n",
             runIncludeFixer("namespace c {\nbar b;\n}\n"));
 
   // Test nested classes.
-  EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar::t b;\n}\n",
+  EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar::t b;\n}\n",
+            runIncludeFixer("namespace d {\nbar::t b;\n}\n"));
+  EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar::t b;\n}\n",
             runIncludeFixer("namespace c {\nbar::t b;\n}\n"));
   EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar::t b;\n}\n",
             runIncludeFixer("namespace a {\nbar::t b;\n}\n"));
Index: include-fixer/IncludeFixerContext.cpp
===================================================================
--- include-fixer/IncludeFixerContext.cpp
+++ include-fixer/IncludeFixerContext.cpp
@@ -42,9 +42,12 @@
   }
   // Append the missing stripped qualifiers.
   std::string FullyQualifiedName = QualifiedName + StrippedQualifiers;
-  auto pos = FullyQualifiedName.find(SymbolScopedQualifiers);
-  return FullyQualifiedName.substr(
-      pos == std::string::npos ? 0 : SymbolScopedQualifiers.size());
+  bool HasScopedQualifiersPrefix =
+      FullyQualifiedName.find(SymbolScopedQualifiers) == 0;
+  if (!HasScopedQualifiersPrefix)
+    return FullyQualifiedName;
+  // Skips symbol scoped qualifiers.
+  return FullyQualifiedName.substr(SymbolScopedQualifiers.size());
 }
 
 } // anonymous namespace
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to