[TargetVersion] Only enable on RISC-V and AArch64 (#115991)
[llvm-project.git] / clang-tools-extra / clang-tidy / readability / BracesAroundStatementsCheck.h
blob4cd37a7b2dd6cc169f9301d6a21b1613748399db
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
12 #include "../ClangTidyCheck.h"
14 namespace clang::tidy::readability {
16 /// Checks that bodies of `if` statements and loops (`for`, `range-for`,
17 /// `do-while`, and `while`) are inside braces
18 ///
19 /// Before:
20 ///
21 /// \code
22 /// if (condition)
23 /// statement;
24 /// \endcode
25 ///
26 /// After:
27 ///
28 /// \code
29 /// if (condition) {
30 /// statement;
31 /// }
32 /// \endcode
33 ///
34 /// Additionally, one can define an option `ShortStatementLines` defining the
35 /// minimal number of lines that the statement should have in order to trigger
36 /// this check.
37 ///
38 /// The number of lines is counted from the end of condition or initial keyword
39 /// (`do`/`else`) until the last line of the inner statement. Default value 0
40 /// means that braces will be added to all statements (not having them already).
41 class BracesAroundStatementsCheck : public ClangTidyCheck {
42 public:
43 BracesAroundStatementsCheck(StringRef Name, ClangTidyContext *Context);
44 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
45 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
46 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
47 void onEndOfTranslationUnit() override;
49 private:
50 bool checkStmt(const ast_matchers::MatchFinder::MatchResult &Result,
51 const Stmt *S, SourceLocation StartLoc,
52 SourceLocation EndLocHint = SourceLocation());
53 template <typename IfOrWhileStmt>
54 SourceLocation findRParenLoc(const IfOrWhileStmt *S, const SourceManager &SM,
55 const LangOptions &LangOpts);
56 std::optional<TraversalKind> getCheckTraversalKind() const override {
57 return TK_IgnoreUnlessSpelledInSource;
60 std::set<const Stmt *> ForceBracesStmts;
61 const unsigned ShortStatementLines;
64 } // namespace clang::tidy::readability
66 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H