[cfe-commits] r135377 - in /cfe/trunk: include/clang/AST/Decl.h lib/AST/ASTContext.cpp lib/AST/Decl.cpp lib/CodeGen/CodeGenModule.cpp test/CodeGen/inline.c

Nick Lewycky nicholas at mxc.ca
Sun Jul 17 22:26:13 PDT 2011


Author: nicholas
Date: Mon Jul 18 00:26:13 2011
New Revision: 135377

URL: http://llvm.org/viewvc/llvm-project?rev=135377&view=rev
Log:
In C99, emit an inline function when encountering an extern redeclaration.
Fixes PR10233!

Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/test/CodeGen/inline.c

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=135377&r1=135376&r2=135377&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Mon Jul 18 00:26:13 2011
@@ -1773,6 +1773,8 @@
   bool isInlined() const;
 
   bool isInlineDefinitionExternallyVisible() const;
+
+  bool doesDeclarationForceExternallyVisibleDefinition() const;
                        
   /// isOverloadedOperator - Whether this function declaration
   /// represents an C++ overloaded operator, e.g., "operator+".

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=135377&r1=135376&r2=135377&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Mon Jul 18 00:26:13 2011
@@ -6405,7 +6405,7 @@
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     // Forward declarations aren't required.
     if (!FD->doesThisDeclarationHaveABody())
-      return false;
+      return FD->doesDeclarationForceExternallyVisibleDefinition();
 
     // Constructors and destructors are required.
     if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=135377&r1=135376&r2=135377&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Mon Jul 18 00:26:13 2011
@@ -1762,6 +1762,32 @@
   return false;
 }
 
+/// \brief For a function declaration in C or C++, determine whether this
+/// declaration causes the definition to be externally visible.
+///
+/// Determines whether this is the first non-inline redeclaration of an inline
+/// function in a language where "inline" does not normally require an
+/// externally visible definition.
+bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
+  assert(!doesThisDeclarationHaveABody() &&
+         "Must have a declaration without a body.");
+
+  ASTContext &Context = getASTContext();
+
+  // In C99 mode, a function may have an inline definition (causing it to
+  // be deferred) then redeclared later.  As a special case, "extern inline"
+  // is not required to produce an external symbol.
+  if (Context.getLangOptions().GNUInline || !Context.getLangOptions().C99 ||
+      Context.getLangOptions().CPlusPlus)
+    return false;
+  if (getLinkage() != ExternalLinkage || isInlineSpecified())
+    return false;
+  const FunctionDecl *InlineDefinition = 0;
+  if (hasBody(InlineDefinition))
+    return InlineDefinition->isInlineDefinitionExternallyVisible();
+  return false;
+}
+
 /// \brief For an inline function definition in C or C++, determine whether the 
 /// definition will be externally visible.
 ///

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=135377&r1=135376&r2=135377&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Mon Jul 18 00:26:13 2011
@@ -597,7 +597,7 @@
 void CodeGenModule::EmitDeferred() {
   // Emit code for any potentially referenced deferred decls.  Since a
   // previously unused static decl may become used during the generation of code
-  // for a static function, iterate until no  changes are made.
+  // for a static function, iterate until no changes are made.
 
   while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) {
     if (!DeferredVTables.empty()) {
@@ -740,8 +740,21 @@
     }
 
     // Forward declarations are emitted lazily on first use.
-    if (!FD->doesThisDeclarationHaveABody())
+    if (!FD->doesThisDeclarationHaveABody()) {
+      if (!FD->doesDeclarationForceExternallyVisibleDefinition())
+        return;
+
+      const FunctionDecl *InlineDefinition = 0;
+      FD->getBody(InlineDefinition);
+
+      llvm::StringRef MangledName = getMangledName(GD);
+      llvm::StringMap<GlobalDecl>::iterator DDI =
+          DeferredDecls.find(MangledName);
+      if (DDI != DeferredDecls.end())
+        DeferredDecls.erase(DDI);
+      EmitGlobalDefinition(InlineDefinition);
       return;
+    }
   } else {
     const VarDecl *VD = cast<VarDecl>(Global);
     assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");

Modified: cfe/trunk/test/CodeGen/inline.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/inline.c?rev=135377&r1=135376&r2=135377&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/inline.c (original)
+++ cfe/trunk/test/CodeGen/inline.c Mon Jul 18 00:26:13 2011
@@ -12,6 +12,7 @@
 // RUN: grep "define void @test3()" %t
 // RUN: grep "define available_externally i32 @test4" %t
 // RUN: grep "define available_externally i32 @test5" %t
+// RUN: grep "define i32 @test6" %t
 
 // RUN: echo "\nC99 tests:"
 // RUN: %clang %s -O1 -emit-llvm -S -o %t -std=c99
@@ -27,6 +28,7 @@
 // RUN: grep "define void @test3" %t
 // RUN: grep "define available_externally i32 @test4" %t
 // RUN: grep "define available_externally i32 @test5" %t
+// RUN: grep "define i32 @test6" %t
 
 // RUN: echo "\nC++ tests:"
 // RUN: %clang -x c++ %s -O1 -emit-llvm -S -o %t -std=c++98
@@ -84,3 +86,8 @@
 }
 
 void test_test5() { test5(); }
+
+// PR10233
+
+__inline int test6() { return 0; }
+extern int test6();





More information about the cfe-commits mailing list