blob: 53542000a1324288b4d433f36c4843a38b089dc4 [file] [log] [blame]
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +00001//===--- NamespaceEndCommentsFixer.cpp --------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file implements NamespaceEndCommentsFixer, a TokenAnalyzer that
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +000011/// fixes namespace end comments.
12///
13//===----------------------------------------------------------------------===//
14
15#include "NamespaceEndCommentsFixer.h"
16#include "llvm/Support/Debug.h"
17#include "llvm/Support/Regex.h"
18
19#define DEBUG_TYPE "namespace-end-comments-fixer"
20
21namespace clang {
22namespace format {
23
24namespace {
Krasimir Georgiev9163fe22017-03-02 09:54:44 +000025// The maximal number of unwrapped lines that a short namespace spans.
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +000026// Short namespaces don't need an end comment.
27static const int kShortNamespaceMaxLines = 1;
28
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +000029// Computes the name of a namespace given the namespace token.
30// Returns "" for anonymous namespace.
31std::string computeName(const FormatToken *NamespaceTok) {
32 assert(NamespaceTok && NamespaceTok->is(tok::kw_namespace) &&
33 "expecting a namespace token");
34 std::string name = "";
35 // Collects all the non-comment tokens between 'namespace' and '{'.
36 const FormatToken *Tok = NamespaceTok->getNextNonComment();
37 while (Tok && !Tok->is(tok::l_brace)) {
38 name += Tok->TokenText;
39 Tok = Tok->getNextNonComment();
40 }
41 return name;
42}
43
44std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline) {
45 std::string text = "// namespace";
46 if (!NamespaceName.empty()) {
47 text += ' ';
48 text += NamespaceName;
49 }
50 if (AddNewline)
51 text += '\n';
52 return text;
53}
54
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +000055bool hasEndComment(const FormatToken *RBraceTok) {
56 return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
57}
58
59bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName) {
60 assert(hasEndComment(RBraceTok));
61 const FormatToken *Comment = RBraceTok->Next;
Benjamin Kramer8ef820a2018-03-20 20:43:12 +000062
63 // Matches a valid namespace end comment.
64 // Valid namespace end comments don't need to be edited.
65 static llvm::Regex *const NamespaceCommentPattern =
66 new llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
67 "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
68 llvm::Regex::IgnoreCase);
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +000069 SmallVector<StringRef, 7> Groups;
Benjamin Kramer8ef820a2018-03-20 20:43:12 +000070 if (NamespaceCommentPattern->match(Comment->TokenText, &Groups)) {
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +000071 StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : "";
72 // Anonymous namespace comments must not mention a namespace name.
73 if (NamespaceName.empty() && !NamespaceNameInComment.empty())
74 return false;
75 StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] : "";
76 // Named namespace comments must not mention anonymous namespace.
77 if (!NamespaceName.empty() && !AnonymousInComment.empty())
78 return false;
79 return NamespaceNameInComment == NamespaceName;
80 }
81 return false;
82}
83
84void addEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
85 const SourceManager &SourceMgr,
86 tooling::Replacements *Fixes) {
87 auto EndLoc = RBraceTok->Tok.getEndLoc();
88 auto Range = CharSourceRange::getCharRange(EndLoc, EndLoc);
89 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
90 if (Err) {
91 llvm::errs() << "Error while adding namespace end comment: "
92 << llvm::toString(std::move(Err)) << "\n";
93 }
94}
95
96void updateEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
97 const SourceManager &SourceMgr,
98 tooling::Replacements *Fixes) {
99 assert(hasEndComment(RBraceTok));
100 const FormatToken *Comment = RBraceTok->Next;
101 auto Range = CharSourceRange::getCharRange(Comment->getStartOfNonWhitespace(),
102 Comment->Tok.getEndLoc());
103 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
104 if (Err) {
105 llvm::errs() << "Error while updating namespace end comment: "
106 << llvm::toString(std::move(Err)) << "\n";
107 }
108}
Krasimir Georgiev62103052018-04-19 13:02:15 +0000109} // namespace
Francois Ferrande56a8292017-06-14 12:29:47 +0000110
111const FormatToken *
Krasimir Georgiev6bbc7062018-04-23 10:02:59 +0000112getNamespaceToken(const AnnotatedLine *Line,
Francois Ferrande56a8292017-06-14 12:29:47 +0000113 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
Krasimir Georgiev6bbc7062018-04-23 10:02:59 +0000114 if (!Line->Affected || Line->InPPDirective || !Line->startsWith(tok::r_brace))
Francois Ferrande56a8292017-06-14 12:29:47 +0000115 return nullptr;
Krasimir Georgiev6bbc7062018-04-23 10:02:59 +0000116 size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;
Francois Ferrande56a8292017-06-14 12:29:47 +0000117 if (StartLineIndex == UnwrappedLine::kInvalidIndex)
118 return nullptr;
119 assert(StartLineIndex < AnnotatedLines.size());
120 const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
Marek Kurdejcaf6fd52017-09-27 07:51:51 +0000121 if (NamespaceTok->is(tok::l_brace)) {
122 // "namespace" keyword can be on the line preceding '{', e.g. in styles
123 // where BraceWrapping.AfterNamespace is true.
124 if (StartLineIndex > 0)
125 NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
126 }
Sam McCall6f3778c2018-09-05 07:44:02 +0000127 return NamespaceTok->getNamespaceToken();
Francois Ferrande56a8292017-06-14 12:29:47 +0000128}
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +0000129
130NamespaceEndCommentsFixer::NamespaceEndCommentsFixer(const Environment &Env,
131 const FormatStyle &Style)
132 : TokenAnalyzer(Env, Style) {}
133
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000134std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +0000135 TokenAnnotator &Annotator, SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
136 FormatTokenLexer &Tokens) {
137 const SourceManager &SourceMgr = Env.getSourceManager();
Manuel Klimek0dddcf72018-04-23 09:34:26 +0000138 AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +0000139 tooling::Replacements Fixes;
Francois Ferrande56a8292017-06-14 12:29:47 +0000140 std::string AllNamespaceNames = "";
141 size_t StartLineIndex = SIZE_MAX;
142 unsigned int CompactedNamespacesCount = 0;
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +0000143 for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +0000144 const AnnotatedLine *EndLine = AnnotatedLines[I];
Francois Ferrande56a8292017-06-14 12:29:47 +0000145 const FormatToken *NamespaceTok =
146 getNamespaceToken(EndLine, AnnotatedLines);
147 if (!NamespaceTok)
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +0000148 continue;
Krasimir Georgievbda77392017-03-06 16:44:45 +0000149 FormatToken *RBraceTok = EndLine->First;
150 if (RBraceTok->Finalized)
151 continue;
152 RBraceTok->Finalized = true;
Krasimir Georgieveb62118e2017-03-07 14:07:43 +0000153 const FormatToken *EndCommentPrevTok = RBraceTok;
154 // Namespaces often end with '};'. In that case, attach namespace end
155 // comments to the semicolon tokens.
156 if (RBraceTok->Next && RBraceTok->Next->is(tok::semi)) {
157 EndCommentPrevTok = RBraceTok->Next;
158 }
Francois Ferrande56a8292017-06-14 12:29:47 +0000159 if (StartLineIndex == SIZE_MAX)
160 StartLineIndex = EndLine->MatchingOpeningBlockLineIndex;
161 std::string NamespaceName = computeName(NamespaceTok);
162 if (Style.CompactNamespaces) {
163 if ((I + 1 < E) &&
164 getNamespaceToken(AnnotatedLines[I + 1], AnnotatedLines) &&
165 StartLineIndex - CompactedNamespacesCount - 1 ==
166 AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
167 !AnnotatedLines[I + 1]->First->Finalized) {
168 if (hasEndComment(EndCommentPrevTok)) {
169 // remove end comment, it will be merged in next one
170 updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
171 }
172 CompactedNamespacesCount++;
173 AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
174 continue;
175 }
Krasimir Georgiev3d4c8122017-06-27 14:07:45 +0000176 NamespaceName += AllNamespaceNames;
Francois Ferrande56a8292017-06-14 12:29:47 +0000177 CompactedNamespacesCount = 0;
178 AllNamespaceNames = std::string();
179 }
Krasimir Georgieveb62118e2017-03-07 14:07:43 +0000180 // The next token in the token stream after the place where the end comment
181 // token must be. This is either the next token on the current line or the
182 // first token on the next line.
183 const FormatToken *EndCommentNextTok = EndCommentPrevTok->Next;
184 if (EndCommentNextTok && EndCommentNextTok->is(tok::comment))
185 EndCommentNextTok = EndCommentNextTok->Next;
186 if (!EndCommentNextTok && I + 1 < E)
187 EndCommentNextTok = AnnotatedLines[I + 1]->First;
188 bool AddNewline = EndCommentNextTok &&
189 EndCommentNextTok->NewlinesBefore == 0 &&
190 EndCommentNextTok->isNot(tok::eof);
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +0000191 const std::string EndCommentText =
192 computeEndCommentText(NamespaceName, AddNewline);
Krasimir Georgieveb62118e2017-03-07 14:07:43 +0000193 if (!hasEndComment(EndCommentPrevTok)) {
Krasimir Georgiev9163fe22017-03-02 09:54:44 +0000194 bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
195 if (!isShort)
Krasimir Georgieveb62118e2017-03-07 14:07:43 +0000196 addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
Francois Ferrande56a8292017-06-14 12:29:47 +0000197 } else if (!validEndComment(EndCommentPrevTok, NamespaceName)) {
Krasimir Georgieveb62118e2017-03-07 14:07:43 +0000198 updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
Francois Ferrande56a8292017-06-14 12:29:47 +0000199 }
200 StartLineIndex = SIZE_MAX;
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +0000201 }
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000202 return {Fixes, 0};
Krasimir Georgiev7cb267a2017-02-27 13:28:36 +0000203}
204
205} // namespace format
206} // namespace clang