Fix PR 2514: Do not flag dead initializations for variables initialized to a constant global variable.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64149 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ted Kremenek 2009-02-09 18:01:00 +00:00
Родитель 353ffceafc
Коммит d3098ee64c
2 изменённых файлов: 31 добавлений и 2 удалений

Просмотреть файл

@ -195,7 +195,20 @@ public:
// If x is EVER assigned a new value later, don't issue
// a warning. This is because such initialization can be
// due to defensive programming.
if (!E->isConstantInitializer(Ctx))
if (E->isConstantInitializer(Ctx))
return;
// Special case: check for initializations from constant
// variables.
//
// e.g. extern const int MyConstant;
// int x = MyConstant;
//
if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
if (VD->hasGlobalStorage() &&
VD->getType().isConstQualified()) return;
Report(V, DeadInit, V->getLocation(), E->getSourceRange());
}
}

Просмотреть файл

@ -147,3 +147,19 @@ int f18() {
return (x = 10); // expected-warning{{Although the value stored to 'x' is used in the enclosing expression, the value is never actually read from 'x'}}
}
// PR 3514: false positive `dead initialization` warning for init to global
// http://llvm.org/bugs/show_bug.cgi?id=3514
extern const int MyConstant;
int f19(void) {
int x = MyConstant; // no-warning
x = 1;
return x;
}
int f19b(void) { // FIXME: Should this case be considered the same as f19?
const int MyConstant = 0;
int x = MyConstant; // expected-warning{{never read}}
x = 1;
return x;
}