aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2010-08-13 18:42:40 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2010-08-13 18:42:40 +0000
commit30c0dd86f0dc3c1a1fba29581d04e8ed0bdb659d (patch)
tree408d41d6a1f30e44706c5c87f51c0531767a653c
parentf6d1d43d68016e975f22264343631a55b9701495 (diff)
The unused warnings extravaganza continues. Warn for:
-static variables -variables in anonymous namespace (fixes rdar://7794535) -static data members in anonymous namespace -static data members specializations in anonymous namespace git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111027 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Sema/Sema.h3
-rw-r--r--lib/Sema/Sema.cpp36
-rw-r--r--lib/Sema/SemaDecl.cpp24
-rw-r--r--lib/Sema/SemaTemplateInstantiateDecl.cpp4
-rw-r--r--test/SemaCXX/warn-unused-filescoped.cpp16
5 files changed, 68 insertions, 15 deletions
diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h
index 31c63705fd..561fc5f03e 100644
--- a/include/clang/Sema/Sema.h
+++ b/include/clang/Sema/Sema.h
@@ -349,8 +349,7 @@ public:
std::vector<VarDecl *> TentativeDefinitions;
/// \brief The set of file scoped decls seen so far that have not been used
- /// and must warn if not used. For functions only contains the first
- /// declaration.
+ /// and must warn if not used. Only contains the first declaration.
std::vector<const DeclaratorDecl*> UnusedFileScopedDecls;
class AccessedEntity {
diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp
index 76e059138e..df9bb07cf9 100644
--- a/lib/Sema/Sema.cpp
+++ b/lib/Sema/Sema.cpp
@@ -241,15 +241,33 @@ static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
return true;
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
- // For functions, UnusedFileScopedDecls stores the first declaration.
+ // UnusedFileScopedDecls stores the first declaration.
+ // The declaration may have become definition so check again.
+ const FunctionDecl *DeclToCheck;
+ if (FD->hasBody(DeclToCheck))
+ return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
+
// Later redecls may add new information resulting in not having to warn,
// so check again.
- const FunctionDecl *DeclToCheck;
- if (!FD->hasBody(DeclToCheck))
- DeclToCheck = FD->getMostRecentDeclaration();
+ DeclToCheck = FD->getMostRecentDeclaration();
if (DeclToCheck != FD)
return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
}
+
+ if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
+ // UnusedFileScopedDecls stores the first declaration.
+ // The declaration may have become definition so check again.
+ const VarDecl *DeclToCheck = VD->getDefinition();
+ if (DeclToCheck)
+ return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
+
+ // Later redecls may add new information resulting in not having to warn,
+ // so check again.
+ DeclToCheck = VD->getMostRecentDeclaration();
+ if (DeclToCheck != VD)
+ return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
+ }
+
return false;
}
@@ -358,9 +376,13 @@ void Sema::ActOnEndOfTranslationUnit() {
DiagD = FD;
Diag(DiagD->getLocation(), diag::warn_unused_function)
<< DiagD->getDeclName();
- } else
- Diag((*I)->getLocation(), diag::warn_unused_variable)
- << cast<VarDecl>(*I)->getDeclName();
+ } else {
+ const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
+ if (!DiagD)
+ DiagD = cast<VarDecl>(*I);
+ Diag(DiagD->getLocation(), diag::warn_unused_variable)
+ << DiagD->getDeclName();
+ }
}
}
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index cf6f20f60a..a37d25a143 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -523,15 +523,17 @@ static void RemoveUsingDecls(LookupResult &R) {
bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
assert(D);
+
if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>())
return false;
- if (D->getLinkage() == ExternalLinkage)
- return false;
// Ignore class templates.
- if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D))
- if (MD->getParent()->getDescribedClassTemplate())
- return false;
+ 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->isThisDeclarationADefinition())
@@ -539,6 +541,10 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
return true;
}
+ if (const VarDecl *VD = dyn_cast<VarDecl>(D))
+ if (VD->isFileVarDecl())
+ return !Context.DeclMustBeEmitted(VD);
+
return false;
}
@@ -552,6 +558,12 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
return; // First should already be in the vector.
}
+ 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);
}
@@ -2758,6 +2770,8 @@ Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
if (NewVD->getLinkage() == ExternalLinkage && !DC->isRecord())
AddPushedVisibilityAttribute(NewVD);
+ MarkUnusedFileScopedDecl(NewVD);
+
return NewVD;
}
diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp
index a48f1e06b8..115eefbe5e 100644
--- a/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -449,7 +449,9 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
// Diagnose unused local variables.
if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
SemaRef.DiagnoseUnusedDecl(Var);
-
+
+ SemaRef.MarkUnusedFileScopedDecl(Var);
+
return Var;
}
diff --git a/test/SemaCXX/warn-unused-filescoped.cpp b/test/SemaCXX/warn-unused-filescoped.cpp
index 0ad2362779..9b4f9ae203 100644
--- a/test/SemaCXX/warn-unused-filescoped.cpp
+++ b/test/SemaCXX/warn-unused-filescoped.cpp
@@ -25,3 +25,19 @@ namespace {
}
void S::m3() { } // expected-warning{{unused}}
+
+static int x1; // expected-warning{{unused}}
+
+namespace {
+ int x2; // expected-warning{{unused}}
+
+ struct S2 {
+ static int x; // expected-warning{{unused}}
+ };
+
+ template <typename T>
+ struct TS2 {
+ static int x;
+ };
+ template <> int TS2<int>::x; // expected-warning{{unused}}
+}