blob: e3a106dd30b88ec2387fc7625174d9232ba40f4a [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///
22/// Examples:
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000023///
24/// =========================================== ================
25/// Initial expression Result
26/// ------------------------------------------- ----------------
27/// `if (b == true)` `if (b)`
28/// `if (b == false)` `if (!b)`
29/// `if (b && true)` `if (b)`
30/// `if (b && false)` `if (false)`
31/// `if (b || true)` `if (true)`
32/// `if (b || false)` `if (b)`
33/// `e ? true : false` `e`
34/// `e ? false : true` `!e`
35/// `if (true) t(); else f();` `t();`
36/// `if (false) t(); else f();` `f();`
37/// `if (e) return true; else return false;` `return e;`
38/// `if (e) return false; else return true;` `return !e;`
39/// `if (e) b = true; else b = false;` `b = e;`
40/// `if (e) b = false; else b = true;` `b = !e;`
41/// `if (e) return true; return false;` `return e;`
42/// `if (e) return false; return true;` `return !e;`
43/// =========================================== ================
Alexander Kornienkofb3e2cd2015-05-17 12:31:12 +000044///
Alexander Kornienko6ae400d2015-07-01 12:39:40 +000045/// The resulting expression `e` is modified as follows:
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000046/// 1. Unnecessary parentheses around the expression are removed.
47/// 2. Negated applications of `!` are eliminated.
48/// 3. Negated applications of comparison operators are changed to use the
49/// opposite condition.
50/// 4. Implicit conversions of pointer to `bool` are replaced with explicit
51/// comparisons to `nullptr`.
52/// 5. Implicit casts to `bool` are replaced with explicit casts to `bool`.
53/// 6. Object expressions with `explicit operator bool` conversion operators
54/// are replaced with explicit casts to `bool`.
Alexander Kornienko6ae400d2015-07-01 12:39:40 +000055///
56/// Examples:
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000057/// 1. The ternary assignment `bool b = (i < 0) ? true : false;` has redundant
58/// parentheses and becomes `bool b = i < 0;`.
Alexander Kornienko6ae400d2015-07-01 12:39:40 +000059///
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000060/// 2. The conditional return `if (!b) return false; return true;` has an
61/// implied double negation and becomes `return b;`.
Alexander Kornienko6ae400d2015-07-01 12:39:40 +000062///
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000063/// 3. The conditional return `if (i < 0) return false; return true;` becomes
64/// `return i >= 0;`.
Alexander Kornienko6ae400d2015-07-01 12:39:40 +000065///
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000066/// The conditional return `if (i != 0) return false; return true;` becomes
67/// `return i == 0;`.
Alexander Kornienko6ae400d2015-07-01 12:39:40 +000068///
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000069/// 4. The conditional return `if (p) return true; return false;` has an
70/// implicit conversion of a pointer to `bool` and becomes
71/// `return p != nullptr;`.
Alexander Kornienko6ae400d2015-07-01 12:39:40 +000072///
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000073/// The ternary assignment `bool b = (i & 1) ? true : false;` has an
74/// implicit conversion of `i & 1` to `bool` and becomes
75/// `bool b = static_cast<bool>(i & 1);`.
76///
77/// 5. The conditional return `if (i & 1) return true; else return false;` has
78/// an implicit conversion of an integer quantity `i & 1` to `bool` and
79/// becomes `return static_cast<bool>(i & 1);`
80///
81/// 6. Given `struct X { explicit operator bool(); };`, and an instance `x` of
82/// `struct X`, the conditional return `if (x) return true; return false;`
83/// becomes `return static_cast<bool>(x);`
Alexander Kornienkofb3e2cd2015-05-17 12:31:12 +000084///
85/// When a conditional boolean return or assignment appears at the end of a
86/// chain of `if`, `else if` statements, the conditional statement is left
87/// unchanged unless the option `ChainedConditionalReturn` or
88/// `ChainedConditionalAssignment`, respectively, is specified as non-zero.
89/// The default value for both options is zero.
Alexander Kornienkof5e72b02015-04-10 19:26:43 +000090///
91class SimplifyBooleanExprCheck : public ClangTidyCheck {
92public:
Alexander Kornienkofb3e2cd2015-05-17 12:31:12 +000093 SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context);
94
95 void storeOptions(ClangTidyOptions::OptionMap &Options) override;
Alexander Kornienkof5e72b02015-04-10 19:26:43 +000096 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
97 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
98
99private:
100 void matchBoolBinOpExpr(ast_matchers::MatchFinder *Finder, bool Value,
101 StringRef OperatorName, StringRef BooleanId);
102
103 void matchExprBinOpBool(ast_matchers::MatchFinder *Finder, bool Value,
104 StringRef OperatorName, StringRef BooleanId);
105
106 void matchBoolCompOpExpr(ast_matchers::MatchFinder *Finder, bool Value,
107 StringRef OperatorName, StringRef BooleanId);
108
109 void matchExprCompOpBool(ast_matchers::MatchFinder *Finder, bool Value,
110 StringRef OperatorName, StringRef BooleanId);
111
112 void matchBoolCondition(ast_matchers::MatchFinder *Finder, bool Value,
113 StringRef BooleanId);
114
115 void matchTernaryResult(ast_matchers::MatchFinder *Finder, bool Value,
116 StringRef TernaryId);
117
118 void matchIfReturnsBool(ast_matchers::MatchFinder *Finder, bool Value,
119 StringRef Id);
120
121 void matchIfAssignsBool(ast_matchers::MatchFinder *Finder, bool Value,
122 StringRef Id);
123
Alexander Kornienko6ae400d2015-07-01 12:39:40 +0000124 void matchCompoundIfReturnsBool(ast_matchers::MatchFinder *Finder, bool Value,
125 StringRef Id);
126
Alexander Kornienkof5e72b02015-04-10 19:26:43 +0000127 void
128 replaceWithExpression(const ast_matchers::MatchFinder::MatchResult &Result,
129 const CXXBoolLiteralExpr *BoolLiteral, bool UseLHS,
130 bool Negated = false);
131
132 void
133 replaceWithThenStatement(const ast_matchers::MatchFinder::MatchResult &Result,
134 const CXXBoolLiteralExpr *BoolLiteral);
135
136 void
137 replaceWithElseStatement(const ast_matchers::MatchFinder::MatchResult &Result,
138 const CXXBoolLiteralExpr *FalseConditionRemoved);
139
140 void
141 replaceWithCondition(const ast_matchers::MatchFinder::MatchResult &Result,
142 const ConditionalOperator *Ternary,
143 bool Negated = false);
144
145 void replaceWithReturnCondition(
146 const ast_matchers::MatchFinder::MatchResult &Result, const IfStmt *If,
147 bool Negated = false);
148
149 void
150 replaceWithAssignment(const ast_matchers::MatchFinder::MatchResult &Result,
151 const IfStmt *If, bool Negated = false);
Alexander Kornienkofb3e2cd2015-05-17 12:31:12 +0000152
Alexander Kornienko6ae400d2015-07-01 12:39:40 +0000153 void replaceCompoundReturnWithCondition(
154 const ast_matchers::MatchFinder::MatchResult &Result,
155 const CompoundStmt *Compound, bool Negated = false);
156
Alexander Kornienkofb3e2cd2015-05-17 12:31:12 +0000157 const bool ChainedConditionalReturn;
158 const bool ChainedConditionalAssignment;
Alexander Kornienkof5e72b02015-04-10 19:26:43 +0000159};
160
161} // namespace readability
162} // namespace tidy
163} // namespace clang
164
165#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_SIMPLIFY_BOOLEAN_EXPR_H