From 77fd3c0d7837040bb84c1617a6a423f833e784fe Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Sat, 19 Jan 2013 00:25:06 +0000 Subject: [PATCH] -Wuninitialized: warn about uninitialized values resulting from ?: that evaluate to lvalues (in C++). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172875 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/UninitializedValues.cpp | 10 ++++++++++ test/SemaCXX/uninitialized.cpp | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/Analysis/UninitializedValues.cpp b/lib/Analysis/UninitializedValues.cpp index fbd49de20e..0eb96dc925 100644 --- a/lib/Analysis/UninitializedValues.cpp +++ b/lib/Analysis/UninitializedValues.cpp @@ -358,6 +358,16 @@ static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) { } void ClassifyRefs::classify(const Expr *E, Class C) { + // The result of a ?: could also be an lvalue. + E = E->IgnoreParens(); + if (const ConditionalOperator *CO = dyn_cast(E)) { + const Expr *TrueExpr = CO->getTrueExpr(); + if (!isa(TrueExpr)) + classify(TrueExpr, C); + classify(CO->getFalseExpr(), C); + return; + } + FindVarResult Var = findVar(E, DC); if (const DeclRefExpr *DRE = Var.getDeclRefExpr()) Classification[DRE] = std::max(Classification[DRE], C); diff --git a/test/SemaCXX/uninitialized.cpp b/test/SemaCXX/uninitialized.cpp index f55f10f7ed..6d7d548521 100644 --- a/test/SemaCXX/uninitialized.cpp +++ b/test/SemaCXX/uninitialized.cpp @@ -41,8 +41,8 @@ void test_stuff () { int j = far(j); int k = __alignof__(k); - int l = k ? l : l; // FIXME: warn here - int m = 1 + (k ? m : m); // FIXME: warn here + int l = k ? l : l; // expected-warning {{variable 'l' is uninitialized when used within its own initialization}} + int m = 1 + (k ? m : m); // expected-warning {{'m' is uninitialized when used within its own initialization}} int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} for (;;) { @@ -61,8 +61,8 @@ void test_stuff () { int j = far(j); int k = __alignof__(k); - int l = k ? l : l; // FIXME: warn here - int m = 1 + (k ? m : m); // FIXME: warn here + int l = k ? l : l; // expected-warning {{variable 'l' is uninitialized when used within its own initialization}} + int m = 1 + (k ? m : m); // expected-warning {{'m' is uninitialized when used within its own initialization}} int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}} } } -- 2.40.0