diff options
Diffstat (limited to 'lib/CodeGen')
-rw-r--r-- | lib/CodeGen/CodeGenModule.cpp | 27 | ||||
-rw-r--r-- | lib/CodeGen/CodeGenModule.h | 11 | ||||
-rw-r--r-- | lib/CodeGen/ModuleBuilder.cpp | 7 |
3 files changed, 20 insertions, 25 deletions
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 0526e7845c..9ae93599df 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -424,13 +424,6 @@ void CodeGenModule::EmitDeferred() { // Otherwise, emit the definition and move on to the next one. EmitGlobalDefinition(D); } - - // Emit any tentative definitions, in reverse order so the most - // important (merged) decl will be seen and emitted first. - for (std::vector<const VarDecl*>::reverse_iterator - it = TentativeDefinitions.rbegin(), ie = TentativeDefinitions.rend(); - it != ie; ++it) - EmitTentativeDefinition(*it); } /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the @@ -502,6 +495,7 @@ bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { const VarDecl *VD = cast<VarDecl>(Global); assert(VD->isFileVarDecl() && "Invalid decl"); + return VD->getStorageClass() == VarDecl::Static; } @@ -520,16 +514,14 @@ void CodeGenModule::EmitGlobal(const ValueDecl *Global) { const VarDecl *VD = cast<VarDecl>(Global); assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); - // If this isn't a definition, defer code generation. - if (!VD->getInit()) { - // If this is a tentative definition, remember it so that we can - // emit the common definition if needed. It is important to - // defer tentative definitions, since they may have incomplete - // type. - if (!VD->hasExternalStorage()) - TentativeDefinitions.push_back(VD); + // In C++, if this is marked "extern", defer code generation. + if (getLangOptions().CPlusPlus && + VD->getStorageClass() == VarDecl::Extern && !VD->getInit()) + return; + + // In C, if this isn't a definition, defer code generation. + if (!getLangOptions().CPlusPlus && !VD->getInit()) return; - } } // Defer code generation when possible if this is a static definition, inline @@ -727,6 +719,9 @@ void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { // See if we have already defined this (as a variable), if so we do // not need to do anything. llvm::GlobalValue *GV = GlobalDeclMap[getMangledName(D)]; + if (!GV && MayDeferGeneration(D)) // this variable was never referenced + return; + if (llvm::GlobalVariable *Var = dyn_cast_or_null<llvm::GlobalVariable>(GV)) if (Var->hasInitializer()) return; diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index e7924fade5..21ef8da863 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -119,14 +119,6 @@ class CodeGenModule : public BlockModule { /// is done. std::vector<const ValueDecl*> DeferredDeclsToEmit; - /// TentativeDefinitions - A list of declarations which are - /// tentative definitions. Code generation for these must be - /// deferred because they are allowed to have incomplete type when - /// they are seen. This also allows us to avoid generating an extra - /// common definiton in situations where the tentative definition is - /// followed by an actual definition. - std::vector<const VarDecl*> TentativeDefinitions; - /// LLVMUsed - List of global values which are required to be /// present in the object file; bitcast to i8*. This is used for /// forcing visibility of symbols which may otherwise be optimized @@ -339,6 +331,8 @@ public: CXXCtorType Type); const char *getMangledCXXDtorName(const CXXDestructorDecl *D, CXXDtorType Type); + + void EmitTentativeDefinition(const VarDecl *D); enum GVALinkage { GVA_Internal, @@ -382,7 +376,6 @@ private: void EmitGlobalDefinition(const ValueDecl *D); void EmitGlobalFunctionDefinition(const FunctionDecl *D); - void EmitTentativeDefinition(const VarDecl *D); void EmitGlobalVarDefinition(const VarDecl *D); void EmitAliasDefinition(const ValueDecl *D); void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D); diff --git a/lib/CodeGen/ModuleBuilder.cpp b/lib/CodeGen/ModuleBuilder.cpp index 896464ed5a..9b85df61da 100644 --- a/lib/CodeGen/ModuleBuilder.cpp +++ b/lib/CodeGen/ModuleBuilder.cpp @@ -83,6 +83,13 @@ namespace { if (Builder) Builder->Release(); }; + + virtual void CompleteTentativeDefinition(VarDecl *D) { + if (Diags.hasErrorOccurred()) + return; + + Builder->EmitTentativeDefinition(D); + } }; } |