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();
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());
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;
129 if (NamespaceTok->is(tok::kw_inline))
130 NamespaceTok = NamespaceTok->getNextNonComment();
131 if (!NamespaceTok || NamespaceTok->isNot(tok::kw_namespace))
146 std::string AllNamespaceNames =
"";
148 unsigned int CompactedNamespacesCount = 0;
149 for (
size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
162 if (RBraceTok->
Next && RBraceTok->
Next->
is(tok::semi)) {
163 EndCommentPrevTok = RBraceTok->
Next;
167 std::string NamespaceName = computeName(NamespaceTok);
171 StartLineIndex - CompactedNamespacesCount - 1 ==
172 AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
173 !AnnotatedLines[I + 1]->
First->Finalized) {
174 if (hasEndComment(EndCommentPrevTok)) {
176 updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
178 CompactedNamespacesCount++;
179 AllNamespaceNames =
"::" + NamespaceName + AllNamespaceNames;
182 NamespaceName += AllNamespaceNames;
183 CompactedNamespacesCount = 0;
184 AllNamespaceNames = std::string();
190 if (EndCommentNextTok && EndCommentNextTok->
is(tok::comment))
191 EndCommentNextTok = EndCommentNextTok->
Next;
192 if (!EndCommentNextTok && I + 1 < E)
193 EndCommentNextTok = AnnotatedLines[I + 1]->First;
194 bool AddNewline = EndCommentNextTok &&
197 const std::string EndCommentText =
198 computeEndCommentText(NamespaceName, AddNewline);
199 if (!hasEndComment(EndCommentPrevTok)) {
200 bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
202 addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
203 }
else if (!validEndComment(EndCommentPrevTok, NamespaceName)) {
204 updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
const AnnotatedLine * Line
static CharSourceRange getCharRange(SourceRange R)
Dataflow Directional Tag Classes.
std::string toString(const til::SExpr *E)
This class handles loading and caching of source files into memory.