summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2009-10-08 21:35:42 +0000
committerDouglas Gregor <dgregor@apple.com>2009-10-08 21:35:42 +0000
commit3beaf9bbcdb371d39f9e1d648a0ef818bc08b3ce (patch)
treeefa38de08e8eda8a809263dc9f7128e3fb1399ee /clang/lib
parent18289eeb1da35c699f401d8ef4ac90ef6ee0d0bc (diff)
downloadbcm5719-llvm-3beaf9bbcdb371d39f9e1d648a0ef818bc08b3ce.tar.gz
bcm5719-llvm-3beaf9bbcdb371d39f9e1d648a0ef818bc08b3ce.zip
Implement support for -Wunused-variable, from Oscar Bonilla!
llvm-svn: 83577
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/Sema/SemaDecl.cpp6
-rw-r--r--clang/lib/Sema/SemaExpr.cpp9
2 files changed, 12 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index fa6d623ae3d..c2a83cdc6f5 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -434,6 +434,12 @@ void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
if (!D->getDeclName()) continue;
+ // Diagnose unused variables in this scope.
+ if (!D->isUsed() && !D->hasAttr<UnusedAttr>() && isa<VarDecl>(D) &&
+ !isa<ParmVarDecl>(D) && !isa<ImplicitParamDecl>(D) &&
+ D->getDeclContext()->isFunctionOrMethod())
+ Diag(D->getLocation(), diag::warn_unused_variable) << D->getDeclName();
+
// Remove this name from our lexical scope.
IdResolver.RemoveDecl(D);
}
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index c9525f39e55..c689a7ac2a5 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6145,9 +6145,12 @@ void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) {
if (D->isUsed())
return;
- // Mark a parameter declaration "used", regardless of whether we're in a
- // template or not.
- if (isa<ParmVarDecl>(D))
+ // Mark a parameter or variable declaration "used", regardless of whether we're in a
+ // template or not. The reason for this is that unevaluated expressions
+ // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and
+ // -Wunused-parameters)
+ if (isa<ParmVarDecl>(D) ||
+ (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod()))
D->setUsed(true);
// Do not mark anything as "used" within a dependent context; wait for
OpenPOWER on IntegriCloud