67 lines · c
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H11 12#include "../ClangTidyCheck.h"13 14namespace clang::tidy::readability {15 16/// Checks that bodies of `if` statements and loops (`for`, `range-for`,17/// `do-while`, and `while`) are inside braces18///19/// Before:20///21/// \code22/// if (condition)23/// statement;24/// \endcode25///26/// After:27///28/// \code29/// if (condition) {30/// statement;31/// }32/// \endcode33///34/// Additionally, one can define an option `ShortStatementLines` defining the35/// minimal number of lines that the statement should have in order to trigger36/// this check.37///38/// The number of lines is counted from the end of condition or initial keyword39/// (`do`/`else`) until the last line of the inner statement. Default value 040/// means that braces will be added to all statements (not having them already).41class BracesAroundStatementsCheck : public ClangTidyCheck {42public: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;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::readability65 66#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H67