1 //===--- UsingDeclarationsSorter.cpp ----------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief This file implements UsingDeclarationsSorter, a TokenAnalyzer that 12 /// sorts consecutive using declarations. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "UsingDeclarationsSorter.h" 17 #include "llvm/Support/Debug.h" 18 #include "llvm/Support/Regex.h" 19 20 #include <algorithm> 21 22 #define DEBUG_TYPE "using-declarations-sorter" 23 24 namespace clang { 25 namespace format { 26 27 namespace { 28 29 struct UsingDeclaration { 30 const AnnotatedLine *Line; 31 std::string Label; 32 33 UsingDeclaration(const AnnotatedLine *Line, const std::string &Label) 34 : Line(Line), Label(Label) {} 35 36 bool operator<(const UsingDeclaration &Other) const { 37 return StringRef(Label).compare_lower(Other.Label) < 0; 38 } 39 }; 40 41 /// Computes the label of a using declaration starting at tthe using token 42 /// \p UsingTok. 43 /// If \p UsingTok doesn't begin a using declaration, returns the empty string. 44 /// Note that this detects specifically using declarations, as in: 45 /// using A::B::C; 46 /// and not type aliases, as in: 47 /// using A = B::C; 48 /// Type aliases are in general not safe to permute. 49 std::string computeUsingDeclarationLabel(const FormatToken *UsingTok) { 50 assert(UsingTok && UsingTok->is(tok::kw_using) && "Expecting a using token"); 51 std::string Label; 52 const FormatToken *Tok = UsingTok->Next; 53 if (Tok && Tok->is(tok::kw_typename)) { 54 Label.append("typename "); 55 Tok = Tok->Next; 56 } 57 if (Tok && Tok->is(tok::coloncolon)) { 58 Label.append("::"); 59 Tok = Tok->Next; 60 } 61 bool HasIdentifier = false; 62 while (Tok && Tok->is(tok::identifier)) { 63 HasIdentifier = true; 64 Label.append(Tok->TokenText.str()); 65 Tok = Tok->Next; 66 if (!Tok || Tok->isNot(tok::coloncolon)) 67 break; 68 Label.append("::"); 69 Tok = Tok->Next; 70 } 71 if (HasIdentifier && Tok && Tok->isOneOf(tok::semi, tok::comma)) 72 return Label; 73 return ""; 74 } 75 76 void endUsingDeclarationBlock( 77 SmallVectorImpl<UsingDeclaration> *UsingDeclarations, 78 const SourceManager &SourceMgr, tooling::Replacements *Fixes) { 79 SmallVector<UsingDeclaration, 4> SortedUsingDeclarations( 80 UsingDeclarations->begin(), UsingDeclarations->end()); 81 std::stable_sort(SortedUsingDeclarations.begin(), 82 SortedUsingDeclarations.end()); 83 for (size_t I = 0, E = UsingDeclarations->size(); I < E; ++I) { 84 if ((*UsingDeclarations)[I].Line == SortedUsingDeclarations[I].Line) 85 continue; 86 auto Begin = (*UsingDeclarations)[I].Line->First->Tok.getLocation(); 87 auto End = (*UsingDeclarations)[I].Line->Last->Tok.getEndLoc(); 88 auto SortedBegin = 89 SortedUsingDeclarations[I].Line->First->Tok.getLocation(); 90 auto SortedEnd = SortedUsingDeclarations[I].Line->Last->Tok.getEndLoc(); 91 StringRef Text(SourceMgr.getCharacterData(SortedBegin), 92 SourceMgr.getCharacterData(SortedEnd) - 93 SourceMgr.getCharacterData(SortedBegin)); 94 DEBUG({ 95 StringRef OldText(SourceMgr.getCharacterData(Begin), 96 SourceMgr.getCharacterData(End) - 97 SourceMgr.getCharacterData(Begin)); 98 llvm::dbgs() << "Replacing '" << OldText << "' with '" << Text << "'\n"; 99 }); 100 auto Range = CharSourceRange::getCharRange(Begin, End); 101 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, Text)); 102 if (Err) { 103 llvm::errs() << "Error while sorting using declarations: " 104 << llvm::toString(std::move(Err)) << "\n"; 105 } 106 } 107 UsingDeclarations->clear(); 108 } 109 110 } // namespace 111 112 UsingDeclarationsSorter::UsingDeclarationsSorter(const Environment &Env, 113 const FormatStyle &Style) 114 : TokenAnalyzer(Env, Style) {} 115 116 tooling::Replacements UsingDeclarationsSorter::analyze( 117 TokenAnnotator &Annotator, SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, 118 FormatTokenLexer &Tokens) { 119 const SourceManager &SourceMgr = Env.getSourceManager(); 120 AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(), 121 AnnotatedLines.end()); 122 tooling::Replacements Fixes; 123 SmallVector<UsingDeclaration, 4> UsingDeclarations; 124 for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) { 125 if (!AnnotatedLines[I]->Affected || AnnotatedLines[I]->InPPDirective || 126 !AnnotatedLines[I]->startsWith(tok::kw_using) || 127 AnnotatedLines[I]->First->Finalized) { 128 endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes); 129 continue; 130 } 131 if (AnnotatedLines[I]->First->NewlinesBefore > 1) 132 endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes); 133 std::string Label = computeUsingDeclarationLabel(AnnotatedLines[I]->First); 134 if (Label.empty()) { 135 endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes); 136 continue; 137 } 138 UsingDeclarations.push_back(UsingDeclaration(AnnotatedLines[I], Label)); 139 } 140 endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes); 141 return Fixes; 142 } 143 144 } // namespace format 145 } // namespace clang 146