Debian Codegraph @

clang/test/Analysis/builtin_assume.cpp

raw
1 // RUN: %clang_analyze_cc1 -std=c++11 -verify %s \
2 // RUN: -triple=x86_64-unknown-linux-gnu \
3 // RUN: -analyzer-checker=core,security.ArrayBound,debug.ExprInspection
4
5 void clang_analyzer_eval(bool);
6 void clang_analyzer_value(int);
7 void clang_analyzer_dump(int);
8
9 // From: https://github.com/llvm/llvm-project/issues/100762
10 extern int arrOf10[10];
11 void using_builtin(int x) {
12 __builtin_assume(x > 101); // CallExpr
13 arrOf10[x] = 404; // expected-warning {{Out of bound access to memory}}
14 }
15
16 void using_assume_attr(int ax) {
17 [[assume(ax > 100)]]; // NullStmt with an "assume" attribute.
18 arrOf10[ax] = 405; // expected-warning {{Out of bound access to memory}}
19 }
20
21 void using_many_assume_attr(int yx) {
22 [[assume(yx > 104), assume(yx > 200), assume(yx < 300)]]; // NullStmt with an attribute
23 arrOf10[yx] = 406; // expected-warning{{Out of bound access to memory}}
24 }
25
26 int using_assume_attr_has_no_sideeffects(int y) {
27 int orig_y = y;
28 clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
29 clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
30 clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
31 clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
32
33 // We should not apply sideeffects of the argument of [[assume(...)]].
34 // "y" should not get incremented;
35 [[assume(++y == 43)]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
36
37 clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
38 clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
39 clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
40 clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
41 clang_analyzer_eval(y == orig_y); // expected-warning {{TRUE}} Good.
42
43 return y;
44 }
45
46 int using_builtin_assume_has_no_sideeffects(int y) {
47 int orig_y = y;
48 clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
49 clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
50 clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
51 clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
52
53 // We should not apply sideeffects of the argument of __builtin_assume(...)
54 // "u" should not get incremented;
55 __builtin_assume(++y == 43); // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
56
57 clang_analyzer_dump(y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
58 clang_analyzer_dump(orig_y); // expected-warning-re {{{{^}}reg_${{[0-9]+}}<int y> [debug.ExprInspection]{{$}}}}
59 clang_analyzer_value(y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
60 clang_analyzer_value(orig_y); // expected-warning {{32s:{ [-2147483648, 2147483647] }}}
61 clang_analyzer_eval(y == orig_y); // expected-warning {{TRUE}} Good.
62
63 return y;
64 }
65
66 template <int ...args>
67 bool issue151529() {
68 // no-crash
69 [[assume((true))]];
70 // no-crash
71 [[assume(((args >= 0) && ...))]]; // expected-warning {{pack fold expression is a C++17 extension}}
72 return ((args >= 0) && ...); // expected-warning {{pack fold expression is a C++17 extension}}
73 }
74
75 void instantiate_issue151529() {
76 issue151529<0>();
77 }