From: Douglas Gregor Date: Thu, 8 Oct 2009 21:35:42 +0000 (+0000) Subject: Implement support for -Wunused-variable, from Oscar Bonilla! X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b5352cf949898cd42c8c5bc96a17a831b61ac2e5;p=clang 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 --- 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, DefaultIgnore; +def warn_unused_variable : Warning<"unused variable %0">, + InGroup, 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() && isa(D) && + !isa(D) && !isa(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(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(D) || + (isa(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 void f() { + T t; + t = 17; +}