[cfe-commits] r49987 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/CodeGen/CodeGenModule.cpp lib/CodeGen/CodeGenModule.h
Chris Lattner
clattner at apple.com
Sun Apr 20 11:13:59 PDT 2008
On Apr 19, 2008, at 11:29 PM, Nate Begeman wrote:
> URL: http://llvm.org/viewvc/llvm-project?rev=49987&view=rev
> Log:
> Only generate code for static functions and global variables that
> are actually used.
> Warn about unused static functions and variables.
> This fixes PR1998.
Nice, some minor things:
> +void CodeGenModule::EmitStatics() {
> + // Emit code for each used static decl encountered. Since a
> previously unused
> + // static decl may become used during the generation of code for
> a static
> + // function, iterate until no changes are made.
> + bool Changed;
> + 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[D] == 0)
> + continue;
This should use: if (!GlobalDeclMap.count(D)) continue. Using '[D]'
will cause an entry (with null as the value) to be inserted into the
map.
> + // 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.
> + if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
> + if (FD->getBody())
> + CodeGenFunction(*this).GenerateCode(FD);
> + } else {
> + const VarDecl *VD = cast<VarDecl>(D);
> + EmitGlobalVarInit(VD);
> + }
You can inline the 'cast<VarDecl>(D)' here.
>
> + // Warn about all statics that are still unused at end of code
> generation.
> + for (unsigned i = 0, e = StaticDecls.size(); i != e; ++i) {
> + const Decl *D = StaticDecls[i];
> + std::string Msg = cast<NamedDecl>(D)->getName();
Maybe StaticDecls should be declared as a list of NamedDecls instead
of Decls? This would eliminate the cast.
Thanks for tackling this Nate!
-Chris
More information about the cfe-commits
mailing list