r317325 - [clang-format] Sort using-declarations case sensitively with a special case for '_'
Krasimir Georgiev via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 3 07:38:07 PDT 2017
Author: krasimir
Date: Fri Nov 3 07:38:07 2017
New Revision: 317325
URL: http://llvm.org/viewvc/llvm-project?rev=317325&view=rev
Log:
[clang-format] Sort using-declarations case sensitively with a special case for '_'
Summary:
This makes clang-format sort using declarations case-sensitive with the
exception that '_' comes just before 'A'. This is better than the current case
insensitive version, because it groups uppercase names in the same namespace
together.
Reviewers: bkramer
Reviewed By: bkramer
Subscribers: cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D39549
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=317325&r1=317324&r2=317325&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp (original)
+++ cfe/trunk/lib/Format/UsingDeclarationsSorter.cpp Fri Nov 3 07:38:07 2017
@@ -33,8 +33,27 @@ struct UsingDeclaration {
UsingDeclaration(const AnnotatedLine *Line, const std::string &Label)
: Line(Line), Label(Label) {}
+ // Compares lexicographically with the exception that '_' is just before 'A'.
bool operator<(const UsingDeclaration &Other) const {
- return StringRef(Label).compare_lower(Other.Label) < 0;
+ size_t Size = Label.size();
+ size_t OtherSize = Other.Label.size();
+ for (size_t I = 0, E = std::min(Size, OtherSize); I < E; ++I) {
+ char Rank = rank(Label[I]);
+ char OtherRank = rank(Other.Label[I]);
+ if (Rank != OtherRank)
+ return Rank < OtherRank;
+ }
+ return Size < OtherSize;
+ }
+
+ // Returns the position of c in a lexicographic ordering with the exception
+ // that '_' is just before 'A'.
+ static char rank(char c) {
+ if (c == '_')
+ return 'A';
+ if ('A' <= c && c < '_')
+ return c + 1;
+ return c;
}
};
@@ -77,7 +96,7 @@ void endUsingDeclarationBlock(
SmallVectorImpl<UsingDeclaration> *UsingDeclarations,
const SourceManager &SourceMgr, tooling::Replacements *Fixes) {
bool BlockAffected = false;
- for (const UsingDeclaration& Declaration : *UsingDeclarations) {
+ for (const UsingDeclaration &Declaration : *UsingDeclarations) {
if (Declaration.Line->Affected) {
BlockAffected = true;
break;
Modified: cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp?rev=317325&r1=317324&r2=317325&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp (original)
+++ cfe/trunk/unittests/Format/UsingDeclarationsSorterTest.cpp Fri Nov 3 07:38:07 2017
@@ -86,19 +86,25 @@ TEST_F(UsingDeclarationsSorterTest, Swap
"using a, b;"));
}
-TEST_F(UsingDeclarationsSorterTest, SortsCaseInsensitively) {
+TEST_F(UsingDeclarationsSorterTest, SortsCaseSensitively) {
EXPECT_EQ("using A;\n"
"using a;",
sortUsingDeclarations("using A;\n"
"using a;"));
- EXPECT_EQ("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;",
+ EXPECT_EQ("using B;\n"
+ "using a;",
sortUsingDeclarations("using B;\n"
"using a;"));
+
+ // Sorts '_' right before 'A'.
+ EXPECT_EQ("using _;\n"
+ "using A;",
+ sortUsingDeclarations("using A;\n"
+ "using _;"));
EXPECT_EQ("using _;\n"
"using a;",
sortUsingDeclarations("using a;\n"
@@ -110,8 +116,8 @@ TEST_F(UsingDeclarationsSorterTest, Sort
EXPECT_EQ("using ::testing::_;\n"
"using ::testing::Aardvark;\n"
- "using ::testing::apple::Honeycrisp;\n"
"using ::testing::Xylophone;\n"
+ "using ::testing::apple::Honeycrisp;\n"
"using ::testing::zebra::Stripes;",
sortUsingDeclarations("using ::testing::Aardvark;\n"
"using ::testing::Xylophone;\n"
@@ -120,43 +126,6 @@ TEST_F(UsingDeclarationsSorterTest, Sort
"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