clang 8.0.0

NamespaceEndCommentsFixer.cpp
Go to the documentation of this file.
1 //===--- NamespaceEndCommentsFixer.cpp --------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements NamespaceEndCommentsFixer, a TokenAnalyzer that
12 /// fixes namespace end comments.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #include "NamespaceEndCommentsFixer.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/Regex.h"
19
20 #define DEBUG_TYPE "namespace-end-comments-fixer"
21
22 namespace clang {
23 namespace format {
24
25 namespace {
26 // The maximal number of unwrapped lines that a short namespace spans.
27 // Short namespaces don't need an end comment.
28 static const int kShortNamespaceMaxLines = 1;
29
30 // Computes the name of a namespace given the namespace token.
31 // Returns "" for anonymous namespace.
32 std::string computeName(const FormatToken *NamespaceTok) {
33 assert(NamespaceTok && NamespaceTok->is(tok::kw_namespace) &&
34 "expecting a namespace token");
35 std::string name = "";
36 // Collects all the non-comment tokens between 'namespace' and '{'.
37 const FormatToken *Tok = NamespaceTok->getNextNonComment();
38 while (Tok && !Tok->is(tok::l_brace)) {
39 name += Tok->TokenText;
40 Tok = Tok->getNextNonComment();
41 }
42 return name;
43 }
44
45 std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline) {
46 std::string text = "// namespace";
47 if (!NamespaceName.empty()) {
48 text += ' ';
49 text += NamespaceName;
50 }
51 if (AddNewline)
52 text += '\n';
53 return text;
54 }
55
56 bool hasEndComment(const FormatToken *RBraceTok) {
57 return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
58 }
59
60 bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName) {
61 assert(hasEndComment(RBraceTok));
62 const FormatToken *Comment = RBraceTok->Next;
63
64 // Matches a valid namespace end comment.
65 // Valid namespace end comments don't need to be edited.
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] : "";
73 // Anonymous namespace comments must not mention a namespace name.
74 if (NamespaceName.empty() && !NamespaceNameInComment.empty())
75 return false;
76 StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] : "";
77 // Named namespace comments must not mention anonymous namespace.
78 if (!NamespaceName.empty() && !AnonymousInComment.empty())
79 return false;
80 return NamespaceNameInComment == NamespaceName;
81 }
82 return false;
83 }
84
85 void addEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
86 const SourceManager &SourceMgr,
87 tooling::Replacements *Fixes) {
88 auto EndLoc = RBraceTok->Tok.getEndLoc();
89 auto Range = CharSourceRange::getCharRange(EndLoc, EndLoc);
90 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
91 if (Err) {
92 llvm::errs() << "Error while adding namespace end comment: "
93 << llvm::toString(std::move(Err)) << "\n";
94 }
95 }
96
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;
102 auto Range = CharSourceRange::getCharRange(Comment->getStartOfNonWhitespace(),
103 Comment->Tok.getEndLoc());
104 auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
105 if (Err) {
106 llvm::errs() << "Error while updating namespace end comment: "
107 << llvm::toString(std::move(Err)) << "\n";
108 }
109 }
110 } // namespace
111
112 const FormatToken *
113 getNamespaceToken(const AnnotatedLine *Line,
114 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
115 if (!Line->Affected || Line->InPPDirective || !Line->startsWith(tok::r_brace))
116 return nullptr;
117 size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;
118 if (StartLineIndex == UnwrappedLine::kInvalidIndex)
119 return nullptr;
120 assert(StartLineIndex < AnnotatedLines.size());
121 const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
122 if (NamespaceTok->is(tok::l_brace)) {
123 // "namespace" keyword can be on the line preceding '{', e.g. in styles
124 // where BraceWrapping.AfterNamespace is true.
125 if (StartLineIndex > 0)
126 NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
127 }
128 return NamespaceTok->getNamespaceToken();
129 }
130
131 NamespaceEndCommentsFixer::NamespaceEndCommentsFixer(const Environment &Env,
132 const FormatStyle &Style)
133 : TokenAnalyzer(Env, Style) {}
134
135 std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(
136 TokenAnnotator &Annotator, SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
137 FormatTokenLexer &Tokens) {
138 const SourceManager &SourceMgr = Env.getSourceManager();
139 AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
140 tooling::Replacements Fixes;
141 std::string AllNamespaceNames = "";
142 size_t StartLineIndex = SIZE_MAX;
143 unsigned int CompactedNamespacesCount = 0;
144 for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
145 const AnnotatedLine *EndLine = AnnotatedLines[I];
146 const FormatToken *NamespaceTok =
147 getNamespaceToken(EndLine, AnnotatedLines);
148 if (!NamespaceTok)
149 continue;
150 FormatToken *RBraceTok = EndLine->First;
151 if (RBraceTok->Finalized)
152 continue;
153 RBraceTok->Finalized = true;
154 const FormatToken *EndCommentPrevTok = RBraceTok;
155 // Namespaces often end with '};'. In that case, attach namespace end
156 // comments to the semicolon tokens.
157 if (RBraceTok->Next && RBraceTok->Next->is(tok::semi)) {
158 EndCommentPrevTok = RBraceTok->Next;
159 }
160 if (StartLineIndex == SIZE_MAX)
161 StartLineIndex = EndLine->MatchingOpeningBlockLineIndex;
162 std::string NamespaceName = computeName(NamespaceTok);
163 if (Style.CompactNamespaces) {
164 if ((I + 1 < E) &&
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)) {
170 // remove end comment, it will be merged in next one
171 updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
172 }
173 CompactedNamespacesCount++;
174 AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
175 continue;
176 }
177 NamespaceName += AllNamespaceNames;
178 CompactedNamespacesCount = 0;
179 AllNamespaceNames = std::string();
180 }
181 // The next token in the token stream after the place where the end comment
182 // token must be. This is either the next token on the current line or the
183 // first token on the next line.
184 const FormatToken *EndCommentNextTok = EndCommentPrevTok->Next;
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 &&
190 EndCommentNextTok->NewlinesBefore == 0 &&
191 EndCommentNextTok->isNot(tok::eof);
192 const std::string EndCommentText =
193 computeEndCommentText(NamespaceName, AddNewline);
194 if (!hasEndComment(EndCommentPrevTok)) {
195 bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
196 if (!isShort)
197 addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
198 } else if (!validEndComment(EndCommentPrevTok, NamespaceName)) {
199 updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
200 }
201 StartLineIndex = SIZE_MAX;
202 }
203 return {Fixes, 0};
204 }
205
206 } // namespace format
207 } // namespace clang
clang::if
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
clang::format::NamespaceEndCommentsFixer::NamespaceEndCommentsFixer
NamespaceEndCommentsFixer(const Environment &Env, const FormatStyle &Style)
Definition: NamespaceEndCommentsFixer.cpp:131
clang::format::FormatToken::getNextNonComment
const FormatToken * getNextNonComment() const
Returns the next token ignoring comments.
Definition: FormatToken.h:481
clang::tooling::Replacements
Maintains a set of replacements that are conflict-free.
Definition: Replacement.h:210
clang::format::AnnotatedLine::InPPDirective
bool InPPDirective
Definition: TokenAnnotator.h:124
clang::format::TokenAnalyzer::AffectedRangeMgr
AffectedRangeManager AffectedRangeMgr
Definition: TokenAnalyzer.h:105
clang::format::FormatToken::NewlinesBefore
unsigned NewlinesBefore
The number of newlines immediately before the Token.
Definition: FormatToken.h:134
clang::format::FormatToken::Next
FormatToken * Next
The next token in the unwrapped line.
Definition: FormatToken.h:293
clang::format::AnnotatedLine::MatchingOpeningBlockLineIndex
size_t MatchingOpeningBlockLineIndex
Definition: TokenAnnotator.h:122
clang::format::TokenAnalyzer::Env
const Environment & Env
Definition: TokenAnalyzer.h:103
toString
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
Definition: SanitizerArgs.cpp:775
clang::format::getNamespaceToken
const FormatToken * getNamespaceToken(const AnnotatedLine *Line, const SmallVectorImpl< AnnotatedLine *> &AnnotatedLines)
Definition: NamespaceEndCommentsFixer.cpp:113
clang::format::TokenAnalyzer::Style
FormatStyle Style
Definition: TokenAnalyzer.h:101
llvm::SmallVectorImpl
Definition: LLVM.h:37
clang::format::FormatToken::isNot
bool isNot(T Kind) const
Definition: FormatToken.h:323
clang::format::Tok
const FormatToken & Tok
Definition: BreakableToken.h:244
clang::ComparisonCategoryType::First
clang::format::FormatTokenLexer
Definition: FormatTokenLexer.h:38
clang::format::Environment::getSourceManager
const SourceManager & getSourceManager() const
Definition: TokenAnalyzer.h:54
clang::format::TokenAnnotator
Determines extra information about the tokens comprising an UnwrappedLine.
Definition: TokenAnnotator.h:150
Line
const AnnotatedLine * Line
Definition: UsingDeclarationsSorter.cpp:69
clang::format::UnwrappedLine::kInvalidIndex
static const size_t kInvalidIndex
Definition: UnwrappedLineParser.h:62
clang::format::FormatToken
A wrapper around a Token storing information about the whitespace characters preceding it...
Definition: FormatToken.h:124
clang::format::TokenAnalyzer
Definition: TokenAnalyzer.h:85
clang::format::AnnotatedLine
Definition: TokenAnnotator.h:38
clang::format::FormatStyle::CompactNamespaces
bool CompactNamespaces
If true, consecutive namespace declarations will be on the same line.
Definition: Format.h:948
clang::format::AnnotatedLine::Affected
bool Affected
True if this line should be formatted, i.e.
Definition: TokenAnnotator.h:131
clang::CharSourceRange::getCharRange
static CharSourceRange getCharRange(SourceRange R)
Definition: SourceLocation.h:248
clang::format::NamespaceEndCommentsFixer::analyze
std::pair< tooling::Replacements, unsigned > analyze(TokenAnnotator &Annotator, SmallVectorImpl< AnnotatedLine *> &AnnotatedLines, FormatTokenLexer &Tokens) override
Definition: NamespaceEndCommentsFixer.cpp:135
clang::format::FormatToken::is
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:307
clang::format::AnnotatedLine::First
FormatToken * First
Definition: TokenAnnotator.h:115
clang::format::AnnotatedLine::startsWith
bool startsWith(Ts... Tokens) const
true if this line starts with the given tokens in order, ignoring comments.
Definition: TokenAnnotator.h:87
clang::format::FormatToken::TokenText
StringRef TokenText
The raw text of the token.
Definition: FormatToken.h:172
SIZE_MAX
#define SIZE_MAX
Definition: stdint.h:664
clang::format::FormatStyle
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:50
clang::format::AffectedRangeManager::computeAffectedLines
bool computeAffectedLines(SmallVectorImpl< AnnotatedLine *> &Lines)
Definition: AffectedRangeManager.cpp:23
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::format::FormatToken::Finalized
bool Finalized
If true, this token has been fully formatted (indented and potentially re-formatted inside)...
Definition: FormatToken.h:305
clang::comments::tok::text
Definition: CommentLexer.h:36
clang::comments::tok::eof
Definition: CommentLexer.h:34
NamespaceEndCommentsFixer.h
This file declares NamespaceEndCommentsFixer, a TokenAnalyzer that fixes namespace end comments...
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582
clang::format::Environment
Definition: TokenAnalyzer.h:37
clang::format::Style
const FormatStyle & Style
Definition: BreakableToken.h:247