clang-tools  14.0.0git
BracesAroundStatementsCheck.h
Go to the documentation of this file.
1 //===--- BracesAroundStatementsCheck.h - clang-tidy -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
11 
12 #include "../ClangTidyCheck.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace readability {
17 
18 /// Checks that bodies of `if` statements and loops (`for`, `range-for`,
19 /// `do-while`, and `while`) are inside braces
20 ///
21 /// Before:
22 ///
23 /// \code
24 /// if (condition)
25 /// statement;
26 /// \endcode
27 ///
28 /// After:
29 ///
30 /// \code
31 /// if (condition) {
32 /// statement;
33 /// }
34 /// \endcode
35 ///
36 /// Additionally, one can define an option `ShortStatementLines` defining the
37 /// minimal number of lines that the statement should have in order to trigger
38 /// this check.
39 ///
40 /// The number of lines is counted from the end of condition or initial keyword
41 /// (`do`/`else`) until the last line of the inner statement. Default value 0
42 /// means that braces will be added to all statements (not having them already).
44 public:
46  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
47  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
48  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
49  void onEndOfTranslationUnit() override;
50 
51 private:
52  bool checkStmt(const ast_matchers::MatchFinder::MatchResult &Result,
53  const Stmt *S, SourceLocation StartLoc,
54  SourceLocation EndLocHint = SourceLocation());
55  template <typename IfOrWhileStmt>
56  SourceLocation findRParenLoc(const IfOrWhileStmt *S, const SourceManager &SM,
57  const ASTContext *Context);
58  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
59  return TK_IgnoreUnlessSpelledInSource;
60  }
61 
62 private:
63  std::set<const Stmt *> ForceBracesStmts;
64  const unsigned ShortStatementLines;
65 };
66 
67 } // namespace readability
68 } // namespace tidy
69 } // namespace clang
70 
71 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::readability::BracesAroundStatementsCheck::BracesAroundStatementsCheck
BracesAroundStatementsCheck(StringRef Name, ClangTidyContext *Context)
Definition: BracesAroundStatementsCheck.cpp:96
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:43
clang::tidy::readability::BracesAroundStatementsCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: BracesAroundStatementsCheck.cpp:264
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
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:115
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
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:102
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::readability::BracesAroundStatementsCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: BracesAroundStatementsCheck.cpp:107