PR19305: Don't issue -Wunused-variable warnings on variable templates. It's not
authorRichard Smith <richard-llvm@metafoo.co.uk>
Wed, 2 Apr 2014 18:28:36 +0000 (18:28 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Wed, 2 Apr 2014 18:28:36 +0000 (18:28 +0000)
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.

llvm-svn: 205448

clang/lib/Sema/SemaDecl.cpp
clang/test/SemaCXX/warn-unused-label-error.cpp [new file with mode: 0644]
clang/test/SemaCXX/warn-unused-variables.cpp

index 9d7f13c..43d855c 100644 (file)
@@ -1204,7 +1204,8 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
   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
diff --git a/clang/test/SemaCXX/warn-unused-label-error.cpp b/clang/test/SemaCXX/warn-unused-label-error.cpp
new file mode 100644 (file)
index 0000000..66b616f
--- /dev/null
@@ -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}}
+      ;
+  }
+}
index 93d2f6f..ecb36ec 100644 (file)
@@ -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}}
-      ;
-  }
-}