From 6f7e2eee917a136ffc36834f020782b3f15d8fa6 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Tue, 27 May 2008 04:58:01 +0000 Subject: [PATCH] Add a more reliable check for whether a static declaration has already been used. In preparation for the fix to PR2360, but also a minor bug in its own right. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51583 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodeGenModule.cpp | 29 ++++++++++++----------------- test/CodeGen/static-forward-decl.c | 5 +++++ 2 files changed, 17 insertions(+), 17 deletions(-) create mode 100644 test/CodeGen/static-forward-decl.c diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 329599c116..f14a0858d3 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -341,19 +341,6 @@ void CodeGenModule::EmitFunction(const FunctionDecl *FD) { CodeGenFunction(*this).GenerateCode(FD); return; } - - // We need to check the Module here to see if GetAddrOfFunctionDecl() has - // already added this function to the Module because the address of the - // function's prototype was taken. If this is the case, call - // GetAddrOfFunctionDecl to insert the static FunctionDecl into the used - // GlobalDeclsMap, so that EmitStatics will generate code for it later. - // - // Example: - // static int foo(); - // int bar() { return foo(); } - // static int foo() { return 5; } - if (getModule().getFunction(FD->getName())) - GetAddrOfFunctionDecl(FD, true); StaticDecls.push_back(FD); } @@ -366,11 +353,19 @@ void CodeGenModule::EmitStatics() { do { Changed = false; for (unsigned i = 0, e = StaticDecls.size(); i != e; ++i) { - // Check the map of used decls for our static. If not found, continue. const Decl *D = StaticDecls[i]; - if (!GlobalDeclMap.count(D)) - continue; - + + // Check if we have used a decl with the same name + // FIXME: The AST should have some sort of aggregate decls or + // global symbol map. + if (const FunctionDecl *FD = dyn_cast(D)) { + if (!getModule().getFunction(FD->getName())) + continue; + } else { + if (!getModule().getNamedGlobal(cast(D)->getName())) + continue; + } + // If this is a function decl, generate code for the static function if it // has a body. Otherwise, we must have a var decl for a static global // variable. diff --git a/test/CodeGen/static-forward-decl.c b/test/CodeGen/static-forward-decl.c new file mode 100644 index 0000000000..8e0825cb6c --- /dev/null +++ b/test/CodeGen/static-forward-decl.c @@ -0,0 +1,5 @@ +// RUN: clang %s -emit-llvm -o - -triple=i686-apple-darwin9 | grep "global i32 10" + +static int i; +int*j=&i; +static int i = 10;