From 1507bf55c958bdd4b14a3ee49f66af874f179cf2 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Tue, 10 Sep 2013 21:10:25 +0000 Subject: [PATCH] Fix regression from r190382. Make sure we perform the correct "referenced-but-not-used" check for static member constants. Fixes bug reported on cfe-commits by Alexey Samsonov. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190437 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/Sema.cpp | 9 +++++++++ lib/Sema/SemaDecl.cpp | 8 -------- test/SemaCXX/warn-unused-filescoped.cpp | 9 +++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp index fbff9499d5..91f6d5f7c2 100644 --- a/lib/Sema/Sema.cpp +++ b/lib/Sema/Sema.cpp @@ -358,6 +358,15 @@ static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) { } if (const VarDecl *VD = dyn_cast(D)) { + // If a variable usable in constant expressions is referenced, + // don't warn if it isn't used: if the value of a variable is required + // for the computation of a constant expression, it doesn't make sense to + // warn even if the variable isn't odr-used. (isReferenced doesn't + // precisely reflect that, but it's a decent approximation.) + if (VD->isReferenced() && + VD->isUsableInConstantExpressions(SemaRef->Context)) + return true; + // UnusedFileScopedDecls stores the first declaration. // The declaration may have become definition so check again. const VarDecl *DeclToCheck = VD->getDefinition(); diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 5dbfa105e1..9c93e11312 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1220,14 +1220,6 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const { if (!isMainFileLoc(*this, VD->getLocation())) return false; - // If a variable usable in constant expressions is referenced, - // don't warn if it isn't used: if the value of a variable is required - // for the computation of a constant expression, it doesn't make sense to - // warn even if the variable isn't odr-used. (isReferenced doesn't - // precisely reflect that, but it's a decent approximation.) - if (VD->isReferenced() && VD->isUsableInConstantExpressions(Context)) - return false; - if (Context.DeclMustBeEmitted(VD)) return false; diff --git a/test/SemaCXX/warn-unused-filescoped.cpp b/test/SemaCXX/warn-unused-filescoped.cpp index aa2b2e5103..65f10e6695 100644 --- a/test/SemaCXX/warn-unused-filescoped.cpp +++ b/test/SemaCXX/warn-unused-filescoped.cpp @@ -178,4 +178,13 @@ namespace pr14776 { auto b = X(); // expected-warning {{unused variable 'b'}} } +namespace UndefinedInternalStaticMember { + namespace { + struct X { + static const unsigned x = 3; + int y[x]; + }; + } +} + #endif -- 2.50.1