[cfe-commits] r166896 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/SemaCXX/warn-unused-filescoped.cpp

Richard Smith richard-llvm at metafoo.co.uk
Sat Oct 27 21:47:21 PDT 2012


Author: rsmith
Date: Sat Oct 27 23:47:21 2012
New Revision: 166896

URL: http://llvm.org/viewvc/llvm-project?rev=166896&view=rev
Log:
In -Wunneeded-internal-declaration, suppress the warning for variables which
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'.

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=166896&r1=166895&r2=166896&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Oct 27 23:47:21 2012
@@ -1205,10 +1205,17 @@
       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;

Modified: cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp?rev=166896&r1=166895&r2=166896&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp Sat Oct 27 23:47:21 2012
@@ -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 @@
     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);
+}





More information about the cfe-commits mailing list