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;
getCheckTraversalKind()30   llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
31     return TK_IgnoreUnlessSpelledInSource;
32   }
33 
34 private:
35   class Visitor;
36 
37   void reportBinOp(const ASTContext &Context, const BinaryOperator *Op);
38 
39   void replaceWithThenStatement(const ASTContext &Context,
40                                 const IfStmt *IfStatement,
41                                 const Expr *BoolLiteral);
42 
43   void replaceWithElseStatement(const ASTContext &Context,
44                                 const IfStmt *IfStatement,
45                                 const Expr *BoolLiteral);
46 
47   void replaceWithCondition(const ASTContext &Context,
48                             const ConditionalOperator *Ternary, bool Negated);
49 
50   void replaceWithReturnCondition(const ASTContext &Context, const IfStmt *If,
51                                   const Expr *BoolLiteral, bool Negated);
52 
53   void replaceWithAssignment(const ASTContext &Context, const IfStmt *If,
54                              const Expr *Var, SourceLocation Loc, bool Negated);
55 
56   void replaceCompoundReturnWithCondition(const ASTContext &Context,
57                                           const ReturnStmt *Ret, bool Negated,
58                                           const IfStmt *If,
59                                           const Expr *ThenReturn);
60 
61   bool reportDeMorgan(const ASTContext &Context, const UnaryOperator *Outer,
62                       const BinaryOperator *Inner, bool TryOfferFix,
63                       const Stmt *Parent, const ParenExpr *Parens);
64 
65   void issueDiag(const ASTContext &Result, SourceLocation Loc,
66                  StringRef Description, SourceRange ReplacementRange,
67                  StringRef Replacement);
68 
69   const bool ChainedConditionalReturn;
70   const bool ChainedConditionalAssignment;
71   const bool SimplifyDeMorgan;
72   const bool SimplifyDeMorganRelaxed;
73 };
74 
75 } // namespace readability
76 } // namespace tidy
77 } // namespace clang
78 
79 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H
80