blob: b4bb2ecd0d20208fff783f5d74efe4ce354dee05 [file] [log] [blame]
Nick Lewycky36e9b3c2014-01-21 04:31:12 +00001// RUN: %clang_cc1 %s -verify
2// RUN: %clang_cc1 %s -DCODEGEN -emit-llvm -o - | FileCheck %s
Nick Lewycky35a6ef42014-01-11 02:50:57 +00003
4#define O_CREAT 0x100
5typedef int mode_t;
6typedef unsigned long size_t;
7
George Burgess IV2a6150d2015-10-16 01:17:38 +00008const int TRUE = 1;
9
Nick Lewycky35a6ef42014-01-11 02:50:57 +000010int 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}}
11int open(const char *pathname, int flags, mode_t mode) __attribute__((overloadable)); // expected-note{{candidate function not viable: requires 3 arguments, but 2 were provided}}
12
13void test1() {
14#ifndef CODEGEN
15 open("path", O_CREAT); // expected-error{{no matching function for call to 'open'}}
16#endif
17 open("path", O_CREAT, 0660);
18 open("path", 0);
19 open("path", 0, 0);
20}
21
22size_t __strnlen_chk(const char *s, size_t requested_amount, size_t s_len);
23
Erik Pilkington13ee62f2019-03-20 19:26:33 +000024size_t strnlen(const char *s, size_t maxlen)
Nick Lewycky35a6ef42014-01-11 02:50:57 +000025 __attribute__((overloadable))
26 __asm__("strnlen_real1");
27
28__attribute__((always_inline))
Erik Pilkington13ee62f2019-03-20 19:26:33 +000029inline size_t strnlen(const char *s, size_t maxlen)
Nick Lewycky35a6ef42014-01-11 02:50:57 +000030 __attribute__((overloadable))
31 __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
32 "chosen when target buffer size is known")))
33{
34 return __strnlen_chk(s, maxlen, __builtin_object_size(s, 0));
35}
36
Erik Pilkington13ee62f2019-03-20 19:26:33 +000037size_t strnlen(const char *s, size_t maxlen)
Nick Lewycky35a6ef42014-01-11 02:50:57 +000038 __attribute__((overloadable))
39 __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
40 "chosen when target buffer size is known")))
41 __attribute__((enable_if(maxlen <= __builtin_object_size(s, 0),
42 "chosen when 'maxlen' is known to be less than or equal to the buffer size")))
43 __asm__("strnlen_real2");
44
Erik Pilkington13ee62f2019-03-20 19:26:33 +000045size_t strnlen(const char *s, size_t maxlen) // expected-note {{'strnlen' has been explicitly marked unavailable here}}
Nick Lewycky35a6ef42014-01-11 02:50:57 +000046 __attribute__((overloadable))
47 __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
48 "chosen when target buffer size is known")))
49 __attribute__((enable_if(maxlen > __builtin_object_size(s, 0),
50 "chosen when 'maxlen' is larger than the buffer size")))
51 __attribute__((unavailable("'maxlen' is larger than the buffer size")));
52
53void test2(const char *s, int i) {
David Blaikieea3e51d2015-06-29 17:29:50 +000054// CHECK: define {{.*}}void @test2
Nick Lewycky35a6ef42014-01-11 02:50:57 +000055 const char c[123];
56 strnlen(s, i);
57// CHECK: call {{.*}}strnlen_real1
58 strnlen(s, 999);
59// CHECK: call {{.*}}strnlen_real1
60 strnlen(c, 1);
61// CHECK: call {{.*}}strnlen_real2
62 strnlen(c, i);
63// CHECK: call {{.*}}strnlen_chk
64#ifndef CODEGEN
Erik Pilkington13ee62f2019-03-20 19:26:33 +000065 strnlen(c, 999); // expected-error{{'strnlen' is unavailable: 'maxlen' is larger than the buffer size}}
Nick Lewycky35a6ef42014-01-11 02:50:57 +000066#endif
67}
68
Erik Pilkington13ee62f2019-03-20 19:26:33 +000069int isdigit(int c) __attribute__((overloadable));
70int isdigit(int c) __attribute__((overloadable)) // expected-note {{'isdigit' has been explicitly marked unavailable here}}
Nick Lewycky35a6ef42014-01-11 02:50:57 +000071 __attribute__((enable_if(c <= -1 || c > 255, "'c' must have the value of an unsigned char or EOF")))
72 __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
73
74void test3(int c) {
Aaron Ballman781fda92016-05-04 21:08:13 +000075 isdigit(c); // expected-warning{{ignoring return value of function declared with pure attribute}}
76 isdigit(10); // expected-warning{{ignoring return value of function declared with pure attribute}}
Nick Lewycky35a6ef42014-01-11 02:50:57 +000077#ifndef CODEGEN
Erik Pilkington13ee62f2019-03-20 19:26:33 +000078 isdigit(-10); // expected-error{{'isdigit' is unavailable: 'c' must have the value of an unsigned char or EOF}}
Nick Lewycky35a6ef42014-01-11 02:50:57 +000079#endif
80}
81
Ulrich Weigandef5aa292015-07-13 14:13:01 +000082// Verify that the alternate spelling __enable_if__ works as well.
Erik Pilkington13ee62f2019-03-20 19:26:33 +000083int isdigit2(int c) __attribute__((overloadable));
84int isdigit2(int c) __attribute__((overloadable)) // expected-note {{'isdigit2' has been explicitly marked unavailable here}}
Ulrich Weigandef5aa292015-07-13 14:13:01 +000085 __attribute__((__enable_if__(c <= -1 || c > 255, "'c' must have the value of an unsigned char or EOF")))
86 __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
87
88void test4(int c) {
89 isdigit2(c);
90 isdigit2(10);
91#ifndef CODEGEN
Erik Pilkington13ee62f2019-03-20 19:26:33 +000092 isdigit2(-10); // expected-error{{'isdigit2' is unavailable: 'c' must have the value of an unsigned char or EOF}}
Ulrich Weigandef5aa292015-07-13 14:13:01 +000093#endif
94}
95
George Burgess IV5f21c712015-10-12 19:57:04 +000096void test5() {
97 int (*p1)(int) = &isdigit2;
98 int (*p2)(int) = isdigit2;
99 void *p3 = (void *)&isdigit2;
100 void *p4 = (void *)isdigit2;
101}
Ulrich Weigandef5aa292015-07-13 14:13:01 +0000102
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000103#ifndef CODEGEN
104__attribute__((enable_if(n == 0, "chosen when 'n' is zero"))) void f1(int n); // expected-error{{use of undeclared identifier 'n'}}
105
106int n __attribute__((enable_if(1, "always chosen"))); // expected-warning{{'enable_if' attribute only applies to functions}}
107
108void f(int n) __attribute__((enable_if("chosen when 'n' is zero", n == 0))); // expected-error{{'enable_if' attribute requires a string}}
109
110void f(int n) __attribute__((enable_if())); // expected-error{{'enable_if' attribute requires exactly 2 arguments}}
111
112void f(int n) __attribute__((enable_if(unresolvedid, "chosen when 'unresolvedid' is non-zero"))); // expected-error{{use of undeclared identifier 'unresolvedid'}}
113
114int global;
115void 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}}
116
117const int cst = 7;
118void return_cst(void) __attribute__((overloadable)) __attribute__((enable_if(cst == 7, "chosen when 'cst' is 7")));
119void test_return_cst() { return_cst(); }
George Burgess IV5f21c712015-10-12 19:57:04 +0000120
121void f2(void) __attribute__((overloadable)) __attribute__((enable_if(1, "always chosen")));
122void f2(void) __attribute__((overloadable)) __attribute__((enable_if(0, "never chosen")));
George Burgess IV2a6150d2015-10-16 01:17:38 +0000123void f2(void) __attribute__((overloadable)) __attribute__((enable_if(TRUE, "always chosen #2")));
George Burgess IV5f21c712015-10-12 19:57:04 +0000124void test6() {
George Burgess IV2a6150d2015-10-16 01:17:38 +0000125 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}}
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}}
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}}
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}}
George Burgess IV5f21c712015-10-12 19:57:04 +0000129}
130
131void f3(int m) __attribute__((overloadable)) __attribute__((enable_if(m >= 0, "positive")));
132void f3(int m) __attribute__((overloadable)) __attribute__((enable_if(m < 0, "negative")));
133void test7() {
George Burgess IV2a6150d2015-10-16 01:17:38 +0000134 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}}
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}}
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}}
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}}
George Burgess IV5f21c712015-10-12 19:57:04 +0000138}
George Burgess IVc3ec9502015-12-03 20:54:58 +0000139
140void f4(int m) __attribute__((enable_if(0, "")));
141void test8() {
Sylvestre Ledru1f63b7e2017-01-14 12:00:40 +0000142 void (*p1)(int) = &f4; // expected-error{{cannot take address of function 'f4' because it has one or more non-tautological enable_if conditions}}
143 void (*p2)(int) = f4; // expected-error{{cannot take address of function 'f4' because it has one or more non-tautological enable_if conditions}}
George Burgess IVc3ec9502015-12-03 20:54:58 +0000144}
George Burgess IV21d3bff2016-03-31 00:16:25 +0000145
146void regular_enable_if(int a) __attribute__((enable_if(a, ""))); // expected-note 3{{declared here}}
147void PR27122_ext() {
148 regular_enable_if(0, 2); // expected-error{{too many arguments}}
149 regular_enable_if(1, 2); // expected-error{{too many arguments}}
150 regular_enable_if(); // expected-error{{too few arguments}}
151}
George Burgess IV53b938d2016-08-12 04:12:31 +0000152
153// We had a bug where we'd crash upon trying to evaluate varargs.
154void variadic_enable_if(int a, ...) __attribute__((enable_if(a, ""))); // expected-note 6 {{disabled}}
155void variadic_test() {
156 variadic_enable_if(1);
157 variadic_enable_if(1, 2);
158 variadic_enable_if(1, "c", 3);
159
160 variadic_enable_if(0); // expected-error{{no matching}}
161 variadic_enable_if(0, 2); // expected-error{{no matching}}
162 variadic_enable_if(0, "c", 3); // expected-error{{no matching}}
163
164 int m;
165 variadic_enable_if(1);
166 variadic_enable_if(1, m);
167 variadic_enable_if(1, m, "c");
168
169 variadic_enable_if(0); // expected-error{{no matching}}
170 variadic_enable_if(0, m); // expected-error{{no matching}}
171 variadic_enable_if(0, m, 3); // expected-error{{no matching}}
172}
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000173#endif