Line data Source code
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 0 : 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
|