[PATCH] Fix PR15558

Matt Beaumont-Gay matthewbg at google.com
Fri Mar 29 15:19:26 PDT 2013


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"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D597.1.patch
Type: text/x-patch
Size: 2545 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130329/3edd3bdf/attachment.bin>


More information about the cfe-commits mailing list