clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

BracesAroundStatementsCheck.h
Go to the documentation of this file.
1 //===--- BracesAroundStatementsCheck.h - clang-tidy -------------*- 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
12
13 #include "../ClangTidy.h"
14
15 namespace clang {
16 namespace tidy {
17 namespace readability {
18
19 /// Checks that bodies of `if` statements and loops (`for`, `range-for`,
20 /// `do-while`, and `while`) are inside braces
21 ///
22 /// Before:
23 ///
24 /// \code
25 /// if (condition)
26 /// statement;
27 /// \endcode
28 ///
29 /// After:
30 ///
31 /// \code
32 /// if (condition) {
33 /// statement;
34 /// }
35 /// \endcode
36 ///
37 /// Additionally, one can define an option `ShortStatementLines` defining the
38 /// minimal number of lines that the statement should have in order to trigger
39 /// this check.
40 ///
41 /// The number of lines is counted from the end of condition or initial keyword
42 /// (`do`/`else`) until the last line of the inner statement. Default value 0
43 /// means that braces will be added to all statements (not having them already).
44 class BracesAroundStatementsCheck : public ClangTidyCheck {
45 public:
46 BracesAroundStatementsCheck(StringRef Name, ClangTidyContext *Context);
47 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
48 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
49 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
50 void onEndOfTranslationUnit() override;
51
52 private:
53 bool checkStmt(const ast_matchers::MatchFinder::MatchResult &Result,
54 const Stmt *S, SourceLocation StartLoc,
55 SourceLocation EndLocHint = SourceLocation());
56 template <typename IfOrWhileStmt>
57 SourceLocation findRParenLoc(const IfOrWhileStmt *S, const SourceManager &SM,
58 const ASTContext *Context);
59
60 private:
61 std::set<const Stmt*> ForceBracesStmts;
62 const unsigned ShortStatementLines;
63 };
64
65 } // namespace readability
66 } // namespace tidy
67 } // namespace clang
68
69 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
clang::tidy::readability::BracesAroundStatementsCheck::BracesAroundStatementsCheck
BracesAroundStatementsCheck(StringRef Name, ClangTidyContext *Context)
Definition: BracesAroundStatementsCheck.cpp:114
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
SM
SourceManager & SM
Definition: IncludeOrderCheck.cpp:44
clang::tidy::readability::BracesAroundStatementsCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: BracesAroundStatementsCheck.cpp:268
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:82
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::readability::BracesAroundStatementsCheck
Checks that bodies of if statements and loops (for, range-for, do-while, and while) are inside braces...
Definition: BracesAroundStatementsCheck.h:44
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