[ELF] Reorder SectionBase/InputSectionBase members
[llvm-project.git] / clang / test / Analysis / builtin-functions.cpp
blobf7bafabd23cbcc03073bfb42cd02063f66819982
1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,debug.ExprInspection %s -std=c++11 -verify
2 // RUN: %clang_analyze_cc1 -triple x86_64-pc-windows-msvc19.11.0 -fms-extensions -analyzer-checker=core,debug.ExprInspection %s -std=c++11 -verify
4 #include "Inputs/system-header-simulator-cxx.h"
6 namespace std {
7 // Intentionally not using an "auto" return type here, as such must also have a definition.
8 template <typename T, typename U> constexpr int&& forward_like(U&& x) noexcept;
9 template <typename T> const T& move_if_noexcept(T& x) noexcept;
10 } // namespace std
12 template <typename T> void clang_analyzer_dump_ref(T&&);
13 template <typename T> void clang_analyzer_dump_ptr(T*);
14 void clang_analyzer_eval(bool);
15 void clang_analyzer_warnIfReached();
17 void testAddressof(int x) {
18 clang_analyzer_eval(&x == __builtin_addressof(x)); // expected-warning{{TRUE}}
21 void testStdBuiltinLikeFunctions(int x) {
22 clang_analyzer_dump_ptr(std::addressof(x)); // expected-warning{{&x}}
23 clang_analyzer_dump_ptr(std::__addressof(x)); // expected-warning{{&x}}
24 clang_analyzer_dump_ref(std::as_const(x)); // expected-warning{{&x}}
25 clang_analyzer_dump_ref(std::forward<int &>(x)); // expected-warning{{&x}}
26 clang_analyzer_dump_ref(std::forward_like<int &>(x)); // expected-warning{{&x}}
27 clang_analyzer_dump_ref(std::move(x)); // expected-warning{{&x}}
28 clang_analyzer_dump_ref(std::move_if_noexcept(x)); // expected-warning{{&x}}
31 void testSize() {
32 struct {
33 int x;
34 int y;
35 char z;
36 } object;
37 clang_analyzer_eval(__builtin_object_size(&object.y, 0) == sizeof(object) - sizeof(int)); // expected-warning{{TRUE}}
39 // Clang can't actually evaluate these builtin "calls", but importantly they don't actually evaluate the argument expression either.
40 int i = 0;
41 char buf[10];
42 clang_analyzer_eval(__builtin_object_size(&buf[i++], 0) == sizeof(buf)); // expected-warning{{FALSE}}
43 clang_analyzer_eval(__builtin_object_size(&buf[++i], 0) == sizeof(buf) - 1); // expected-warning{{FALSE}}
45 clang_analyzer_eval(i == 0); // expected-warning{{TRUE}}
48 void test_assume_aligned_1(char *p) {
49 char *q;
51 q = (char*) __builtin_assume_aligned(p, 16);
52 clang_analyzer_eval(p == q); // expected-warning{{TRUE}}
55 void test_assume_aligned_2(char *p) {
56 char *q;
58 q = (char*) __builtin_assume_aligned(p, 16, 8);
59 clang_analyzer_eval(p == q); // expected-warning{{TRUE}}
62 void test_assume_aligned_3(char *p) {
63 void *q;
65 q = __builtin_assume_aligned(p, 16, 8);
66 clang_analyzer_eval(p == q); // expected-warning{{TRUE}}
69 void test_assume_aligned_4(char *p) {
70 char *q;
72 q = (char*) __builtin_assume_aligned(p + 1, 16);
73 clang_analyzer_eval(p == q); // expected-warning{{FALSE}}
76 void f(int i) {
77 __builtin_assume(i < 10);
78 clang_analyzer_eval(i < 15); // expected-warning {{TRUE}}
81 void g(int i) {
82 if (i > 5) {
83 __builtin_assume(i < 5);
84 clang_analyzer_warnIfReached(); // Assumtion contradicts constraints.
85 // We give up the analysis on this path.
89 #ifdef _WIN32
90 namespace ms {
91 void f(int i) {
92 __assume(i < 10);
93 clang_analyzer_eval(i < 15); // expected-warning {{TRUE}}
96 void g(int i) {
97 if (i > 5) {
98 __assume(i < 5);
99 clang_analyzer_warnIfReached(); // Assumtion contradicts constraints.
100 // We give up the analysis on this path.
103 } // namespace ms
104 #endif
106 void test_constant_p(void *ptr) {
107 int i = 1;
108 const int j = 2;
109 constexpr int k = 3;
110 clang_analyzer_eval(__builtin_constant_p(42) == 1); // expected-warning {{TRUE}}
111 clang_analyzer_eval(__builtin_constant_p(i) == 0); // expected-warning {{TRUE}}
112 clang_analyzer_eval(__builtin_constant_p(j) == 1); // expected-warning {{TRUE}}
113 clang_analyzer_eval(__builtin_constant_p(k) == 1); // expected-warning {{TRUE}}
114 clang_analyzer_eval(__builtin_constant_p(i + 42) == 0); // expected-warning {{TRUE}}
115 clang_analyzer_eval(__builtin_constant_p(j + 42) == 1); // expected-warning {{TRUE}}
116 clang_analyzer_eval(__builtin_constant_p(k + 42) == 1); // expected-warning {{TRUE}}
117 clang_analyzer_eval(__builtin_constant_p(" ") == 1); // expected-warning {{TRUE}}
118 clang_analyzer_eval(__builtin_constant_p(test_constant_p) == 0); // expected-warning {{TRUE}}
119 clang_analyzer_eval(__builtin_constant_p(k - 3) == 0); // expected-warning {{FALSE}}
120 clang_analyzer_eval(__builtin_constant_p(k - 3) == 1); // expected-warning {{TRUE}}
121 clang_analyzer_eval(__builtin_constant_p(ptr == 0)); // expected-warning {{FALSE}}