annotate clang/test/SemaCXX/attr-nonnull.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 1d019706d866
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
anatofuz
parents:
diff changeset
2 struct S {
anatofuz
parents:
diff changeset
3 S(const char *) __attribute__((nonnull(2)));
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 static void f(const char*, const char*) __attribute__((nonnull(1)));
anatofuz
parents:
diff changeset
6
anatofuz
parents:
diff changeset
7 // GCC has a hidden 'this' argument in member functions, so the middle
anatofuz
parents:
diff changeset
8 // argument is the one that must not be null.
anatofuz
parents:
diff changeset
9 void g(const char*, const char*, const char*) __attribute__((nonnull(3)));
anatofuz
parents:
diff changeset
10
anatofuz
parents:
diff changeset
11 void h(const char*) __attribute__((nonnull(1))); // \
anatofuz
parents:
diff changeset
12 expected-error{{invalid for the implicit this argument}}
anatofuz
parents:
diff changeset
13 };
anatofuz
parents:
diff changeset
14
anatofuz
parents:
diff changeset
15 void test() {
anatofuz
parents:
diff changeset
16 S s(0); // expected-warning{{null passed}}
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 s.f(0, ""); // expected-warning{{null passed}}
anatofuz
parents:
diff changeset
19 s.f("", 0);
anatofuz
parents:
diff changeset
20 s.g("", 0, ""); // expected-warning{{null passed}}
anatofuz
parents:
diff changeset
21 s.g(0, "", 0);
anatofuz
parents:
diff changeset
22 }
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 namespace rdar8769025 {
anatofuz
parents:
diff changeset
25 __attribute__((nonnull)) void f0(int *&p);
anatofuz
parents:
diff changeset
26 __attribute__((nonnull)) void f1(int * const &p);
anatofuz
parents:
diff changeset
27 __attribute__((nonnull(2))) void f2(int i, int * const &p);
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 void test_f1() {
anatofuz
parents:
diff changeset
30 f1(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
anatofuz
parents:
diff changeset
31 f2(0, 0); // expected-warning{{null passed to a callee that requires a non-null argument}}
anatofuz
parents:
diff changeset
32 }
anatofuz
parents:
diff changeset
33 }
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 namespace test3 {
anatofuz
parents:
diff changeset
36 __attribute__((nonnull(1))) void f(void *ptr);
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 void g() {
anatofuz
parents:
diff changeset
39 f(static_cast<char*>((void*)0)); // expected-warning{{null passed}}
anatofuz
parents:
diff changeset
40 f(static_cast<char*>(0)); // expected-warning{{null passed}}
anatofuz
parents:
diff changeset
41 }
anatofuz
parents:
diff changeset
42 }
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 namespace test4 {
anatofuz
parents:
diff changeset
45 struct X {
anatofuz
parents:
diff changeset
46 bool operator!=(const void *) const __attribute__((nonnull(2)));
anatofuz
parents:
diff changeset
47 };
anatofuz
parents:
diff changeset
48 bool operator==(const X&, const void *) __attribute__((nonnull(2)));
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 void test(const X& x) {
anatofuz
parents:
diff changeset
51 (void)(x == 0); // expected-warning{{null passed}}
anatofuz
parents:
diff changeset
52 (void)(x != 0); // expected-warning{{null passed}}
anatofuz
parents:
diff changeset
53 }
anatofuz
parents:
diff changeset
54 }
anatofuz
parents:
diff changeset
55
anatofuz
parents:
diff changeset
56 namespace test5 {
anatofuz
parents:
diff changeset
57
anatofuz
parents:
diff changeset
58 constexpr int c = 0;
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 __attribute__((nonnull))
anatofuz
parents:
diff changeset
61 constexpr int f1(const int*, const int*) {
anatofuz
parents:
diff changeset
62 return 0;
anatofuz
parents:
diff changeset
63 }
anatofuz
parents:
diff changeset
64 constexpr int i1 = f1(&c, &c);
anatofuz
parents:
diff changeset
65 constexpr int i12 = f1(&c, 0); //expected-error {{constant expression}} expected-note {{null passed}}
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 constexpr int f2(const int*, const int*) {
anatofuz
parents:
diff changeset
68 return 0;
anatofuz
parents:
diff changeset
69 }
anatofuz
parents:
diff changeset
70 constexpr int i2 = f2(0, 0);
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 __attribute__((nonnull(2)))
anatofuz
parents:
diff changeset
73 constexpr int f3(const int*, const int*) {
anatofuz
parents:
diff changeset
74 return 0;
anatofuz
parents:
diff changeset
75 }
anatofuz
parents:
diff changeset
76 constexpr int i3 = f3(&c, 0); //expected-error {{constant expression}} expected-note {{null passed}}
anatofuz
parents:
diff changeset
77 constexpr int i32 = f3(0, &c);
anatofuz
parents:
diff changeset
78
anatofuz
parents:
diff changeset
79 __attribute__((nonnull(4))) __attribute__((nonnull)) //expected-error {{out of bounds}}
anatofuz
parents:
diff changeset
80 constexpr int f4(const int*, const int*, int) {
anatofuz
parents:
diff changeset
81 return 0;
anatofuz
parents:
diff changeset
82 }
anatofuz
parents:
diff changeset
83 constexpr int i4 = f4(&c, 0, 0); //expected-error {{constant expression}} expected-note {{null passed}}
anatofuz
parents:
diff changeset
84 constexpr int i42 = f4(0, &c, 1); //expected-error {{constant expression}} expected-note {{null passed}}
anatofuz
parents:
diff changeset
85 constexpr int i43 = f4(&c, &c, 0);
anatofuz
parents:
diff changeset
86
anatofuz
parents:
diff changeset
87 }