17#define DEBUG_TYPE "namespace-end-comments-fixer"
29 llvm::function_ref<
void(
const FormatToken *)> Fn) {
30 if (!Tok || Tok->isNot(StartTok))
34 if (Tok->is(StartTok))
36 else if (Tok->is(EndTok))
40 Tok = Tok->getNextNonComment();
41 }
while (Tok && NestLevel > 0);
45const FormatToken *skipAttribute(
const FormatToken *Tok) {
48 if (Tok->isAttribute()) {
49 Tok = Tok->getNextNonComment();
50 Tok = processTokens(Tok, tok::l_paren, tok::r_paren,
nullptr);
51 }
else if (Tok->is(tok::l_square)) {
52 Tok = processTokens(Tok, tok::l_square, tok::r_square,
nullptr);
59std::string computeName(
const FormatToken *NamespaceTok) {
60 assert(NamespaceTok &&
61 NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
62 "expecting a namespace token");
64 const FormatToken *Tok = NamespaceTok->getNextNonComment();
65 if (NamespaceTok->is(TT_NamespaceMacro)) {
68 assert(Tok && Tok->is(tok::l_paren) &&
"expected an opening parenthesis");
69 Tok = Tok->getNextNonComment();
70 while (Tok && !Tok->isOneOf(tok::r_paren, tok::comma)) {
71 name += Tok->TokenText;
72 Tok = Tok->getNextNonComment();
76 Tok = skipAttribute(Tok);
78 std::string FirstNSName;
84 const FormatToken *FirstNSTok =
nullptr;
85 while (Tok && !Tok->isOneOf(tok::l_brace, tok::coloncolon, tok::l_paren)) {
87 FirstNSName += FirstNSTok->TokenText;
89 Tok = Tok->getNextNonComment();
94 Tok = skipAttribute(Tok);
98 auto AddToken = [&name](
const FormatToken *Tok) { name += Tok->TokenText; };
99 bool IsPrevColoncolon =
false;
100 bool HasColoncolon =
false;
101 bool IsPrevInline =
false;
102 bool NameFinished =
false;
105 while (Tok && Tok->isNot(tok::l_brace)) {
107 if (!IsPrevInline && HasColoncolon && !IsPrevColoncolon) {
108 if (FirstNSTok->is(tok::l_paren)) {
110 processTokens(FirstNSTok, tok::l_paren, tok::r_paren, AddToken);
113 if (FirstNSTok->isNot(tok::coloncolon)) {
118 name += FirstNSTok->TokenText;
119 IsPrevColoncolon = FirstNSTok->is(tok::coloncolon);
120 HasColoncolon = HasColoncolon || IsPrevColoncolon;
121 if (FirstNSTok->is(tok::kw_inline)) {
127 Tok = Tok->getNextNonComment();
128 const FormatToken *TokAfterAttr = skipAttribute(Tok);
129 if (TokAfterAttr != Tok)
130 FirstNSTok = Tok = TokAfterAttr;
132 if (!NameFinished && FirstNSTok && FirstNSTok->isNot(tok::l_brace))
133 name += FirstNSTok->TokenText;
134 if (FirstNSName.empty() || HasColoncolon)
136 return name.empty() ? FirstNSName : FirstNSName +
" " + name;
139std::string computeEndCommentText(StringRef NamespaceName,
bool AddNewline,
140 const FormatToken *NamespaceTok,
141 unsigned SpacesToAdd) {
142 std::string text =
"//";
143 text.append(SpacesToAdd,
' ');
144 text += NamespaceTok->TokenText;
145 if (NamespaceTok->is(TT_NamespaceMacro))
147 else if (!NamespaceName.empty())
149 text += NamespaceName;
150 if (NamespaceTok->is(TT_NamespaceMacro))
157bool hasEndComment(
const FormatToken *RBraceTok) {
158 return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
161bool validEndComment(
const FormatToken *RBraceTok, StringRef NamespaceName,
162 const FormatToken *NamespaceTok) {
163 assert(hasEndComment(RBraceTok));
164 const FormatToken *Comment = RBraceTok->Next;
168 static const llvm::Regex NamespaceCommentPattern =
169 llvm::Regex(
"^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
170 "namespace( +([a-zA-Z0-9:_ ]+))?\\.? *(\\*/)?$",
171 llvm::Regex::IgnoreCase);
172 static const llvm::Regex NamespaceMacroCommentPattern =
173 llvm::Regex(
"^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
174 "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*|\".+\")\\)\\.? *(\\*/)?$",
175 llvm::Regex::IgnoreCase);
177 SmallVector<StringRef, 8> Groups;
178 if (NamespaceTok->is(TT_NamespaceMacro) &&
179 NamespaceMacroCommentPattern.match(Comment->TokenText, &Groups)) {
180 StringRef NamespaceTokenText = Groups.size() > 4 ? Groups[4] :
"";
182 if (NamespaceTokenText != NamespaceTok->TokenText)
184 }
else if (NamespaceTok->isNot(tok::kw_namespace) ||
185 !NamespaceCommentPattern.match(Comment->TokenText, &Groups)) {
189 StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5].rtrim() :
"";
191 if (NamespaceName.empty() && !NamespaceNameInComment.empty())
193 StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] :
"";
195 if (!NamespaceName.empty() && !AnonymousInComment.empty())
197 if (NamespaceNameInComment == NamespaceName)
203 if (!(Comment->Next && Comment->Next->is(TT_LineComment)))
206 static const llvm::Regex CommentPattern = llvm::Regex(
207 "^/[/*] *( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$", llvm::Regex::IgnoreCase);
210 if (!CommentPattern.match(Comment->Next->TokenText, &Groups))
212 NamespaceNameInComment = Groups.size() > 2 ? Groups[2] :
"";
214 return NamespaceNameInComment == NamespaceName;
217void addEndComment(
const FormatToken *RBraceTok, StringRef EndCommentText,
218 const SourceManager &SourceMgr,
219 tooling::Replacements *Fixes) {
220 auto EndLoc = RBraceTok->Tok.getEndLoc();
222 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
224 llvm::errs() <<
"Error while adding namespace end comment: "
225 << llvm::toString(std::move(Err)) <<
"\n";
229void updateEndComment(
const FormatToken *RBraceTok, StringRef EndCommentText,
230 const SourceManager &SourceMgr,
231 tooling::Replacements *Fixes) {
232 assert(hasEndComment(RBraceTok));
233 const FormatToken *Comment = RBraceTok->Next;
235 Comment->Tok.getEndLoc());
236 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
238 llvm::errs() <<
"Error while updating namespace end comment: "
239 << llvm::toString(std::move(Err)) <<
"\n";
247 if (!
Line->Affected ||
Line->InPPDirective || !
Line->startsWith(tok::r_brace))
249 size_t StartLineIndex =
Line->MatchingOpeningBlockLineIndex;
252 assert(StartLineIndex < AnnotatedLines.size());
253 const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
254 if (NamespaceTok->
is(tok::l_brace)) {
257 if (StartLineIndex > 0) {
258 NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
259 if (AnnotatedLines[StartLineIndex - 1]->endsWith(tok::semi))
271 return NamespaceTok ? NamespaceTok->
TokenText : StringRef();
290 Braces += Tok->
is(tok::l_brace) ? 1 : Tok->
is(tok::r_brace) ? -1 : 0;
300 std::string AllNamespaceNames;
302 StringRef NamespaceTokenText;
303 unsigned int CompactedNamespacesCount = 0;
304 for (
size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
317 if (RBraceTok->
Next && RBraceTok->
Next->
is(tok::semi))
318 EndCommentPrevTok = RBraceTok->
Next;
321 std::string NamespaceName = computeName(NamespaceTok);
323 if (CompactedNamespacesCount == 0)
324 NamespaceTokenText = NamespaceTok->
TokenText;
326 NamespaceTokenText ==
328 StartLineIndex - CompactedNamespacesCount - 1 ==
329 AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
330 !AnnotatedLines[I + 1]->
First->Finalized) {
331 if (hasEndComment(EndCommentPrevTok)) {
333 updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
335 ++CompactedNamespacesCount;
336 if (!NamespaceName.empty())
337 AllNamespaceNames =
"::" + NamespaceName + AllNamespaceNames;
340 NamespaceName += AllNamespaceNames;
341 CompactedNamespacesCount = 0;
342 AllNamespaceNames = std::string();
348 if (EndCommentNextTok && EndCommentNextTok->
is(tok::comment))
349 EndCommentNextTok = EndCommentNextTok->
Next;
350 if (!EndCommentNextTok && I + 1 < E)
351 EndCommentNextTok = AnnotatedLines[I + 1]->First;
352 bool AddNewline = EndCommentNextTok &&
354 EndCommentNextTok->
isNot(tok::eof);
355 const std::string EndCommentText =
356 computeEndCommentText(NamespaceName, AddNewline, NamespaceTok,
358 if (!hasEndComment(EndCommentPrevTok)) {
359 unsigned LineCount = 0;
360 for (
auto J = StartLineIndex + 1; J < I; ++J)
361 LineCount += AnnotatedLines[J]->size();
363 addEndComment(EndCommentPrevTok,
368 }
else if (!validEndComment(EndCommentPrevTok, NamespaceName,
370 updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
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.