From b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 21 Apr 2009 17:11:58 +0000 Subject: Explictly track tentative definitions within Sema, then hand those tentative definitions off to the ASTConsumer at the end of the translation unit. Eliminate CodeGen's internal tracking of tentative definitions, and instead hook into ASTConsumer::CompleteTentativeDefinition. Also, tweak the definition-deferal logic for C++, where there are no tentative definitions. Fixes , and will make it much easier for precompiled headers to cope with tentative definitions in the future. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69681 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodeGenModule.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'lib/CodeGen/CodeGenModule.cpp') 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::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(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(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(GV)) if (Var->hasInitializer()) return; -- cgit v1.2.3-18-g5258