Debian Codegraph @

clang/test/SemaCXX/attr-nonnull.cpp

raw
1 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s -fexperimental-new-constant-interpreter
3 struct S {
4 S(const char *) __attribute__((nonnull(2)));
5
6 static void f(const char*, const char*) __attribute__((nonnull(1)));
7
8 // GCC has a hidden 'this' argument in member functions, so the middle
9 // argument is the one that must not be null.
10 void g(const char*, const char*, const char*) __attribute__((nonnull(3)));
11
12 void h(const char*) __attribute__((nonnull(1))); // \
13 expected-error{{invalid for the implicit this argument}}
14
15 void i(this S* self, const char*) __attribute__((nonnull(1)));
16
17 void j(this S* self, const char*) __attribute__((nonnull(2)));
18
19 void k(this S* self, const char*) __attribute__((nonnull(3))); // \
20 expected-error{{'nonnull' attribute parameter 1 is out of bounds}}
21 };
22
23 void test() {
24 S s(0); // expected-warning{{null passed}}
25
26 s.f(0, ""); // expected-warning{{null passed}}
27 s.f("", 0);
28 s.g("", 0, ""); // expected-warning{{null passed}}
29 s.g(0, "", 0);
30 }
31
32 namespace rdar8769025 {
33 __attribute__((nonnull)) void f0(int *&p);
34 __attribute__((nonnull)) void f1(int * const &p);
35 __attribute__((nonnull(2))) void f2(int i, int * const &p);
36
37 void test_f1() {
38 f1(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
39 f2(0, 0); // expected-warning{{null passed to a callee that requires a non-null argument}}
40 }
41 }
42
43 namespace test3 {
44 __attribute__((nonnull(1))) void f(void *ptr);
45
46 void g() {
47 f(static_cast<char*>((void*)0)); // expected-warning{{null passed}}
48 f(static_cast<char*>(0)); // expected-warning{{null passed}}
49 }
50 }
51
52 namespace test4 {
53 struct X {
54 bool operator!=(const void *) const __attribute__((nonnull(2)));
55 };
56 bool operator==(const X&, const void *) __attribute__((nonnull(2)));
57
58 void test(const X& x) {
59 (void)(x == 0); // expected-warning{{null passed}}
60 (void)(x != 0); // expected-warning{{null passed}}
61 }
62 }
63
64 namespace test5 {
65
66 constexpr int c = 0;
67
68 __attribute__((nonnull))
69 constexpr int f1(const int*, const int*) {
70 return 0;
71 }
72 constexpr int i1 = f1(&c, &c);
73 constexpr int i12 = f1(&c, 0); //expected-error {{constant expression}} expected-note {{null passed}}
74
75 constexpr int f2(const int*, const int*) {
76 return 0;
77 }
78 constexpr int i2 = f2(0, 0);
79
80 __attribute__((nonnull(2)))
81 constexpr int f3(const int*, const int*) {
82 return 0;
83 }
84 constexpr int i3 = f3(&c, 0); //expected-error {{constant expression}} expected-note {{null passed}}
85 constexpr int i32 = f3(0, &c);
86
87 __attribute__((nonnull(4))) __attribute__((nonnull)) //expected-error {{out of bounds}}
88 constexpr int f4(const int*, const int*, int) {
89 return 0;
90 }
91 constexpr int i4 = f4(&c, 0, 0); //expected-error {{constant expression}} expected-note {{null passed}}
92 constexpr int i42 = f4(0, &c, 1); //expected-error {{constant expression}} expected-note {{null passed}}
93 constexpr int i43 = f4(&c, &c, 0);
94
95 }