1 //===--- SimplifyBooleanExpr.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_SIMPLIFY_BOOLEAN_EXPR_H 10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H 11 12 #include "../ClangTidyCheck.h" 13 14 namespace clang::tidy::readability { 15 16 /// Looks for boolean expressions involving boolean constants and simplifies 17 /// them to use the appropriate boolean expression directly. 18 /// 19 /// For the user-facing documentation see: 20 /// http://clang.llvm.org/extra/clang-tidy/checks/readability/simplify-boolean-expr.html 21 class SimplifyBooleanExprCheck : public ClangTidyCheck { 22 public: 23 SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context); 24 25 void storeOptions(ClangTidyOptions::OptionMap &Opts) override; 26 void registerMatchers(ast_matchers::MatchFinder *Finder) override; 27 void check(const ast_matchers::MatchFinder::MatchResult &Result) override; getCheckTraversalKind()28 std::optional<TraversalKind> getCheckTraversalKind() const override { 29 return TK_IgnoreUnlessSpelledInSource; 30 } 31 32 private: 33 class Visitor; 34 35 void reportBinOp(const ASTContext &Context, const BinaryOperator *Op); 36 37 void replaceWithThenStatement(const ASTContext &Context, 38 const IfStmt *IfStatement, 39 const Expr *BoolLiteral); 40 41 void replaceWithElseStatement(const ASTContext &Context, 42 const IfStmt *IfStatement, 43 const Expr *BoolLiteral); 44 45 void replaceWithCondition(const ASTContext &Context, 46 const ConditionalOperator *Ternary, bool Negated); 47 48 void replaceWithReturnCondition(const ASTContext &Context, const IfStmt *If, 49 const Expr *BoolLiteral, bool Negated); 50 51 void replaceWithAssignment(const ASTContext &Context, const IfStmt *If, 52 const Expr *Var, SourceLocation Loc, bool Negated); 53 54 void replaceCompoundReturnWithCondition(const ASTContext &Context, 55 const ReturnStmt *Ret, bool Negated, 56 const IfStmt *If, 57 const Expr *ThenReturn); 58 59 bool reportDeMorgan(const ASTContext &Context, const UnaryOperator *Outer, 60 const BinaryOperator *Inner, bool TryOfferFix, 61 const Stmt *Parent, const ParenExpr *Parens); 62 63 void issueDiag(const ASTContext &Context, SourceLocation Loc, 64 StringRef Description, SourceRange ReplacementRange, 65 StringRef Replacement); 66 67 const bool ChainedConditionalReturn; 68 const bool ChainedConditionalAssignment; 69 const bool SimplifyDeMorgan; 70 const bool SimplifyDeMorganRelaxed; 71 }; 72 73 } // namespace clang::tidy::readability 74 75 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H 76