NamespaceEndCommentsFixer.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //===--- NamespaceEndCommentsFixer.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 NamespaceEndCommentsFixer, a TokenAnalyzer that
  12. /// fixes namespace end comments.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #include "NamespaceEndCommentsFixer.h"
  16. #include "llvm/Support/Debug.h"
  17. #include "llvm/Support/Regex.h"
  18. #define DEBUG_TYPE "namespace-end-comments-fixer"
  19. namespace clang {
  20. namespace format {
  21. namespace {
  22. // The maximal number of unwrapped lines that a short namespace spans.
  23. // Short namespaces don't need an end comment.
  24. static const int kShortNamespaceMaxLines = 1;
  25. // Matches a valid namespace end comment.
  26. // Valid namespace end comments don't need to be edited.
  27. static llvm::Regex kNamespaceCommentPattern =
  28. llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
  29. "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
  30. llvm::Regex::IgnoreCase);
  31. // Computes the name of a namespace given the namespace token.
  32. // Returns "" for anonymous namespace.
  33. std::string computeName(const FormatToken *NamespaceTok) {
  34. assert(NamespaceTok && NamespaceTok->is(tok::kw_namespace) &&
  35. "expecting a namespace token");
  36. std::string name = "";
  37. // Collects all the non-comment tokens between 'namespace' and '{'.
  38. const FormatToken *Tok = NamespaceTok->getNextNonComment();
  39. while (Tok && !Tok->is(tok::l_brace)) {
  40. name += Tok->TokenText;
  41. Tok = Tok->getNextNonComment();
  42. }
  43. return name;
  44. }
  45. std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline) {
  46. std::string text = "// namespace";
  47. if (!NamespaceName.empty()) {
  48. text += ' ';
  49. text += NamespaceName;
  50. }
  51. if (AddNewline)
  52. text += '\n';
  53. return text;
  54. }
  55. bool hasEndComment(const FormatToken *RBraceTok) {
  56. return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
  57. }
  58. bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName) {
  59. assert(hasEndComment(RBraceTok));
  60. const FormatToken *Comment = RBraceTok->Next;
  61. SmallVector<StringRef, 7> Groups;
  62. if (kNamespaceCommentPattern.match(Comment->TokenText, &Groups)) {
  63. StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : "";
  64. // Anonymous namespace comments must not mention a namespace name.
  65. if (NamespaceName.empty() && !NamespaceNameInComment.empty())
  66. return false;
  67. StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] : "";
  68. // Named namespace comments must not mention anonymous namespace.
  69. if (!NamespaceName.empty() && !AnonymousInComment.empty())
  70. return false;
  71. return NamespaceNameInComment == NamespaceName;
  72. }
  73. return false;
  74. }
  75. void addEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
  76. const SourceManager &SourceMgr,
  77. tooling::Replacements *Fixes) {
  78. auto EndLoc = RBraceTok->Tok.getEndLoc();
  79. auto Range = CharSourceRange::getCharRange(EndLoc, EndLoc);
  80. auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
  81. if (Err) {
  82. llvm::errs() << "Error while adding namespace end comment: "
  83. << llvm::toString(std::move(Err)) << "\n";
  84. }
  85. }
  86. void updateEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
  87. const SourceManager &SourceMgr,
  88. tooling::Replacements *Fixes) {
  89. assert(hasEndComment(RBraceTok));
  90. const FormatToken *Comment = RBraceTok->Next;
  91. auto Range = CharSourceRange::getCharRange(Comment->getStartOfNonWhitespace(),
  92. Comment->Tok.getEndLoc());
  93. auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
  94. if (Err) {
  95. llvm::errs() << "Error while updating namespace end comment: "
  96. << llvm::toString(std::move(Err)) << "\n";
  97. }
  98. }
  99. const FormatToken *
  100. getNamespaceToken(const AnnotatedLine *line,
  101. const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
  102. if (!line->Affected || line->InPPDirective || !line->startsWith(tok::r_brace))
  103. return nullptr;
  104. size_t StartLineIndex = line->MatchingOpeningBlockLineIndex;
  105. if (StartLineIndex == UnwrappedLine::kInvalidIndex)
  106. return nullptr;
  107. assert(StartLineIndex < AnnotatedLines.size());
  108. const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
  109. if (NamespaceTok->is(tok::l_brace)) {
  110. // "namespace" keyword can be on the line preceding '{', e.g. in styles
  111. // where BraceWrapping.AfterNamespace is true.
  112. if (StartLineIndex > 0)
  113. NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
  114. }
  115. // Detect "(inline)? namespace" in the beginning of a line.
  116. if (NamespaceTok->is(tok::kw_inline))
  117. NamespaceTok = NamespaceTok->getNextNonComment();
  118. if (!NamespaceTok || NamespaceTok->isNot(tok::kw_namespace))
  119. return nullptr;
  120. return NamespaceTok;
  121. }
  122. } // namespace
  123. NamespaceEndCommentsFixer::NamespaceEndCommentsFixer(const Environment &Env,
  124. const FormatStyle &Style)
  125. : TokenAnalyzer(Env, Style) {}
  126. std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(
  127. TokenAnnotator &Annotator, SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
  128. FormatTokenLexer &Tokens) {
  129. const SourceManager &SourceMgr = Env.getSourceManager();
  130. AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
  131. AnnotatedLines.end());
  132. tooling::Replacements Fixes;
  133. std::string AllNamespaceNames = "";
  134. size_t StartLineIndex = SIZE_MAX;
  135. unsigned int CompactedNamespacesCount = 0;
  136. for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
  137. const AnnotatedLine *EndLine = AnnotatedLines[I];
  138. const FormatToken *NamespaceTok =
  139. getNamespaceToken(EndLine, AnnotatedLines);
  140. if (!NamespaceTok)
  141. continue;
  142. FormatToken *RBraceTok = EndLine->First;
  143. if (RBraceTok->Finalized)
  144. continue;
  145. RBraceTok->Finalized = true;
  146. const FormatToken *EndCommentPrevTok = RBraceTok;
  147. // Namespaces often end with '};'. In that case, attach namespace end
  148. // comments to the semicolon tokens.
  149. if (RBraceTok->Next && RBraceTok->Next->is(tok::semi)) {
  150. EndCommentPrevTok = RBraceTok->Next;
  151. }
  152. if (StartLineIndex == SIZE_MAX)
  153. StartLineIndex = EndLine->MatchingOpeningBlockLineIndex;
  154. std::string NamespaceName = computeName(NamespaceTok);
  155. if (Style.CompactNamespaces) {
  156. if ((I + 1 < E) &&
  157. getNamespaceToken(AnnotatedLines[I + 1], AnnotatedLines) &&
  158. StartLineIndex - CompactedNamespacesCount - 1 ==
  159. AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
  160. !AnnotatedLines[I + 1]->First->Finalized) {
  161. if (hasEndComment(EndCommentPrevTok)) {
  162. // remove end comment, it will be merged in next one
  163. updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
  164. }
  165. CompactedNamespacesCount++;
  166. AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
  167. continue;
  168. }
  169. NamespaceName += AllNamespaceNames;
  170. CompactedNamespacesCount = 0;
  171. AllNamespaceNames = std::string();
  172. }
  173. // The next token in the token stream after the place where the end comment
  174. // token must be. This is either the next token on the current line or the
  175. // first token on the next line.
  176. const FormatToken *EndCommentNextTok = EndCommentPrevTok->Next;
  177. if (EndCommentNextTok && EndCommentNextTok->is(tok::comment))
  178. EndCommentNextTok = EndCommentNextTok->Next;
  179. if (!EndCommentNextTok && I + 1 < E)
  180. EndCommentNextTok = AnnotatedLines[I + 1]->First;
  181. bool AddNewline = EndCommentNextTok &&
  182. EndCommentNextTok->NewlinesBefore == 0 &&
  183. EndCommentNextTok->isNot(tok::eof);
  184. const std::string EndCommentText =
  185. computeEndCommentText(NamespaceName, AddNewline);
  186. if (!hasEndComment(EndCommentPrevTok)) {
  187. bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
  188. if (!isShort)
  189. addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
  190. } else if (!validEndComment(EndCommentPrevTok, NamespaceName)) {
  191. updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
  192. }
  193. StartLineIndex = SIZE_MAX;
  194. }
  195. return {Fixes, 0};
  196. }
  197. } // namespace format
  198. } // namespace clang