clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

BracesAroundStatementsCheck.cpp
Go to the documentation of this file.
1 //===--- BracesAroundStatementsCheck.cpp - clang-tidy ---------------------===//
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 #include "BracesAroundStatementsCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/Lex/Lexer.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19 namespace readability {
20 namespace {
21
22 tok::TokenKind getTokenKind(SourceLocation Loc, const SourceManager &SM,
23 const ASTContext *Context) {
24 Token Tok;
25 SourceLocation Beginning =
26 Lexer::GetBeginningOfToken(Loc, SM, Context->getLangOpts());
27 const bool Invalid =
28 Lexer::getRawToken(Beginning, Tok, SM, Context->getLangOpts());
29 assert(!Invalid && "Expected a valid token.");
30
31 if (Invalid)
32 return tok::NUM_TOKENS;
33
34 return Tok.getKind();
35 }
36
37 SourceLocation forwardSkipWhitespaceAndComments(SourceLocation Loc,
38 const SourceManager &SM,
39 const ASTContext *Context) {
40 assert(Loc.isValid());
41 for (;;) {
42 while (isWhitespace(*FullSourceLoc(Loc, SM).getCharacterData()))
43 Loc = Loc.getLocWithOffset(1);
44
45 tok::TokenKind TokKind = getTokenKind(Loc, SM, Context);
46 if (TokKind == tok::NUM_TOKENS || TokKind != tok::comment)
47 return Loc;
48
49 // Fast-forward current token.
50 Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, Context->getLangOpts());
51 }
52 }
53
54 SourceLocation findEndLocation(SourceLocation LastTokenLoc,
55 const SourceManager &SM,
56 const ASTContext *Context) {
57 SourceLocation Loc = LastTokenLoc;
58 // Loc points to the beginning of the last (non-comment non-ws) token
59 // before end or ';'.
60 assert(Loc.isValid());
61 bool SkipEndWhitespaceAndComments = true;
62 tok::TokenKind TokKind = getTokenKind(Loc, SM, Context);
63 if (TokKind == tok::NUM_TOKENS || TokKind == tok::semi ||
64 TokKind == tok::r_brace) {
65 // If we are at ";" or "}", we found the last token. We could use as well
66 // `if (isa<NullStmt>(S))`, but it wouldn't work for nested statements.
67 SkipEndWhitespaceAndComments = false;
68 }
69
70 Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, Context->getLangOpts());
71 // Loc points past the last token before end or after ';'.
72
73 if (SkipEndWhitespaceAndComments) {
74 Loc = forwardSkipWhitespaceAndComments(Loc, SM, Context);
75 tok::TokenKind TokKind = getTokenKind(Loc, SM, Context);
76 if (TokKind == tok::semi)
77 Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, Context->getLangOpts());
78 }
79
80 for (;;) {
81 assert(Loc.isValid());
82 while (isHorizontalWhitespace(*FullSourceLoc(Loc, SM).getCharacterData()))
83 Loc = Loc.getLocWithOffset(1);
84
85 if (isVerticalWhitespace(*FullSourceLoc(Loc, SM).getCharacterData())) {
86 // EOL, insert brace before.
87 break;
88 }
89 tok::TokenKind TokKind = getTokenKind(Loc, SM, Context);
90 if (TokKind != tok::comment) {
91 // Non-comment token, insert brace before.
92 break;
93 }
94
95 SourceLocation TokEndLoc =
96 Lexer::getLocForEndOfToken(Loc, 0, SM, Context->getLangOpts());
97 SourceRange TokRange(Loc, TokEndLoc);
98 StringRef Comment = Lexer::getSourceText(
99 CharSourceRange::getTokenRange(TokRange), SM, Context->getLangOpts());
100 if (Comment.startswith("/*") && Comment.find('\n') != StringRef::npos) {
101 // Multi-line block comment, insert brace before.
102 break;
103 }
104 // else: Trailing comment, insert brace after the newline.
105
106 // Fast-forward current token.
107 Loc = TokEndLoc;
108 }
109 return Loc;
110 }
111
112 } // namespace
113
114 BracesAroundStatementsCheck::BracesAroundStatementsCheck(
115 StringRef Name, ClangTidyContext *Context)
116 : ClangTidyCheck(Name, Context),
117 // Always add braces by default.
118 ShortStatementLines(Options.get("ShortStatementLines", 0U)) {}
119
120 void
121 BracesAroundStatementsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
122 Options.store(Opts, "ShortStatementLines", ShortStatementLines);
123 }
124
125 void BracesAroundStatementsCheck::registerMatchers(MatchFinder *Finder) {
126 Finder->addMatcher(ifStmt().bind("if"), this);
127 Finder->addMatcher(whileStmt().bind("while"), this);
128 Finder->addMatcher(doStmt().bind("do"), this);
129 Finder->addMatcher(forStmt().bind("for"), this);
130 Finder->addMatcher(cxxForRangeStmt().bind("for-range"), this);
131 }
132
133 void
134 BracesAroundStatementsCheck::check(const MatchFinder::MatchResult &Result) {
135 const SourceManager &SM = *Result.SourceManager;
136 const ASTContext *Context = Result.Context;
137
138 // Get location of closing parenthesis or 'do' to insert opening brace.
139 if (auto S = Result.Nodes.getNodeAs<ForStmt>("for")) {
140 checkStmt(Result, S->getBody(), S->getRParenLoc());
141 } else if (auto S = Result.Nodes.getNodeAs<CXXForRangeStmt>("for-range")) {
142 checkStmt(Result, S->getBody(), S->getRParenLoc());
143 } else if (auto S = Result.Nodes.getNodeAs<DoStmt>("do")) {
144 checkStmt(Result, S->getBody(), S->getDoLoc(), S->getWhileLoc());
145 } else if (auto S = Result.Nodes.getNodeAs<WhileStmt>("while")) {
146 SourceLocation StartLoc = findRParenLoc(S, SM, Context);
147 if (StartLoc.isInvalid())
148 return;
149 checkStmt(Result, S->getBody(), StartLoc);
150 } else if (auto S = Result.Nodes.getNodeAs<IfStmt>("if")) {
151 SourceLocation StartLoc = findRParenLoc(S, SM, Context);
152 if (StartLoc.isInvalid())
153 return;
154 if (ForceBracesStmts.erase(S))
155 ForceBracesStmts.insert(S->getThen());
156 bool BracedIf = checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
157 const Stmt *Else = S->getElse();
158 if (Else && BracedIf)
159 ForceBracesStmts.insert(Else);
160 if (Else && !isa<IfStmt>(Else)) {
161 // Omit 'else if' statements here, they will be handled directly.
162 checkStmt(Result, Else, S->getElseLoc(), SourceLocation());
163 }
164 } else {
165 llvm_unreachable("Invalid match");
166 }
167 }
168
169 /// Find location of right parenthesis closing condition
170 template <typename IfOrWhileStmt>
171 SourceLocation
172 BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S,
173 const SourceManager &SM,
174 const ASTContext *Context) {
175 // Skip macros.
176 if (S->getLocStart().isMacroID())
177 return SourceLocation();
178
179 SourceLocation CondEndLoc = S->getCond()->getLocEnd();
180 if (const DeclStmt *CondVar = S->getConditionVariableDeclStmt())
181 CondEndLoc = CondVar->getLocEnd();
182
183 assert(CondEndLoc.isValid());
184 SourceLocation PastCondEndLoc =
185 Lexer::getLocForEndOfToken(CondEndLoc, 0, SM, Context->getLangOpts());
186 if (PastCondEndLoc.isInvalid())
187 return SourceLocation();
188 SourceLocation RParenLoc =
189 forwardSkipWhitespaceAndComments(PastCondEndLoc, SM, Context);
190 if (RParenLoc.isInvalid())
191 return SourceLocation();
192 tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, Context);
193 if (TokKind != tok::r_paren)
194 return SourceLocation();
195 return RParenLoc;
196 }
197
198 /// Determine if the statement needs braces around it, and add them if it does.
199 /// Returns true if braces where added.
200 bool BracesAroundStatementsCheck::checkStmt(
201 const MatchFinder::MatchResult &Result, const Stmt *S,
202 SourceLocation InitialLoc, SourceLocation EndLocHint) {
203 // 1) If there's a corresponding "else" or "while", the check inserts "} "
204 // right before that token.
205 // 2) If there's a multi-line block comment starting on the same line after
206 // the location we're inserting the closing brace at, or there's a non-comment
207 // token, the check inserts "\n}" right before that token.
208 // 3) Otherwise the check finds the end of line (possibly after some block or
209 // line comments) and inserts "\n}" right before that EOL.
210 if (!S || isa<CompoundStmt>(S)) {
211 // Already inside braces.
212 return false;
213 }
214
215 const SourceManager &SM = *Result.SourceManager;
216 const ASTContext *Context = Result.Context;
217
218 // Treat macros.
219 CharSourceRange FileRange = Lexer::makeFileCharRange(
220 CharSourceRange::getTokenRange(S->getSourceRange()), SM,
221 Context->getLangOpts());
222 if (FileRange.isInvalid())
223 return false;
224
225 // InitialLoc points at the last token before opening brace to be inserted.
226 assert(InitialLoc.isValid());
227 // Convert InitialLoc to file location, if it's on the same macro expansion
228 // level as the start of the statement. We also need file locations for
229 // Lexer::getLocForEndOfToken working properly.
230 InitialLoc = Lexer::makeFileCharRange(
231 CharSourceRange::getCharRange(InitialLoc, S->getLocStart()),
232 SM, Context->getLangOpts())
233 .getBegin();
234 if (InitialLoc.isInvalid())
235 return false;
236 SourceLocation StartLoc =
237 Lexer::getLocForEndOfToken(InitialLoc, 0, SM, Context->getLangOpts());
238
239 // StartLoc points at the location of the opening brace to be inserted.
240 SourceLocation EndLoc;
241 std::string ClosingInsertion;
242 if (EndLocHint.isValid()) {
243 EndLoc = EndLocHint;
244 ClosingInsertion = "} ";
245 } else {
246 const auto FREnd = FileRange.getEnd().getLocWithOffset(-1);
247 EndLoc = findEndLocation(FREnd, SM, Context);
248 ClosingInsertion = "\n}";
249 }
250
251 assert(StartLoc.isValid());
252 assert(EndLoc.isValid());
253 // Don't require braces for statements spanning less than certain number of
254 // lines.
255 if (ShortStatementLines && !ForceBracesStmts.erase(S)) {
256 unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
257 unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
258 if (EndLine - StartLine < ShortStatementLines)
259 return false;
260 }
261
262 auto Diag = diag(StartLoc, "statement should be inside braces");
263 Diag << FixItHint::CreateInsertion(StartLoc, " {")
264 << FixItHint::CreateInsertion(EndLoc, ClosingInsertion);
265 return true;
266 }
267
268 void BracesAroundStatementsCheck::onEndOfTranslationUnit() {
269 ForceBracesStmts.clear();
270 }
271
272 } // namespace readability
273 } // namespace tidy
274 } // namespace clang
Loc
SourceLocation Loc
'#' location in the include directive
Definition: IncludeOrderCheck.cpp:34
clang::tidy::readability::BracesAroundStatementsCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers with Finder.
Definition: BracesAroundStatementsCheck.cpp:125
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:102
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:158
SM
SourceManager & SM
Definition: IncludeOrderCheck.cpp:44
clang::tidy::readability::BracesAroundStatementsCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: BracesAroundStatementsCheck.cpp:268
clang::tidy::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:344
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:82
BracesAroundStatementsCheck.h
clang::tidy::readability::BracesAroundStatementsCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: BracesAroundStatementsCheck.cpp:121
Context
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:124
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:323
clang::tidy::readability::BracesAroundStatementsCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: BracesAroundStatementsCheck.cpp:134
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121