Skip to content

Commit 4d12939

Browse files
author
Endre Fülöp
committed
add trackexpression test
track subexpression instead of the whole cast
1 parent b9807ec commit 4d12939

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void EnumCastOutOfRangeChecker::reportWarning(CheckerContext &C,
100100

101101
auto BR = std::make_unique<PathSensitiveBugReport>(*EnumValueCastOutOfRange,
102102
Msg, N);
103-
bugreporter::trackExpressionValue(N, CE, *BR);
103+
bugreporter::trackExpressionValue(N, CE->getSubExpr(), *BR);
104104
BR->addNote("enum declared here",
105105
PathDiagnosticLocation::create(E, C.getSourceManager()),
106106
{E->getSourceRange()});

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// RUN: %clang_analyze_cc1 \
22
// RUN: -analyzer-checker=core,alpha.cplusplus.EnumCastOutOfRange \
3+
// RUN: -analyzer-output text \
34
// RUN: -verify %s
45

5-
// expected-note@+1 6 {{enum declared here}}
6+
// expected-note@+1 + {{enum declared here}}
67
enum En_t {
78
En_0 = -4,
89
En_1,
@@ -13,16 +14,22 @@ enum En_t {
1314

1415
void unscopedUnspecifiedCStyle(void) {
1516
enum En_t Below = (enum En_t)(-5); // expected-warning {{not in the valid range of values for 'En_t'}}
17+
// expected-note@-1 {{not in the valid range of values for 'En_t'}}
1618
enum En_t NegVal1 = (enum En_t)(-4); // OK.
1719
enum En_t NegVal2 = (enum En_t)(-3); // OK.
1820
enum En_t InRange1 = (enum En_t)(-2); // expected-warning {{not in the valid range of values for 'En_t'}}
21+
// expected-note@-1 {{not in the valid range of values for 'En_t'}}
1922
enum En_t InRange2 = (enum En_t)(-1); // expected-warning {{not in the valid range of values for 'En_t'}}
23+
// expected-note@-1 {{not in the valid range of values for 'En_t'}}
2024
enum En_t InRange3 = (enum En_t)(0); // expected-warning {{not in the valid range of values for 'En_t'}}
25+
// expected-note@-1 {{not in the valid range of values for 'En_t'}}
2126
enum En_t PosVal1 = (enum En_t)(1); // OK.
2227
enum En_t PosVal2 = (enum En_t)(2); // OK.
2328
enum En_t InRange4 = (enum En_t)(3); // expected-warning {{not in the valid range of values for 'En_t'}}
29+
// expected-note@-1 {{not in the valid range of values for 'En_t'}}
2430
enum En_t PosVal3 = (enum En_t)(4); // OK.
2531
enum En_t Above = (enum En_t)(5); // expected-warning {{not in the valid range of values for 'En_t'}}
32+
// expected-note@-1 {{not in the valid range of values for 'En_t'}}
2633
}
2734

2835
enum En_t unused;
@@ -33,3 +40,17 @@ void unusedExpr(void) {
3340
// generate a warning having nothing to do with this checker.
3441
unused; // expected-warning {{expression result unused}}
3542
}
43+
44+
// Test expression tracking
45+
void set(int* p, int v) {
46+
*p = v; // expected-note {{The value -1 is assigned to 'i'}}
47+
}
48+
49+
50+
void testTrackExpression(int i) {
51+
set(&i, -1); // expected-note {{Passing the value -1 via 2nd parameter 'v'}}
52+
// expected-note@-1 {{Calling 'set'}}
53+
// expected-note@-2 {{Returning from 'set'}}
54+
(void)(enum En_t)(i); // expected-warning {{not in the valid range of values for 'En_t'}}
55+
// expected-note@-1 {{not in the valid range of values for 'En_t'}}
56+
}

0 commit comments

Comments
 (0)