16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/Regex.h" 19 #define DEBUG_TYPE "namespace-end-comments-fixer" 27 static const int kShortNamespaceMaxLines = 1;
31 std::string computeName(
const FormatToken *NamespaceTok) {
32 assert(NamespaceTok &&
33 NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
34 "expecting a namespace token");
35 std::string
name =
"";
37 if (NamespaceTok->is(TT_NamespaceMacro)) {
40 assert(Tok && Tok->is(tok::l_paren) &&
"expected an opening parenthesis");
42 while (Tok && !Tok->isOneOf(tok::r_paren, tok::comma)) {
44 Tok = Tok->getNextNonComment();
48 while (Tok && !Tok->is(tok::l_brace)) {
49 name += Tok->TokenText;
50 Tok = Tok->getNextNonComment();
56 std::string computeEndCommentText(StringRef NamespaceName,
bool AddNewline,
57 const FormatToken *NamespaceTok) {
58 std::string
text =
"// ";
59 text += NamespaceTok->TokenText;
60 if (NamespaceTok->is(TT_NamespaceMacro))
62 else if (!NamespaceName.empty())
64 text += NamespaceName;
65 if (NamespaceTok->is(TT_NamespaceMacro))
72 bool hasEndComment(
const FormatToken *RBraceTok) {
73 return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
76 bool validEndComment(
const FormatToken *RBraceTok, StringRef NamespaceName,
77 const FormatToken *NamespaceTok) {
78 assert(hasEndComment(RBraceTok));
79 const FormatToken *Comment = RBraceTok->Next;
83 static llvm::Regex *
const NamespaceCommentPattern =
84 new llvm::Regex(
"^/[/*] *(end (of )?)? *(anonymous|unnamed)? *" 85 "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
86 llvm::Regex::IgnoreCase);
87 static llvm::Regex *
const NamespaceMacroCommentPattern =
88 new llvm::Regex(
"^/[/*] *(end (of )?)? *(anonymous|unnamed)? *" 89 "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*)\\)\\.? *(\\*/)?$",
90 llvm::Regex::IgnoreCase);
92 SmallVector<StringRef, 8> Groups;
93 if (NamespaceTok->is(TT_NamespaceMacro) &&
94 NamespaceMacroCommentPattern->match(Comment->TokenText, &Groups)) {
95 StringRef NamespaceTokenText = Groups.size() > 4 ? Groups[4] :
"";
97 if (NamespaceTokenText != NamespaceTok->TokenText)
99 }
else if (NamespaceTok->isNot(tok::kw_namespace) ||
100 !NamespaceCommentPattern->match(Comment->TokenText, &Groups)) {
104 StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] :
"";
106 if (NamespaceName.empty() && !NamespaceNameInComment.empty())
108 StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] :
"";
110 if (!NamespaceName.empty() && !AnonymousInComment.empty())
112 return NamespaceNameInComment == NamespaceName;
115 void addEndComment(
const FormatToken *RBraceTok, StringRef EndCommentText,
116 const SourceManager &SourceMgr,
117 tooling::Replacements *Fixes) {
118 auto EndLoc = RBraceTok->Tok.getEndLoc();
120 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
122 llvm::errs() <<
"Error while adding namespace end comment: " 127 void updateEndComment(
const FormatToken *RBraceTok, StringRef EndCommentText,
128 const SourceManager &SourceMgr,
129 tooling::Replacements *Fixes) {
130 assert(hasEndComment(RBraceTok));
131 const FormatToken *Comment = RBraceTok->Next;
133 Comment->Tok.getEndLoc());
134 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
136 llvm::errs() <<
"Error while updating namespace end comment: " 150 assert(StartLineIndex < AnnotatedLines.size());
151 const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
152 if (NamespaceTok->is(tok::l_brace)) {
155 if (StartLineIndex > 0)
156 NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
158 return NamespaceTok->getNamespaceToken();
165 return NamespaceTok ? NamespaceTok->
TokenText : StringRef();
178 std::string AllNamespaceNames =
"";
180 StringRef NamespaceTokenText;
181 unsigned int CompactedNamespacesCount = 0;
182 for (
size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
195 if (RBraceTok->
Next && RBraceTok->
Next->
is(tok::semi)) {
196 EndCommentPrevTok = RBraceTok->
Next;
200 std::string NamespaceName = computeName(NamespaceTok);
201 if (
Style.CompactNamespaces) {
202 if (CompactedNamespacesCount == 0)
203 NamespaceTokenText = NamespaceTok->
TokenText;
205 NamespaceTokenText ==
207 StartLineIndex - CompactedNamespacesCount - 1 ==
208 AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
209 !AnnotatedLines[I + 1]->
First->Finalized) {
210 if (hasEndComment(EndCommentPrevTok)) {
212 updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
214 CompactedNamespacesCount++;
215 AllNamespaceNames =
"::" + NamespaceName + AllNamespaceNames;
218 NamespaceName += AllNamespaceNames;
219 CompactedNamespacesCount = 0;
220 AllNamespaceNames = std::string();
226 if (EndCommentNextTok && EndCommentNextTok->
is(tok::comment))
227 EndCommentNextTok = EndCommentNextTok->
Next;
228 if (!EndCommentNextTok && I + 1 < E)
229 EndCommentNextTok = AnnotatedLines[I + 1]->First;
230 bool AddNewline = EndCommentNextTok &&
233 const std::string EndCommentText =
234 computeEndCommentText(NamespaceName, AddNewline, NamespaceTok);
235 if (!hasEndComment(EndCommentPrevTok)) {
236 bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
238 addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
239 }
else if (!validEndComment(EndCommentPrevTok, NamespaceName,
241 updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
const AnnotatedLine * Line
static CharSourceRange getCharRange(SourceRange R)
Dataflow Directional Tag Classes.
This class handles loading and caching of source files into memory.