From f87d7f14d48195ead20473bc44d24f9947916bd3 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Fri, 22 Sep 2017 04:48:17 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313963 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UsingDeclarationsSorter.cpp | 5 +- .../Format/UsingDeclarationsSorterTest.cpp | 71 +++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/lib/Format/UsingDeclarationsSorter.cpp b/lib/Format/UsingDeclarationsSorter.cpp index fb4f59fbc9..d4ff7c25ea 100644 --- a/lib/Format/UsingDeclarationsSorter.cpp +++ b/lib/Format/UsingDeclarationsSorter.cpp @@ -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 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; diff --git a/unittests/Format/UsingDeclarationsSorterTest.cpp b/unittests/Format/UsingDeclarationsSorterTest.cpp index 858a62c2d7..219d0302a4 100644 --- a/unittests/Format/UsingDeclarationsSorterTest.cpp +++ b/unittests/Format/UsingDeclarationsSorterTest.cpp @@ -86,6 +86,77 @@ TEST_F(UsingDeclarationsSorterTest, SwapsTwoConsecutiveUsingDeclarations) { "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" -- 2.40.0