Alexander Kornienko | f5e72b0 | 2015-04-10 19:26:43 +0000 | [diff] [blame^] | 1 | //===--- SimplifyBooleanExpr.h clang-tidy -----------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H |
| 12 | |
| 13 | #include "../ClangTidy.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
| 17 | namespace readability { |
| 18 | |
| 19 | /// \brief Looks for boolean expressions involving boolean constants and |
| 20 | // simplifies them to use the appropriate boolean expression directly. |
| 21 | /// |
| 22 | /// Examples: |
| 23 | /// `if (b == true)` becomes `if (b)` |
| 24 | /// `if (b == false)` becomes `if (!b)` |
| 25 | /// `if (b && true)` becomes `if (b)` |
| 26 | /// `if (b && false)` becomes `if (false)` |
| 27 | /// `if (b || true)` becomes `if (true)` |
| 28 | /// `if (b || false)` becomes `if (b)` |
| 29 | /// `e ? true : false` becomes `e` |
| 30 | /// `e ? false : true` becomes `!e` |
| 31 | /// `if (true) t(); else f();` becomes `t();` |
| 32 | /// `if (false) t(); else f();` becomes `f();` |
| 33 | /// `if (e) return true; else return false;` becomes `return (e);` |
| 34 | /// `if (e) return false; else return true;` becomes `return !(e);` |
| 35 | /// `if (e) b = true; else b = false;` becomes `b = e;` |
| 36 | /// `if (e) b = false; else b = true;` becomes `b = !(e);` |
| 37 | /// |
| 38 | class SimplifyBooleanExprCheck : public ClangTidyCheck { |
| 39 | public: |
| 40 | SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context) |
| 41 | : ClangTidyCheck(Name, Context) {} |
| 42 | void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 43 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
| 44 | |
| 45 | private: |
| 46 | void matchBoolBinOpExpr(ast_matchers::MatchFinder *Finder, bool Value, |
| 47 | StringRef OperatorName, StringRef BooleanId); |
| 48 | |
| 49 | void matchExprBinOpBool(ast_matchers::MatchFinder *Finder, bool Value, |
| 50 | StringRef OperatorName, StringRef BooleanId); |
| 51 | |
| 52 | void matchBoolCompOpExpr(ast_matchers::MatchFinder *Finder, bool Value, |
| 53 | StringRef OperatorName, StringRef BooleanId); |
| 54 | |
| 55 | void matchExprCompOpBool(ast_matchers::MatchFinder *Finder, bool Value, |
| 56 | StringRef OperatorName, StringRef BooleanId); |
| 57 | |
| 58 | void matchBoolCondition(ast_matchers::MatchFinder *Finder, bool Value, |
| 59 | StringRef BooleanId); |
| 60 | |
| 61 | void matchTernaryResult(ast_matchers::MatchFinder *Finder, bool Value, |
| 62 | StringRef TernaryId); |
| 63 | |
| 64 | void matchIfReturnsBool(ast_matchers::MatchFinder *Finder, bool Value, |
| 65 | StringRef Id); |
| 66 | |
| 67 | void matchIfAssignsBool(ast_matchers::MatchFinder *Finder, bool Value, |
| 68 | StringRef Id); |
| 69 | |
| 70 | void |
| 71 | replaceWithExpression(const ast_matchers::MatchFinder::MatchResult &Result, |
| 72 | const CXXBoolLiteralExpr *BoolLiteral, bool UseLHS, |
| 73 | bool Negated = false); |
| 74 | |
| 75 | void |
| 76 | replaceWithThenStatement(const ast_matchers::MatchFinder::MatchResult &Result, |
| 77 | const CXXBoolLiteralExpr *BoolLiteral); |
| 78 | |
| 79 | void |
| 80 | replaceWithElseStatement(const ast_matchers::MatchFinder::MatchResult &Result, |
| 81 | const CXXBoolLiteralExpr *FalseConditionRemoved); |
| 82 | |
| 83 | void |
| 84 | replaceWithCondition(const ast_matchers::MatchFinder::MatchResult &Result, |
| 85 | const ConditionalOperator *Ternary, |
| 86 | bool Negated = false); |
| 87 | |
| 88 | void replaceWithReturnCondition( |
| 89 | const ast_matchers::MatchFinder::MatchResult &Result, const IfStmt *If, |
| 90 | bool Negated = false); |
| 91 | |
| 92 | void |
| 93 | replaceWithAssignment(const ast_matchers::MatchFinder::MatchResult &Result, |
| 94 | const IfStmt *If, bool Negated = false); |
| 95 | }; |
| 96 | |
| 97 | } // namespace readability |
| 98 | } // namespace tidy |
| 99 | } // namespace clang |
| 100 | |
| 101 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H |