[cfe-commits] r43058 - in /cfe/trunk: AST/Expr.cpp CodeGen/CGDecl.cpp CodeGen/CodeGenFunction.h Sema/Sema.h Sema/SemaDecl.cpp test/Sema/static-init.c

Anders Carlsson andersca at mac.com
Tue Oct 16 17:52:44 PDT 2007


Author: andersca
Date: Tue Oct 16 19:52:43 2007
New Revision: 43058

URL: http://llvm.org/viewvc/llvm-project?rev=43058&view=rev
Log:
Generate code for static variables that don't have initializers. Also, report an error if a static initializer is not constant.

Added:
    cfe/trunk/test/Sema/static-init.c
Modified:
    cfe/trunk/AST/Expr.cpp
    cfe/trunk/CodeGen/CGDecl.cpp
    cfe/trunk/CodeGen/CodeGenFunction.h
    cfe/trunk/Sema/Sema.h
    cfe/trunk/Sema/SemaDecl.cpp

Modified: cfe/trunk/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Expr.cpp?rev=43058&r1=43057&r2=43058&view=diff

==============================================================================
--- cfe/trunk/AST/Expr.cpp (original)
+++ cfe/trunk/AST/Expr.cpp Tue Oct 16 19:52:43 2007
@@ -365,7 +365,8 @@
   case IntegerLiteralClass:
   case CharacterLiteralClass:
   case ImaginaryLiteralClass:
-  case TypesCompatibleExprClass: 
+  case TypesCompatibleExprClass:
+  case CXXBoolLiteralExprClass:
     break;
   case CallExprClass: {
     const CallExpr *CE = cast<CallExpr>(this);

Modified: cfe/trunk/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGDecl.cpp?rev=43058&r1=43057&r2=43058&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/CodeGen/CGDecl.cpp Tue Oct 16 19:52:43 2007
@@ -12,7 +12,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "CodeGenFunction.h"
+#include "CodeGenModule.h"
 #include "clang/AST/AST.h"
+#include "llvm/GlobalVariable.h"
 #include "llvm/Type.h"
 using namespace clang;
 using namespace CodeGen;
@@ -50,7 +52,7 @@
 void CodeGenFunction::EmitBlockVarDecl(const BlockVarDecl &D) {
   switch (D.getStorageClass()) {
   case VarDecl::Static:
-    assert(0 && "FIXME: local static vars not implemented yet");
+    return EmitStaticBlockVarDecl(D);
   case VarDecl::Extern:
     assert(0 && "FIXME: should call up to codegenmodule");
   default:
@@ -62,6 +64,28 @@
   }
 }
 
+void CodeGenFunction::EmitStaticBlockVarDecl(const BlockVarDecl &D) {
+  QualType Ty = D.getCanonicalType();
+  assert(Ty->isConstantSizeType(getContext()) && "VLAs can't be static");
+  
+  llvm::Value *&DMEntry = LocalDeclMap[&D];
+  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
+  
+  const llvm::Type *LTy = ConvertType(Ty);
+  llvm::Constant *Init = 0;
+  if (D.getInit() == 0) {
+    Init = llvm::Constant::getNullValue(LTy);
+  } else
+    assert(0 && "FIXME: Support initializers");
+  
+  
+  DMEntry = 
+    new llvm::GlobalVariable(LTy, false, 
+                            llvm::GlobalValue::InternalLinkage,
+                             Init, D.getName(), &CGM.getModule());
+  
+}
+  
 /// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
 /// variable declaration with auto, register, or no storage class specifier.
 /// These turn into simple stack objects.

Modified: cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.h?rev=43058&r1=43057&r2=43058&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.h Tue Oct 16 19:52:43 2007
@@ -304,6 +304,7 @@
   void EmitEnumConstantDecl(const EnumConstantDecl &D);
   void EmitBlockVarDecl(const BlockVarDecl &D);
   void EmitLocalBlockVarDecl(const BlockVarDecl &D);
+  void EmitStaticBlockVarDecl(const BlockVarDecl &D);
   void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
   
   //===--------------------------------------------------------------------===//

Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=43058&r1=43057&r2=43058&view=diff

==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Tue Oct 16 19:52:43 2007
@@ -623,7 +623,8 @@
   /// type checking declaration initializers (C99 6.7.8)
   bool CheckInitializer(Expr *&simpleInit_or_initList, QualType &declType,
                         bool isStatic);
-  bool CheckSingleInitializer(Expr *&simpleInit, QualType declType);
+  bool CheckSingleInitializer(Expr *&simpleInit, bool isStatic, 
+                              QualType declType);
   bool CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot,
                      bool isStatic, QualType ElementType);
   void CheckVariableInitList(QualType DeclType, InitListExpr *IList, 

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

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Tue Oct 16 19:52:43 2007
@@ -299,9 +299,20 @@
   return 0;
 }
 
-bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) {
+bool Sema::CheckSingleInitializer(Expr *&Init, bool isStatic, 
+                                  QualType DeclType) {
+  SourceLocation loc;
+  
+  // FIXME: Remove the isReferenceType check and handle assignment
+  // to a reference.
+  if (isStatic && !DeclType->isReferenceType() &&
+      !Init->isConstantExpr(Context, &loc)) { // C99 6.7.8p4.
+    Diag(loc, diag::err_init_element_not_constant, Init->getSourceRange());
+    return true;
+  }
+  
   AssignmentCheckResult result;
-  SourceLocation loc = Init->getLocStart();
+  loc = Init->getLocStart();
   // Get the type before calling CheckSingleAssignmentConstraints(), since
   // it can promote the expression.
   QualType rhsType = Init->getType(); 
@@ -357,7 +368,7 @@
   if (isStatic && !expr->isConstantExpr(Context, &loc)) { // C99 6.7.8p4.
     Diag(loc, diag::err_init_element_not_constant, expr->getSourceRange());
     return true;
-  } else if (CheckSingleInitializer(expr, ElementType)) {
+  } else if (CheckSingleInitializer(expr, isStatic, ElementType)) {
     return true; // types weren't compatible.
   }
   if (savExpr != expr) // The type was promoted, update initializer list.
@@ -437,8 +448,8 @@
 bool Sema::CheckInitializer(Expr *&Init, QualType &DeclType, bool isStatic) {
   InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
   if (!InitList)
-    return CheckSingleInitializer(Init, DeclType);
-
+    return CheckSingleInitializer(Init, isStatic, DeclType);
+  
   // We have an InitListExpr, make sure we set the type.
   Init->setType(DeclType);
 

Added: cfe/trunk/test/Sema/static-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/static-init.c?rev=43058&view=auto

==============================================================================
--- cfe/trunk/test/Sema/static-init.c (added)
+++ cfe/trunk/test/Sema/static-init.c Tue Oct 16 19:52:43 2007
@@ -0,0 +1,3 @@
+// RUN: clang -fsyntax-only -verify %s
+static int f = 10;
+static int b = f; // expected-error {{initializer element is not constant}}





More information about the cfe-commits mailing list