diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2011-04-15 00:35:48 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2011-04-15 00:35:48 +0000 |
commit | 91147596414d94e39c2d04a2f91a13bda877093c (patch) | |
tree | da823c184dd34a13fcaade472a96413a3c9fa163 /clang/test | |
parent | a686b5f8bf7b5a2ab636c0c2de5ad4c174aa33e0 (diff) | |
download | bcm5719-llvm-91147596414d94e39c2d04a2f91a13bda877093c.tar.gz bcm5719-llvm-91147596414d94e39c2d04a2f91a13bda877093c.zip |
C1X: implement generic selections
As an extension, generic selection support has been added for all
supported languages. The syntax is the same as for C1X.
llvm-svn: 129554
Diffstat (limited to 'clang/test')
-rw-r--r-- | clang/test/PCH/exprs.c | 3 | ||||
-rw-r--r-- | clang/test/PCH/exprs.h | 4 | ||||
-rw-r--r-- | clang/test/Parser/c1x-generic-selection.c | 10 | ||||
-rw-r--r-- | clang/test/Sema/generic-selection.c | 26 | ||||
-rw-r--r-- | clang/test/SemaCXX/generic-selection.cpp | 46 |
5 files changed, 89 insertions, 0 deletions
diff --git a/clang/test/PCH/exprs.c b/clang/test/PCH/exprs.c index ffeab732f84..5928abda58f 100644 --- a/clang/test/PCH/exprs.c +++ b/clang/test/PCH/exprs.c @@ -93,3 +93,6 @@ choose_expr *int_ptr8 = &integer; // ShuffleVectorExpr shuffle_expr *vec_ptr = &vec2; + +// GenericSelectionExpr +generic_selection_expr *double_ptr6 = &floating; diff --git a/clang/test/PCH/exprs.h b/clang/test/PCH/exprs.h index c9adbe8aad7..3495b8b4bbd 100644 --- a/clang/test/PCH/exprs.h +++ b/clang/test/PCH/exprs.h @@ -99,3 +99,7 @@ typedef typeof(__builtin_choose_expr(17 > 19, d0, 1)) choose_expr; // ShuffleVectorExpr typedef typeof(__builtin_shufflevector(vec2, vec2b, 2, 1)) shuffle_expr; + +// GenericSelectionExpr +typedef typeof(_Generic(i, char*: 0, int: 0., default: hello)) + generic_selection_expr; diff --git a/clang/test/Parser/c1x-generic-selection.c b/clang/test/Parser/c1x-generic-selection.c new file mode 100644 index 00000000000..ee23059cc4d --- /dev/null +++ b/clang/test/Parser/c1x-generic-selection.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -std=c1x -fsyntax-only -verify %s + +void foo(void) { + _Generic; // expected-error {{expected '('}} + (void) _Generic(0); // expected-error {{expected ','}} + (void) _Generic(0, void); // expected-error {{expected ':'}} + (void) _Generic(0, + default: 0, // expected-note {{previous default generic association is here}} + default: 0); // expected-error {{duplicate default generic association}} +} diff --git a/clang/test/Sema/generic-selection.c b/clang/test/Sema/generic-selection.c new file mode 100644 index 00000000000..8cef975c709 --- /dev/null +++ b/clang/test/Sema/generic-selection.c @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -std=c1x -fsyntax-only -verify %s + +void foo(int n) { + (void) _Generic(0, + struct A: 0, // expected-error {{type 'struct A' in generic association incomplete}} + void(): 0, // expected-error {{type 'void ()' in generic association not an object type}} + int[n]: 0); // expected-error {{type 'int [n]' in generic association is a variably modified type}} + + (void) _Generic(0, + void (*)(): 0, // expected-note {{compatible type 'void (*)()' specified here}} + void (*)(void): 0); // expected-error {{type 'void (*)(void)' in generic association compatible with previously specified type 'void (*)()'}} + + (void) _Generic((void (*)()) 0, // expected-error {{controlling expression type 'void (*)()' compatible with 2 generic association types}} + void (*)(int): 0, // expected-note {{compatible type 'void (*)(int)' specified here}} + void (*)(void): 0); // expected-note {{compatible type 'void (*)(void)' specified here}} + + (void) _Generic(0, // expected-error {{controlling expression type 'int' not compatible with any generic association type}} + char: 0, short: 0, long: 0); + + int a1[_Generic(0, int: 1, short: 2, float: 3, default: 4) == 1 ? 1 : -1]; + int a2[_Generic(0, default: 1, short: 2, float: 3, int: 4) == 4 ? 1 : -1]; + int a3[_Generic(0L, int: 1, short: 2, float: 3, default: 4) == 4 ? 1 : -1]; + int a4[_Generic(0L, default: 1, short: 2, float: 3, int: 4) == 1 ? 1 : -1]; + int a5[_Generic(0, int: 1, short: 2, float: 3) == 1 ? 1 : -1]; + int a6[_Generic(0, short: 1, float: 2, int: 3) == 3 ? 1 : -1]; +} diff --git a/clang/test/SemaCXX/generic-selection.cpp b/clang/test/SemaCXX/generic-selection.cpp new file mode 100644 index 00000000000..b171fce540d --- /dev/null +++ b/clang/test/SemaCXX/generic-selection.cpp @@ -0,0 +1,46 @@ +// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s + +template <typename T, typename U = void*> +struct A { + enum { + id = _Generic(T(), // expected-error {{controlling expression type 'char' not compatible with any generic association type}} + int: 1, // expected-note {{compatible type 'int' specified here}} + float: 2, + U: 3) // expected-error {{type 'int' in generic association compatible with previously specified type 'int'}} + }; +}; + +static_assert(A<int>::id == 1, "fail"); +static_assert(A<float>::id == 2, "fail"); +static_assert(A<double, double>::id == 3, "fail"); + +A<char> a1; // expected-note {{in instantiation of template class 'A<char, void *>' requested here}} +A<short, int> a2; // expected-note {{in instantiation of template class 'A<short, int>' requested here}} + +template <typename T, typename U> +struct B { + enum { + id = _Generic(T(), + int: 1, // expected-note {{compatible type 'int' specified here}} + int: 2, // expected-error {{type 'int' in generic association compatible with previously specified type 'int'}} + U: 3) + }; +}; + +template <unsigned Arg, unsigned... Args> struct Or { + enum { result = Arg | Or<Args...>::result }; +}; + +template <unsigned Arg> struct Or<Arg> { + enum { result = Arg }; +}; + +template <class... Args> struct TypeMask { + enum { + result = Or<_Generic(Args(), int: 1, long: 2, short: 4, float: 8)...>::result + }; +}; + +static_assert(TypeMask<int, long, short>::result == 7, "fail"); +static_assert(TypeMask<float, short>::result == 12, "fail"); +static_assert(TypeMask<int, float, float>::result == 9, "fail"); |