Skip to content

Commit 0c05e2e

Browse files
committedJul 14, 2016
[include-fixer] Correct an incorrecst judgement about prefix scoped qualifiers.
Summary: 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. Reviewers: bkramer Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D22343 llvm-svn: 275386
1 parent b030411 commit 0c05e2e

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed
 

‎clang-tools-extra/include-fixer/IncludeFixerContext.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ std::string createQualifiedNameForReplacement(
4242
}
4343
// Append the missing stripped qualifiers.
4444
std::string FullyQualifiedName = QualifiedName + StrippedQualifiers;
45-
auto pos = FullyQualifiedName.find(SymbolScopedQualifiers);
46-
return FullyQualifiedName.substr(
47-
pos == std::string::npos ? 0 : SymbolScopedQualifiers.size());
45+
46+
// Skips symbol scoped qualifiers prefix.
47+
if (llvm::StringRef(FullyQualifiedName).startswith(SymbolScopedQualifiers))
48+
return FullyQualifiedName.substr(SymbolScopedQualifiers.size());
49+
50+
return FullyQualifiedName;
4851
}
4952

5053
} // anonymous namespace

‎clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ static std::string runIncludeFixer(
6363
SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar.h\"", 1,
6464
{{SymbolInfo::ContextType::Namespace, "b"},
6565
{SymbolInfo::ContextType::Namespace, "a"}}),
66+
SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar2.h\"", 1,
67+
{{SymbolInfo::ContextType::Namespace, "c"},
68+
{SymbolInfo::ContextType::Namespace, "a"}}),
6669
SymbolInfo("Green", SymbolInfo::SymbolKind::Class, "\"color.h\"", 1,
6770
{{SymbolInfo::ContextType::EnumDecl, "Color"},
6871
{SymbolInfo::ContextType::Namespace, "b"},
@@ -237,11 +240,15 @@ TEST(IncludeFixer, FixNamespaceQualifiers) {
237240
runIncludeFixer("namespace a {\nnamespace b{\nbar b;\n}\n}\n"));
238241
EXPECT_EQ("c::b::bar b;\n",
239242
runIncludeFixer("c::b::bar b;\n"));
240-
EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar b;\n}\n",
243+
EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar b;\n}\n",
244+
runIncludeFixer("namespace d {\nbar b;\n}\n"));
245+
EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar b;\n}\n",
241246
runIncludeFixer("namespace c {\nbar b;\n}\n"));
242247

243248
// Test nested classes.
244-
EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar::t b;\n}\n",
249+
EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar::t b;\n}\n",
250+
runIncludeFixer("namespace d {\nbar::t b;\n}\n"));
251+
EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar::t b;\n}\n",
245252
runIncludeFixer("namespace c {\nbar::t b;\n}\n"));
246253
EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar::t b;\n}\n",
247254
runIncludeFixer("namespace a {\nbar::t b;\n}\n"));

0 commit comments

Comments
 (0)