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.

llvm-svn: 190437
This commit is contained in:
Eli Friedman 2013-09-10 21:10:25 +00:00
parent 1ea6f1e7f9
commit a5dfebdcfd
3 changed files with 18 additions and 8 deletions

View File

@ -358,6 +358,15 @@ static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
}
if (const VarDecl *VD = dyn_cast<VarDecl>(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();

View File

@ -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;

View File

@ -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