2011-10-25 11:07:45 +08:00
// RUN: %clang_cc1 -fsyntax-only -verify -Wc++11-compat %s
2019-07-10 04:49:07 +08:00
// RUN: %clang_cc1 -fsyntax-only -verify -Wc++11-compat %s -std=c++98
2008-07-01 18:37:29 +08:00
class C {
public :
2016-04-15 07:47:07 +08:00
auto int errx ; // expected-error {{storage class specified for a member declaration}}
# if __cplusplus <= 199711L
// expected-warning@-2 {{'auto' storage class specifier is redundant}}
# else
// expected-warning@-4 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}}
# endif
2011-12-15 08:38:15 +08:00
register int erry ; // expected-error {{storage class specified for a member declaration}}
extern int errz ; // expected-error {{storage class specified for a member declaration}}
2008-07-01 18:37:29 +08:00
static void sm ( ) {
sx = 0 ;
2012-04-05 09:13:04 +08:00
this - > x = 0 ; // expected-error {{invalid use of 'this' outside of a non-static member function}}
2011-12-15 08:38:15 +08:00
x = 0 ; // expected-error {{invalid use of member 'x' in static member function}}
2008-07-01 18:37:29 +08:00
}
class NestedC {
2010-09-11 07:21:22 +08:00
public :
NestedC ( int ) ;
2012-04-05 09:13:04 +08:00
void f ( ) {
2008-07-01 18:37:29 +08:00
sx = 0 ;
2012-04-05 09:13:04 +08:00
x = 0 ; // expected-error {{use of non-static data member 'x' of 'C' from nested type 'NestedC'}}
sm ( ) ;
m ( ) ; // expected-error {{call to non-static member function 'm' of 'C' from nested type 'NestedC'}}
2008-07-01 18:37:29 +08:00
}
} ;
int b : 1 , w : 2 ;
int : 1 , : 2 ;
2009-03-06 07:01:03 +08:00
typedef int E : 1 ; // expected-error {{typedef member 'E' cannot be a bit-field}}
2010-09-11 07:21:22 +08:00
static int sb : 1 ; // expected-error {{static member 'sb' cannot be a bit-field}}
2008-07-01 18:37:29 +08:00
static int vs ;
typedef int func ( ) ;
func tm ;
2008-10-16 04:23:22 +08:00
func * ptm ;
2009-03-12 02:59:21 +08:00
func btm : 1 ; // expected-error {{bit-field 'btm' has non-integral type}}
NestedC bc : 1 ; // expected-error {{bit-field 'bc' has non-integral type}}
2008-07-01 18:37:29 +08:00
2009-01-29 01:15:10 +08:00
enum E1 { en1 , en2 } ;
2008-07-01 18:37:29 +08:00
2016-04-15 07:47:07 +08:00
int i = 0 ;
# if __cplusplus <= 199711L
2020-09-30 03:03:29 +08:00
// expected-warning@-2 {{default member initializer for non-static data member is a C++11 extension}}
2016-04-15 07:47:07 +08:00
# endif
2010-09-11 07:21:22 +08:00
static int si = 0 ; // expected-error {{non-const static data member must be initialized out of line}}
[clang] Implement ElaboratedType sugaring for types written bare
Without this patch, clang will not wrap in an ElaboratedType node types written
without a keyword and nested name qualifier, which goes against the intent that
we should produce an AST which retains enough details to recover how things are
written.
The lack of this sugar is incompatible with the intent of the type printer
default policy, which is to print types as written, but to fall back and print
them fully qualified when they are desugared.
An ElaboratedTypeLoc without keyword / NNS uses no storage by itself, but still
requires pointer alignment due to pre-existing bug in the TypeLoc buffer
handling.
---
Troubleshooting list to deal with any breakage seen with this patch:
1) The most likely effect one would see by this patch is a change in how
a type is printed. The type printer will, by design and default,
print types as written. There are customization options there, but
not that many, and they mainly apply to how to print a type that we
somehow failed to track how it was written. This patch fixes a
problem where we failed to distinguish between a type
that was written without any elaborated-type qualifiers,
such as a 'struct'/'class' tags and name spacifiers such as 'std::',
and one that has been stripped of any 'metadata' that identifies such,
the so called canonical types.
Example:
```
namespace foo {
struct A {};
A a;
};
```
If one were to print the type of `foo::a`, prior to this patch, this
would result in `foo::A`. This is how the type printer would have,
by default, printed the canonical type of A as well.
As soon as you add any name qualifiers to A, the type printer would
suddenly start accurately printing the type as written. This patch
will make it print it accurately even when written without
qualifiers, so we will just print `A` for the initial example, as
the user did not really write that `foo::` namespace qualifier.
2) This patch could expose a bug in some AST matcher. Matching types
is harder to get right when there is sugar involved. For example,
if you want to match a type against being a pointer to some type A,
then you have to account for getting a type that is sugar for a
pointer to A, or being a pointer to sugar to A, or both! Usually
you would get the second part wrong, and this would work for a
very simple test where you don't use any name qualifiers, but
you would discover is broken when you do. The usual fix is to
either use the matcher which strips sugar, which is annoying
to use as for example if you match an N level pointer, you have
to put N+1 such matchers in there, beginning to end and between
all those levels. But in a lot of cases, if the property you want
to match is present in the canonical type, it's easier and faster
to just match on that... This goes with what is said in 1), if
you want to match against the name of a type, and you want
the name string to be something stable, perhaps matching on
the name of the canonical type is the better choice.
3) This patch could expose a bug in how you get the source range of some
TypeLoc. For some reason, a lot of code is using getLocalSourceRange(),
which only looks at the given TypeLoc node. This patch introduces a new,
and more common TypeLoc node which contains no source locations on itself.
This is not an inovation here, and some other, more rare TypeLoc nodes could
also have this property, but if you use getLocalSourceRange on them, it's not
going to return any valid locations, because it doesn't have any. The right fix
here is to always use getSourceRange() or getBeginLoc/getEndLoc which will dive
into the inner TypeLoc to get the source range if it doesn't find it on the
top level one. You can use getLocalSourceRange if you are really into
micro-optimizations and you have some outside knowledge that the TypeLocs you are
dealing with will always include some source location.
4) Exposed a bug somewhere in the use of the normal clang type class API, where you
have some type, you want to see if that type is some particular kind, you try a
`dyn_cast` such as `dyn_cast<TypedefType>` and that fails because now you have an
ElaboratedType which has a TypeDefType inside of it, which is what you wanted to match.
Again, like 2), this would usually have been tested poorly with some simple tests with
no qualifications, and would have been broken had there been any other kind of type sugar,
be it an ElaboratedType or a TemplateSpecializationType or a SubstTemplateParmType.
The usual fix here is to use `getAs` instead of `dyn_cast`, which will look deeper
into the type. Or use `getAsAdjusted` when dealing with TypeLocs.
For some reason the API is inconsistent there and on TypeLocs getAs behaves like a dyn_cast.
5) It could be a bug in this patch perhaps.
Let me know if you need any help!
Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>
Differential Revision: https://reviews.llvm.org/D112374
2021-10-12 00:15:36 +08:00
static const NestedC ci = 0 ; // expected-error {{static data member of type 'const NestedC' must be initialized out of line}}
2011-12-30 05:57:33 +08:00
static const int nci = vs ; // expected-error {{in-class initializer for static data member is not a constant expression}}
2008-07-01 18:37:29 +08:00
static const int vi = 0 ;
2011-10-13 03:26:40 +08:00
static const volatile int cvi = 0 ; // ok, illegal in C++11
2016-04-15 07:47:07 +08:00
# if __cplusplus >= 201103L
// expected-error@-2 {{static const volatile data member must be initialized out of line}}
# endif
2008-07-01 18:37:29 +08:00
static const E evi = 0 ;
void m ( ) {
sx = 0 ;
this - > x = 0 ;
y = 0 ;
2011-12-15 08:38:15 +08:00
this = 0 ; // expected-error {{expression is not assignable}}
2008-07-01 18:37:29 +08:00
}
int f1 ( int p ) {
2008-11-06 23:59:35 +08:00
A z = 6 ;
return p + x + this - > y + z ;
2008-07-01 18:37:29 +08:00
}
typedef int A ;
2009-03-12 07:00:04 +08:00
virtual int viv ; // expected-error {{'virtual' can only appear on non-static member functions}}
2011-12-15 08:38:15 +08:00
virtual static int vsif ( ) ; // expected-error {{'virtual' can only appear on non-static member functions}}
2008-11-06 23:59:35 +08:00
virtual int vif ( ) ;
2008-07-01 18:37:29 +08:00
private :
int x , y ;
static int sx ;
2008-10-09 06:20:31 +08:00
2008-11-15 07:42:31 +08:00
mutable int mi ;
2011-12-15 08:38:15 +08:00
mutable int & mir ; // expected-error {{'mutable' cannot be applied to references}}
mutable void mfn ( ) ; // expected-error {{'mutable' cannot be applied to functions}}
mutable const int mci ; // expected-error {{'mutable' and 'const' cannot be mixed}}
2008-11-15 07:42:31 +08:00
2008-10-09 06:20:31 +08:00
static const int number = 50 ;
static int arr [ number ] ;
2008-07-01 18:37:29 +08:00
} ;
class C2 {
void f ( ) {
static int lx ;
class LC1 {
int m ( ) { return lx ; }
} ;
class LC2 {
int m ( ) { return lx ; }
} ;
}
} ;
2008-11-15 07:42:31 +08:00
2008-11-18 07:24:37 +08:00
struct C3 {
int i ;
mutable int j ;
} ;
void f ( )
{
const C3 c3 = { 1 , 2 } ;
2010-09-05 08:04:01 +08:00
( void ) static_cast < int * > ( & c3 . i ) ; // expected-error {{static_cast from 'const int *' to 'int *' is not allowed}}
2008-11-18 07:24:37 +08:00
// but no error here
( void ) static_cast < int * > ( & c3 . j ) ;
}
2008-11-15 07:42:31 +08:00
// Play with mutable a bit more, to make sure it doesn't crash anything.
2011-12-15 08:38:15 +08:00
mutable int gi ; // expected-error {{'mutable' can only be applied to member variables}}
2008-11-15 07:42:31 +08:00
mutable void gfn ( ) ; // expected-error {{illegal storage class on function}}
void ogfn ( )
{
2011-12-15 08:38:15 +08:00
mutable int ml ; // expected-error {{'mutable' can only be applied to member variables}}
2008-12-28 23:28:59 +08:00
// PR3020: This used to crash due to double ownership of C4.
struct C4 ;
2010-04-09 05:33:23 +08:00
C4 ; // expected-warning {{declaration does not declare anything}}
2008-11-15 07:42:31 +08:00
}
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
struct C4 {
void f ( ) ; // expected-note{{previous declaration is here}}
int f ; // expected-error{{duplicate member 'f'}}
} ;
2009-11-25 01:14:34 +08:00
// PR5415 - don't hang!
struct S
{
2015-01-17 10:33:17 +08:00
void f ( ) ; // expected-note 1 {{previous declaration}} expected-note {{previous declaration}}
void S : : f ( ) { } // expected-error {{extra qualification on member}} expected-error {{class member cannot be redeclared}}
void f ( ) { } // expected-error {{class member cannot be redeclared}}
2009-11-25 01:14:34 +08:00
} ;
2010-03-17 09:31:25 +08:00
// Don't crash on this bogus code.
namespace pr6629 {
template < class T1 , class T2 > struct foo :
2019-05-09 11:31:27 +08:00
bogus < foo < T1 , T2 > > // expected-error {{no template named 'bogus'}}
2010-03-17 09:31:25 +08:00
{ } ;
2012-04-12 04:59:20 +08:00
template < > struct foo < unknown , unknown > { // expected-error {{undeclared identifier 'unknown'}}
2010-03-17 09:31:25 +08:00
template < typename U1 , typename U2 > struct bar {
typedef bar type ;
static const int value = 0 ;
} ;
} ;
}
2010-05-18 02:19:56 +08:00
namespace PR7153 {
class EnclosingClass {
2010-05-18 03:45:25 +08:00
public :
2010-05-18 02:19:56 +08:00
struct A { } mutable * member ;
} ;
2010-05-18 03:45:25 +08:00
void f ( const EnclosingClass & ec ) {
ec . member = 0 ;
}
2010-05-18 02:19:56 +08:00
}
2010-05-23 00:25:05 +08:00
namespace PR7196 {
struct A {
int a ;
void f ( ) {
char i [ sizeof ( a ) ] ;
enum { x = sizeof ( i ) } ;
enum { y = sizeof ( a ) } ;
}
} ;
}
2010-06-17 07:45:56 +08:00
namespace rdar8066414 {
class C {
C ( ) { }
} // expected-error{{expected ';' after class}}
}
2010-09-11 07:21:22 +08:00
namespace rdar8367341 {
float foo ( ) ;
2016-04-15 07:47:07 +08:00
# if __cplusplus >= 201103L
// expected-note@-2 {{declared here}}
# endif
2010-09-11 07:21:22 +08:00
struct A {
2016-04-15 07:47:07 +08:00
# if __cplusplus <= 199711L
2011-09-30 07:18:34 +08:00
static const float x = 5.0f ; // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}}
2011-12-30 05:57:33 +08:00
static const float y = foo ( ) ; // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}} expected-error {{in-class initializer for static data member is not a constant expression}}
2016-04-15 07:47:07 +08:00
# else
static constexpr float x = 5.0f ;
static constexpr float y = foo ( ) ; // expected-error {{constexpr variable 'y' must be initialized by a constant expression}} expected-note {{non-constexpr function 'foo' cannot be used in a constant expression}}
# endif
2010-09-11 07:21:22 +08:00
} ;
}
2011-01-31 15:04:33 +08:00
namespace with_anon {
struct S {
union {
char c ;
} ;
} ;
void f ( ) {
2012-04-05 09:13:04 +08:00
S : : c ; // expected-error {{invalid use of non-static data member}}
2011-01-31 15:04:33 +08:00
}
}
2011-10-10 22:49:18 +08:00
struct PR9989 {
static int const PR9989_Member = sizeof PR9989_Member ;
} ;