File: track_subexpressions.cpp

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-16
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,368 kB
  • sloc: cpp: 5,593,980; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (83 lines) | stat: -rw-r--r-- 3,184 bytes parent folder | download | duplicates (15)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// RUN: %clang_analyze_cc1 -x c++ -analyzer-checker=core -analyzer-output=text -verify %s

typedef unsigned char uint8_t;

#define UINT8_MAX 255
#define TCP_MAXWIN 65535

uint8_t get_uint8_max() {
  uint8_t rcvscale = UINT8_MAX; // expected-note{{'rcvscale' initialized to 255}}
  return rcvscale; // expected-note{{Returning the value 255 (loaded from 'rcvscale')}}
}

void shift_by_undefined_value() {
  uint8_t shift_amount = get_uint8_max(); // expected-note{{'shift_amount' initialized to 255}}
                                // expected-note@-1{{Calling 'get_uint8_max'}}
                                // expected-note@-2{{Returning from 'get_uint8_max'}}
  (void)(TCP_MAXWIN << shift_amount); // expected-warning{{The result of the left shift is undefined due to shifting by '255', which is greater or equal to the width of type 'int'}}
                                      // expected-note@-1{{The result of the left shift is undefined due to shifting by '255', which is greater or equal to the width of type 'int'}}
}

namespace array_index_tracking {
void consume(int);

int getIndex(int x) {
  int a;
  if (x > 0) // expected-note {{Assuming 'x' is > 0}}
             // expected-note@-1 {{Taking true branch}}
    a = 3; // expected-note {{The value 3 is assigned to 'a'}}
  else
    a = 2;
  return a; // expected-note {{Returning the value 3 (loaded from 'a')}}
}

int getInt();

void testArrayIndexTracking() {
  int arr[10];

  for (int i = 0; i < 3; ++i)
    // expected-note@-1 3{{Loop condition is true.  Entering loop body}}
    // expected-note@-2 {{Loop condition is false. Execution continues on line 43}}
    arr[i] = 0;
  int x = getInt();
  int n = getIndex(x); // expected-note {{Calling 'getIndex'}}
                       // expected-note@-1 {{Returning from 'getIndex'}}
                       // expected-note@-2 {{'n' initialized to 3}}
  consume(arr[n]);
  // expected-note@-1 {{1st function call argument is an uninitialized value}}
  // expected-warning@-2{{1st function call argument is an uninitialized value}}
}
} // end of namespace array_index_tracking

namespace multi_array_index_tracking {
void consume(int);

int getIndex(int x) {
  int a;
  if (x > 0) // expected-note {{Assuming 'x' is > 0}}
             // expected-note@-1 {{Taking true branch}}
    a = 3; // expected-note {{The value 3 is assigned to 'a'}}
  else
    a = 2;
  return a; // expected-note {{Returning the value 3 (loaded from 'a')}}
}

int getInt();

void testArrayIndexTracking() {
  int arr[2][10];

  for (int i = 0; i < 3; ++i)
    // expected-note@-1 3{{Loop condition is true.  Entering loop body}}
    // expected-note@-2 {{Loop condition is false. Execution continues on line 75}}
    arr[1][i] = 0;
  int x = getInt();
  int n = getIndex(x); // expected-note {{Calling 'getIndex'}}
                       // expected-note@-1 {{Returning from 'getIndex'}}
                       // expected-note@-2 {{'n' initialized to 3}}
  consume(arr[1][n]);
  // expected-note@-1 {{1st function call argument is an uninitialized value}}
  // expected-warning@-2{{1st function call argument is an uninitialized value}}
}
} // end of namespace mulit_array_index_tracking