blob: 4a8076582a4e0f5d31bb3181c2c802aa22973c7c [file] [log] [blame]
Alexander Kornienkof5e72b02015-04-10 19:26:43 +00001//===--- 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
15namespace clang {
16namespace tidy {
17namespace readability {
18
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000019/// Looks for boolean expressions involving boolean constants and simplifies
20/// them to use the appropriate boolean expression directly.
Alexander Kornienkof5e72b02015-04-10 19:26:43 +000021///
Aaron Ballmanf034a8c2016-02-12 15:09:05 +000022/// For the user-facing documentation see:
23/// http://clang.llvm.org/extra/clang-tidy/checks/readability-simplify-boolean-expr.html
Alexander Kornienkof5e72b02015-04-10 19:26:43 +000024class SimplifyBooleanExprCheck : public ClangTidyCheck {
25public:
Alexander Kornienkofb3e2cd2015-05-17 12:31:12 +000026 SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context);
27
28 void storeOptions(ClangTidyOptions::OptionMap &Options) override;
Alexander Kornienkof5e72b02015-04-10 19:26:43 +000029 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
30 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31
32private:
33 void matchBoolBinOpExpr(ast_matchers::MatchFinder *Finder, bool Value,
34 StringRef OperatorName, StringRef BooleanId);
35
36 void matchExprBinOpBool(ast_matchers::MatchFinder *Finder, bool Value,
37 StringRef OperatorName, StringRef BooleanId);
38
39 void matchBoolCompOpExpr(ast_matchers::MatchFinder *Finder, bool Value,
40 StringRef OperatorName, StringRef BooleanId);
41
42 void matchExprCompOpBool(ast_matchers::MatchFinder *Finder, bool Value,
43 StringRef OperatorName, StringRef BooleanId);
44
45 void matchBoolCondition(ast_matchers::MatchFinder *Finder, bool Value,
46 StringRef BooleanId);
47
48 void matchTernaryResult(ast_matchers::MatchFinder *Finder, bool Value,
49 StringRef TernaryId);
50
51 void matchIfReturnsBool(ast_matchers::MatchFinder *Finder, bool Value,
52 StringRef Id);
53
54 void matchIfAssignsBool(ast_matchers::MatchFinder *Finder, bool Value,
55 StringRef Id);
56
Alexander Kornienko6ae400d2015-07-01 12:39:40 +000057 void matchCompoundIfReturnsBool(ast_matchers::MatchFinder *Finder, bool Value,
58 StringRef Id);
59
Alexander Kornienkof5e72b02015-04-10 19:26:43 +000060 void
61 replaceWithExpression(const ast_matchers::MatchFinder::MatchResult &Result,
62 const CXXBoolLiteralExpr *BoolLiteral, bool UseLHS,
63 bool Negated = false);
64
65 void
66 replaceWithThenStatement(const ast_matchers::MatchFinder::MatchResult &Result,
67 const CXXBoolLiteralExpr *BoolLiteral);
68
69 void
70 replaceWithElseStatement(const ast_matchers::MatchFinder::MatchResult &Result,
71 const CXXBoolLiteralExpr *FalseConditionRemoved);
72
73 void
74 replaceWithCondition(const ast_matchers::MatchFinder::MatchResult &Result,
75 const ConditionalOperator *Ternary,
76 bool Negated = false);
77
78 void replaceWithReturnCondition(
79 const ast_matchers::MatchFinder::MatchResult &Result, const IfStmt *If,
80 bool Negated = false);
81
82 void
83 replaceWithAssignment(const ast_matchers::MatchFinder::MatchResult &Result,
84 const IfStmt *If, bool Negated = false);
Alexander Kornienkofb3e2cd2015-05-17 12:31:12 +000085
Alexander Kornienko6ae400d2015-07-01 12:39:40 +000086 void replaceCompoundReturnWithCondition(
87 const ast_matchers::MatchFinder::MatchResult &Result,
88 const CompoundStmt *Compound, bool Negated = false);
89
Alexander Kornienko4f74ec02015-12-28 13:21:22 +000090 void issueDiag(const ast_matchers::MatchFinder::MatchResult &Result,
91 SourceLocation Loc, StringRef Description,
92 SourceRange ReplacementRange, StringRef Replacement);
93
Alexander Kornienkofb3e2cd2015-05-17 12:31:12 +000094 const bool ChainedConditionalReturn;
95 const bool ChainedConditionalAssignment;
Alexander Kornienkof5e72b02015-04-10 19:26:43 +000096};
97
98} // namespace readability
99} // namespace tidy
100} // namespace clang
101
102#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H