diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2011-06-21 23:42:09 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2011-06-21 23:42:09 +0000 |
commit | e3499cae8e5323ac553ad56977bf1cd42b9a5a35 (patch) | |
tree | b765e3dcfd1e84cd2c6f544a1794b526e88409fe | |
parent | 0894976ba1384a34357e333846adedc6a42334fd (diff) |
Fix PR10168: don't warn for unused non-dependent variables in both the template definition and each instantiation.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133580 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Sema/SemaTemplateInstantiateDecl.cpp | 6 | ||||
-rw-r--r-- | test/SemaCXX/for-range-unused.cpp | 5 | ||||
-rw-r--r-- | test/SemaCXX/warn-unused-variables.cpp | 26 |
3 files changed, 32 insertions, 5 deletions
diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index e78aa2991e..cc66ec6759 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -415,8 +415,10 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { !Var->isCXXForRangeDecl()) SemaRef.ActOnUninitializedDecl(Var, false); - // Diagnose unused local variables. - if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed()) + // Diagnose unused local variables with dependent types, where the diagnostic + // will have been deferred. + if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() && + D->getType()->isDependentType()) SemaRef.DiagnoseUnusedDecl(Var); return Var; diff --git a/test/SemaCXX/for-range-unused.cpp b/test/SemaCXX/for-range-unused.cpp index 7e26c786ed..7b7d84d3f9 100644 --- a/test/SemaCXX/for-range-unused.cpp +++ b/test/SemaCXX/for-range-unused.cpp @@ -5,8 +5,7 @@ template <typename T> struct Vector { void doIt() { - // FIXME: PR10168: Only warn once for this! - int a; // expected-warning 2{{unused variable 'a'}} + int a; // expected-warning {{unused variable 'a'}} for (auto& e : elements) ; @@ -18,5 +17,5 @@ template <typename T> int main(int, char**) { Vector<int> vector; - vector.doIt(); // expected-note {{requested here}} + vector.doIt(); } diff --git a/test/SemaCXX/warn-unused-variables.cpp b/test/SemaCXX/warn-unused-variables.cpp index 81f22a796a..5ba1f2a5f3 100644 --- a/test/SemaCXX/warn-unused-variables.cpp +++ b/test/SemaCXX/warn-unused-variables.cpp @@ -54,3 +54,29 @@ void unused_local_static() { static int y = 0; // expected-warning{{unused variable 'y'}} #pragma unused(x) } + +// PR10168 +namespace PR10168 { + // We expect a warning in the definition only for non-dependent variables, and + // a warning in the instantiation only for dependent variables. + template<typename T> + struct S { + void f() { + int a; // expected-warning {{unused variable 'a'}} + T b; // expected-warning 2{{unused variable 'b'}} + } + }; + + template<typename T> + void f() { + int a; // expected-warning {{unused variable 'a'}} + T b; // expected-warning 2{{unused variable 'b'}} + } + + void g() { + S<int>().f(); // expected-note {{here}} + S<char>().f(); // expected-note {{here}} + f<int>(); // expected-note {{here}} + f<char>(); // expected-note {{here}} + } +} |