]> granicus.if.org Git - clang/commitdiff
Don't assert on initialized typedef declarations in classes:
authorRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 12 Jun 2011 11:43:46 +0000 (11:43 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 12 Jun 2011 11:43:46 +0000 (11:43 +0000)
  struct {
    typedef int A = 0;
  };

According to the C++11 standard, this is not ill-formed, but does not have any ascribed meaning. We can't reasonably accept it, so treat it as ill-formed.

Also switch C++ from an incorrect 'fields can only be initialized in constructors' diagnostic for this case to C's 'illegal initializer (only variables can be initialized)'

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@132890 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Parse/ParseDeclCXX.cpp
lib/Sema/SemaDecl.cpp
test/SemaCXX/member-init.cpp

index 5cfa61b3972d63a7b0881a754f5c3499f2e869c8..50c409bd03b4d17bb8b42d7883f3ac3fd04dbd9d 100644 (file)
@@ -729,8 +729,6 @@ def err_not_integral_type_bitfield : Error<
   "bit-field %0 has non-integral type %1">;
 def err_not_integral_type_anon_bitfield : Error<
   "anonymous bit-field has non-integral type %0">;
-def err_member_initialization : Error<
-  "fields can only be initialized in constructors">;
 def err_member_function_initialization : Error<
   "initializer on function does not look like a pure-specifier">;
 def err_non_virtual_pure : Error<
index 51aa01091e730ab4947bb685f6e6d2631426fc3c..640e50176bc3c3c471fbcbf067d220c443ad62d1 100644 (file)
@@ -1830,7 +1830,9 @@ void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
       } else {
         HasDeferredInitializer = !DeclaratorInfo.isFunctionDeclarator() &&
           DeclaratorInfo.getDeclSpec().getStorageClassSpec()
-            != DeclSpec::SCS_static;
+            != DeclSpec::SCS_static &&
+          DeclaratorInfo.getDeclSpec().getStorageClassSpec()
+            != DeclSpec::SCS_typedef;
 
         if (!HasDeferredInitializer) {
           SourceLocation EqualLoc;
index 9446c0e8c03f03ee331225f091b244c4bbf4bf64..a783575278f72ae8dd209b9af8abeba4ea0f642a 100644 (file)
@@ -5208,12 +5208,8 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init,
 
   VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
   if (!VDecl) {
-    if (getLangOptions().CPlusPlus &&
-        RealDecl->getLexicalDeclContext()->isRecord() &&
-        isa<NamedDecl>(RealDecl))
-      Diag(RealDecl->getLocation(), diag::err_member_initialization);
-    else
-      Diag(RealDecl->getLocation(), diag::err_illegal_initializer);
+    assert(!isa<FieldDecl>(RealDecl) && "field init shouldn't get here");
+    Diag(RealDecl->getLocation(), diag::err_illegal_initializer);
     RealDecl->setInvalidDecl();
     return;
   }
index 1b8c523e4586f6f39fec1dadfaf620539256470b..7ca1f0e4513e4740b1629ed61762f72c5d643bfd 100644 (file)
@@ -48,3 +48,7 @@ struct CheckExcSpecFail {
   CheckExcSpecFail() noexcept(true) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}}
   ThrowCtor tc = 123;
 };
+
+struct TypedefInit {
+  typedef int A = 0; // expected-error {{illegal initializer}}
+};