[cfe-commits] r72408 - in /cfe/trunk: lib/CodeGen/CodeGenModule.cpp lib/CodeGen/CodeGenModule.h test/CodeGen/weak-incomplete.c

Eli Friedman eli.friedman at gmail.com
Mon May 25 18:22:57 PDT 2009


Author: efriedma
Date: Mon May 25 20:22:57 2009
New Revision: 72408

URL: http://llvm.org/viewvc/llvm-project?rev=72408&view=rev
Log:
Handle the edge case of a weak function with incomplete type correctly.  
Found by code inspection; I haven't seen this in real-world code.


Added:
    cfe/trunk/test/CodeGen/weak-incomplete.c
Modified:
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=72408&r1=72407&r2=72408&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Mon May 25 20:22:57 2009
@@ -373,8 +373,10 @@
 }
 
 void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD,
-                                          llvm::Function *F) {
-  SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F);
+                                          llvm::Function *F,
+                                          bool IsIncompleteFunction) {
+  if (!IsIncompleteFunction)
+    SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F);
   
   // Only a few attributes are set on declarations; these may later be
   // overridden by a definition.
@@ -625,18 +627,19 @@
   // This function doesn't have a complete type (for example, the return
   // type is an incomplete struct). Use a fake type instead, and make
   // sure not to try to set attributes.
-  bool ShouldSetAttributes = true;
+  bool IsIncompleteFunction = false;
   if (!isa<llvm::FunctionType>(Ty)) {
     Ty = llvm::FunctionType::get(llvm::Type::VoidTy,
                                  std::vector<const llvm::Type*>(), false);
-    ShouldSetAttributes = false;
+    IsIncompleteFunction = true;
   }
   llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 
                                              llvm::Function::ExternalLinkage,
                                              "", &getModule());
   F->setName(MangledName);
-  if (D.getDecl() && ShouldSetAttributes)
-    SetFunctionAttributes(cast<FunctionDecl>(D.getDecl()), F);
+  if (D.getDecl())
+    SetFunctionAttributes(cast<FunctionDecl>(D.getDecl()), F,
+                          IsIncompleteFunction);
   Entry = F;
   return F;
 }

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=72408&r1=72407&r2=72408&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Mon May 25 20:22:57 2009
@@ -397,12 +397,13 @@
 
   /// SetFunctionDefinitionAttributes - Set attributes for a global definition.
   void SetFunctionDefinitionAttributes(const FunctionDecl *D, 
-                                 llvm::GlobalValue *GV);
+                                       llvm::GlobalValue *GV);
     
   /// SetFunctionAttributes - Set function attributes for a function
   /// declaration.
   void SetFunctionAttributes(const FunctionDecl *FD,
-                             llvm::Function *F);
+                             llvm::Function *F,
+                             bool IsIncompleteFunction);
 
   /// EmitGlobal - Emit code for a singal global function or var decl. Forward
   /// declarations are emitted lazily.

Added: cfe/trunk/test/CodeGen/weak-incomplete.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/weak-incomplete.c?rev=72408&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/weak-incomplete.c (added)
+++ cfe/trunk/test/CodeGen/weak-incomplete.c Mon May 25 20:22:57 2009
@@ -0,0 +1,5 @@
+// RUN: clang-cc -emit-llvm < %s | grep 'extern_weak' | count 1
+
+struct S;
+void __attribute__((weak)) foo1(struct S);
+void (*foo2)(struct S) = foo1;





More information about the cfe-commits mailing list