aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Sema/SemaDecl.cpp9
-rw-r--r--test/SemaCXX/warn-unused-filescoped.cpp15
2 files changed, 22 insertions, 2 deletions
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index e8d017c098..3576190310 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -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;
diff --git a/test/SemaCXX/warn-unused-filescoped.cpp b/test/SemaCXX/warn-unused-filescoped.cpp
index dbff4b0e68..328d8bbb5b 100644
--- a/test/SemaCXX/warn-unused-filescoped.cpp
+++ b/test/SemaCXX/warn-unused-filescoped.cpp
@@ -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);
+}