diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2010-08-15 01:15:20 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2010-08-15 01:15:20 +0000 |
commit | bbc6454bb98d6a6ecbaafa715222c5db834307f2 (patch) | |
tree | 88489a29e8ba5ed8e92fd0a4b704fff48721dfd9 /lib/Sema/SemaDecl.cpp | |
parent | f2aabe199a9a2c794faf6ec16c26a340d18e9eb8 (diff) |
Commit improved version of 111026 & 111027.
Unused warnings for functions:
-static functions
-functions in anonymous namespace
-class methods in anonymous namespace
-class method specializations in anonymous namespace
-function specializations in anonymous namespace
Unused warnings for variables:
-static variables
-variables in anonymous namespace
-static data members in anonymous namespace
-static data members specializations in anonymous namespace
Reveals lots of opportunities for dead code removal in llvm codebase that will
interest my esteemed colleagues.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111086 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaDecl.cpp')
-rw-r--r-- | lib/Sema/SemaDecl.cpp | 83 |
1 files changed, 65 insertions, 18 deletions
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index e1f9c82fec..ea65c8cf30 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -521,25 +521,71 @@ static void RemoveUsingDecls(LookupResult &R) { F.done(); } -static bool ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) { +bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const { + assert(D); + + if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>()) + return false; + + // Ignore class templates. + if (D->getDeclContext()->isDependentContext()) + return false; + + // We warn for unused decls internal to the translation unit. + if (D->getLinkage() == ExternalLinkage) + return false; + + if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { + if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) + return false; + + if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { + if (MD->isVirtual()) + return false; + } else { + // 'static inline' functions are used in headers; don't warn. + if (FD->getStorageClass() == FunctionDecl::Static && + FD->isInlineSpecified()) + return false; + } + + if (FD->isThisDeclarationADefinition()) + return !Context.DeclMustBeEmitted(FD); + return true; + } + + if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { + if (VD->isStaticDataMember() && + VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) + return false; + + if ( VD->isFileVarDecl() && + !VD->getType().isConstant(Context)) + return !Context.DeclMustBeEmitted(VD); + } + + return false; + } + + void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) { + if (!D) + return; + if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { - // Warn for static, non-inlined function definitions that - // have not been used. - // FIXME: Also include static functions declared but not defined. - return (!FD->isInvalidDecl() - && !FD->isInlined() && FD->getLinkage() == InternalLinkage - && !FD->isUsed() && !FD->hasAttr<UnusedAttr>() - && !FD->hasAttr<ConstructorAttr>() - && !FD->hasAttr<DestructorAttr>()); + const FunctionDecl *First = FD->getFirstDeclaration(); + if (FD != First && ShouldWarnIfUnusedFileScopedDecl(First)) + return; // First should already be in the vector. } - - return false; -} -void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) { - if (ShouldWarnIfUnusedFileScopedDecl(D)) - UnusedFileScopedDecls.push_back(D); -} + if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { + const VarDecl *First = VD->getFirstDeclaration(); + if (VD != First && ShouldWarnIfUnusedFileScopedDecl(First)) + return; // First should already be in the vector. + } + + if (ShouldWarnIfUnusedFileScopedDecl(D)) + UnusedFileScopedDecls.push_back(D); + } static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { if (D->isInvalidDecl()) @@ -2743,6 +2789,8 @@ Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC, if (NewVD->getLinkage() == ExternalLinkage && !DC->isRecord()) AddPushedVisibilityAttribute(NewVD); + MarkUnusedFileScopedDecl(NewVD); + return NewVD; } @@ -3638,8 +3686,7 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC, if (FunctionTemplate) return FunctionTemplate; - if (IsFunctionDefinition) - MarkUnusedFileScopedDecl(NewFD); + MarkUnusedFileScopedDecl(NewFD); return NewFD; } |