r205448 - PR19305: Don't issue -Wunused-variable warnings on variable templates. It's not

Richard Smith richard-llvm at metafoo.co.uk
Wed Apr 2 11:28:36 PDT 2014


Author: rsmith
Date: Wed Apr  2 13:28:36 2014
New Revision: 205448

URL: http://llvm.org/viewvc/llvm-project?rev=205448&view=rev
Log:
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.

Added:
    cfe/trunk/test/SemaCXX/warn-unused-label-error.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/warn-unused-variables.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=205448&r1=205447&r2=205448&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Apr  2 13:28:36 2014
@@ -1204,7 +1204,8 @@ bool Sema::ShouldWarnIfUnusedFileScopedD
   if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>())
     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

Added: cfe/trunk/test/SemaCXX/warn-unused-label-error.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-label-error.cpp?rev=205448&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-label-error.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-unused-label-error.cpp Wed Apr  2 13:28:36 2014
@@ -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}}
+      ;
+  }
+}

Modified: cfe/trunk/test/SemaCXX/warn-unused-variables.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-variables.cpp?rev=205448&r1=205447&r2=205448&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-variables.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-variables.cpp Wed Apr  2 13:28:36 2014
@@ -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<typename T> void f() {
   T t;
   t = 17;
@@ -115,6 +115,17 @@ namespace PR11550 {
   }
 }
 
+namespace PR19305 {
+  template<typename T> int n = 0; // no warning
+  int a = n<int>;
+
+  template<typename T> const int l = 0; // no warning
+  int b = l<int>;
+
+  // FIXME: It'd be nice to warn here.
+  template<typename T> 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}}
-      ;
-  }
-}





More information about the cfe-commits mailing list