diff options
author | Krasimir Georgiev <krasimir@google.com> | 2017-11-03 14:38:07 +0000 |
---|---|---|
committer | Krasimir Georgiev <krasimir@google.com> | 2017-11-03 14:38:07 +0000 |
commit | c47fc31da10e8f068902fa8e6f231a358eff9f18 (patch) | |
tree | 41d2cc87ac7b152ae78e54194d8086a275e3ada6 /clang/lib/Format | |
parent | 68797214533e898b1e596a873304e7398d9d4a73 (diff) | |
download | bcm5719-llvm-c47fc31da10e8f068902fa8e6f231a358eff9f18.tar.gz bcm5719-llvm-c47fc31da10e8f068902fa8e6f231a358eff9f18.zip |
[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
llvm-svn: 317325
Diffstat (limited to 'clang/lib/Format')
-rw-r--r-- | clang/lib/Format/UsingDeclarationsSorter.cpp | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/clang/lib/Format/UsingDeclarationsSorter.cpp b/clang/lib/Format/UsingDeclarationsSorter.cpp index d6753b545e2..e9c535da925 100644 --- a/clang/lib/Format/UsingDeclarationsSorter.cpp +++ b/clang/lib/Format/UsingDeclarationsSorter.cpp @@ -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; |