]> granicus.if.org Git - clang/commitdiff
implement support for functions that initialize globals.
authorChris Lattner <sabre@nondot.org>
Sun, 9 Dec 2007 00:36:01 +0000 (00:36 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 9 Dec 2007 00:36:01 +0000 (00:36 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44730 91177308-0d34-0410-b5e6-96231b3b80d8

CodeGen/CodeGenModule.cpp
test/CodeGen/globalinit.c

index c5e6a26859e135d8ad4893a0650b70f667e421df..ecabb814ae767d2572c75fc4a7d41038cd04f3b4 100644 (file)
@@ -202,8 +202,8 @@ static llvm::Constant *GenerateConversionToBool(llvm::Constant *Expression,
 /// GenerateConstantCast - Generates a constant cast to convert the Expression
 /// into the Target type.
 static llvm::Constant *GenerateConstantCast(const Expr *Expression, 
-                                                QualType Target, 
-                                                CodeGenModule &CGM) {
+                                            QualType Target, 
+                                            CodeGenModule &CGM) {
   CodeGenTypes& Types = CGM.getTypes(); 
   QualType Source = Expression->getType().getCanonicalType();
   Target = Target.getCanonicalType();
@@ -346,6 +346,14 @@ static llvm::Constant *GenerateConstantExpr(const Expr *Expression,
   }
 
   switch (Expression->getStmtClass()) {
+  default: break; // default emits a warning and returns bogus value.
+  case Stmt::DeclRefExprClass:  {
+    const ValueDecl *Decl = cast<DeclRefExpr>(Expression)->getDecl();
+    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
+      return CGM.GetAddrOfFunctionDecl(FD, false);
+    break;
+  }
+      
   // Generate constant for floating point literal values.
   case Stmt::FloatingLiteralClass: {
     const FloatingLiteral *FLiteral = cast<FloatingLiteral>(Expression);
@@ -416,11 +424,10 @@ static llvm::Constant *GenerateConstantExpr(const Expr *Expression,
   // an array or struct.
   case Stmt::InitListExprClass: 
     return GenerateAggregateInit(cast<InitListExpr>(Expression), CGM);
-
-  default:
-    CGM.WarnUnsupported(Expression, "initializer");
-    return llvm::UndefValue::get(Types.ConvertType(type));
   }
+        
+  CGM.WarnUnsupported(Expression, "initializer");
+  return llvm::UndefValue::get(Types.ConvertType(type));
 }
 
 llvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expression) {
index f08760f53a24e51ad2b3049adacb7e9e1f666d64..bfa909bd753427514504cde67b89f93980dc2c3a 100644 (file)
@@ -12,3 +12,6 @@ void bar() { x[0] = 1; }
 extern int y[];
 void *g = y;
 
+int latin_ptr2len (char *p);
+int (*mb_ptr2len) (char *p) = latin_ptr2len;
+