File: enum-cast-out-of-range.c

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (66 lines) | stat: -rw-r--r-- 3,089 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// RUN: %clang_analyze_cc1 \
// RUN:   -analyzer-checker=core,optin.core.EnumCastOutOfRange \
// RUN:   -analyzer-output text \
// RUN:   -verify %s

// expected-note@+1 + {{enum declared here}}
enum En_t {
  En_0 = -4,
  En_1,
  En_2 = 1,
  En_3,
  En_4 = 4
};

void unscopedUnspecifiedCStyle(void) {
  enum En_t Below = (enum En_t)(-5);    // expected-warning {{not in the valid range of values for 'En_t'}}
                                        // expected-note@-1 {{not in the valid range of values for 'En_t'}}
  enum En_t NegVal1 = (enum En_t)(-4);  // OK.
  enum En_t NegVal2 = (enum En_t)(-3);  // OK.
  enum En_t InRange1 = (enum En_t)(-2); // expected-warning {{not in the valid range of values for 'En_t'}}
                                        // expected-note@-1 {{not in the valid range of values for 'En_t'}}
  enum En_t InRange2 = (enum En_t)(-1); // expected-warning {{not in the valid range of values for 'En_t'}}
                                        // expected-note@-1 {{not in the valid range of values for 'En_t'}}
  enum En_t InRange3 = (enum En_t)(0);  // expected-warning {{not in the valid range of values for 'En_t'}}
                                        // expected-note@-1 {{not in the valid range of values for 'En_t'}}
  enum En_t PosVal1 = (enum En_t)(1);   // OK.
  enum En_t PosVal2 = (enum En_t)(2);   // OK.
  enum En_t InRange4 = (enum En_t)(3);  // expected-warning {{not in the valid range of values for 'En_t'}}
                                        // expected-note@-1 {{not in the valid range of values for 'En_t'}}
  enum En_t PosVal3 = (enum En_t)(4);   // OK.
  enum En_t Above = (enum En_t)(5);     // expected-warning {{not in the valid range of values for 'En_t'}}
                                        // expected-note@-1 {{not in the valid range of values for 'En_t'}}
}

enum En_t unused;
void unusedExpr(void) {
  // Following line is not something that EnumCastOutOfRangeChecker should
  // evaluate.  Checker should either ignore this line or process it without
  // producing any warnings.  However, compilation will (and should) still
  // generate a warning having nothing to do with this checker.
  unused; // expected-warning {{expression result unused}}
}

// Test typedef-ed anonymous enums
typedef enum { // expected-note {{enum declared here}}
    TD_0 = 0,
} TD_t;

void testTypeDefEnum(void) {
  (void)(TD_t)(-1); // expected-warning {{not in the valid range of values for the enum}}
                    // expected-note@-1 {{not in the valid range of values for the enum}}
}

// Test expression tracking
void set(int* p, int v) {
  *p = v; // expected-note {{The value -1 is assigned to 'i'}}
}


void testTrackExpression(int i) {
  set(&i, -1); // expected-note {{Passing the value -1 via 2nd parameter 'v'}}
               // expected-note@-1 {{Calling 'set'}}
               // expected-note@-2 {{Returning from 'set'}}
  (void)(enum En_t)(i); // expected-warning {{not in the valid range of values for 'En_t'}}
                        // expected-note@-1 {{not in the valid range of values for 'En_t'}}
}