SimplifyBooleanExprCheck.h 3.1 KB

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