From 084b21494a847f537b86c743eaf8496afd6dff2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 20 Feb 2010 11:43:53 +0100 Subject: [PATCH] Fixed #1422 (false positive: uninitialized variable) --- lib/tokenize.cpp | 15 ++++++++++++++- test/testtokenize.cpp | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d1ead4a59..16e59334e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3194,11 +3194,14 @@ void Tokenizer::simplifyIfAddBraces() tok = tok->next(); Token *tempToken = tok; + bool innerIf = Token::simpleMatch(tempToken->next(), "if"); + // insert close brace.. // In most cases it would work to just search for the next ';' and insert a closing brace after it. // But here are special cases.. // * if (cond) for (;;) break; // * if (cond1) if (cond2) { } + // * if (cond1) if (cond2) ; else ; int parlevel = 0; int indentlevel = 0; while ((tempToken = tempToken->next()) != NULL) @@ -3235,7 +3238,17 @@ void Tokenizer::simplifyIfAddBraces() } else if (indentlevel == 0 && parlevel == 0 && tempToken->str() == ";") - break; + { + if (!innerIf) + break; + + if (Token::Match(tempToken, "; else if")) + ; + else if (Token::Match(tempToken, "; else")) + innerIf = false; + else + break; + } } if (tempToken) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 717c6882a..5a96adf65 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -71,6 +71,7 @@ private: TEST_CASE(ifAddBraces7); TEST_CASE(ifAddBraces9); TEST_CASE(ifAddBraces10); + TEST_CASE(ifAddBraces11); TEST_CASE(whileAddBraces); TEST_CASE(doWhileAddBraces); @@ -633,6 +634,14 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } + void ifAddBraces11() + { + // ticket #1361 + const char code[] = "{ if (x) if (y) ; else ; }"; + const char expected[] = "{ if ( x ) { if ( y ) { ; } else { ; } } }"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + void whileAddBraces() { {