@@ -127,11 +127,7 @@ const Expr *Expr::skipRValueSubobjectAdjustments(
127
127
return E;
128
128
}
129
129
130
- // / isKnownToHaveBooleanValue - Return true if this is an integer expression
131
- // / that is known to return 0 or 1. This happens for _Bool/bool expressions
132
- // / but also int expressions which are produced by things like comparisons in
133
- // / C.
134
- bool Expr::isKnownToHaveBooleanValue () const {
130
+ bool Expr::isKnownToHaveBooleanValue (bool Semantic) const {
135
131
const Expr *E = IgnoreParens ();
136
132
137
133
// If this value has _Bool type, it is obvious 0/1.
@@ -142,7 +138,7 @@ bool Expr::isKnownToHaveBooleanValue() const {
142
138
if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
143
139
switch (UO->getOpcode ()) {
144
140
case UO_Plus:
145
- return UO->getSubExpr ()->isKnownToHaveBooleanValue ();
141
+ return UO->getSubExpr ()->isKnownToHaveBooleanValue (Semantic );
146
142
case UO_LNot:
147
143
return true ;
148
144
default :
@@ -152,8 +148,9 @@ bool Expr::isKnownToHaveBooleanValue() const {
152
148
153
149
// Only look through implicit casts. If the user writes
154
150
// '(int) (a && b)' treat it as an arbitrary int.
151
+ // FIXME: Should we look through any cast expression in !Semantic mode?
155
152
if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
156
- return CE->getSubExpr ()->isKnownToHaveBooleanValue ();
153
+ return CE->getSubExpr ()->isKnownToHaveBooleanValue (Semantic );
157
154
158
155
if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
159
156
switch (BO->getOpcode ()) {
@@ -172,27 +169,27 @@ bool Expr::isKnownToHaveBooleanValue() const {
172
169
case BO_Xor: // Bitwise XOR operator.
173
170
case BO_Or: // Bitwise OR operator.
174
171
// Handle things like (x==2)|(y==12).
175
- return BO->getLHS ()->isKnownToHaveBooleanValue () &&
176
- BO->getRHS ()->isKnownToHaveBooleanValue ();
172
+ return BO->getLHS ()->isKnownToHaveBooleanValue (Semantic ) &&
173
+ BO->getRHS ()->isKnownToHaveBooleanValue (Semantic );
177
174
178
175
case BO_Comma:
179
176
case BO_Assign:
180
- return BO->getRHS ()->isKnownToHaveBooleanValue ();
177
+ return BO->getRHS ()->isKnownToHaveBooleanValue (Semantic );
181
178
}
182
179
}
183
180
184
181
if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
185
- return CO->getTrueExpr ()->isKnownToHaveBooleanValue () &&
186
- CO->getFalseExpr ()->isKnownToHaveBooleanValue ();
182
+ return CO->getTrueExpr ()->isKnownToHaveBooleanValue (Semantic ) &&
183
+ CO->getFalseExpr ()->isKnownToHaveBooleanValue (Semantic );
187
184
188
185
if (isa<ObjCBoolLiteralExpr>(E))
189
186
return true ;
190
187
191
188
if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E))
192
- return OVE->getSourceExpr ()->isKnownToHaveBooleanValue ();
189
+ return OVE->getSourceExpr ()->isKnownToHaveBooleanValue (Semantic );
193
190
194
191
if (const FieldDecl *FD = E->getSourceBitField ())
195
- if (FD->getType ()->isUnsignedIntegerType () &&
192
+ if (!Semantic && FD->getType ()->isUnsignedIntegerType () &&
196
193
!FD->getBitWidth ()->isValueDependent () &&
197
194
FD->getBitWidthValue (FD->getASTContext ()) == 1 )
198
195
return true ;
0 commit comments