96 lines · cpp
1// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s2// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s -fexperimental-new-constant-interpreter3struct 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 middle9 // 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 23void 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 32namespace 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 43namespace test3 {44__attribute__((nonnull(1))) void f(void *ptr);45 46void 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 52namespace test4 {53struct X {54 bool operator!=(const void *) const __attribute__((nonnull(2)));55};56bool operator==(const X&, const void *) __attribute__((nonnull(2)));57 58void test(const X& x) {59 (void)(x == 0); // expected-warning{{null passed}}60 (void)(x != 0); // expected-warning{{null passed}}61}62}63 64namespace test5 {65 66constexpr int c = 0;67 68__attribute__((nonnull))69constexpr int f1(const int*, const int*) {70 return 0;71}72constexpr int i1 = f1(&c, &c);73constexpr int i12 = f1(&c, 0); //expected-error {{constant expression}} expected-note {{null passed}}74 75constexpr int f2(const int*, const int*) {76 return 0;77}78constexpr int i2 = f2(0, 0);79 80__attribute__((nonnull(2)))81constexpr int f3(const int*, const int*) {82 return 0;83}84constexpr int i3 = f3(&c, 0); //expected-error {{constant expression}} expected-note {{null passed}}85constexpr int i32 = f3(0, &c);86 87__attribute__((nonnull(4))) __attribute__((nonnull)) //expected-error {{out of bounds}}88constexpr int f4(const int*, const int*, int) {89 return 0;90}91constexpr int i4 = f4(&c, 0, 0); //expected-error {{constant expression}} expected-note {{null passed}}92constexpr int i42 = f4(0, &c, 1); //expected-error {{constant expression}} expected-note {{null passed}}93constexpr int i43 = f4(&c, &c, 0);94 95}96