UsingDeclarationsSorter.cpp 9.1 KB

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