Index: lib/Format/UsingDeclarationsSorter.cpp =================================================================== --- lib/Format/UsingDeclarationsSorter.cpp +++ lib/Format/UsingDeclarationsSorter.cpp @@ -33,8 +33,27 @@ 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; + return computeSortLabel(Label) < computeSortLabel(Other.Label); + } + + // 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; + } + + static std::string computeSortLabel(const std::string &Label) { + std::string SortLabel; + SortLabel.reserve(Label.size()); + for (char C : Label) + SortLabel += rank(C); + return SortLabel; } }; @@ -77,7 +96,7 @@ SmallVectorImpl *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; Index: unittests/Format/UsingDeclarationsSorterTest.cpp =================================================================== --- unittests/Format/UsingDeclarationsSorterTest.cpp +++ unittests/Format/UsingDeclarationsSorterTest.cpp @@ -86,19 +86,25 @@ "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 @@ 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 @@ "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"