17#include "llvm/Support/Debug.h"
18#include "llvm/Support/Regex.h"
20#define DEBUG_TYPE "namespace-end-comments-fixer"
32 llvm::function_ref<
void(
const FormatToken *)> Fn) {
33 if (!Tok || Tok->isNot(StartTok))
37 if (Tok->is(StartTok))
39 else if (Tok->is(EndTok))
43 Tok = Tok->getNextNonComment();
44 }
while (Tok && NestLevel > 0);
48const FormatToken *skipAttribute(
const FormatToken *Tok) {
51 if (Tok->isAttribute()) {
52 Tok = Tok->getNextNonComment();
53 Tok = processTokens(Tok, tok::l_paren, tok::r_paren,
nullptr);
54 }
else if (Tok->is(tok::l_square)) {
55 Tok = processTokens(Tok, tok::l_square, tok::r_square,
nullptr);
62std::string computeName(
const FormatToken *NamespaceTok) {
63 assert(NamespaceTok &&
64 NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
65 "expecting a namespace token");
67 const FormatToken *Tok = NamespaceTok->getNextNonComment();
68 if (NamespaceTok->is(TT_NamespaceMacro)) {
71 assert(Tok && Tok->is(tok::l_paren) &&
"expected an opening parenthesis");
72 Tok = Tok->getNextNonComment();
73 while (Tok && !Tok->isOneOf(tok::r_paren, tok::comma)) {
74 name += Tok->TokenText;
75 Tok = Tok->getNextNonComment();
79 Tok = skipAttribute(Tok);
81 std::string FirstNSName;
87 const FormatToken *FirstNSTok =
nullptr;
88 while (Tok && !Tok->isOneOf(tok::l_brace, tok::coloncolon, tok::l_paren)) {
90 FirstNSName += FirstNSTok->TokenText;
92 Tok = Tok->getNextNonComment();
97 Tok = skipAttribute(Tok);
101 auto AddToken = [&name](
const FormatToken *Tok) { name += Tok->TokenText; };
102 bool IsPrevColoncolon =
false;
103 bool HasColoncolon =
false;
104 bool IsPrevInline =
false;
105 bool NameFinished =
false;
108 while (Tok && Tok->isNot(tok::l_brace)) {
110 if (!IsPrevInline && HasColoncolon && !IsPrevColoncolon) {
111 if (FirstNSTok->is(tok::l_paren)) {
113 processTokens(FirstNSTok, tok::l_paren, tok::r_paren, AddToken);
116 if (FirstNSTok->isNot(tok::coloncolon)) {
121 name += FirstNSTok->TokenText;
122 IsPrevColoncolon = FirstNSTok->is(tok::coloncolon);
123 HasColoncolon = HasColoncolon || IsPrevColoncolon;
124 if (FirstNSTok->is(tok::kw_inline)) {
130 Tok = Tok->getNextNonComment();
131 const FormatToken *TokAfterAttr = skipAttribute(Tok);
132 if (TokAfterAttr != Tok)
133 FirstNSTok = Tok = TokAfterAttr;
135 if (!NameFinished && FirstNSTok && FirstNSTok->isNot(tok::l_brace))
136 name += FirstNSTok->TokenText;
137 if (FirstNSName.empty() || HasColoncolon)
139 return name.empty() ? FirstNSName : FirstNSName +
" " + name;
142std::string computeEndCommentText(StringRef NamespaceName,
bool AddNewline,
143 const FormatToken *NamespaceTok,
144 unsigned SpacesToAdd) {
145 std::string text =
"//";
146 text.append(SpacesToAdd,
' ');
147 text += NamespaceTok->TokenText;
148 if (NamespaceTok->is(TT_NamespaceMacro))
150 else if (!NamespaceName.empty())
152 text += NamespaceName;
153 if (NamespaceTok->is(TT_NamespaceMacro))
160bool hasEndComment(
const FormatToken *RBraceTok) {
161 return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
164bool validEndComment(
const FormatToken *RBraceTok, StringRef NamespaceName,
165 const FormatToken *NamespaceTok) {
166 assert(hasEndComment(RBraceTok));
167 const FormatToken *Comment = RBraceTok->Next;
171 static const llvm::Regex NamespaceCommentPattern =
172 llvm::Regex(
"^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
173 "namespace( +([a-zA-Z0-9:_ ]+))?\\.? *(\\*/)?$",
174 llvm::Regex::IgnoreCase);
175 static const llvm::Regex NamespaceMacroCommentPattern =
176 llvm::Regex(
"^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
177 "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*|\".+\")\\)\\.? *(\\*/)?$",
178 llvm::Regex::IgnoreCase);
180 SmallVector<StringRef, 8> Groups;
181 if (NamespaceTok->is(TT_NamespaceMacro) &&
182 NamespaceMacroCommentPattern.match(Comment->TokenText, &Groups)) {
183 StringRef NamespaceTokenText = Groups.size() > 4 ? Groups[4] :
"";
185 if (NamespaceTokenText != NamespaceTok->TokenText)
187 }
else if (NamespaceTok->isNot(tok::kw_namespace) ||
188 !NamespaceCommentPattern.match(Comment->TokenText, &Groups)) {
192 StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5].rtrim() :
"";
194 if (NamespaceName.empty() && !NamespaceNameInComment.empty())
196 StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] :
"";
198 if (!NamespaceName.empty() && !AnonymousInComment.empty())
200 if (NamespaceNameInComment == NamespaceName)
206 if (!(Comment->Next && Comment->Next->is(TT_LineComment)))
209 static const llvm::Regex CommentPattern = llvm::Regex(
210 "^/[/*] *( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$", llvm::Regex::IgnoreCase);
213 if (!CommentPattern.match(Comment->Next->TokenText, &Groups))
215 NamespaceNameInComment = Groups.size() > 2 ? Groups[2] :
"";
217 return NamespaceNameInComment == NamespaceName;
220void addEndComment(
const FormatToken *RBraceTok, StringRef EndCommentText,
221 const SourceManager &SourceMgr,
222 tooling::Replacements *Fixes) {
223 auto EndLoc = RBraceTok->Tok.getEndLoc();
225 auto Err = Fixes->add(tooling::Replacement(SourceMgr,
Range, EndCommentText));
227 llvm::errs() <<
"Error while adding namespace end comment: "
228 << llvm::toString(std::move(Err)) <<
"\n";
232void updateEndComment(
const FormatToken *RBraceTok, StringRef EndCommentText,
233 const SourceManager &SourceMgr,
234 tooling::Replacements *Fixes) {
235 assert(hasEndComment(RBraceTok));
236 const FormatToken *Comment = RBraceTok->Next;
238 Comment->Tok.getEndLoc());
239 auto Err = Fixes->add(tooling::Replacement(SourceMgr,
Range, EndCommentText));
241 llvm::errs() <<
"Error while updating namespace end comment: "
242 << llvm::toString(std::move(Err)) <<
"\n";
250 if (!
Line->Affected ||
Line->InPPDirective || !
Line->startsWith(tok::r_brace))
252 size_t StartLineIndex =
Line->MatchingOpeningBlockLineIndex;
255 assert(StartLineIndex < AnnotatedLines.size());
256 const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
257 if (NamespaceTok->
is(tok::l_brace)) {
260 if (StartLineIndex > 0) {
261 NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
262 if (AnnotatedLines[StartLineIndex - 1]->endsWith(tok::semi))
274 return NamespaceTok ? NamespaceTok->
TokenText : StringRef();
293 Braces += Tok->
is(tok::l_brace) ? 1 : Tok->
is(tok::r_brace) ? -1 : 0;
303 std::string AllNamespaceNames;
305 StringRef NamespaceTokenText;
306 unsigned int CompactedNamespacesCount = 0;
307 for (
size_t I = 0,
E = AnnotatedLines.size(); I !=
E; ++I) {
320 if (RBraceTok->
Next && RBraceTok->
Next->
is(tok::semi))
321 EndCommentPrevTok = RBraceTok->
Next;
324 std::string NamespaceName = computeName(NamespaceTok);
326 if (CompactedNamespacesCount == 0)
327 NamespaceTokenText = NamespaceTok->
TokenText;
329 NamespaceTokenText ==
331 StartLineIndex - CompactedNamespacesCount - 1 ==
332 AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
333 !AnnotatedLines[I + 1]->
First->Finalized) {
334 if (hasEndComment(EndCommentPrevTok)) {
336 updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
338 ++CompactedNamespacesCount;
339 if (!NamespaceName.empty())
340 AllNamespaceNames =
"::" + NamespaceName + AllNamespaceNames;
343 NamespaceName += AllNamespaceNames;
344 CompactedNamespacesCount = 0;
345 AllNamespaceNames = std::string();
351 if (EndCommentNextTok && EndCommentNextTok->
is(tok::comment))
352 EndCommentNextTok = EndCommentNextTok->
Next;
353 if (!EndCommentNextTok && I + 1 <
E)
354 EndCommentNextTok = AnnotatedLines[I + 1]->First;
355 bool AddNewline = EndCommentNextTok &&
357 EndCommentNextTok->
isNot(tok::eof);
358 const std::string EndCommentText =
359 computeEndCommentText(NamespaceName, AddNewline, NamespaceTok,
361 if (!hasEndComment(EndCommentPrevTok)) {
362 unsigned LineCount = 0;
363 for (
auto J = StartLineIndex + 1; J < I; ++J)
364 LineCount += AnnotatedLines[J]->size();
366 addEndComment(EndCommentPrevTok,
371 }
else if (!validEndComment(EndCommentPrevTok, NamespaceName,
373 updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
Defines the clang::TokenKind enum and support functions.
static CharSourceRange getCharRange(SourceRange R)
This class handles loading and caching of source files into memory.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
The JSON file list parser is used to communicate input to InstallAPI.
@ Braces
New-expression has a C++11 list-initializer.