r313963 - [clang-format] Ignore case and stable sort using-declarations
Krasimir Georgiev via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 21 21:48:17 PDT 2017
Author: krasimir
Date: Thu Sep 21 21:48:17 2017
New Revision: 313963
URL: http://llvm.org/viewvc/llvm-project?rev=313963&view=rev
Log:
[clang-format] Ignore case and stable sort using-declarations
Summary:
This ignores case while sorting using-declarations, fixing a case where `_` would appear between lowercase and uppercase characters.
It also applies stable sort, so that replacements for the exact same using declarations are not generated.
Reviewers: klimek, alexfh
Reviewed By: alexfh
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D37263
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=313963&r1=313962&r2=313963&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp (original)
+++ cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp Thu Sep 21 21:48:17 2017
@@ -34,7 +34,7 @@ struct UsingDeclaration {
: Line(Line), Label(Label) {}
bool operator<(const UsingDeclaration &Other) const {
- return Label < Other.Label;
+ return StringRef(Label).compare_lower(Other.Label) < 0;
}
};
@@ -78,7 +78,8 @@ void endUsingDeclarationBlock(
const SourceManager &SourceMgr, tooling::Replacements *Fixes) {
SmallVector<UsingDeclaration, 4> SortedUsingDeclarations(
UsingDeclarations->begin(), UsingDeclarations->end());
- std::sort(SortedUsingDeclarations.begin(), SortedUsingDeclarations.end());
+ std::stable_sort(SortedUsingDeclarations.begin(),
+ SortedUsingDeclarations.end());
for (size_t I = 0, E = UsingDeclarations->size(); I < E; ++I) {
if ((*UsingDeclarations)[I].Line == SortedUsingDeclarations[I].Line)
continue;
Modified: cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp?rev=313963&r1=313962&r2=313963&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp (original)
+++ cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp Thu Sep 21 21:48:17 2017
@@ -86,6 +86,77 @@ TEST_F(UsingDeclarationsSorterTest, Swap
"using a, b;"));
}
+TEST_F(UsingDeclarationsSorterTest, SortsCaseInsensitively) {
+ EXPECT_EQ("using A;\n"
+ "using a;",
+ sortUsingDeclarations("using A;\n"
+ "using a;"));
+ EXPECT_EQ("using a;\n"
+ "using A;",
+ sortUsingDeclarations("using a;\n"
+ "using A;"));
+ EXPECT_EQ("using a;\n"
+ "using B;",
+ sortUsingDeclarations("using B;\n"
+ "using a;"));
+ EXPECT_EQ("using _;\n"
+ "using a;",
+ sortUsingDeclarations("using a;\n"
+ "using _;"));
+ EXPECT_EQ("using a::_;\n"
+ "using a::a;",
+ sortUsingDeclarations("using a::a;\n"
+ "using a::_;"));
+
+ EXPECT_EQ("using ::testing::_;\n"
+ "using ::testing::Aardvark;\n"
+ "using ::testing::apple::Honeycrisp;\n"
+ "using ::testing::Xylophone;\n"
+ "using ::testing::zebra::Stripes;",
+ sortUsingDeclarations("using ::testing::Aardvark;\n"
+ "using ::testing::Xylophone;\n"
+ "using ::testing::_;\n"
+ "using ::testing::apple::Honeycrisp;\n"
+ "using ::testing::zebra::Stripes;"));
+}
+
+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"
+ "using B;\n"
+ "using a;\n"
+ "using b;\n"
+ "using A;\n"
+ "using a;\n"
+ "using b;\n"
+ "using B;\n"
+ "using b;\n"
+ "using A;\n"
+ "using a;\n"
+ "using b;\n"
+ "using b;\n"
+ "using B;\n"
+ "using b;\n"
+ "using A;\n"
+ "using a;"));
+}
+
TEST_F(UsingDeclarationsSorterTest, SortsMultipleTopLevelDeclarations) {
EXPECT_EQ("using a;\n"
"using b;\n"
More information about the cfe-commits
mailing list