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 static llvm::Regex kNamespaceCommentPattern =
33 llvm::Regex(
"^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
34 "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
35 llvm::Regex::IgnoreCase);
39 std::string computeName(
const FormatToken *NamespaceTok) {
40 assert(NamespaceTok && NamespaceTok->is(tok::kw_namespace) &&
41 "expecting a namespace token");
42 std::string name =
"";
44 const FormatToken *Tok = NamespaceTok->getNextNonComment();
45 while (Tok && !Tok->is(tok::l_brace)) {
46 name += Tok->TokenText;
47 Tok = Tok->getNextNonComment();
52 std::string computeEndCommentText(StringRef NamespaceName,
bool AddNewline) {
53 std::string
text =
"// namespace";
54 if (!NamespaceName.empty()) {
56 text += NamespaceName;
63 bool hasEndComment(
const FormatToken *RBraceTok) {
64 return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
67 bool validEndComment(
const FormatToken *RBraceTok, StringRef NamespaceName) {
68 assert(hasEndComment(RBraceTok));
69 const FormatToken *Comment = RBraceTok->Next;
70 SmallVector<StringRef, 7> Groups;
71 if (kNamespaceCommentPattern.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,
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: "
112 getNamespaceToken(
const AnnotatedLine *line,
114 if (!line->Affected || line->InPPDirective || !line->startsWith(tok::r_brace))
116 size_t StartLineIndex = line->MatchingOpeningBlockLineIndex;
119 assert(StartLineIndex < AnnotatedLines.size());
120 const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
122 if (NamespaceTok->is(tok::kw_inline))
123 NamespaceTok = NamespaceTok->getNextNonComment();
124 if (!NamespaceTok || NamespaceTok->isNot(tok::kw_namespace))
139 AnnotatedLines.end());
141 std::string AllNamespaceNames =
"";
143 unsigned int CompactedNamespacesCount = 0;
144 for (
size_t I = 0,
E = AnnotatedLines.size();
I !=
E; ++
I) {
147 getNamespaceToken(EndLine, AnnotatedLines);
157 if (RBraceTok->
Next && RBraceTok->
Next->
is(tok::semi)) {
158 EndCommentPrevTok = RBraceTok->
Next;
162 std::string NamespaceName = computeName(NamespaceTok);
165 getNamespaceToken(AnnotatedLines[
I + 1], AnnotatedLines) &&
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);
detail::InMemoryDirectory::const_iterator I
static CharSourceRange getCharRange(SourceRange R)
ArrayRef< FormatToken * > Tokens
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T-> getSizeExpr()))
detail::InMemoryDirectory::const_iterator E
std::string toString(const til::SExpr *E)
This class handles loading and caching of source files into memory.