r179138 - Suppress -Wunused-variable for variables declared in headers, which may in

Matt Beaumont-Gay matthewbg at google.com
Tue Apr 9 17:47:10 PDT 2013


Author: matthewbg
Date: Tue Apr  9 19:47:10 2013
New Revision: 179138

URL: http://llvm.org/viewvc/llvm-project?rev=179138&view=rev
Log:
Suppress -Wunused-variable for variables declared in headers, which may in
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.

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

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=179138&r1=179137&r2=179138&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Tue Apr  9 19:47:10 2013
@@ -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();
         }
       }
     }

Added: cfe/trunk/test/SemaCXX/Inputs/warn-unused-variables.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/Inputs/warn-unused-variables.h?rev=179138&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/Inputs/warn-unused-variables.h (added)
+++ cfe/trunk/test/SemaCXX/Inputs/warn-unused-variables.h Tue Apr  9 19:47:10 2013
@@ -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;
+};
+}

Added: cfe/trunk/test/SemaCXX/warn-unused-variables-error.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-variables-error.cpp?rev=179138&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-variables-error.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-unused-variables-error.cpp Tue Apr  9 19:47:10 2013
@@ -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>'}}
+  }
+}

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=179138&r1=179137&r2=179138&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-variables.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-variables.cpp Tue Apr  9 19:47:10 2013
@@ -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"





More information about the cfe-commits mailing list