]> granicus.if.org Git - clang/commitdiff
Handle the edge case of a weak function with incomplete type correctly.
authorEli Friedman <eli.friedman@gmail.com>
Tue, 26 May 2009 01:22:57 +0000 (01:22 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Tue, 26 May 2009 01:22:57 +0000 (01:22 +0000)
Found by code inspection; I haven't seen this in real-world code.

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

lib/CodeGen/CodeGenModule.cpp
lib/CodeGen/CodeGenModule.h
test/CodeGen/weak-incomplete.c [new file with mode: 0644]

index a9d67b35641f402692376ae53d5f7cb53c8dea3f..04693fd502e0c4255a475d5f539f874860e3d89e 100644 (file)
@@ -373,8 +373,10 @@ void CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
 }
 
 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 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName,
   // 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;
 }
index 5c780e755b258c43bae0351349f39989f456031c..4d50e8946bd6195b53642a2cf9fa00b34663eb21 100644 (file)
@@ -397,12 +397,13 @@ private:
 
   /// 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.
diff --git a/test/CodeGen/weak-incomplete.c b/test/CodeGen/weak-incomplete.c
new file mode 100644 (file)
index 0000000..a15dbac
--- /dev/null
@@ -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;