annotate clang/test/Sema/enable_if.c @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 1d019706d866
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 %s -verify
anatofuz
parents:
diff changeset
2 // RUN: %clang_cc1 %s -DCODEGEN -emit-llvm -o - | FileCheck %s
anatofuz
parents:
diff changeset
3
anatofuz
parents:
diff changeset
4 #define O_CREAT 0x100
anatofuz
parents:
diff changeset
5 typedef int mode_t;
anatofuz
parents:
diff changeset
6 typedef unsigned long size_t;
anatofuz
parents:
diff changeset
7
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
8 enum { TRUE = 1 };
150
anatofuz
parents:
diff changeset
9
anatofuz
parents:
diff changeset
10 int open(const char *pathname, int flags) __attribute__((enable_if(!(flags & O_CREAT), "must specify mode when using O_CREAT"))) __attribute__((overloadable)); // expected-note{{candidate disabled: must specify mode when using O_CREAT}}
anatofuz
parents:
diff changeset
11 int open(const char *pathname, int flags, mode_t mode) __attribute__((overloadable)); // expected-note{{candidate function not viable: requires 3 arguments, but 2 were provided}}
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 void test1() {
anatofuz
parents:
diff changeset
14 #ifndef CODEGEN
anatofuz
parents:
diff changeset
15 open("path", O_CREAT); // expected-error{{no matching function for call to 'open'}}
anatofuz
parents:
diff changeset
16 #endif
anatofuz
parents:
diff changeset
17 open("path", O_CREAT, 0660);
anatofuz
parents:
diff changeset
18 open("path", 0);
anatofuz
parents:
diff changeset
19 open("path", 0, 0);
anatofuz
parents:
diff changeset
20 }
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 size_t __strnlen_chk(const char *s, size_t requested_amount, size_t s_len);
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 size_t strnlen(const char *s, size_t maxlen)
anatofuz
parents:
diff changeset
25 __attribute__((overloadable))
anatofuz
parents:
diff changeset
26 __asm__("strnlen_real1");
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 __attribute__((always_inline))
anatofuz
parents:
diff changeset
29 inline size_t strnlen(const char *s, size_t maxlen)
anatofuz
parents:
diff changeset
30 __attribute__((overloadable))
anatofuz
parents:
diff changeset
31 __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
anatofuz
parents:
diff changeset
32 "chosen when target buffer size is known")))
anatofuz
parents:
diff changeset
33 {
anatofuz
parents:
diff changeset
34 return __strnlen_chk(s, maxlen, __builtin_object_size(s, 0));
anatofuz
parents:
diff changeset
35 }
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 size_t strnlen(const char *s, size_t maxlen)
anatofuz
parents:
diff changeset
38 __attribute__((overloadable))
anatofuz
parents:
diff changeset
39 __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
anatofuz
parents:
diff changeset
40 "chosen when target buffer size is known")))
anatofuz
parents:
diff changeset
41 __attribute__((enable_if(maxlen <= __builtin_object_size(s, 0),
anatofuz
parents:
diff changeset
42 "chosen when 'maxlen' is known to be less than or equal to the buffer size")))
anatofuz
parents:
diff changeset
43 __asm__("strnlen_real2");
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 size_t strnlen(const char *s, size_t maxlen) // expected-note {{'strnlen' has been explicitly marked unavailable here}}
anatofuz
parents:
diff changeset
46 __attribute__((overloadable))
anatofuz
parents:
diff changeset
47 __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
anatofuz
parents:
diff changeset
48 "chosen when target buffer size is known")))
anatofuz
parents:
diff changeset
49 __attribute__((enable_if(maxlen > __builtin_object_size(s, 0),
anatofuz
parents:
diff changeset
50 "chosen when 'maxlen' is larger than the buffer size")))
anatofuz
parents:
diff changeset
51 __attribute__((unavailable("'maxlen' is larger than the buffer size")));
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 void test2(const char *s, int i) {
anatofuz
parents:
diff changeset
54 // CHECK: define {{.*}}void @test2
anatofuz
parents:
diff changeset
55 const char c[123];
anatofuz
parents:
diff changeset
56 strnlen(s, i);
anatofuz
parents:
diff changeset
57 // CHECK: call {{.*}}strnlen_real1
anatofuz
parents:
diff changeset
58 strnlen(s, 999);
anatofuz
parents:
diff changeset
59 // CHECK: call {{.*}}strnlen_real1
anatofuz
parents:
diff changeset
60 strnlen(c, 1);
anatofuz
parents:
diff changeset
61 // CHECK: call {{.*}}strnlen_real2
anatofuz
parents:
diff changeset
62 strnlen(c, i);
anatofuz
parents:
diff changeset
63 // CHECK: call {{.*}}strnlen_chk
anatofuz
parents:
diff changeset
64 #ifndef CODEGEN
anatofuz
parents:
diff changeset
65 strnlen(c, 999); // expected-error{{'strnlen' is unavailable: 'maxlen' is larger than the buffer size}}
anatofuz
parents:
diff changeset
66 #endif
anatofuz
parents:
diff changeset
67 }
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 int isdigit(int c) __attribute__((overloadable));
anatofuz
parents:
diff changeset
70 int isdigit(int c) __attribute__((overloadable)) // expected-note {{'isdigit' has been explicitly marked unavailable here}}
anatofuz
parents:
diff changeset
71 __attribute__((enable_if(c <= -1 || c > 255, "'c' must have the value of an unsigned char or EOF")))
anatofuz
parents:
diff changeset
72 __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 void test3(int c) {
anatofuz
parents:
diff changeset
75 isdigit(c); // expected-warning{{ignoring return value of function declared with pure attribute}}
anatofuz
parents:
diff changeset
76 isdigit(10); // expected-warning{{ignoring return value of function declared with pure attribute}}
anatofuz
parents:
diff changeset
77 #ifndef CODEGEN
anatofuz
parents:
diff changeset
78 isdigit(-10); // expected-error{{'isdigit' is unavailable: 'c' must have the value of an unsigned char or EOF}}
anatofuz
parents:
diff changeset
79 #endif
anatofuz
parents:
diff changeset
80 }
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82 // Verify that the alternate spelling __enable_if__ works as well.
anatofuz
parents:
diff changeset
83 int isdigit2(int c) __attribute__((overloadable));
anatofuz
parents:
diff changeset
84 int isdigit2(int c) __attribute__((overloadable)) // expected-note {{'isdigit2' has been explicitly marked unavailable here}}
anatofuz
parents:
diff changeset
85 __attribute__((__enable_if__(c <= -1 || c > 255, "'c' must have the value of an unsigned char or EOF")))
anatofuz
parents:
diff changeset
86 __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 void test4(int c) {
anatofuz
parents:
diff changeset
89 isdigit2(c);
anatofuz
parents:
diff changeset
90 isdigit2(10);
anatofuz
parents:
diff changeset
91 #ifndef CODEGEN
anatofuz
parents:
diff changeset
92 isdigit2(-10); // expected-error{{'isdigit2' is unavailable: 'c' must have the value of an unsigned char or EOF}}
anatofuz
parents:
diff changeset
93 #endif
anatofuz
parents:
diff changeset
94 }
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 void test5() {
anatofuz
parents:
diff changeset
97 int (*p1)(int) = &isdigit2;
anatofuz
parents:
diff changeset
98 int (*p2)(int) = isdigit2;
anatofuz
parents:
diff changeset
99 void *p3 = (void *)&isdigit2;
anatofuz
parents:
diff changeset
100 void *p4 = (void *)isdigit2;
anatofuz
parents:
diff changeset
101 }
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 #ifndef CODEGEN
anatofuz
parents:
diff changeset
104 __attribute__((enable_if(n == 0, "chosen when 'n' is zero"))) void f1(int n); // expected-error{{use of undeclared identifier 'n'}}
anatofuz
parents:
diff changeset
105
anatofuz
parents:
diff changeset
106 int n __attribute__((enable_if(1, "always chosen"))); // expected-warning{{'enable_if' attribute only applies to functions}}
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 void f(int n) __attribute__((enable_if("chosen when 'n' is zero", n == 0))); // expected-error{{'enable_if' attribute requires a string}}
anatofuz
parents:
diff changeset
109
anatofuz
parents:
diff changeset
110 void f(int n) __attribute__((enable_if())); // expected-error{{'enable_if' attribute requires exactly 2 arguments}}
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 void f(int n) __attribute__((enable_if(unresolvedid, "chosen when 'unresolvedid' is non-zero"))); // expected-error{{use of undeclared identifier 'unresolvedid'}}
anatofuz
parents:
diff changeset
113
anatofuz
parents:
diff changeset
114 int global;
anatofuz
parents:
diff changeset
115 void f(int n) __attribute__((enable_if(global == 0, "chosen when 'global' is zero"))); // expected-error{{'enable_if' attribute expression never produces a constant expression}} // expected-note{{subexpression not valid in a constant expression}}
anatofuz
parents:
diff changeset
116
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
117 enum { cst = 7 };
150
anatofuz
parents:
diff changeset
118 void return_cst(void) __attribute__((overloadable)) __attribute__((enable_if(cst == 7, "chosen when 'cst' is 7")));
anatofuz
parents:
diff changeset
119 void test_return_cst() { return_cst(); }
anatofuz
parents:
diff changeset
120
anatofuz
parents:
diff changeset
121 void f2(void) __attribute__((overloadable)) __attribute__((enable_if(1, "always chosen")));
anatofuz
parents:
diff changeset
122 void f2(void) __attribute__((overloadable)) __attribute__((enable_if(0, "never chosen")));
anatofuz
parents:
diff changeset
123 void f2(void) __attribute__((overloadable)) __attribute__((enable_if(TRUE, "always chosen #2")));
anatofuz
parents:
diff changeset
124 void test6() {
anatofuz
parents:
diff changeset
125 void (*p1)(void) = &f2; // expected-error{{initializing 'void (*)(void)' with an expression of incompatible type '<overloaded function type>'}} expected-note@121{{candidate function}} expected-note@122{{candidate function made ineligible by enable_if}} expected-note@123{{candidate function}}
anatofuz
parents:
diff changeset
126 void (*p2)(void) = f2; // expected-error{{initializing 'void (*)(void)' with an expression of incompatible type '<overloaded function type>'}} expected-note@121{{candidate function}} expected-note@122{{candidate function made ineligible by enable_if}} expected-note@123{{candidate function}}
anatofuz
parents:
diff changeset
127 void *p3 = (void*)&f2; // expected-error{{address of overloaded function 'f2' is ambiguous}} expected-note@121{{candidate function}} expected-note@122{{candidate function made ineligible by enable_if}} expected-note@123{{candidate function}}
anatofuz
parents:
diff changeset
128 void *p4 = (void*)f2; // expected-error{{address of overloaded function 'f2' is ambiguous}} expected-note@121{{candidate function}} expected-note@122{{candidate function made ineligible by enable_if}} expected-note@123{{candidate function}}
anatofuz
parents:
diff changeset
129 }
anatofuz
parents:
diff changeset
130
anatofuz
parents:
diff changeset
131 void f3(int m) __attribute__((overloadable)) __attribute__((enable_if(m >= 0, "positive")));
anatofuz
parents:
diff changeset
132 void f3(int m) __attribute__((overloadable)) __attribute__((enable_if(m < 0, "negative")));
anatofuz
parents:
diff changeset
133 void test7() {
anatofuz
parents:
diff changeset
134 void (*p1)(int) = &f3; // expected-error{{initializing 'void (*)(int)' with an expression of incompatible type '<overloaded function type>'}} expected-note@131{{candidate function made ineligible by enable_if}} expected-note@132{{candidate function made ineligible by enable_if}}
anatofuz
parents:
diff changeset
135 void (*p2)(int) = f3; // expected-error{{initializing 'void (*)(int)' with an expression of incompatible type '<overloaded function type>'}} expected-note@131{{candidate function made ineligible by enable_if}} expected-note@132{{candidate function made ineligible by enable_if}}
anatofuz
parents:
diff changeset
136 void *p3 = (void*)&f3; // expected-error{{address of overloaded function 'f3' does not match required type 'void'}} expected-note@131{{candidate function made ineligible by enable_if}} expected-note@132{{candidate function made ineligible by enable_if}}
anatofuz
parents:
diff changeset
137 void *p4 = (void*)f3; // expected-error{{address of overloaded function 'f3' does not match required type 'void'}} expected-note@131{{candidate function made ineligible by enable_if}} expected-note@132{{candidate function made ineligible by enable_if}}
anatofuz
parents:
diff changeset
138 }
anatofuz
parents:
diff changeset
139
anatofuz
parents:
diff changeset
140 void f4(int m) __attribute__((enable_if(0, "")));
anatofuz
parents:
diff changeset
141 void test8() {
anatofuz
parents:
diff changeset
142 void (*p1)(int) = &f4; // expected-error{{cannot take address of function 'f4' because it has one or more non-tautological enable_if conditions}}
anatofuz
parents:
diff changeset
143 void (*p2)(int) = f4; // expected-error{{cannot take address of function 'f4' because it has one or more non-tautological enable_if conditions}}
anatofuz
parents:
diff changeset
144 }
anatofuz
parents:
diff changeset
145
anatofuz
parents:
diff changeset
146 void regular_enable_if(int a) __attribute__((enable_if(a, ""))); // expected-note 3{{declared here}}
anatofuz
parents:
diff changeset
147 void PR27122_ext() {
anatofuz
parents:
diff changeset
148 regular_enable_if(0, 2); // expected-error{{too many arguments}}
anatofuz
parents:
diff changeset
149 regular_enable_if(1, 2); // expected-error{{too many arguments}}
anatofuz
parents:
diff changeset
150 regular_enable_if(); // expected-error{{too few arguments}}
anatofuz
parents:
diff changeset
151 }
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 // We had a bug where we'd crash upon trying to evaluate varargs.
anatofuz
parents:
diff changeset
154 void variadic_enable_if(int a, ...) __attribute__((enable_if(a, ""))); // expected-note 6 {{disabled}}
anatofuz
parents:
diff changeset
155 void variadic_test() {
anatofuz
parents:
diff changeset
156 variadic_enable_if(1);
anatofuz
parents:
diff changeset
157 variadic_enable_if(1, 2);
anatofuz
parents:
diff changeset
158 variadic_enable_if(1, "c", 3);
anatofuz
parents:
diff changeset
159
anatofuz
parents:
diff changeset
160 variadic_enable_if(0); // expected-error{{no matching}}
anatofuz
parents:
diff changeset
161 variadic_enable_if(0, 2); // expected-error{{no matching}}
anatofuz
parents:
diff changeset
162 variadic_enable_if(0, "c", 3); // expected-error{{no matching}}
anatofuz
parents:
diff changeset
163
anatofuz
parents:
diff changeset
164 int m;
anatofuz
parents:
diff changeset
165 variadic_enable_if(1);
anatofuz
parents:
diff changeset
166 variadic_enable_if(1, m);
anatofuz
parents:
diff changeset
167 variadic_enable_if(1, m, "c");
anatofuz
parents:
diff changeset
168
anatofuz
parents:
diff changeset
169 variadic_enable_if(0); // expected-error{{no matching}}
anatofuz
parents:
diff changeset
170 variadic_enable_if(0, m); // expected-error{{no matching}}
anatofuz
parents:
diff changeset
171 variadic_enable_if(0, m, 3); // expected-error{{no matching}}
anatofuz
parents:
diff changeset
172 }
anatofuz
parents:
diff changeset
173 #endif