1*0b57cec5SDimitry Andric //===--- UsingDeclarationsSorter.cpp ----------------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric ///
9*0b57cec5SDimitry Andric /// \file
10*0b57cec5SDimitry Andric /// This file implements UsingDeclarationsSorter, a TokenAnalyzer that
11*0b57cec5SDimitry Andric /// sorts consecutive using declarations.
12*0b57cec5SDimitry Andric ///
13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric #include "UsingDeclarationsSorter.h"
16*0b57cec5SDimitry Andric #include "clang/Format/Format.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
18*0b57cec5SDimitry Andric #include "llvm/Support/Regex.h"
19*0b57cec5SDimitry Andric 
20*0b57cec5SDimitry Andric #include <algorithm>
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric #define DEBUG_TYPE "using-declarations-sorter"
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric namespace clang {
25*0b57cec5SDimitry Andric namespace format {
26*0b57cec5SDimitry Andric 
27*0b57cec5SDimitry Andric namespace {
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric // The order of using declaration is defined as follows:
30*0b57cec5SDimitry Andric // Split the strings by "::" and discard any initial empty strings. The last
31*0b57cec5SDimitry Andric // element of each list is a non-namespace name; all others are namespace
32*0b57cec5SDimitry Andric // names. Sort the lists of names lexicographically, where the sort order of
33*0b57cec5SDimitry Andric // individual names is that all non-namespace names come before all namespace
34*0b57cec5SDimitry Andric // names, and within those groups, names are in case-insensitive lexicographic
35*0b57cec5SDimitry Andric // order.
compareLabelsLexicographicNumeric(StringRef A,StringRef B)36*0b57cec5SDimitry Andric int compareLabelsLexicographicNumeric(StringRef A, StringRef B) {
37*0b57cec5SDimitry Andric   SmallVector<StringRef, 2> NamesA;
38*0b57cec5SDimitry Andric   A.split(NamesA, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
39*0b57cec5SDimitry Andric   SmallVector<StringRef, 2> NamesB;
40*0b57cec5SDimitry Andric   B.split(NamesB, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
41*0b57cec5SDimitry Andric   size_t SizeA = NamesA.size();
42*0b57cec5SDimitry Andric   size_t SizeB = NamesB.size();
43*0b57cec5SDimitry Andric   for (size_t I = 0, E = std::min(SizeA, SizeB); I < E; ++I) {
44*0b57cec5SDimitry Andric     if (I + 1 == SizeA) {
45*0b57cec5SDimitry Andric       // I is the last index of NamesA and NamesA[I] is a non-namespace name.
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric       // Non-namespace names come before all namespace names.
48*0b57cec5SDimitry Andric       if (SizeB > SizeA)
49*0b57cec5SDimitry Andric         return -1;
50*0b57cec5SDimitry Andric 
51*0b57cec5SDimitry Andric       // Two names within a group compare case-insensitively.
52*0b57cec5SDimitry Andric       return NamesA[I].compare_insensitive(NamesB[I]);
53*0b57cec5SDimitry Andric     }
54*0b57cec5SDimitry Andric 
55*0b57cec5SDimitry Andric     // I is the last index of NamesB and NamesB[I] is a non-namespace name.
56*0b57cec5SDimitry Andric     // Non-namespace names come before all namespace names.
57*0b57cec5SDimitry Andric     if (I + 1 == SizeB)
58*0b57cec5SDimitry Andric       return 1;
59*0b57cec5SDimitry Andric 
60*0b57cec5SDimitry Andric     // Two namespaces names within a group compare case-insensitively.
61*0b57cec5SDimitry Andric     int C = NamesA[I].compare_insensitive(NamesB[I]);
62*0b57cec5SDimitry Andric     if (C != 0)
63*0b57cec5SDimitry Andric       return C;
64*0b57cec5SDimitry Andric   }
65*0b57cec5SDimitry Andric   return 0;
66*0b57cec5SDimitry Andric }
67*0b57cec5SDimitry Andric 
compareLabelsLexicographic(StringRef A,StringRef B)68*0b57cec5SDimitry Andric int compareLabelsLexicographic(StringRef A, StringRef B) {
69*0b57cec5SDimitry Andric   SmallVector<StringRef, 2> NamesA;
70*0b57cec5SDimitry Andric   A.split(NamesA, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
71*0b57cec5SDimitry Andric   SmallVector<StringRef, 2> NamesB;
72*0b57cec5SDimitry Andric   B.split(NamesB, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
73*0b57cec5SDimitry Andric   size_t SizeA = NamesA.size();
74*0b57cec5SDimitry Andric   size_t SizeB = NamesB.size();
75*0b57cec5SDimitry Andric   for (size_t I = 0, E = std::min(SizeA, SizeB); I < E; ++I) {
76*0b57cec5SDimitry Andric     // Two namespaces names within a group compare case-insensitively.
77*0b57cec5SDimitry Andric     int C = NamesA[I].compare_insensitive(NamesB[I]);
78*0b57cec5SDimitry Andric     if (C != 0)
79*0b57cec5SDimitry Andric       return C;
80*0b57cec5SDimitry Andric   }
81*0b57cec5SDimitry Andric   if (SizeA < SizeB)
82*0b57cec5SDimitry Andric     return -1;
83*0b57cec5SDimitry Andric   return SizeA == SizeB ? 0 : 1;
84*0b57cec5SDimitry Andric }
85*0b57cec5SDimitry Andric 
compareLabels(StringRef A,StringRef B,FormatStyle::SortUsingDeclarationsOptions SortUsingDeclarations)86*0b57cec5SDimitry Andric int compareLabels(
87*0b57cec5SDimitry Andric     StringRef A, StringRef B,
88*0b57cec5SDimitry Andric     FormatStyle::SortUsingDeclarationsOptions SortUsingDeclarations) {
89*0b57cec5SDimitry Andric   if (SortUsingDeclarations == FormatStyle::SUD_LexicographicNumeric)
90*0b57cec5SDimitry Andric     return compareLabelsLexicographicNumeric(A, B);
91*0b57cec5SDimitry Andric   return compareLabelsLexicographic(A, B);
92*0b57cec5SDimitry Andric }
93*0b57cec5SDimitry Andric 
94*0b57cec5SDimitry Andric struct UsingDeclaration {
95*0b57cec5SDimitry Andric   const AnnotatedLine *Line;
96*0b57cec5SDimitry Andric   std::string Label;
97*0b57cec5SDimitry Andric 
UsingDeclarationclang::format::__anon854919e60111::UsingDeclaration98*0b57cec5SDimitry Andric   UsingDeclaration(const AnnotatedLine *Line, const std::string &Label)
99*0b57cec5SDimitry Andric       : Line(Line), Label(Label) {}
100*0b57cec5SDimitry Andric };
101*0b57cec5SDimitry Andric 
102*0b57cec5SDimitry Andric /// Computes the label of a using declaration starting at tthe using token
103*0b57cec5SDimitry Andric /// \p UsingTok.
104*0b57cec5SDimitry Andric /// If \p UsingTok doesn't begin a using declaration, returns the empty string.
105*0b57cec5SDimitry Andric /// Note that this detects specifically using declarations, as in:
106*0b57cec5SDimitry Andric /// using A::B::C;
107*0b57cec5SDimitry Andric /// and not type aliases, as in:
108*0b57cec5SDimitry Andric /// using A = B::C;
109*0b57cec5SDimitry Andric /// Type aliases are in general not safe to permute.
computeUsingDeclarationLabel(const FormatToken * UsingTok)110*0b57cec5SDimitry Andric std::string computeUsingDeclarationLabel(const FormatToken *UsingTok) {
111*0b57cec5SDimitry Andric   assert(UsingTok && UsingTok->is(tok::kw_using) && "Expecting a using token");
112*0b57cec5SDimitry Andric   std::string Label;
113*0b57cec5SDimitry Andric   const FormatToken *Tok = UsingTok->Next;
114*0b57cec5SDimitry Andric   if (Tok && Tok->is(tok::kw_typename)) {
115*0b57cec5SDimitry Andric     Label.append("typename ");
116*0b57cec5SDimitry Andric     Tok = Tok->Next;
117*0b57cec5SDimitry Andric   }
118*0b57cec5SDimitry Andric   if (Tok && Tok->is(tok::coloncolon)) {
119*0b57cec5SDimitry Andric     Label.append("::");
120*0b57cec5SDimitry Andric     Tok = Tok->Next;
121*0b57cec5SDimitry Andric   }
122*0b57cec5SDimitry Andric   bool HasIdentifier = false;
123*0b57cec5SDimitry Andric   while (Tok && Tok->is(tok::identifier)) {
124*0b57cec5SDimitry Andric     HasIdentifier = true;
125*0b57cec5SDimitry Andric     Label.append(Tok->TokenText.str());
126*0b57cec5SDimitry Andric     Tok = Tok->Next;
127*0b57cec5SDimitry Andric     if (!Tok || Tok->isNot(tok::coloncolon))
128*0b57cec5SDimitry Andric       break;
129*0b57cec5SDimitry Andric     Label.append("::");
130*0b57cec5SDimitry Andric     Tok = Tok->Next;
131*0b57cec5SDimitry Andric   }
132*0b57cec5SDimitry Andric   if (HasIdentifier && Tok && Tok->isOneOf(tok::semi, tok::comma))
133*0b57cec5SDimitry Andric     return Label;
134*0b57cec5SDimitry Andric   return "";
135*0b57cec5SDimitry Andric }
136*0b57cec5SDimitry Andric 
endUsingDeclarationBlock(SmallVectorImpl<UsingDeclaration> * UsingDeclarations,const SourceManager & SourceMgr,tooling::Replacements * Fixes,FormatStyle::SortUsingDeclarationsOptions SortUsingDeclarations)137*0b57cec5SDimitry Andric void endUsingDeclarationBlock(
138*0b57cec5SDimitry Andric     SmallVectorImpl<UsingDeclaration> *UsingDeclarations,
139*0b57cec5SDimitry Andric     const SourceManager &SourceMgr, tooling::Replacements *Fixes,
140*0b57cec5SDimitry Andric     FormatStyle::SortUsingDeclarationsOptions SortUsingDeclarations) {
141*0b57cec5SDimitry Andric   bool BlockAffected = false;
142*0b57cec5SDimitry Andric   for (const UsingDeclaration &Declaration : *UsingDeclarations) {
143*0b57cec5SDimitry Andric     if (Declaration.Line->Affected) {
144*0b57cec5SDimitry Andric       BlockAffected = true;
145*0b57cec5SDimitry Andric       break;
146*0b57cec5SDimitry Andric     }
147*0b57cec5SDimitry Andric   }
148*0b57cec5SDimitry Andric   if (!BlockAffected) {
149*0b57cec5SDimitry Andric     UsingDeclarations->clear();
150*0b57cec5SDimitry Andric     return;
151*0b57cec5SDimitry Andric   }
152*0b57cec5SDimitry Andric   SmallVector<UsingDeclaration, 4> SortedUsingDeclarations(
153*0b57cec5SDimitry Andric       UsingDeclarations->begin(), UsingDeclarations->end());
154*0b57cec5SDimitry Andric   auto Comp = [SortUsingDeclarations](const UsingDeclaration &Lhs,
155*0b57cec5SDimitry Andric                                       const UsingDeclaration &Rhs) -> bool {
156*0b57cec5SDimitry Andric     return compareLabels(Lhs.Label, Rhs.Label, SortUsingDeclarations) < 0;
157*0b57cec5SDimitry Andric   };
158*0b57cec5SDimitry Andric   llvm::stable_sort(SortedUsingDeclarations, Comp);
159*0b57cec5SDimitry Andric   SortedUsingDeclarations.erase(
160*0b57cec5SDimitry Andric       std::unique(SortedUsingDeclarations.begin(),
161*0b57cec5SDimitry Andric                   SortedUsingDeclarations.end(),
162*0b57cec5SDimitry Andric                   [](const UsingDeclaration &a, const UsingDeclaration &b) {
163*0b57cec5SDimitry Andric                     return a.Label == b.Label;
164*0b57cec5SDimitry Andric                   }),
165*0b57cec5SDimitry Andric       SortedUsingDeclarations.end());
166*0b57cec5SDimitry Andric   for (size_t I = 0, E = UsingDeclarations->size(); I < E; ++I) {
167*0b57cec5SDimitry Andric     if (I >= SortedUsingDeclarations.size()) {
168*0b57cec5SDimitry Andric       // This using declaration has been deduplicated, delete it.
169*0b57cec5SDimitry Andric       auto Begin =
170*0b57cec5SDimitry Andric           (*UsingDeclarations)[I].Line->First->WhitespaceRange.getBegin();
171*0b57cec5SDimitry Andric       auto End = (*UsingDeclarations)[I].Line->Last->Tok.getEndLoc();
172*0b57cec5SDimitry Andric       auto Range = CharSourceRange::getCharRange(Begin, End);
173*0b57cec5SDimitry Andric       auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, ""));
174*0b57cec5SDimitry Andric       if (Err) {
175*0b57cec5SDimitry Andric         llvm::errs() << "Error while sorting using declarations: "
176*0b57cec5SDimitry Andric                      << llvm::toString(std::move(Err)) << "\n";
177*0b57cec5SDimitry Andric       }
178*0b57cec5SDimitry Andric       continue;
179*0b57cec5SDimitry Andric     }
180*0b57cec5SDimitry Andric     if ((*UsingDeclarations)[I].Line == SortedUsingDeclarations[I].Line)
181*0b57cec5SDimitry Andric       continue;
182*0b57cec5SDimitry Andric     auto Begin = (*UsingDeclarations)[I].Line->First->Tok.getLocation();
183*0b57cec5SDimitry Andric     auto End = (*UsingDeclarations)[I].Line->Last->Tok.getEndLoc();
184*0b57cec5SDimitry Andric     auto SortedBegin =
185*0b57cec5SDimitry Andric         SortedUsingDeclarations[I].Line->First->Tok.getLocation();
186*0b57cec5SDimitry Andric     auto SortedEnd = SortedUsingDeclarations[I].Line->Last->Tok.getEndLoc();
187*0b57cec5SDimitry Andric     StringRef Text(SourceMgr.getCharacterData(SortedBegin),
188*0b57cec5SDimitry Andric                    SourceMgr.getCharacterData(SortedEnd) -
189*0b57cec5SDimitry Andric                        SourceMgr.getCharacterData(SortedBegin));
190*0b57cec5SDimitry Andric     LLVM_DEBUG({
191*0b57cec5SDimitry Andric       StringRef OldText(SourceMgr.getCharacterData(Begin),
192*0b57cec5SDimitry Andric                         SourceMgr.getCharacterData(End) -
193*0b57cec5SDimitry Andric                             SourceMgr.getCharacterData(Begin));
194*0b57cec5SDimitry Andric       llvm::dbgs() << "Replacing '" << OldText << "' with '" << Text << "'\n";
195*0b57cec5SDimitry Andric     });
196*0b57cec5SDimitry Andric     auto Range = CharSourceRange::getCharRange(Begin, End);
197*0b57cec5SDimitry Andric     auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, Text));
198*0b57cec5SDimitry Andric     if (Err) {
199*0b57cec5SDimitry Andric       llvm::errs() << "Error while sorting using declarations: "
200*0b57cec5SDimitry Andric                    << llvm::toString(std::move(Err)) << "\n";
201*0b57cec5SDimitry Andric     }
202*0b57cec5SDimitry Andric   }
203*0b57cec5SDimitry Andric   UsingDeclarations->clear();
204*0b57cec5SDimitry Andric }
205*0b57cec5SDimitry Andric 
206*0b57cec5SDimitry Andric } // namespace
207*0b57cec5SDimitry Andric 
UsingDeclarationsSorter(const Environment & Env,const FormatStyle & Style)208*0b57cec5SDimitry Andric UsingDeclarationsSorter::UsingDeclarationsSorter(const Environment &Env,
209*0b57cec5SDimitry Andric                                                  const FormatStyle &Style)
210*0b57cec5SDimitry Andric     : TokenAnalyzer(Env, Style) {}
211*0b57cec5SDimitry Andric 
analyze(TokenAnnotator & Annotator,SmallVectorImpl<AnnotatedLine * > & AnnotatedLines,FormatTokenLexer & Tokens)212*0b57cec5SDimitry Andric std::pair<tooling::Replacements, unsigned> UsingDeclarationsSorter::analyze(
213*0b57cec5SDimitry Andric     TokenAnnotator &Annotator, SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
214*0b57cec5SDimitry Andric     FormatTokenLexer &Tokens) {
215   const SourceManager &SourceMgr = Env.getSourceManager();
216   AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
217   tooling::Replacements Fixes;
218   SmallVector<UsingDeclaration, 4> UsingDeclarations;
219   for (const AnnotatedLine *Line : AnnotatedLines) {
220     const auto *FirstTok = Line->First;
221     if (Line->InPPDirective || !Line->startsWith(tok::kw_using) ||
222         FirstTok->Finalized) {
223       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes,
224                                Style.SortUsingDeclarations);
225       continue;
226     }
227     if (FirstTok->NewlinesBefore > 1) {
228       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes,
229                                Style.SortUsingDeclarations);
230     }
231     const auto *UsingTok =
232         FirstTok->is(tok::comment) ? FirstTok->getNextNonComment() : FirstTok;
233     std::string Label = computeUsingDeclarationLabel(UsingTok);
234     if (Label.empty()) {
235       endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes,
236                                Style.SortUsingDeclarations);
237       continue;
238     }
239     UsingDeclarations.push_back(UsingDeclaration(Line, Label));
240   }
241   endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes,
242                            Style.SortUsingDeclarations);
243   return {Fixes, 0};
244 }
245 
246 } // namespace format
247 } // namespace clang
248