1743682bbSChandler Carruth // RUN: %clang_cc1 -fsyntax-only -verify %s
2743682bbSChandler Carruth struct S {
3d4693211SNick Lewycky   S(const char *) __attribute__((nonnull(2)));
4d4693211SNick Lewycky 
5743682bbSChandler Carruth   static void f(const char*, const char*) __attribute__((nonnull(1)));
6743682bbSChandler Carruth 
7743682bbSChandler Carruth   // GCC has a hidden 'this' argument in member functions, so the middle
8743682bbSChandler Carruth   // argument is the one that must not be null.
9743682bbSChandler Carruth   void g(const char*, const char*, const char*) __attribute__((nonnull(3)));
10743682bbSChandler Carruth 
11743682bbSChandler Carruth   void h(const char*) __attribute__((nonnull(1))); // \
12743682bbSChandler Carruth       expected-error{{invalid for the implicit this argument}}
13743682bbSChandler Carruth };
14743682bbSChandler Carruth 
test()15d4693211SNick Lewycky void test() {
16d4693211SNick Lewycky   S s(0); // expected-warning{{null passed}}
17d4693211SNick Lewycky 
18743682bbSChandler Carruth   s.f(0, ""); // expected-warning{{null passed}}
19743682bbSChandler Carruth   s.f("", 0);
20743682bbSChandler Carruth   s.g("", 0, ""); // expected-warning{{null passed}}
21743682bbSChandler Carruth   s.g(0, "", 0);
22743682bbSChandler Carruth }
231e2cdf59SDouglas Gregor 
241e2cdf59SDouglas Gregor namespace rdar8769025 {
251e2cdf59SDouglas Gregor   __attribute__((nonnull)) void f0(int *&p);
261e2cdf59SDouglas Gregor   __attribute__((nonnull)) void f1(int * const &p);
271e2cdf59SDouglas Gregor   __attribute__((nonnull(2))) void f2(int i, int * const &p);
281e2cdf59SDouglas Gregor 
test_f1()291e2cdf59SDouglas Gregor   void test_f1() {
30588bd9b7SRichard Smith     f1(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
31588bd9b7SRichard Smith     f2(0, 0); // expected-warning{{null passed to a callee that requires a non-null argument}}
321e2cdf59SDouglas Gregor   }
331e2cdf59SDouglas Gregor }
34c77c8e95SNick Lewycky 
35c77c8e95SNick Lewycky namespace test3 {
36c77c8e95SNick Lewycky __attribute__((nonnull(1))) void f(void *ptr);
37c77c8e95SNick Lewycky 
g()38c77c8e95SNick Lewycky void g() {
39c77c8e95SNick Lewycky   f(static_cast<char*>((void*)0));  // expected-warning{{null passed}}
40c77c8e95SNick Lewycky   f(static_cast<char*>(0));  // expected-warning{{null passed}}
41c77c8e95SNick Lewycky }
42c77c8e95SNick Lewycky }
43d24d5f2bSNick Lewycky 
44d24d5f2bSNick Lewycky namespace test4 {
45d24d5f2bSNick Lewycky struct X {
46d24d5f2bSNick Lewycky   bool operator!=(const void *) const __attribute__((nonnull(2)));
47d24d5f2bSNick Lewycky };
48d24d5f2bSNick Lewycky bool operator==(const X&, const void *) __attribute__((nonnull(2)));
49d24d5f2bSNick Lewycky 
test(const X & x)50d24d5f2bSNick Lewycky void test(const X& x) {
51d24d5f2bSNick Lewycky   (void)(x == 0);  // expected-warning{{null passed}}
52d24d5f2bSNick Lewycky   (void)(x != 0);  // expected-warning{{null passed}}
53d24d5f2bSNick Lewycky }
54d24d5f2bSNick Lewycky }
550bb4d46bSGauthier Harnisch 
560bb4d46bSGauthier Harnisch namespace test5 {
570bb4d46bSGauthier Harnisch 
580bb4d46bSGauthier Harnisch constexpr int c = 0;
590bb4d46bSGauthier Harnisch 
600bb4d46bSGauthier Harnisch __attribute__((nonnull))
f1(const int *,const int *)610bb4d46bSGauthier Harnisch constexpr int f1(const int*, const int*) {
620bb4d46bSGauthier Harnisch   return 0;
630bb4d46bSGauthier Harnisch }
640bb4d46bSGauthier Harnisch constexpr int i1 = f1(&c, &c);
650bb4d46bSGauthier Harnisch constexpr int i12 = f1(&c, 0); //expected-error {{constant expression}} expected-note {{null passed}}
660bb4d46bSGauthier Harnisch 
f2(const int *,const int *)670bb4d46bSGauthier Harnisch constexpr int f2(const int*, const int*) {
680bb4d46bSGauthier Harnisch   return 0;
690bb4d46bSGauthier Harnisch }
700bb4d46bSGauthier Harnisch constexpr int i2 = f2(0, 0);
710bb4d46bSGauthier Harnisch 
720bb4d46bSGauthier Harnisch __attribute__((nonnull(2)))
f3(const int *,const int *)730bb4d46bSGauthier Harnisch constexpr int f3(const int*, const int*) {
740bb4d46bSGauthier Harnisch   return 0;
750bb4d46bSGauthier Harnisch }
760bb4d46bSGauthier Harnisch constexpr int i3 = f3(&c, 0); //expected-error {{constant expression}} expected-note {{null passed}}
770bb4d46bSGauthier Harnisch constexpr int i32 = f3(0, &c);
780bb4d46bSGauthier Harnisch 
790bb4d46bSGauthier Harnisch __attribute__((nonnull(4))) __attribute__((nonnull)) //expected-error {{out of bounds}}
f4(const int *,const int *,int)80*59c6df9bSGauthier Harnisch constexpr int f4(const int*, const int*, int) {
810bb4d46bSGauthier Harnisch   return 0;
820bb4d46bSGauthier Harnisch }
83*59c6df9bSGauthier Harnisch constexpr int i4 = f4(&c, 0, 0); //expected-error {{constant expression}} expected-note {{null passed}}
84*59c6df9bSGauthier Harnisch constexpr int i42 = f4(0, &c, 1); //expected-error {{constant expression}} expected-note {{null passed}}
85*59c6df9bSGauthier Harnisch constexpr int i43 = f4(&c, &c, 0);
860bb4d46bSGauthier Harnisch 
870bb4d46bSGauthier Harnisch }