]> granicus.if.org Git - clang/commitdiff
Suppress -Wunused-variable for variables declared in headers, which may in
authorMatt Beaumont-Gay <matthewbg@google.com>
Wed, 10 Apr 2013 00:47:10 +0000 (00:47 +0000)
committerMatt Beaumont-Gay <matthewbg@google.com>
Wed, 10 Apr 2013 00:47:10 +0000 (00:47 +0000)
fact be defined and used in another TU.

Reshuffle some test cases because we suppress -Wunused-variable after we've
emitted an error.

This fixes PR15558.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@179138 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 6bab9e80cbf75c0fcccc8a882024a83e9cc421c3..e271f78ed0c556ce5c9b470585a02adf338589a0 100644 (file)
@@ -751,9 +751,13 @@ void Sema::ActOnEndOfTranslationUnit() {
         if (DiagD->isReferenced()) {
           Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
                 << /*variable*/1 << DiagD->getDeclName();
-        } else {
+        } else if (getSourceManager().isFromMainFile(DiagD->getLocation())) {
+          // If the declaration is in a header which is included into multiple
+          // TUs, it will declare one variable per TU, and one of the other
+          // variables may be used. So, only warn if the declaration is in the
+          // main file.
           Diag(DiagD->getLocation(), diag::warn_unused_variable)
-                << DiagD->getDeclName();
+              << DiagD->getDeclName();
         }
       }
     }
diff --git a/test/SemaCXX/Inputs/warn-unused-variables.h b/test/SemaCXX/Inputs/warn-unused-variables.h
new file mode 100644 (file)
index 0000000..5fac459
--- /dev/null
@@ -0,0 +1,11 @@
+// Verify that we don't warn about variables of internal-linkage type in
+// headers, as the use may be in another TU.
+namespace PR15558 {
+namespace {
+class A {};
+}
+
+class B {
+  static A a;
+};
+}
diff --git a/test/SemaCXX/warn-unused-variables-error.cpp b/test/SemaCXX/warn-unused-variables-error.cpp
new file mode 100644 (file)
index 0000000..6386c4b
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -verify %s
+
+namespace PR6948 {
+  template<typename T> class X; // expected-note{{template is declared here}}
+  
+  void f() {
+    X<char> str (read_from_file()); // expected-error{{use of undeclared identifier 'read_from_file'}} \
+                                       expected-error{{implicit instantiation of undefined template 'PR6948::X<char>'}}
+  }
+}
index 4e8d51d319e932373f39fc61819457e16ae23e9e..00597f929b259962ed1bced1cb2313b057ab2324 100644 (file)
@@ -41,15 +41,6 @@ void test_dependent_init(T *p) {
   (void)i;
 }
 
-namespace PR6948 {
-  template<typename T> class X; // expected-note{{template is declared here}}
-  
-  void f() {
-    X<char> str (read_from_file()); // expected-error{{use of undeclared identifier 'read_from_file'}} \
-                                       expected-error{{implicit instantiation of undefined template 'PR6948::X<char>'}}
-  }
-}
-
 void unused_local_static() {
   static int x = 0;
   static int y = 0; // expected-warning{{unused variable 'y'}}
@@ -135,3 +126,5 @@ namespace ctor_with_cleanups {
     S2 s((S1()));
   }
 }
+
+#include "Inputs/warn-unused-variables.h"