From: Richard Smith Date: Wed, 2 Apr 2014 18:28:36 +0000 (+0000) Subject: PR19305: Don't issue -Wunused-variable warnings on variable templates. It's not X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ce167d998928ace8e12dd0edf7b5104850b2b325;p=clang PR19305: Don't issue -Wunused-variable warnings on variable templates. It's not meaningful to odr-use the VarDecl inside a variable template. (Separately, it'd be nice to track referenced-ness for templates, and warn on unused ones, but that's really a distinct issue...) Move a test that generates and tests a warning-suppressing error out to its own test file, so it doesn't have weird effects on the other tests in the same file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205448 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 9d7f13cdd7..43d855c0cb 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1204,7 +1204,8 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const { if (D->isInvalidDecl() || D->isUsed() || D->hasAttr()) return false; - // Ignore class templates. + // Ignore all entities declared within templates, and out-of-line definitions + // of members of class templates. if (D->getDeclContext()->isDependentContext() || D->getLexicalDeclContext()->isDependentContext()) return false; @@ -9044,7 +9045,8 @@ Sema::FinalizeDeclaration(Decl *ThisDecl) { if (DC->getRedeclContext()->isFileContext() && VD->isExternallyVisible()) AddPushedVisibilityAttribute(VD); - if (VD->isFileVarDecl()) + // FIXME: Warn on unused templates. + if (VD->isFileVarDecl() && !VD->getDescribedVarTemplate()) MarkUnusedFileScopedDecl(VD); // Now we have parsed the initializer and can update the table of magic diff --git a/test/SemaCXX/warn-unused-label-error.cpp b/test/SemaCXX/warn-unused-label-error.cpp new file mode 100644 index 0000000000..66b616f3cf --- /dev/null +++ b/test/SemaCXX/warn-unused-label-error.cpp @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -verify %s + +static int unused_local_static; + +namespace PR8455 { + void f() { + A: // expected-warning {{unused label 'A'}} + __attribute__((unused)) int i; // attribute applies to variable + B: // attribute applies to label + __attribute__((unused)); int j; // expected-warning {{unused variable 'j'}} + } + + void g() { + C: // unused label 'C' will not appear here because an error has occurred + __attribute__((unused)) + #pragma weak unused_local_static // expected-error {{expected ';' after __attribute__}} + ; + } + + void h() { + D: // expected-warning {{unused label 'D'}} + #pragma weak unused_local_static + __attribute__((unused)) // expected-warning {{declaration does not declare anything}} + ; + } +} diff --git a/test/SemaCXX/warn-unused-variables.cpp b/test/SemaCXX/warn-unused-variables.cpp index 93d2f6f956..ecb36ec274 100644 --- a/test/SemaCXX/warn-unused-variables.cpp +++ b/test/SemaCXX/warn-unused-variables.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -verify %s +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s template void f() { T t; t = 17; @@ -115,6 +115,17 @@ namespace PR11550 { } } +namespace PR19305 { + template int n = 0; // no warning + int a = n; + + template const int l = 0; // no warning + int b = l; + + // FIXME: It'd be nice to warn here. + template int m = 0; +} + namespace ctor_with_cleanups { struct S1 { ~S1(); @@ -128,26 +139,3 @@ namespace ctor_with_cleanups { } #include "Inputs/warn-unused-variables.h" - -namespace PR8455 { - void f() { - A: // expected-warning {{unused label 'A'}} - __attribute__((unused)) int i; // attribute applies to variable - B: // attribute applies to label - __attribute__((unused)); int j; // expected-warning {{unused variable 'j'}} - } - - void g() { - C: // unused label 'C' will not appear here because an error occurs - __attribute__((unused)) - #pragma weak unused_local_static // expected-error {{expected ';' after __attribute__}} - ; - } - - void h() { - D: // expected-warning {{unused label 'D'}} - #pragma weak unused_local_static - __attribute__((unused)) // expected-warning {{declaration does not declare anything}} - ; - } -}