xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/enable_if.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -verify
2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -DCODEGEN -emit-llvm -o - | FileCheck %s
3*0a6a1f1dSLionel Sambuc 
4*0a6a1f1dSLionel Sambuc #define O_CREAT 0x100
5*0a6a1f1dSLionel Sambuc typedef int mode_t;
6*0a6a1f1dSLionel Sambuc typedef unsigned long size_t;
7*0a6a1f1dSLionel Sambuc 
8*0a6a1f1dSLionel Sambuc 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}}
9*0a6a1f1dSLionel Sambuc 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}}
10*0a6a1f1dSLionel Sambuc 
test1()11*0a6a1f1dSLionel Sambuc void test1() {
12*0a6a1f1dSLionel Sambuc #ifndef CODEGEN
13*0a6a1f1dSLionel Sambuc   open("path", O_CREAT);  // expected-error{{no matching function for call to 'open'}}
14*0a6a1f1dSLionel Sambuc #endif
15*0a6a1f1dSLionel Sambuc   open("path", O_CREAT, 0660);
16*0a6a1f1dSLionel Sambuc   open("path", 0);
17*0a6a1f1dSLionel Sambuc   open("path", 0, 0);
18*0a6a1f1dSLionel Sambuc }
19*0a6a1f1dSLionel Sambuc 
20*0a6a1f1dSLionel Sambuc size_t __strnlen_chk(const char *s, size_t requested_amount, size_t s_len);
21*0a6a1f1dSLionel Sambuc 
22*0a6a1f1dSLionel Sambuc size_t strnlen(const char *s, size_t maxlen)  // expected-note{{candidate function}}
23*0a6a1f1dSLionel Sambuc   __attribute__((overloadable))
24*0a6a1f1dSLionel Sambuc   __asm__("strnlen_real1");
25*0a6a1f1dSLionel Sambuc 
26*0a6a1f1dSLionel Sambuc __attribute__((always_inline))
strnlen(const char * s,size_t maxlen)27*0a6a1f1dSLionel Sambuc inline size_t strnlen(const char *s, size_t maxlen)  // expected-note{{candidate function}}
28*0a6a1f1dSLionel Sambuc   __attribute__((overloadable))
29*0a6a1f1dSLionel Sambuc   __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
30*0a6a1f1dSLionel Sambuc                            "chosen when target buffer size is known")))
31*0a6a1f1dSLionel Sambuc {
32*0a6a1f1dSLionel Sambuc   return __strnlen_chk(s, maxlen, __builtin_object_size(s, 0));
33*0a6a1f1dSLionel Sambuc }
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc size_t strnlen(const char *s, size_t maxlen)  // expected-note{{candidate disabled: chosen when 'maxlen' is known to be less than or equal to the buffer size}}
36*0a6a1f1dSLionel Sambuc   __attribute__((overloadable))
37*0a6a1f1dSLionel Sambuc   __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
38*0a6a1f1dSLionel Sambuc                            "chosen when target buffer size is known")))
39*0a6a1f1dSLionel Sambuc   __attribute__((enable_if(maxlen <= __builtin_object_size(s, 0),
40*0a6a1f1dSLionel Sambuc                            "chosen when 'maxlen' is known to be less than or equal to the buffer size")))
41*0a6a1f1dSLionel Sambuc   __asm__("strnlen_real2");
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc size_t strnlen(const char *s, size_t maxlen)  // expected-note{{candidate function has been explicitly made unavailable}}
44*0a6a1f1dSLionel Sambuc   __attribute__((overloadable))
45*0a6a1f1dSLionel Sambuc   __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
46*0a6a1f1dSLionel Sambuc                            "chosen when target buffer size is known")))
47*0a6a1f1dSLionel Sambuc   __attribute__((enable_if(maxlen > __builtin_object_size(s, 0),
48*0a6a1f1dSLionel Sambuc                            "chosen when 'maxlen' is larger than the buffer size")))
49*0a6a1f1dSLionel Sambuc   __attribute__((unavailable("'maxlen' is larger than the buffer size")));
50*0a6a1f1dSLionel Sambuc 
test2(const char * s,int i)51*0a6a1f1dSLionel Sambuc void test2(const char *s, int i) {
52*0a6a1f1dSLionel Sambuc // CHECK: define void @test2
53*0a6a1f1dSLionel Sambuc   const char c[123];
54*0a6a1f1dSLionel Sambuc   strnlen(s, i);
55*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}}strnlen_real1
56*0a6a1f1dSLionel Sambuc   strnlen(s, 999);
57*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}}strnlen_real1
58*0a6a1f1dSLionel Sambuc   strnlen(c, 1);
59*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}}strnlen_real2
60*0a6a1f1dSLionel Sambuc   strnlen(c, i);
61*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}}strnlen_chk
62*0a6a1f1dSLionel Sambuc #ifndef CODEGEN
63*0a6a1f1dSLionel Sambuc   strnlen(c, 999);  // expected-error{{call to unavailable function 'strnlen': 'maxlen' is larger than the buffer size}}
64*0a6a1f1dSLionel Sambuc #endif
65*0a6a1f1dSLionel Sambuc }
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc int isdigit(int c) __attribute__((overloadable));  // expected-note{{candidate function}}
68*0a6a1f1dSLionel Sambuc int isdigit(int c) __attribute__((overloadable))  // expected-note{{candidate function has been explicitly made unavailable}}
69*0a6a1f1dSLionel Sambuc   __attribute__((enable_if(c <= -1 || c > 255, "'c' must have the value of an unsigned char or EOF")))
70*0a6a1f1dSLionel Sambuc   __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
71*0a6a1f1dSLionel Sambuc 
test3(int c)72*0a6a1f1dSLionel Sambuc void test3(int c) {
73*0a6a1f1dSLionel Sambuc   isdigit(c);
74*0a6a1f1dSLionel Sambuc   isdigit(10);
75*0a6a1f1dSLionel Sambuc #ifndef CODEGEN
76*0a6a1f1dSLionel Sambuc   isdigit(-10);  // expected-error{{call to unavailable function 'isdigit': 'c' must have the value of an unsigned char or EOF}}
77*0a6a1f1dSLionel Sambuc #endif
78*0a6a1f1dSLionel Sambuc }
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc #ifndef CODEGEN
81*0a6a1f1dSLionel Sambuc __attribute__((enable_if(n == 0, "chosen when 'n' is zero"))) void f1(int n); // expected-error{{use of undeclared identifier 'n'}}
82*0a6a1f1dSLionel Sambuc 
83*0a6a1f1dSLionel Sambuc int n __attribute__((enable_if(1, "always chosen"))); // expected-warning{{'enable_if' attribute only applies to functions}}
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc void f(int n) __attribute__((enable_if("chosen when 'n' is zero", n == 0)));  // expected-error{{'enable_if' attribute requires a string}}
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc void f(int n) __attribute__((enable_if()));  // expected-error{{'enable_if' attribute requires exactly 2 arguments}}
88*0a6a1f1dSLionel Sambuc 
89*0a6a1f1dSLionel Sambuc void f(int n) __attribute__((enable_if(unresolvedid, "chosen when 'unresolvedid' is non-zero")));  // expected-error{{use of undeclared identifier 'unresolvedid'}}
90*0a6a1f1dSLionel Sambuc 
91*0a6a1f1dSLionel Sambuc int global;
92*0a6a1f1dSLionel Sambuc 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}}
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc const int cst = 7;
95*0a6a1f1dSLionel Sambuc void return_cst(void) __attribute__((overloadable)) __attribute__((enable_if(cst == 7, "chosen when 'cst' is 7")));
test_return_cst()96*0a6a1f1dSLionel Sambuc void test_return_cst() { return_cst(); }
97*0a6a1f1dSLionel Sambuc #endif
98