diff options
| author | Ted Kremenek <kremenek@apple.com> | 2009-02-09 18:01:00 +0000 |
|---|---|---|
| committer | Ted Kremenek <kremenek@apple.com> | 2009-02-09 18:01:00 +0000 |
| commit | 0203db73ee97215a16deaf8133d0082b488265a3 (patch) | |
| tree | dac90b0793b0a988f08f77a253c68ed2b1d6cb8f /clang | |
| parent | 9280a685dbc8203b634765b826d2cb2c3a39da91 (diff) | |
| download | bcm5719-llvm-0203db73ee97215a16deaf8133d0082b488265a3.tar.gz bcm5719-llvm-0203db73ee97215a16deaf8133d0082b488265a3.zip | |
Fix PR 2514: Do not flag dead initializations for variables initialized to a constant global variable.
llvm-svn: 64149
Diffstat (limited to 'clang')
| -rw-r--r-- | clang/lib/Analysis/CheckDeadStores.cpp | 17 | ||||
| -rw-r--r-- | clang/test/Analysis/dead-stores.c | 16 |
2 files changed, 31 insertions, 2 deletions
diff --git a/clang/lib/Analysis/CheckDeadStores.cpp b/clang/lib/Analysis/CheckDeadStores.cpp index d5e5f4c9b2a..504de3c97dc 100644 --- a/clang/lib/Analysis/CheckDeadStores.cpp +++ b/clang/lib/Analysis/CheckDeadStores.cpp @@ -195,8 +195,21 @@ 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)) - Report(V, DeadInit, V->getLocation(), E->getSourceRange()); + 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()); } } } diff --git a/clang/test/Analysis/dead-stores.c b/clang/test/Analysis/dead-stores.c index c72bdcf2b45..7d7b3696379 100644 --- a/clang/test/Analysis/dead-stores.c +++ b/clang/test/Analysis/dead-stores.c @@ -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; +} |

