aboutsummaryrefslogtreecommitdiff
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
commitb5352cf949898cd42c8c5bc96a17a831b61ac2e5 (patch)
tree574a96542b2b77e6c86b0821b2149b2ac9ac189d
parentb46f57d9daa0c30d79dc8149d30c3e3a12fe2b32 (diff)
Implement support for -Wunused-variable, from Oscar Bonilla!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83577 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Basic/DiagnosticSemaKinds.td2
-rw-r--r--lib/Sema/SemaDecl.cpp6
-rw-r--r--lib/Sema/SemaExpr.cpp9
-rw-r--r--test/Sema/warn-unused-variables.c19
-rw-r--r--test/SemaCXX/warn-unused-variables.cpp6
5 files changed, 39 insertions, 3 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 0a267091f5..9d39389f51 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -77,6 +77,8 @@ def err_bad_variable_name : Error<
def err_parameter_name_omitted : Error<"parameter name omitted">;
def warn_unused_parameter : Warning<"unused parameter %0">,
InGroup<UnusedParameter>, DefaultIgnore;
+def warn_unused_variable : Warning<"unused variable %0">,
+ InGroup<UnusedVariable>, DefaultIgnore;
def warn_decl_in_param_list : Warning<
"declaration of %0 will not be visible outside of this function">;
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index fa6d623ae3..c2a83cdc6f 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/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/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index c9525f39e5..c689a7ac2a 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/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
diff --git a/test/Sema/warn-unused-variables.c b/test/Sema/warn-unused-variables.c
new file mode 100644
index 0000000000..fd225436be
--- /dev/null
+++ b/test/Sema/warn-unused-variables.c
@@ -0,0 +1,19 @@
+// RUN: clang-cc -fsyntax-only -Wunused-variable -verify %s
+
+struct s0 {
+ unsigned int i;
+};
+
+int proto(int a, int b);
+
+void f0(void) {
+ int a __attribute__((unused)),
+ b; // expected-warning{{unused}}
+ return;
+}
+
+void f1(void) {
+ int i;
+ (void)sizeof(i);
+ return;
+}
diff --git a/test/SemaCXX/warn-unused-variables.cpp b/test/SemaCXX/warn-unused-variables.cpp
new file mode 100644
index 0000000000..d8b9a00ad6
--- /dev/null
+++ b/test/SemaCXX/warn-unused-variables.cpp
@@ -0,0 +1,6 @@
+// RUN: clang -fsyntax-only -Wunused-variable -verify %s
+
+template<typename T> void f() {
+ T t;
+ t = 17;
+}