xref: /llvm-project/clang/test/Analysis/std-c-library-functions-arg-constraints-notes.cpp (revision 82a50812f7e539b69fb8031bee9c823e5290d1c9)
1 // RUN: %clang_analyze_cc1 %s \
2 // RUN:   -analyzer-checker=core \
3 // RUN:   -analyzer-checker=apiModeling.StdCLibraryFunctions \
4 // RUN:   -analyzer-checker=alpha.unix.StdCLibraryFunctionArgs \
5 // RUN:   -analyzer-checker=debug.StdCLibraryFunctionsTester \
6 // RUN:   -analyzer-config apiModeling.StdCLibraryFunctions:DisplayLoadedSummaries=true \
7 // RUN:   -analyzer-checker=debug.ExprInspection \
8 // RUN:   -analyzer-config eagerly-assume=false \
9 // RUN:   -triple i686-unknown-linux \
10 // RUN:   -verify
11 
12 // In this test we verify that each argument constraints are described properly.
13 
14 // Check NotNullConstraint violation notes.
15 int __not_null(int *);
16 void test_not_null(int *x) {
17   __not_null(nullptr); // \
18   // expected-note{{The 1st argument should not be NULL}} \
19   // expected-warning{{}}
20 }
21 
22 // Check the BufferSizeConstraint violation notes.
23 using size_t = decltype(sizeof(int));
24 int __buf_size_arg_constraint_concrete(const void *); // size <= 10
25 int __buf_size_arg_constraint(const void *, size_t);  // size <= Arg1
26 int __buf_size_arg_constraint_mul(const void *, size_t, size_t); // size <= Arg1 * Arg2
27 void test_buffer_size(int x) {
28   switch (x) {
29   case 1: {
30     char buf[9];
31     __buf_size_arg_constraint_concrete(buf); // \
32     // expected-note{{The size of the 1st argument should be equal to or greater than the value of 10}} \
33     // expected-warning{{}}
34     break;
35   }
36   case 2: {
37     char buf[3];
38     __buf_size_arg_constraint(buf, 4); // \
39     // expected-note{{The size of the 1st argument should be equal to or greater than the value of the 2nd arg}} \
40     // expected-warning{{}}
41     break;
42   }
43   case 3: {
44     char buf[3];
45     __buf_size_arg_constraint_mul(buf, 4, 2); // \
46     // expected-note{{The size of the 1st argument should be equal to or greater than the value of the 2nd argument times the 3rd argument}} \
47     // expected-warning{{}}
48     break;
49   }
50   }
51 }
52 
53 // Check the RangeConstraint violation notes.
54 int __single_val_1(int);      // [1, 1]
55 int __range_1_2(int);         // [1, 2]
56 int __range_1_2__4_5(int);    // [1, 2], [4, 5]
57 void test_range(int x) {
58     __single_val_1(2); // \
59     // expected-note{{The 1st argument should be within the range [1, 1]}} \
60     // expected-warning{{}}
61 }
62 // Do more specific check against the range strings.
63 void test_range_values(int x) {
64   switch (x) {
65     case 1:
66       __single_val_1(2);      // expected-note{{[1, 1]}} \
67                               // expected-warning{{}}
68       break;
69     case 2:
70       __range_1_2(3);         // expected-note{{[1, 2]}} \
71                               // expected-warning{{}}
72       break;
73     case 3:
74       __range_1_2__4_5(3);    // expected-note{{[[1, 2], [4, 5]]}} \
75                               // expected-warning{{}}
76       break;
77   }
78 }
79 // Do more specific check against the range kinds.
80 int __within(int);       // [1, 1]
81 int __out_of(int);       // [1, 1]
82 void test_range_kind(int x) {
83   switch (x) {
84     case 1:
85       __within(2);       // expected-note{{within}} \
86                          // expected-warning{{}}
87       break;
88     case 2:
89       __out_of(1);       // expected-note{{out of}} \
90                          // expected-warning{{}}
91       break;
92   }
93 }
94 
95