]> granicus.if.org Git - clang/commitdiff
In -Wunneeded-internal-declaration, suppress the warning for variables which
authorRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 28 Oct 2012 04:47:21 +0000 (04:47 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 28 Oct 2012 04:47:21 +0000 (04:47 +0000)
might have been used in constant expressions, rather than suppressing it for
variables which are const. The important thing here is that such variables
can have their values used without actually being marked as 'used'.

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

lib/Sema/SemaDecl.cpp
test/SemaCXX/warn-unused-filescoped.cpp

index e8d017c09820a62447ee738744a1386521d9b5a2..357619031066883d9275f3662bef403c5154c750 100644 (file)
@@ -1205,10 +1205,17 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
       return false;
   } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
     if (!VD->isFileVarDecl() ||
-        VD->getType().isConstant(Context) ||
         Context.DeclMustBeEmitted(VD))
       return false;
 
+    // If a variable is usable in constant expressions and it's not odr-used,
+    // its value may still have been used. Conservatively suppress the warning
+    // in this case.
+    const VarDecl *VDWithInit = 0;
+    if (VD->isUsableInConstantExpressions(Context) &&
+        VD->getAnyInitializer(VDWithInit) && VDWithInit->checkInitIsICE())
+      return false;
+
     if (VD->isStaticDataMember() &&
         VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
       return false;
index dbff4b0e68c187bdf5d7bd0606aa7e8d8cc353fd..328d8bbb5b4051b569a97364564875aa3c6743b2 100644 (file)
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -std=c++98 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -std=c++11 %s
 
 static void f1(); // expected-warning{{unused}}
 
@@ -87,3 +88,15 @@ namespace rdar8733476 {
     foo();
   }
 }
+
+namespace test5 {
+  static int n = 0;
+  static int &r = n;
+  int f(int &);
+  int k = f(r);
+
+  static const int m = n; // expected-warning {{not needed and will not be emitted}}
+  int x = sizeof(m);
+  static const double d = 0.0; // expected-warning {{not needed and will not be emitted}}
+  int y = sizeof(d);
+}