Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:21

0001 //===--- BracesAroundStatementsCheck.h - clang-tidy -------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 
0014 namespace clang::tidy::readability {
0015 
0016 /// Checks that bodies of `if` statements and loops (`for`, `range-for`,
0017 /// `do-while`, and `while`) are inside braces
0018 ///
0019 /// Before:
0020 ///
0021 /// \code
0022 ///   if (condition)
0023 ///     statement;
0024 /// \endcode
0025 ///
0026 /// After:
0027 ///
0028 /// \code
0029 ///   if (condition) {
0030 ///     statement;
0031 ///   }
0032 /// \endcode
0033 ///
0034 /// Additionally, one can define an option `ShortStatementLines` defining the
0035 /// minimal number of lines that the statement should have in order to trigger
0036 /// this check.
0037 ///
0038 /// The number of lines is counted from the end of condition or initial keyword
0039 /// (`do`/`else`) until the last line of the inner statement.  Default value 0
0040 /// means that braces will be added to all statements (not having them already).
0041 class BracesAroundStatementsCheck : public ClangTidyCheck {
0042 public:
0043   BracesAroundStatementsCheck(StringRef Name, ClangTidyContext *Context);
0044   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0045   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0046   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0047   void onEndOfTranslationUnit() override;
0048 
0049 private:
0050   bool checkStmt(const ast_matchers::MatchFinder::MatchResult &Result,
0051                  const Stmt *S, SourceLocation StartLoc,
0052                  SourceLocation EndLocHint = SourceLocation());
0053   template <typename IfOrWhileStmt>
0054   SourceLocation findRParenLoc(const IfOrWhileStmt *S, const SourceManager &SM,
0055                                const LangOptions &LangOpts);
0056   std::optional<TraversalKind> getCheckTraversalKind() const override {
0057     return TK_IgnoreUnlessSpelledInSource;
0058   }
0059 
0060   std::set<const Stmt *> ForceBracesStmts;
0061   const unsigned ShortStatementLines;
0062 };
0063 
0064 } // namespace clang::tidy::readability
0065 
0066 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H