From 19b6a707a86302adc80d64464cbc3cb8a7a7f3a6 Mon Sep 17 00:00:00 2001 From: Matt Beaumont-Gay Date: Wed, 10 Apr 2013 00:47:10 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@179138 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/Sema.cpp | 8 ++++++-- test/SemaCXX/Inputs/warn-unused-variables.h | 11 +++++++++++ test/SemaCXX/warn-unused-variables-error.cpp | 10 ++++++++++ test/SemaCXX/warn-unused-variables.cpp | 11 ++--------- 4 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 test/SemaCXX/Inputs/warn-unused-variables.h create mode 100644 test/SemaCXX/warn-unused-variables-error.cpp diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp index 6bab9e80cb..e271f78ed0 100644 --- a/lib/Sema/Sema.cpp +++ b/lib/Sema/Sema.cpp @@ -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 index 0000000000..5fac45922c --- /dev/null +++ b/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; +}; +} diff --git a/test/SemaCXX/warn-unused-variables-error.cpp b/test/SemaCXX/warn-unused-variables-error.cpp new file mode 100644 index 0000000000..6386c4b682 --- /dev/null +++ b/test/SemaCXX/warn-unused-variables-error.cpp @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -verify %s + +namespace PR6948 { + template class X; // expected-note{{template is declared here}} + + void f() { + X str (read_from_file()); // expected-error{{use of undeclared identifier 'read_from_file'}} \ + expected-error{{implicit instantiation of undefined template 'PR6948::X'}} + } +} diff --git a/test/SemaCXX/warn-unused-variables.cpp b/test/SemaCXX/warn-unused-variables.cpp index 4e8d51d319..00597f929b 100644 --- a/test/SemaCXX/warn-unused-variables.cpp +++ b/test/SemaCXX/warn-unused-variables.cpp @@ -41,15 +41,6 @@ void test_dependent_init(T *p) { (void)i; } -namespace PR6948 { - template class X; // expected-note{{template is declared here}} - - void f() { - X str (read_from_file()); // expected-error{{use of undeclared identifier 'read_from_file'}} \ - expected-error{{implicit instantiation of undefined template 'PR6948::X'}} - } -} - 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" -- 2.40.0