mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-29 08:36:07 +00:00
[analyzer] Start fixing modeling of bool based types
This is a follow up for one of the previous diffs https://reviews.llvm.org/D32328. getTypeSize and with getIntWidth are not equivalent for bool (see https://clang.llvm.org/doxygen/ASTContext_8cpp_source.html#l08444), this causes a number of issues (for instance, if APint X representing a bool is created with the wrong bit width then X is not comparable against Min/Max (because of the different bit width), that results in crashes (triggered asserts) inside assume* methods), for examples see the newly added test cases. Test plan: make check-all Differential revision: https://reviews.llvm.org/D35041 llvm-svn: 307604
This commit is contained in:
parent
3136a4d5d6
commit
59d10a4cbc
@ -124,7 +124,7 @@ public:
|
|||||||
/// Returns the type of the APSInt used to store values of the given QualType.
|
/// Returns the type of the APSInt used to store values of the given QualType.
|
||||||
APSIntType getAPSIntType(QualType T) const {
|
APSIntType getAPSIntType(QualType T) const {
|
||||||
assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
|
assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
|
||||||
return APSIntType(Ctx.getTypeSize(T),
|
return APSIntType(Ctx.getIntWidth(T),
|
||||||
!T->isSignedIntegerOrEnumerationType());
|
!T->isSignedIntegerOrEnumerationType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,18 +71,15 @@ SVal SimpleSValBuilder::dispatchCast(SVal Val, QualType CastTy) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SVal SimpleSValBuilder::evalCastFromNonLoc(NonLoc val, QualType castTy) {
|
SVal SimpleSValBuilder::evalCastFromNonLoc(NonLoc val, QualType castTy) {
|
||||||
|
|
||||||
bool isLocType = Loc::isLocType(castTy);
|
bool isLocType = Loc::isLocType(castTy);
|
||||||
|
|
||||||
if (val.getAs<nonloc::PointerToMember>())
|
if (val.getAs<nonloc::PointerToMember>())
|
||||||
return val;
|
return val;
|
||||||
|
|
||||||
if (Optional<nonloc::LocAsInteger> LI = val.getAs<nonloc::LocAsInteger>()) {
|
if (Optional<nonloc::LocAsInteger> LI = val.getAs<nonloc::LocAsInteger>()) {
|
||||||
if (isLocType)
|
if (isLocType)
|
||||||
return LI->getLoc();
|
return LI->getLoc();
|
||||||
|
|
||||||
// FIXME: Correctly support promotions/truncations.
|
// FIXME: Correctly support promotions/truncations.
|
||||||
unsigned castSize = Context.getTypeSize(castTy);
|
unsigned castSize = Context.getIntWidth(castTy);
|
||||||
if (castSize == LI->getNumBits())
|
if (castSize == LI->getNumBits())
|
||||||
return val;
|
return val;
|
||||||
return makeLocAsInteger(LI->getLoc(), castSize);
|
return makeLocAsInteger(LI->getLoc(), castSize);
|
||||||
@ -173,7 +170,7 @@ SVal SimpleSValBuilder::evalCastFromLoc(Loc val, QualType castTy) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (castTy->isIntegralOrEnumerationType()) {
|
if (castTy->isIntegralOrEnumerationType()) {
|
||||||
unsigned BitWidth = Context.getTypeSize(castTy);
|
unsigned BitWidth = Context.getIntWidth(castTy);
|
||||||
|
|
||||||
if (!val.getAs<loc::ConcreteInt>())
|
if (!val.getAs<loc::ConcreteInt>())
|
||||||
return makeLocAsInteger(val, BitWidth);
|
return makeLocAsInteger(val, BitWidth);
|
||||||
|
@ -37,3 +37,33 @@ bool testNoCrashOnSwitchEnumBool(EnumBool E) {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool testNoCrashOnSwitchEnumBoolConstant() {
|
||||||
|
EnumBool E = EnumBool::F;
|
||||||
|
switch (E) {
|
||||||
|
case EnumBool::F:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef __INTPTR_TYPE__ intptr_t;
|
||||||
|
bool testNoCrashOnSwitchEnumBoolConstantCastedFromNullptr() {
|
||||||
|
EnumBool E = static_cast<EnumBool>((intptr_t)nullptr);
|
||||||
|
switch (E) {
|
||||||
|
case EnumBool::F:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool testNoCrashOnSwitchEnumBoolConstantCastedFromPtr() {
|
||||||
|
int X;
|
||||||
|
intptr_t P = (intptr_t)&X;
|
||||||
|
EnumBool E = static_cast<EnumBool>(P);
|
||||||
|
switch (E) {
|
||||||
|
case EnumBool::F:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user