File: std-c-library-functions-arg-constraints-notes.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (94 lines) | stat: -rw-r--r-- 3,144 bytes parent folder | download | duplicates (7)
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
84
85
86
87
88
89
90
91
92
93
94
// RUN: %clang_analyze_cc1 %s \
// RUN:   -analyzer-checker=core \
// RUN:   -analyzer-checker=apiModeling.StdCLibraryFunctions \
// RUN:   -analyzer-checker=alpha.unix.StdCLibraryFunctionArgs \
// RUN:   -analyzer-checker=debug.StdCLibraryFunctionsTester \
// RUN:   -analyzer-config apiModeling.StdCLibraryFunctions:DisplayLoadedSummaries=true \
// RUN:   -analyzer-checker=debug.ExprInspection \
// RUN:   -analyzer-config eagerly-assume=false \
// RUN:   -triple i686-unknown-linux \
// RUN:   -verify

// In this test we verify that each argument constraints are described properly.

// Check NotNullConstraint violation notes.
int __not_null(int *);
void test_not_null(int *x) {
  __not_null(nullptr); // \
  // expected-note{{The 1st arg should not be NULL}} \
  // expected-warning{{}}
}

// Check the BufferSizeConstraint violation notes.
using size_t = decltype(sizeof(int));
int __buf_size_arg_constraint_concrete(const void *); // size <= 10
int __buf_size_arg_constraint(const void *, size_t);  // size <= Arg1
int __buf_size_arg_constraint_mul(const void *, size_t, size_t); // size <= Arg1 * Arg2
void test_buffer_size(int x) {
  switch (x) {
  case 1: {
    char buf[9];
    __buf_size_arg_constraint_concrete(buf); // \
    // expected-note{{The size of the 1st arg should be equal to or less than the value of 10}} \
    // expected-warning{{}}
    break;
  }
  case 2: {
    char buf[3];
    __buf_size_arg_constraint(buf, 4); // \
    // expected-note{{The size of the 1st arg should be equal to or less than the value of the 2nd arg}} \
    // expected-warning{{}}
    break;
  }
  case 3: {
    char buf[3];
    __buf_size_arg_constraint_mul(buf, 4, 2); // \
    // expected-note{{The size of the 1st arg should be equal to or less than the value of the 2nd arg times the 3rd arg}} \
    // expected-warning{{}}
    break;
  }
  }
}

// Check the RangeConstraint violation notes.
int __single_val_1(int);      // [1, 1]
int __range_1_2(int);         // [1, 2]
int __range_1_2__4_5(int);    // [1, 2], [4, 5]
void test_range(int x) {
    __single_val_1(2); // \
    // expected-note{{The 1st arg should be within the range [1, 1]}} \
    // expected-warning{{}}
}
// Do more specific check against the range strings.
void test_range_values(int x) {
  switch (x) {
    case 1:
      __single_val_1(2);      // expected-note{{[1, 1]}} \
                              // expected-warning{{}}
      break;
    case 2:
      __range_1_2(3);         // expected-note{{[1, 2]}} \
                              // expected-warning{{}}
      break;
    case 3:
      __range_1_2__4_5(3);    // expected-note{{[[1, 2], [4, 5]]}} \
                              // expected-warning{{}}
      break;
  }
}
// Do more specific check against the range kinds.
int __within(int);       // [1, 1]
int __out_of(int);       // [1, 1]
void test_range_kind(int x) {
  switch (x) {
    case 1:
      __within(2);       // expected-note{{within}} \
                         // expected-warning{{}}
      break;
    case 2:
      __out_of(1);       // expected-note{{out of}} \
                         // expected-warning{{}}
      break;
  }
}