r318960 - [clang-format] Deduplicate using declarations
Krasimir Georgiev via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 24 10:00:01 PST 2017
Author: krasimir
Date: Fri Nov 24 10:00:01 2017
New Revision: 318960
URL: http://llvm.org/viewvc/llvm-project?rev=318960&view=rev
Log:
[clang-format] Deduplicate using declarations
Summary: This deduplicated equivalent using declarations within a block.
Reviewers: bkramer
Reviewed By: bkramer
Subscribers: cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D40435
Modified:
cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp
cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp
Modified: cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp?rev=318960&r1=318959&r2=318960&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp (original)
+++ cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp Fri Nov 24 10:00:01 2017
@@ -130,7 +130,27 @@ void endUsingDeclarationBlock(
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();
Modified: cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp?rev=318960&r1=318959&r2=318960&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp (original)
+++ cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp Fri Nov 24 10:00:01 2017
@@ -142,7 +142,6 @@ TEST_F(UsingDeclarationsSorterTest, Usin
TEST_F(UsingDeclarationsSorterTest, SortsStably) {
EXPECT_EQ("using a;\n"
- "using a;\n"
"using A;\n"
"using a;\n"
"using A;\n"
@@ -151,11 +150,8 @@ TEST_F(UsingDeclarationsSorterTest, Sort
"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 @@ TEST_F(UsingDeclarationsSorterTest, Sort
"/* 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
More information about the cfe-commits
mailing list