2011-05-10 07:05:33 +08:00
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s
2009-01-08 00:22:09 +08:00
struct X {
union {
float f3 ;
double d2 ;
} named ;
union {
int i ;
float f ;
2013-01-31 11:11:12 +08:00
union { // expected-warning{{anonymous types declared in an anonymous union are an extension}}
2009-01-08 00:22:09 +08:00
float f2 ;
mutable double d ;
} ;
} ;
void test_unqual_references ( ) ;
2011-05-10 07:05:33 +08:00
struct { // expected-warning{{anonymous structs are a GNU extension}}
2009-01-08 00:22:09 +08:00
int a ;
float b ;
} ;
void test_unqual_references_const ( ) const ;
mutable union { // expected-error{{anonymous union at class scope must not have a storage specifier}}
float c1 ;
double c2 ;
} ;
} ;
void X : : test_unqual_references ( ) {
i = 0 ;
f = 0.0 ;
f2 = f ;
d = f ;
f3 = 0 ; // expected-error{{use of undeclared identifier 'f3'}}
a = 0 ;
}
void X : : test_unqual_references_const ( ) const {
d = 0.0 ;
f2 = 0 ; // expected-error{{read-only variable is not assignable}}
a = 0 ; // expected-error{{read-only variable is not assignable}}
}
void test_unqual_references ( X x , const X xc ) {
x . i = 0 ;
x . f = 0.0 ;
x . f2 = x . f ;
x . d = x . f ;
x . f3 = 0 ; // expected-error{{no member named 'f3'}}
x . a = 0 ;
xc . d = 0.0 ;
xc . f = 0 ; // expected-error{{read-only variable is not assignable}}
xc . a = 0 ; // expected-error{{read-only variable is not assignable}}
}
struct Redecl {
int x ; // expected-note{{previous declaration is here}}
class y { } ;
union {
int x ; // expected-error{{member of anonymous union redeclares 'x'}}
float y ;
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
llvm-svn: 61940
2009-01-09 04:45:30 +08:00
double z ; // expected-note{{previous declaration is here}}
2009-01-08 00:22:09 +08:00
double zz ; // expected-note{{previous definition is here}}
} ;
2009-01-08 03:46:03 +08:00
int z ; // expected-error{{duplicate member 'z'}}
2009-01-08 00:22:09 +08:00
void zz ( ) ; // expected-error{{redefinition of 'zz' as different kind of symbol}}
} ;
union { // expected-error{{anonymous unions at namespace or global scope must be declared 'static'}}
int int_val ;
float float_val ;
} ;
static union {
int int_val2 ;
float float_val2 ;
} ;
void f ( ) {
int_val2 = 0 ;
float_val2 = 0.0 ;
}
void g ( ) {
union {
int i ;
2009-01-08 03:46:03 +08:00
float f2 ;
2009-01-08 00:22:09 +08:00
} ;
i = 0 ;
2009-01-08 03:46:03 +08:00
f2 = 0.0 ;
2009-01-08 00:22:09 +08:00
}
2009-01-08 03:46:03 +08:00
struct BadMembers {
union {
struct X { } ; // expected-error {{types cannot be declared in an anonymous union}}
2013-01-31 11:11:12 +08:00
struct { int x ; int y ; } y ; // expected-warning{{anonymous types declared in an anonymous union are an extension}}
2009-01-08 03:46:03 +08:00
void f ( ) ; // expected-error{{functions cannot be declared in an anonymous union}}
private : int x1 ; // expected-error{{anonymous union cannot contain a private data member}}
protected : float x2 ; // expected-error{{anonymous union cannot contain a protected data member}}
} ;
} ;
2009-01-13 06:49:06 +08:00
// <rdar://problem/6481130>
2013-03-19 06:52:47 +08:00
typedef union { } ; // expected-warning{{typedef requires a name}}
2010-01-23 02:30:17 +08:00
// <rdar://problem/7562438>
typedef struct objc_module * Foo ;
typedef struct _s {
union {
int a ;
int Foo ;
} ;
} s , * ps ;
2010-05-22 04:45:30 +08:00
// <rdar://problem/7987650>
namespace test4 {
class A {
2011-05-10 07:05:33 +08:00
struct { // expected-warning{{anonymous structs are a GNU extension}}
2010-05-22 04:45:30 +08:00
int s0 ; // expected-note {{declared private here}}
double s1 ; // expected-note {{declared private here}}
2013-01-31 11:11:12 +08:00
union { // expected-warning{{anonymous types declared in an anonymous struct are an extension}}
2010-05-22 04:45:30 +08:00
int su0 ; // expected-note {{declared private here}}
double su1 ; // expected-note {{declared private here}}
} ;
} ;
union {
int u0 ; // expected-note {{declared private here}}
double u1 ; // expected-note {{declared private here}}
2013-01-31 11:11:12 +08:00
struct { // expected-warning{{anonymous structs are a GNU extension}} expected-warning{{anonymous types declared in an anonymous union are an extension}}
2010-05-22 04:45:30 +08:00
int us0 ; // expected-note {{declared private here}}
double us1 ; // expected-note {{declared private here}}
} ;
} ;
} ;
void test ( ) {
A a ;
( void ) a . s0 ; // expected-error {{private member}}
( void ) a . s1 ; // expected-error {{private member}}
( void ) a . su0 ; // expected-error {{private member}}
( void ) a . su1 ; // expected-error {{private member}}
( void ) a . u0 ; // expected-error {{private member}}
( void ) a . u1 ; // expected-error {{private member}}
( void ) a . us0 ; // expected-error {{private member}}
( void ) a . us1 ; // expected-error {{private member}}
}
}
2010-09-23 22:26:01 +08:00
typedef void * voidPtr ;
void f2 ( ) {
union { int * * ctxPtr ; void * * voidPtr ; } ;
}
void foo_PR6741 ( ) {
union {
char * m_a ;
int * m_b ;
} ;
if ( 1 ) {
union {
char * m_a ;
int * m_b ;
} ;
}
}
2011-05-10 07:05:33 +08:00
namespace PR8326 {
template < class T >
class Foo {
public :
Foo ( )
: x ( 0 )
, y ( 1 ) {
}
private :
const union { // expected-warning{{anonymous union cannot be 'const'}}
2013-01-31 11:11:12 +08:00
struct { // expected-warning{{anonymous structs are a GNU extension}} expected-warning{{declared in an anonymous union}}
2011-05-10 07:05:33 +08:00
T x ;
T y ;
} ;
T v [ 2 ] ;
} ;
} ;
Foo < int > baz ;
}
2013-07-16 08:01:31 +08:00
namespace PR16630 {
struct A { union { int x ; float y ; } ; } ; // expected-note {{member is declared here}}
struct B : private A { using A : : x ; } b ; // expected-note 2 {{private}}
void foo ( ) {
b . x = 10 ;
b . y = 0 ; // expected-error {{cannot cast 'struct B' to its private base class 'PR16630::A'}} expected-error {{'y' is a private member of 'PR16630::A'}}
}
}