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 { 15 namespace tidy { 16 namespace readability { 17 18 /// Looks for boolean expressions involving boolean constants and simplifies 19 /// them to use the appropriate boolean expression directly. 20 /// 21 /// For the user-facing documentation see: 22 /// http://clang.llvm.org/extra/clang-tidy/checks/readability-simplify-boolean-expr.html 23 class SimplifyBooleanExprCheck : public ClangTidyCheck { 24 public: 25 SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context); 26 27 void storeOptions(ClangTidyOptions::OptionMap &Options) override; 28 void registerMatchers(ast_matchers::MatchFinder *Finder) override; 29 void check(const ast_matchers::MatchFinder::MatchResult &Result) override; 30 31 private: 32 class Visitor; 33 34 void reportBinOp(const ast_matchers::MatchFinder::MatchResult &Result, 35 const BinaryOperator *Op); 36 37 void matchBoolCondition(ast_matchers::MatchFinder *Finder, bool Value, 38 StringRef BooleanId); 39 40 void matchTernaryResult(ast_matchers::MatchFinder *Finder, bool Value, 41 StringRef TernaryId); 42 43 void matchIfReturnsBool(ast_matchers::MatchFinder *Finder, bool Value, 44 StringRef Id); 45 46 void matchIfAssignsBool(ast_matchers::MatchFinder *Finder, bool Value, 47 StringRef Id); 48 49 void matchCompoundIfReturnsBool(ast_matchers::MatchFinder *Finder, bool Value, 50 StringRef Id); 51 52 void 53 replaceWithThenStatement(const ast_matchers::MatchFinder::MatchResult &Result, 54 const Expr *BoolLiteral); 55 56 void 57 replaceWithElseStatement(const ast_matchers::MatchFinder::MatchResult &Result, 58 const Expr *FalseConditionRemoved); 59 60 void 61 replaceWithCondition(const ast_matchers::MatchFinder::MatchResult &Result, 62 const ConditionalOperator *Ternary, 63 bool Negated = false); 64 65 void replaceWithReturnCondition( 66 const ast_matchers::MatchFinder::MatchResult &Result, const IfStmt *If, 67 bool Negated = false); 68 69 void 70 replaceWithAssignment(const ast_matchers::MatchFinder::MatchResult &Result, 71 const IfStmt *If, bool Negated = false); 72 73 void replaceCompoundReturnWithCondition( 74 const ast_matchers::MatchFinder::MatchResult &Result, 75 const CompoundStmt *Compound, bool Negated = false); 76 77 void issueDiag(const ast_matchers::MatchFinder::MatchResult &Result, 78 SourceLocation Loc, StringRef Description, 79 SourceRange ReplacementRange, StringRef Replacement); 80 81 const bool ChainedConditionalReturn; 82 const bool ChainedConditionalAssignment; 83 }; 84 85 } // namespace readability 86 } // namespace tidy 87 } // namespace clang 88 89 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H 90