[PATCH] D40435: [clang-format] Deduplicate using declarations
Krasimir Georgiev via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 24 08:47:28 PST 2017
krasimir updated this revision to Diff 124215.
krasimir added a comment.
- Address review comments
https://reviews.llvm.org/D40435
Files:
lib/Format/UsingDeclarationsSorter.cpp
unittests/Format/UsingDeclarationsSorterTest.cpp
Index: unittests/Format/UsingDeclarationsSorterTest.cpp
===================================================================
--- unittests/Format/UsingDeclarationsSorterTest.cpp
+++ unittests/Format/UsingDeclarationsSorterTest.cpp
@@ -142,20 +142,16 @@
TEST_F(UsingDeclarationsSorterTest, SortsStably) {
EXPECT_EQ("using a;\n"
- "using a;\n"
"using A;\n"
"using a;\n"
"using A;\n"
"using a;\n"
"using A;\n"
"using a;\n"
"using B;\n"
"using b;\n"
- "using b;\n"
"using B;\n"
"using b;\n"
- "using b;\n"
- "using b;\n"
"using B;\n"
"using b;",
sortUsingDeclarations("using a;\n"
@@ -355,6 +351,25 @@
"/* comment */ using a;"));
}
+TEST_F(UsingDeclarationsSorterTest, DeduplicatesUsingDeclarations) {
+ EXPECT_EQ("using a;\n"
+ "using b;\n"
+ "using c;\n"
+ "\n"
+ "using a;\n"
+ "using e;",
+ sortUsingDeclarations("using c;\n"
+ "using a;\n"
+ "using b;\n"
+ "using a;\n"
+ "using b;\n"
+ "\n"
+ "using e;\n"
+ "using a;\n"
+ "using e;"));
+
+}
+
} // end namespace
} // end namespace format
} // end namespace clang
Index: lib/Format/UsingDeclarationsSorter.cpp
===================================================================
--- lib/Format/UsingDeclarationsSorter.cpp
+++ lib/Format/UsingDeclarationsSorter.cpp
@@ -130,7 +130,27 @@
UsingDeclarations->begin(), UsingDeclarations->end());
std::stable_sort(SortedUsingDeclarations.begin(),
SortedUsingDeclarations.end());
+ SortedUsingDeclarations.erase(
+ std::unique(SortedUsingDeclarations.begin(),
+ SortedUsingDeclarations.end(),
+ [](const UsingDeclaration &a, const UsingDeclaration &b) {
+ return a.Label == b.Label;
+ }),
+ SortedUsingDeclarations.end());
for (size_t I = 0, E = UsingDeclarations->size(); I < E; ++I) {
+ if (I >= SortedUsingDeclarations.size()) {
+ // This using declaration has been deduplicated, delete it.
+ auto Begin =
+ (*UsingDeclarations)[I].Line->First->WhitespaceRange.getBegin();
+ auto End = (*UsingDeclarations)[I].Line->Last->Tok.getEndLoc();
+ auto Range = CharSourceRange::getCharRange(Begin, End);
+ auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, ""));
+ if (Err) {
+ llvm::errs() << "Error while sorting using declarations: "
+ << llvm::toString(std::move(Err)) << "\n";
+ }
+ continue;
+ }
if ((*UsingDeclarations)[I].Line == SortedUsingDeclarations[I].Line)
continue;
auto Begin = (*UsingDeclarations)[I].Line->First->Tok.getLocation();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40435.124215.patch
Type: text/x-patch
Size: 3153 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171124/dd15271f/attachment.bin>
More information about the cfe-commits
mailing list