From: Ted Kremenek Date: Wed, 26 Jan 2011 04:49:43 +0000 (+0000) Subject: Tweak -Wuninitialized-experimental to not emit X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dd0f7942c5415ce146dcc02d57fc503c683f8625;p=clang Tweak -Wuninitialized-experimental to not emit a warning for uses of an uninitialized variable when the use is a void cast, e.g. (void) x. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124278 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Analysis/UninitializedValuesV2.cpp b/lib/Analysis/UninitializedValuesV2.cpp index 4edc1a965b..3263054959 100644 --- a/lib/Analysis/UninitializedValuesV2.cpp +++ b/lib/Analysis/UninitializedValuesV2.cpp @@ -291,6 +291,7 @@ class TransferFunctions : public CFGRecStmtVisitor { AnalysisContext ∾ UninitVariablesHandler *handler; const DeclRefExpr *currentDR; + const Expr *currentVoidCast; const bool flagBlockUses; public: TransferFunctions(CFGBlockValues &vals, const CFG &cfg, @@ -298,7 +299,7 @@ public: UninitVariablesHandler *handler, bool flagBlockUses) : vals(vals), cfg(cfg), ac(ac), handler(handler), currentDR(0), - flagBlockUses(flagBlockUses) {} + currentVoidCast(0), flagBlockUses(flagBlockUses) {} const CFG &getCFG() { return cfg; } void reportUninit(const DeclRefExpr *ex, const VarDecl *vd); @@ -446,14 +447,23 @@ void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) { SaveAndRestore lastDR(currentDR, res.getDeclRefExpr()); Visit(ce->getSubExpr()); - if (vals[vd] == Uninitialized) { + if (currentVoidCast != ce && vals[vd] == Uninitialized) { reportUninit(res.getDeclRefExpr(), vd); // Don't cascade warnings. vals[vd] = Initialized; } return; } - } + } + else if (CStyleCastExpr *cse = dyn_cast(ce)) { + if (cse->getType()->isVoidType()) { + // e.g. (void) x; + SaveAndRestore + lastVoidCast(currentVoidCast, cse->getSubExpr()->IgnoreParens()); + Visit(cse->getSubExpr()); + return; + } + } Visit(ce->getSubExpr()); } diff --git a/test/Sema/uninit-variables.c b/test/Sema/uninit-variables.c index 200fc83b10..1bcfc8ad64 100644 --- a/test/Sema/uninit-variables.c +++ b/test/Sema/uninit-variables.c @@ -212,3 +212,14 @@ void test32() { (void) ^{ (void) test32_x; }; // no-warning } +void test_33() { + int x; // no-warning + (void) x; +} + +int test_34() { + int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} + (void) x; + return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} +} +