[cfe-commits] r41889 - in /cfe/trunk: Sema/SemaDecl.cpp include/clang/Basic/DiagnosticKinds.def test/Sema/array-init.c

Steve Naroff snaroff at apple.com
Wed Sep 12 13:13:51 PDT 2007


Author: snaroff
Date: Wed Sep 12 15:13:48 2007
New Revision: 41889

URL: http://llvm.org/viewvc/llvm-project?rev=41889&view=rev
Log:
Remove a FIXME. Replace a couple asserts with an appropriate error
diagnostic for illegal initializers.

Modified:
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/test/Sema/array-init.c

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=41889&r1=41888&r2=41889&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Wed Sep 12 15:13:48 2007
@@ -561,32 +561,31 @@
 }
 
 void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) {
-  VarDecl *Dcl = dyn_cast<VarDecl>(static_cast<Decl *>(dcl));
+  Decl *RealDecl = static_cast<Decl *>(dcl);
   Expr *Init = static_cast<Expr *>(init);
   
-  assert((Dcl && Init) && "missing decl or initializer");
-  
-  // FIXME: moved these directly from ParseDeclarator(). Need to convert
-  // asserts to actual error diagnostics!
-  if (isa<FunctionDecl>(Dcl))
-    assert(0 && "Can't have an initializer for a functiondecl!");
-  if (isa<TypedefDecl>(Dcl))
-    assert(0 && "Can't have an initializer for a typedef!");
+  assert((RealDecl && Init) && "missing decl or initializer");
   
+  VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl);
+  if (!VDecl) {
+    Diag(RealDecl->getLocation(), diag::err_illegal_initializer);
+    RealDecl->setInvalidDecl();
+    return;
+  }  
   // Get the decls type and save a reference for later, since
   // CheckInitializer may change it.
-  QualType DclT = Dcl->getType(), SavT = DclT;
-  if (BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(Dcl)) {
+  QualType DclT = VDecl->getType(), SavT = DclT;
+  if (BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(VDecl)) {
     VarDecl::StorageClass SC = BVD->getStorageClass();
     if (SC == VarDecl::Extern) { // C99 6.7.8p5
-      Diag(Dcl->getLocation(), diag::err_block_extern_cant_init);
+      Diag(VDecl->getLocation(), diag::err_block_extern_cant_init);
       BVD->setInvalidDecl();
     } else if (!BVD->isInvalidDecl()) {
       CheckInitializer(Init, DclT, SC == VarDecl::Static);
     }
-  } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(Dcl)) {
+  } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(VDecl)) {
     if (FVD->getStorageClass() == VarDecl::Extern)
-      Diag(Dcl->getLocation(), diag::warn_extern_init);
+      Diag(VDecl->getLocation(), diag::warn_extern_init);
     if (!FVD->isInvalidDecl())
       CheckInitializer(Init, DclT, true);
   }
@@ -594,11 +593,11 @@
   // completed by the initializer. For example: 
   //   int ary[] = { 1, 3, 5 };
   // "ary" transitions from a VariableArrayType to a ConstantArrayType.
-  if (!Dcl->isInvalidDecl() && (DclT != SavT))
-    Dcl->setType(DclT);
+  if (!VDecl->isInvalidDecl() && (DclT != SavT))
+    VDecl->setType(DclT);
     
   // Attach the initializer to the decl.
-  Dcl->setInit(Init);
+  VDecl->setInit(Init);
   return;
 }
 

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=41889&r1=41888&r2=41889&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Sep 12 15:13:48 2007
@@ -553,6 +553,8 @@
      "excess elements in array initializer")
 DIAG(warn_braces_around_scalar_init, WARNING,
      "braces around scalar initializer")
+DIAG(err_illegal_initializer, ERROR,
+     "illegal initializer (only variables can be initialized)")
 
 DIAG(err_redefinition_of_label, ERROR,
      "redefinition of label '%0'")

Modified: cfe/trunk/test/Sema/array-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/array-init.c?rev=41889&r1=41888&r2=41889&view=diff

==============================================================================
--- cfe/trunk/test/Sema/array-init.c (original)
+++ cfe/trunk/test/Sema/array-init.c Wed Sep 12 15:13:48 2007
@@ -1,5 +1,7 @@
 // RUN: clang -parse-ast-check -pedantic %s
 
+extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
+
 static int x, y, z;
 
 static int ary[] = { x, y, z }; // expected-error{{initializer element is not constant}}
@@ -12,6 +14,8 @@
 void func() {
   int x = 1;
 
+  typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
+
   int xComputeSize[] = { 1, 3, 5 };
 
   int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}





More information about the cfe-commits mailing list