Hi rsmith, nlewycky,

When preparing to warn about an unused static member variable, which we've 
decided is unused because its type has internal linkage, don't warn if the 
variable is in a header. The definition and use may be in another TU.

I had to reshuffle the test a little because we suppress unused-variable 
warnings after we've emitted an error.

http://llvm-reviews.chandlerc.com/D597

Files:
  lib/Sema/Sema.cpp
  test/SemaCXX/Inputs/warn-unused-variables.h
  test/SemaCXX/warn-unused-variables-error.cpp
  test/SemaCXX/warn-unused-variables.cpp

Index: lib/Sema/Sema.cpp
===================================================================
--- lib/Sema/Sema.cpp
+++ lib/Sema/Sema.cpp
@@ -751,9 +751,11 @@
         if (DiagD->isReferenced()) {
           Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
                 << /*variable*/1 << DiagD->getDeclName();
-        } else {
+        } else if (getSourceManager().isFromMainFile(DiagD->getLocation())) {
+          // If the decl is in a header, it may be used in some other TU, so
+          // only warn if it's in the main file.
           Diag(DiagD->getLocation(), diag::warn_unused_variable)
-                << DiagD->getDeclName();
+              << DiagD->getDeclName();
         }
       }
     }
Index: test/SemaCXX/Inputs/warn-unused-variables.h
===================================================================
--- /dev/null
+++ test/SemaCXX/Inputs/warn-unused-variables.h
@@ -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;
+};
+}
Index: test/SemaCXX/warn-unused-variables-error.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/warn-unused-variables-error.cpp
@@ -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: test/SemaCXX/warn-unused-variables.cpp
===================================================================
--- test/SemaCXX/warn-unused-variables.cpp
+++ test/SemaCXX/warn-unused-variables.cpp
@@ -41,15 +41,6 @@
   (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 @@
     S2 s((S1()));
   }
 }
+
+#include "Inputs/warn-unused-variables.h"
Index: lib/Sema/Sema.cpp
===================================================================
--- lib/Sema/Sema.cpp
+++ lib/Sema/Sema.cpp
@@ -751,9 +751,11 @@
         if (DiagD->isReferenced()) {
           Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
                 << /*variable*/1 << DiagD->getDeclName();
-        } else {
+        } else if (getSourceManager().isFromMainFile(DiagD->getLocation())) {
+          // If the decl is in a header, it may be used in some other TU, so
+          // only warn if it's in the main file.
           Diag(DiagD->getLocation(), diag::warn_unused_variable)
-                << DiagD->getDeclName();
+              << DiagD->getDeclName();
         }
       }
     }
Index: test/SemaCXX/Inputs/warn-unused-variables.h
===================================================================
--- /dev/null
+++ test/SemaCXX/Inputs/warn-unused-variables.h
@@ -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;
+};
+}
Index: test/SemaCXX/warn-unused-variables-error.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/warn-unused-variables-error.cpp
@@ -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: test/SemaCXX/warn-unused-variables.cpp
===================================================================
--- test/SemaCXX/warn-unused-variables.cpp
+++ test/SemaCXX/warn-unused-variables.cpp
@@ -41,15 +41,6 @@
   (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 @@
     S2 s((S1()));
   }
 }
+
+#include "Inputs/warn-unused-variables.h"
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to