[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Analysis / builtin-functions.cpp
blob37e522049b17489732e2851d0a11171069456bec
1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,debug.ExprInspection %s -std=c++11 -verify
3 void clang_analyzer_eval(bool);
4 void clang_analyzer_warnIfReached();
6 void testAddressof(int x) {
7 clang_analyzer_eval(&x == __builtin_addressof(x)); // expected-warning{{TRUE}}
10 void testSize() {
11 struct {
12 int x;
13 int y;
14 char z;
15 } object;
16 clang_analyzer_eval(__builtin_object_size(&object.y, 0) == sizeof(object) - sizeof(int)); // expected-warning{{TRUE}}
18 // Clang can't actually evaluate these builtin "calls", but importantly they don't actually evaluate the argument expression either.
19 int i = 0;
20 char buf[10];
21 clang_analyzer_eval(__builtin_object_size(&buf[i++], 0) == sizeof(buf)); // expected-warning{{FALSE}}
22 clang_analyzer_eval(__builtin_object_size(&buf[++i], 0) == sizeof(buf) - 1); // expected-warning{{FALSE}}
24 clang_analyzer_eval(i == 0); // expected-warning{{TRUE}}
27 void test_assume_aligned_1(char *p) {
28 char *q;
30 q = (char*) __builtin_assume_aligned(p, 16);
31 clang_analyzer_eval(p == q); // expected-warning{{TRUE}}
34 void test_assume_aligned_2(char *p) {
35 char *q;
37 q = (char*) __builtin_assume_aligned(p, 16, 8);
38 clang_analyzer_eval(p == q); // expected-warning{{TRUE}}
41 void test_assume_aligned_3(char *p) {
42 void *q;
44 q = __builtin_assume_aligned(p, 16, 8);
45 clang_analyzer_eval(p == q); // expected-warning{{TRUE}}
48 void test_assume_aligned_4(char *p) {
49 char *q;
51 q = (char*) __builtin_assume_aligned(p + 1, 16);
52 clang_analyzer_eval(p == q); // expected-warning{{FALSE}}
55 void f(int i) {
56 __builtin_assume(i < 10);
57 clang_analyzer_eval(i < 15); // expected-warning {{TRUE}}
60 void g(int i) {
61 if (i > 5) {
62 __builtin_assume(i < 5);
63 clang_analyzer_warnIfReached(); // Assumtion contradicts constraints.
64 // We give up the analysis on this path.
68 void test_constant_p(void *ptr) {
69 int i = 1;
70 const int j = 2;
71 constexpr int k = 3;
72 clang_analyzer_eval(__builtin_constant_p(42) == 1); // expected-warning {{TRUE}}
73 clang_analyzer_eval(__builtin_constant_p(i) == 0); // expected-warning {{TRUE}}
74 clang_analyzer_eval(__builtin_constant_p(j) == 1); // expected-warning {{TRUE}}
75 clang_analyzer_eval(__builtin_constant_p(k) == 1); // expected-warning {{TRUE}}
76 clang_analyzer_eval(__builtin_constant_p(i + 42) == 0); // expected-warning {{TRUE}}
77 clang_analyzer_eval(__builtin_constant_p(j + 42) == 1); // expected-warning {{TRUE}}
78 clang_analyzer_eval(__builtin_constant_p(k + 42) == 1); // expected-warning {{TRUE}}
79 clang_analyzer_eval(__builtin_constant_p(" ") == 1); // expected-warning {{TRUE}}
80 clang_analyzer_eval(__builtin_constant_p(test_constant_p) == 0); // expected-warning {{TRUE}}
81 clang_analyzer_eval(__builtin_constant_p(k - 3) == 0); // expected-warning {{FALSE}}
82 clang_analyzer_eval(__builtin_constant_p(k - 3) == 1); // expected-warning {{TRUE}}
83 clang_analyzer_eval(__builtin_constant_p(ptr == 0)); // expected-warning {{FALSE}}