17 #include "llvm/Support/Debug.h" 18 #include "llvm/Support/Regex.h" 20 #define DEBUG_TYPE "namespace-end-comments-fixer" 28 static const int kShortNamespaceMaxLines = 1;
32 std::string computeName(
const FormatToken *NamespaceTok) {
33 assert(NamespaceTok && NamespaceTok->is(tok::kw_namespace) &&
34 "expecting a namespace token");
35 std::string name =
"";
38 while (Tok && !Tok->is(tok::l_brace)) {
40 Tok = Tok->getNextNonComment();
45 std::string computeEndCommentText(StringRef NamespaceName,
bool AddNewline) {
46 std::string
text =
"// namespace";
47 if (!NamespaceName.empty()) {
49 text += NamespaceName;
56 bool hasEndComment(
const FormatToken *RBraceTok) {
57 return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
60 bool validEndComment(
const FormatToken *RBraceTok, StringRef NamespaceName) {
61 assert(hasEndComment(RBraceTok));
62 const FormatToken *Comment = RBraceTok->Next;
66 static llvm::Regex *
const NamespaceCommentPattern =
67 new llvm::Regex(
"^/[/*] *(end (of )?)? *(anonymous|unnamed)? *" 68 "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
69 llvm::Regex::IgnoreCase);
70 SmallVector<StringRef, 7> Groups;
71 if (NamespaceCommentPattern->match(Comment->TokenText, &Groups)) {
72 StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] :
"";
74 if (NamespaceName.empty() && !NamespaceNameInComment.empty())
76 StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] :
"";
78 if (!NamespaceName.empty() && !AnonymousInComment.empty())
80 return NamespaceNameInComment == NamespaceName;
85 void addEndComment(
const FormatToken *RBraceTok, StringRef EndCommentText,
86 const SourceManager &SourceMgr,
87 tooling::Replacements *Fixes) {
88 auto EndLoc = RBraceTok->Tok.getEndLoc();
90 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
92 llvm::errs() <<
"Error while adding namespace end comment: " 97 void updateEndComment(
const FormatToken *RBraceTok, StringRef EndCommentText,
98 const SourceManager &SourceMgr,
99 tooling::Replacements *Fixes) {
100 assert(hasEndComment(RBraceTok));
101 const FormatToken *Comment = RBraceTok->Next;
103 Comment->Tok.getEndLoc());
104 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
106 llvm::errs() <<
"Error while updating namespace end comment: " 120 assert(StartLineIndex < AnnotatedLines.size());
121 const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
122 if (NamespaceTok->is(tok::l_brace)) {
125 if (StartLineIndex > 0)
126 NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
128 return NamespaceTok->getNamespaceToken();
141 std::string AllNamespaceNames =
"";
143 unsigned int CompactedNamespacesCount = 0;
144 for (
size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
157 if (RBraceTok->
Next && RBraceTok->
Next->
is(tok::semi)) {
158 EndCommentPrevTok = RBraceTok->
Next;
162 std::string NamespaceName = computeName(NamespaceTok);
166 StartLineIndex - CompactedNamespacesCount - 1 ==
167 AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
168 !AnnotatedLines[I + 1]->
First->Finalized) {
169 if (hasEndComment(EndCommentPrevTok)) {
171 updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
173 CompactedNamespacesCount++;
174 AllNamespaceNames =
"::" + NamespaceName + AllNamespaceNames;
177 NamespaceName += AllNamespaceNames;
178 CompactedNamespacesCount = 0;
179 AllNamespaceNames = std::string();
185 if (EndCommentNextTok && EndCommentNextTok->
is(tok::comment))
186 EndCommentNextTok = EndCommentNextTok->
Next;
187 if (!EndCommentNextTok && I + 1 < E)
188 EndCommentNextTok = AnnotatedLines[I + 1]->First;
189 bool AddNewline = EndCommentNextTok &&
192 const std::string EndCommentText =
193 computeEndCommentText(NamespaceName, AddNewline);
194 if (!hasEndComment(EndCommentPrevTok)) {
195 bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
197 addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
198 }
else if (!validEndComment(EndCommentPrevTok, NamespaceName)) {
199 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.