2014-10-03 03:09:56 +08:00
|
|
|
//===--- BracesAroundStatementsCheck.h - clang-tidy -------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-10-03 03:09:56 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-10-15 18:51:57 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
|
2014-10-03 03:09:56 +08:00
|
|
|
|
2019-03-25 20:38:26 +08:00
|
|
|
#include "../ClangTidyCheck.h"
|
2014-10-03 03:09:56 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2014-10-15 18:51:57 +08:00
|
|
|
namespace readability {
|
2014-10-03 03:09:56 +08:00
|
|
|
|
2015-08-28 02:01:58 +08:00
|
|
|
/// Checks that bodies of `if` statements and loops (`for`, `range-for`,
|
|
|
|
/// `do-while`, and `while`) are inside braces
|
2014-10-13 20:46:22 +08:00
|
|
|
///
|
|
|
|
/// Before:
|
2015-08-28 02:01:58 +08:00
|
|
|
///
|
|
|
|
/// \code
|
|
|
|
/// if (condition)
|
|
|
|
/// statement;
|
|
|
|
/// \endcode
|
2014-10-13 20:46:22 +08:00
|
|
|
///
|
|
|
|
/// After:
|
2015-08-28 02:01:58 +08:00
|
|
|
///
|
|
|
|
/// \code
|
|
|
|
/// if (condition) {
|
|
|
|
/// statement;
|
|
|
|
/// }
|
|
|
|
/// \endcode
|
2014-10-13 20:46:22 +08:00
|
|
|
///
|
|
|
|
/// Additionally, one can define an option `ShortStatementLines` defining the
|
|
|
|
/// minimal number of lines that the statement should have in order to trigger
|
|
|
|
/// this check.
|
2015-08-28 02:01:58 +08:00
|
|
|
///
|
2014-10-13 20:46:22 +08:00
|
|
|
/// The number of lines is counted from the end of condition or initial keyword
|
2015-08-28 02:01:58 +08:00
|
|
|
/// (`do`/`else`) until the last line of the inner statement. Default value 0
|
|
|
|
/// means that braces will be added to all statements (not having them already).
|
2014-10-03 03:09:56 +08:00
|
|
|
class BracesAroundStatementsCheck : public ClangTidyCheck {
|
|
|
|
public:
|
2014-10-13 20:46:22 +08:00
|
|
|
BracesAroundStatementsCheck(StringRef Name, ClangTidyContext *Context);
|
|
|
|
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
|
2014-10-03 03:09:56 +08:00
|
|
|
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
|
|
|
|
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
|
2015-06-05 00:36:58 +08:00
|
|
|
void onEndOfTranslationUnit() override;
|
2014-10-03 03:09:56 +08:00
|
|
|
|
|
|
|
private:
|
2015-03-31 21:53:03 +08:00
|
|
|
bool checkStmt(const ast_matchers::MatchFinder::MatchResult &Result,
|
2014-10-03 03:09:56 +08:00
|
|
|
const Stmt *S, SourceLocation StartLoc,
|
2015-06-05 00:36:58 +08:00
|
|
|
SourceLocation EndLocHint = SourceLocation());
|
2014-10-03 03:09:56 +08:00
|
|
|
template <typename IfOrWhileStmt>
|
|
|
|
SourceLocation findRParenLoc(const IfOrWhileStmt *S, const SourceManager &SM,
|
|
|
|
const ASTContext *Context);
|
2014-10-13 20:46:22 +08:00
|
|
|
|
|
|
|
private:
|
2016-11-08 15:50:19 +08:00
|
|
|
std::set<const Stmt *> ForceBracesStmts;
|
2014-10-13 20:46:22 +08:00
|
|
|
const unsigned ShortStatementLines;
|
2014-10-03 03:09:56 +08:00
|
|
|
};
|
|
|
|
|
2014-10-15 18:51:57 +08:00
|
|
|
} // namespace readability
|
2014-10-03 03:09:56 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|
|
|
|
|
2014-10-15 18:51:57 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_BRACESAROUNDSTATEMENTSCHECK_H
|