Skip to content

Commit 8d4ccfe

Browse files
committed
Merging r369760:
------------------------------------------------------------------------ r369760 | szelethus | 2019-08-23 16:21:13 +0200 (Fri, 23 Aug 2019) | 13 lines [analyzer] Avoid unnecessary enum range check on LValueToRValue casts Summary: EnumCastOutOfRangeChecker should not perform enum range checks on LValueToRValue casts, since this type of cast does not actually change the underlying type. Performing the unnecessary check actually triggered an assertion failure deeper in EnumCastOutOfRange for certain input (which is captured in the accompanying test code). Reviewers: #clang, Szelethus, gamesh411, NoQ Reviewed By: Szelethus, gamesh411, NoQ Subscribers: NoQ, gamesh411, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, dkrupp, Charusso, bjope, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66014 ------------------------------------------------------------------------ llvm-svn: 371058
1 parent ab62fa5 commit 8d4ccfe

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ void EnumCastOutOfRangeChecker::reportWarning(CheckerContext &C) const {
9191

9292
void EnumCastOutOfRangeChecker::checkPreStmt(const CastExpr *CE,
9393
CheckerContext &C) const {
94+
95+
// Only perform enum range check on casts where such checks are valid. For
96+
// all other cast kinds (where enum range checks are unnecessary or invalid),
97+
// just return immediately. TODO: The set of casts whitelisted for enum
98+
// range checking may be incomplete. Better to add a missing cast kind to
99+
// enable a missing check than to generate false negatives and have to remove
100+
// those later.
101+
switch (CE->getCastKind()) {
102+
case CK_IntegralCast:
103+
break;
104+
105+
default:
106+
return;
107+
break;
108+
}
109+
94110
// Get the value of the expression to cast.
95111
const llvm::Optional<DefinedOrUnknownSVal> ValueToCast =
96112
C.getSVal(CE->getSubExpr()).getAs<DefinedOrUnknownSVal>();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %clang_analyze_cc1 \
2+
// RUN: -analyzer-checker=core,alpha.cplusplus.EnumCastOutOfRange \
3+
// RUN: -verify %s
4+
5+
enum En_t {
6+
En_0 = -4,
7+
En_1,
8+
En_2 = 1,
9+
En_3,
10+
En_4 = 4
11+
};
12+
13+
void unscopedUnspecifiedCStyle() {
14+
enum En_t Below = (enum En_t)(-5); // expected-warning {{not in the valid range}}
15+
enum En_t NegVal1 = (enum En_t)(-4); // OK.
16+
enum En_t NegVal2 = (enum En_t)(-3); // OK.
17+
enum En_t InRange1 = (enum En_t)(-2); // expected-warning {{not in the valid range}}
18+
enum En_t InRange2 = (enum En_t)(-1); // expected-warning {{not in the valid range}}
19+
enum En_t InRange3 = (enum En_t)(0); // expected-warning {{not in the valid range}}
20+
enum En_t PosVal1 = (enum En_t)(1); // OK.
21+
enum En_t PosVal2 = (enum En_t)(2); // OK.
22+
enum En_t InRange4 = (enum En_t)(3); // expected-warning {{not in the valid range}}
23+
enum En_t PosVal3 = (enum En_t)(4); // OK.
24+
enum En_t Above = (enum En_t)(5); // expected-warning {{not in the valid range}}
25+
}
26+
27+
enum En_t unused;
28+
void unusedExpr() {
29+
// Following line is not something that EnumCastOutOfRangeChecker should
30+
// evaluate. Checker should either ignore this line or process it without
31+
// producing any warnings. However, compilation will (and should) still
32+
// generate a warning having nothing to do with this checker.
33+
unused; // expected-warning {{expression result unused}}
34+
}

clang/test/Analysis/enum-cast-out-of-range.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,15 @@ void scopedSpecifiedCStyle() {
150150
scoped_specified_t InvalidAfterRangeEnd = (scoped_specified_t)(5); // expected-warning {{The value provided to the cast expression is not in the valid range of values for the enum}}
151151
}
152152

153-
void rangeContstrained1(int input) {
153+
unscoped_unspecified_t unused;
154+
void unusedExpr() {
155+
// following line is not something that EnumCastOutOfRangeChecker should evaluate. checker should either ignore this line
156+
// or process it without producing any warnings. However, compilation will (and should) still generate a warning having
157+
// nothing to do with this checker.
158+
unused; // expected-warning {{expression result unused}}
159+
}
160+
161+
void rangeConstrained1(int input) {
154162
if (input > -5 && input < 5)
155163
auto value = static_cast<scoped_specified_t>(input); // OK. Being conservative, this is a possibly good value.
156164
}

0 commit comments

Comments
 (0)