clang-tools 20.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
15
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).
42public:
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;
48
49private:
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;
58 }
59
60 std::set<const Stmt *> ForceBracesStmts;
61 const unsigned ShortStatementLines;
62};
63
64} // namespace clang::tidy::readability
65
66#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
llvm::SmallString< 256U > Name
Base class for all clang-tidy checks.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Checks that bodies of if statements and loops (for, range-for, do-while, and while) are inside braces...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
llvm::StringMap< ClangTidyValue > OptionMap