diff options
Diffstat (limited to 'clang/test')
-rw-r--r-- | clang/test/CodeGen/enable_if.c | 51 | ||||
-rw-r--r-- | clang/test/Sema/enable_if.c | 25 | ||||
-rw-r--r-- | clang/test/SemaCXX/enable_if.cpp | 71 |
3 files changed, 147 insertions, 0 deletions
diff --git a/clang/test/CodeGen/enable_if.c b/clang/test/CodeGen/enable_if.c new file mode 100644 index 00000000000..d1173d291ad --- /dev/null +++ b/clang/test/CodeGen/enable_if.c @@ -0,0 +1,51 @@ +// RUN: %clang_cc1 -emit-llvm %s -o - -triple=x86_64-pc-linux-gnu | FileCheck %s + +// Verifying that we do, in fact, select the correct function in the following +// cases. + +void foo(int m) __attribute__((overloadable, enable_if(m > 0, ""))); +void foo(int m) __attribute__((overloadable)); + +// CHECK-LABEL: define void @test1 +void test1() { + // CHECK: store void (i32)* @_Z3fooi + void (*p)(int) = foo; + // CHECK: store void (i32)* @_Z3fooi + void (*p2)(int) = &foo; + // CHECK: store void (i32)* @_Z3fooi + p = foo; + // CHECK: store void (i32)* @_Z3fooi + p = &foo; + + // CHECK: store i8* bitcast (void (i32)* @_Z3fooi to i8*) + void *vp1 = (void*)&foo; + // CHECK: store i8* bitcast (void (i32)* @_Z3fooi to i8*) + void *vp2 = (void*)foo; + // CHECK: store i8* bitcast (void (i32)* @_Z3fooi to i8*) + vp1 = (void*)&foo; + // CHECK: store i8* bitcast (void (i32)* @_Z3fooi to i8*) + vp1 = (void*)foo; +} + +void bar(int m) __attribute__((overloadable, enable_if(m > 0, ""))); +void bar(int m) __attribute__((overloadable, enable_if(1, ""))); +// CHECK-LABEL: define void @test2 +void test2() { + // CHECK: store void (i32)* @_Z3barUa9enable_ifIXLi1EEEi + void (*p)(int) = bar; + // CHECK: store void (i32)* @_Z3barUa9enable_ifIXLi1EEEi + void (*p2)(int) = &bar; + // CHECK: store void (i32)* @_Z3barUa9enable_ifIXLi1EEEi + p = bar; + // CHECK: store void (i32)* @_Z3barUa9enable_ifIXLi1EEEi + p = &bar; + + // CHECK: store i8* bitcast (void (i32)* @_Z3barUa9enable_ifIXLi1EEEi to i8*) + void *vp1 = (void*)&bar; + // CHECK: store i8* bitcast (void (i32)* @_Z3barUa9enable_ifIXLi1EEEi to i8*) + void *vp2 = (void*)bar; + // CHECK: store i8* bitcast (void (i32)* @_Z3barUa9enable_ifIXLi1EEEi to i8*) + vp1 = (void*)&bar; + // CHECK: store i8* bitcast (void (i32)* @_Z3barUa9enable_ifIXLi1EEEi to i8*) + vp1 = (void*)bar; +} diff --git a/clang/test/Sema/enable_if.c b/clang/test/Sema/enable_if.c index 7faae43b577..82b902a7f59 100644 --- a/clang/test/Sema/enable_if.c +++ b/clang/test/Sema/enable_if.c @@ -91,6 +91,12 @@ void test4(int c) { #endif } +void test5() { + int (*p1)(int) = &isdigit2; + int (*p2)(int) = isdigit2; + void *p3 = (void *)&isdigit2; + void *p4 = (void *)isdigit2; +} #ifndef CODEGEN __attribute__((enable_if(n == 0, "chosen when 'n' is zero"))) void f1(int n); // expected-error{{use of undeclared identifier 'n'}} @@ -109,4 +115,23 @@ void f(int n) __attribute__((enable_if(global == 0, "chosen when 'global' is zer const int cst = 7; void return_cst(void) __attribute__((overloadable)) __attribute__((enable_if(cst == 7, "chosen when 'cst' is 7"))); void test_return_cst() { return_cst(); } + +void f2(void) __attribute__((overloadable)) __attribute__((enable_if(1, "always chosen"))); +void f2(void) __attribute__((overloadable)) __attribute__((enable_if(0, "never chosen"))); +void f2(void) __attribute__((overloadable)); +void test6() { + void (*p1)(void) = &f2; // expected-error{{initializing 'void (*)(void)' with an expression of incompatible type '<overloaded function type>'}} expected-note@119{{candidate function}} expected-note@120{{candidate function made ineligible by enable_if}} expected-note@121{{candidate function}} + void (*p2)(void) = f2; // expected-error{{initializing 'void (*)(void)' with an expression of incompatible type '<overloaded function type>'}} expected-note@119{{candidate function}} expected-note@120{{candidate function made ineligible by enable_if}} expected-note@121{{candidate function}} + void *p3 = (void*)&f2; // expected-error{{address of overloaded function 'f2' is ambiguous}} expected-note@119{{candidate function}} expected-note@120{{candidate function made ineligible by enable_if}} expected-note@121{{candidate function}} + void *p4 = (void*)f2; // expected-error{{address of overloaded function 'f2' is ambiguous}} expected-note@119{{candidate function}} expected-note@120{{candidate function made ineligible by enable_if}} expected-note@121{{candidate function}} +} + +void f3(int m) __attribute__((overloadable)) __attribute__((enable_if(m >= 0, "positive"))); +void f3(int m) __attribute__((overloadable)) __attribute__((enable_if(m < 0, "negative"))); +void test7() { + void (*p1)(int) = &f3; // expected-error{{initializing 'void (*)(int)' with an expression of incompatible type '<overloaded function type>'}} expected-note@129{{candidate function made ineligible by enable_if}} expected-note@130{{candidate function made ineligible by enable_if}} + void (*p2)(int) = f3; // expected-error{{initializing 'void (*)(int)' with an expression of incompatible type '<overloaded function type>'}} expected-note@129{{candidate function made ineligible by enable_if}} expected-note@130{{candidate function made ineligible by enable_if}} + void *p3 = (void*)&f3; // expected-error{{address of overloaded function 'f3' does not match required type 'void'}} expected-note@129{{candidate function made ineligible by enable_if}} expected-note@130{{candidate function made ineligible by enable_if}} + void *p4 = (void*)f3; // expected-error{{address of overloaded function 'f3' does not match required type 'void'}} expected-note@129{{candidate function made ineligible by enable_if}} expected-note@130{{candidate function made ineligible by enable_if}} +} #endif diff --git a/clang/test/SemaCXX/enable_if.cpp b/clang/test/SemaCXX/enable_if.cpp index 58460692b79..eb3b265776f 100644 --- a/clang/test/SemaCXX/enable_if.cpp +++ b/clang/test/SemaCXX/enable_if.cpp @@ -163,3 +163,74 @@ namespace PR20988 { fn3(sizeof(T) == 1); } } + +namespace FnPtrs { + int ovlFoo(int m) __attribute__((enable_if(m > 0, ""))); + int ovlFoo(int m); + + void test() { + // Assignment gives us a different code path than declarations, and `&foo` + // gives us a different code path than `foo` + int (*p)(int) = ovlFoo; + int (*p2)(int) = &ovlFoo; + int (*a)(int); + a = ovlFoo; + a = &ovlFoo; + } + + int ovlBar(int) __attribute__((enable_if(true, ""))); + int ovlBar(int m) __attribute__((enable_if(false, ""))); + void test2() { + int (*p)(int) = ovlBar; + int (*p2)(int) = &ovlBar; + int (*a)(int); + a = ovlBar; + a = &ovlBar; + } + + int ovlConflict(int m) __attribute__((enable_if(true, ""))); + int ovlConflict(int m); + void test3() { + int (*p)(int) = ovlConflict; // expected-error{{address of overloaded function 'ovlConflict' is ambiguous}} expected-note@191{{candidate function}} expected-note@192{{candidate function}} + int (*p2)(int) = &ovlConflict; // expected-error{{address of overloaded function 'ovlConflict' is ambiguous}} expected-note@191{{candidate function}} expected-note@192{{candidate function}} + int (*a)(int); + a = ovlConflict; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@191{{candidate function}} expected-note@192{{candidate function}} + a = &ovlConflict; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@191{{candidate function}} expected-note@192{{candidate function}} + } + + template <typename T> + T templated(T m) __attribute__((enable_if(true, ""))) { return T(); } + template <typename T> + T templated(T m) __attribute__((enable_if(false, ""))) { return T(); } + void test4() { + int (*p)(int) = templated<int>; + int (*p2)(int) = &templated<int>; + int (*a)(int); + a = templated<int>; + a = &templated<int>; + } + + template <typename T> + T templatedBar(T m) __attribute__((enable_if(m > 0, ""))) { return T(); } + void test5() { + int (*p)(int) = templatedBar<int>; // expected-error{{address of overloaded function 'templatedBar' does not match required type 'int (int)'}} expected-note@214{{candidate function made ineligible by enable_if}} + int (*p2)(int) = &templatedBar<int>; // expected-error{{address of overloaded function 'templatedBar' does not match required type 'int (int)'}} expected-note@214{{candidate function made ineligible by enable_if}} + int (*a)(int); + a = templatedBar<int>; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@214{{candidate function made ineligible by enable_if}} + a = &templatedBar<int>; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@214{{candidate function made ineligible by enable_if}} + } + + template <typename T> + T templatedConflict(T m) __attribute__((enable_if(false, ""))) { return T(); } + template <typename T> + T templatedConflict(T m) __attribute__((enable_if(true, ""))) { return T(); } + template <typename T> + T templatedConflict(T m) { return T(); } + void test6() { + int (*p)(int) = templatedConflict<int>; // expected-error{{address of overloaded function 'templatedConflict' is ambiguous}} expected-note@224{{candidate function made ineligible by enable_if}} expected-note@226{{candidate function}} expected-note@228{{candidate function}} + int (*p0)(int) = &templatedConflict<int>; // expected-error{{address of overloaded function 'templatedConflict' is ambiguous}} expected-note@224{{candidate function made ineligible by enable_if}} expected-note@226{{candidate function}} expected-note@228{{candidate function}} + int (*a)(int); + a = templatedConflict<int>; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@226{{candidate function}} expected-note@228{{candidate function}} + a = &templatedConflict<int>; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@226{{candidate function}} expected-note@228{{candidate function}} + } +} |