Debian Codegraph @

clang/test/Sema/diagnose_if.c

raw
1 // RUN: %clang_cc1 %s -verify -fno-builtin
2 // RUN: %clang_cc1 %s -verify -fno-builtin -fexperimental-new-constant-interpreter
3
4 #define _diagnose_if(...) __attribute__((diagnose_if(__VA_ARGS__)))
5
6 void failure1(void) _diagnose_if(); // expected-error{{at least 3 arguments}}
7 void failure2(void) _diagnose_if(0); // expected-error{{at least 3 arguments}}
8 void failure3(void) _diagnose_if(0, ""); // expected-error{{at least 3 arguments}}
9 void failure4(void) _diagnose_if(0, "", "error", 1); // expected-error{{expected string literal as argument}}
10 void failure5(void) _diagnose_if(0, 0, "error"); // expected-error{{expected string literal as argument of 'diagnose_if' attribute}}
11 void failure6(void) _diagnose_if(0, "", "invalid"); // expected-error{{invalid diagnostic type for 'diagnose_if'; use "error" or "warning" instead}}
12 void failure7(void) _diagnose_if(0, "", "ERROR"); // expected-error{{invalid diagnostic type}}
13 void failure8(int a) _diagnose_if(a, "", ""); // expected-error{{invalid diagnostic type}}
14 void failure9(void) _diagnose_if(a, "", ""); // expected-error{{undeclared identifier 'a'}}
15
16 int globalVar;
17 void never_constant(void) _diagnose_if(globalVar, "", "error"); // expected-error{{'diagnose_if' attribute expression never produces a constant expression}} expected-note{{subexpression not valid}}
18 void never_constant(void) _diagnose_if(globalVar, "", "warning"); // expected-error{{'diagnose_if' attribute expression never produces a constant expression}} expected-note{{subexpression not valid}}
19
20 int alwaysok(int q) _diagnose_if(0, "", "error");
21 int neverok(int q) _diagnose_if(1, "oh no", "error"); // expected-note 5{{from 'diagnose_if' attribute on 'neverok'}}
22 int alwayswarn(int q) _diagnose_if(1, "oh no", "warning"); // expected-note 5{{from 'diagnose_if' attribute}}
23 int neverwarn(int q) _diagnose_if(0, "", "warning");
24
25 void runConstant(void) {
26 int m;
27 alwaysok(0);
28 alwaysok(1);
29 alwaysok(m);
30
31 {
32 int (*pok)(int) = alwaysok;
33 pok = &alwaysok;
34 }
35
36 neverok(0); // expected-error{{oh no}}
37 neverok(1); // expected-error{{oh no}}
38 neverok(m); // expected-error{{oh no}}
39 {
40 int (*pok)(int) = neverok; // expected-error{{oh no}}
41 pok = &neverok; // expected-error{{oh no}}
42 }
43
44 alwayswarn(0); // expected-warning{{oh no}}
45 alwayswarn(1); // expected-warning{{oh no}}
46 alwayswarn(m); // expected-warning{{oh no}}
47 {
48 int (*pok)(int) = alwayswarn; // expected-warning{{oh no}}
49 pok = &alwayswarn; // expected-warning{{oh no}}
50 }
51
52 neverwarn(0);
53 neverwarn(1);
54 neverwarn(m);
55 {
56 int (*pok)(int) = neverwarn;
57 pok = &neverwarn;
58 }
59 }
60
61 int abs(int q) _diagnose_if(q >= 0, "redundant abs call", "error"); //expected-note{{from 'diagnose_if'}}
62 void runVariable(void) {
63 int m;
64 abs(-1);
65 abs(1); // expected-error{{redundant abs call}}
66 abs(m);
67
68 int (*pabs)(int) = abs;
69 pabs = &abs;
70 }
71
72 #define _overloadable __attribute__((overloadable))
73
74 int ovl1(const char *n) _overloadable _diagnose_if(n, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
75 int ovl1(void *m) _overloadable;
76
77 int ovl2(const char *n) _overloadable _diagnose_if(n, "oh no", "error"); // expected-note{{candidate function}}
78 int ovl2(char *m) _overloadable; // expected-note{{candidate function}}
79 void overloadsYay(void) {
80 ovl1((void *)0);
81 ovl1(""); // expected-error{{oh no}}
82
83 ovl2((void *)0); // expected-error{{ambiguous}}
84 }
85
86 void errorWarnDiagnose1(void) _diagnose_if(1, "oh no", "error") // expected-note{{from 'diagnose_if'}}
87 _diagnose_if(1, "nop", "warning");
88 void errorWarnDiagnose2(void) _diagnose_if(1, "oh no", "error") // expected-note{{from 'diagnose_if'}}
89 _diagnose_if(1, "nop", "error");
90 void errorWarnDiagnose3(void) _diagnose_if(1, "nop", "warning")
91 _diagnose_if(1, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
92
93 void errorWarnDiagnoseArg1(int a) _diagnose_if(a == 1, "oh no", "error") // expected-note{{from 'diagnose_if'}}
94 _diagnose_if(a == 1, "nop", "warning");
95 void errorWarnDiagnoseArg2(int a) _diagnose_if(a == 1, "oh no", "error") // expected-note{{from 'diagnose_if'}}
96 _diagnose_if(a == 1, "nop", "error");
97 void errorWarnDiagnoseArg3(int a) _diagnose_if(a == 1, "nop", "warning")
98 _diagnose_if(a == 1, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
99
100 void runErrorWarnDiagnose(void) {
101 errorWarnDiagnose1(); // expected-error{{oh no}}
102 errorWarnDiagnose2(); // expected-error{{oh no}}
103 errorWarnDiagnose3(); // expected-error{{oh no}}
104
105 errorWarnDiagnoseArg1(1); // expected-error{{oh no}}
106 errorWarnDiagnoseArg2(1); // expected-error{{oh no}}
107 errorWarnDiagnoseArg3(1); // expected-error{{oh no}}
108 }
109
110 void warnWarnDiagnose(void) _diagnose_if(1, "oh no!", "warning") _diagnose_if(1, "foo", "warning"); // expected-note 2{{from 'diagnose_if'}}
111 void runWarnWarnDiagnose(void) {
112 warnWarnDiagnose(); // expected-warning{{oh no!}} expected-warning{{foo}}
113 }
114
115 void declsStackErr1(int a) _diagnose_if(a & 1, "decl1", "error"); // expected-note 2{{from 'diagnose_if'}}
116 void declsStackErr1(int a) _diagnose_if(a & 2, "decl2", "error"); // expected-note{{from 'diagnose_if'}}
117 void declsStackErr2(void);
118 void declsStackErr2(void) _diagnose_if(1, "complaint", "error"); // expected-note{{from 'diagnose_if'}}
119 void declsStackErr3(void) _diagnose_if(1, "complaint", "error"); // expected-note{{from 'diagnose_if'}}
120 void declsStackErr3(void);
121 void runDeclsStackErr(void) {
122 declsStackErr1(0);
123 declsStackErr1(1); // expected-error{{decl1}}
124 declsStackErr1(2); // expected-error{{decl2}}
125 declsStackErr1(3); // expected-error{{decl1}}
126 declsStackErr2(); // expected-error{{complaint}}
127 declsStackErr3(); // expected-error{{complaint}}
128 }
129
130 void declsStackWarn1(int a) _diagnose_if(a & 1, "decl1", "warning"); // expected-note 2{{from 'diagnose_if'}}
131 void declsStackWarn1(int a) _diagnose_if(a & 2, "decl2", "warning"); // expected-note 2{{from 'diagnose_if'}}
132 void declsStackWarn2(void);
133 void declsStackWarn2(void) _diagnose_if(1, "complaint", "warning"); // expected-note{{from 'diagnose_if'}}
134 void declsStackWarn3(void) _diagnose_if(1, "complaint", "warning"); // expected-note{{from 'diagnose_if'}}
135 void declsStackWarn3(void);
136 void runDeclsStackWarn(void) {
137 declsStackWarn1(0);
138 declsStackWarn1(1); // expected-warning{{decl1}}
139 declsStackWarn1(2); // expected-warning{{decl2}}
140 declsStackWarn1(3); // expected-warning{{decl1}} expected-warning{{decl2}}
141 declsStackWarn2(); // expected-warning{{complaint}}
142 declsStackWarn3(); // expected-warning{{complaint}}
143 }
144
145 void noMsg(int n) _diagnose_if(n, "", "warning"); // expected-note{{from 'diagnose_if'}}
146 void runNoMsg(void) {
147 noMsg(1); // expected-warning{{<no message provided>}}
148 }
149
150 void alwaysWarnWithArg(int a) _diagnose_if(1 || a, "alwaysWarn", "warning"); // expected-note{{from 'diagnose_if'}}
151 void runAlwaysWarnWithArg(int a) {
152 alwaysWarnWithArg(a); // expected-warning{{alwaysWarn}}
153 }
154
155 // Test that diagnose_if warnings generated in system headers are not ignored.
156 #include "Inputs/diagnose-if-warn-system-header.h"
157
158 // Bug: we would complain about `a` being undeclared if this was spelled
159 // __diagnose_if__.
160 void underbarName(int a) __attribute__((__diagnose_if__(a, "", "warning")));
161
162 // PR38095
163 void constCharStar(const char *str) __attribute__((__diagnose_if__(!str[0], "empty string not allowed", "error"))); // expected-note {{from}}
164 void charStar(char *str) __attribute__((__diagnose_if__(!str[0], "empty string not allowed", "error"))); // expected-note {{from}}
165 void runConstCharStar(void) {
166 constCharStar("foo");
167 charStar("bar");
168 constCharStar(""); // expected-error {{empty string not allowed}}
169 charStar(""); // expected-error {{empty string not allowed}}
170 }