From 22a4dd2cc3483e7d319856df47430278b247f864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 23 Nov 2010 20:35:08 +0100 Subject: [PATCH] Fixed #2225 (false positive: variable is assigned a value that is never used '*(stack[0])+=arg') --- lib/tokenize.cpp | 52 ++++++++++++++++++++++++++----------------- test/testtokenize.cpp | 1 + 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 31a200f05..8d0e08779 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4467,7 +4467,7 @@ void Tokenizer::simplifyCompoundAssignment() // "a+=b" => "a = a + b" for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok, "[;{}:] *| %var%")) + if (Token::Match(tok, "[;{}:] *| (| %var%")) { if (tok->str() == ":") { @@ -4481,24 +4481,31 @@ void Tokenizer::simplifyCompoundAssignment() if (tok->strAt(1) == "*") tok = tok->next(); - // variable.. - tok = tok->tokAt(2); - while (Token::Match(tok, ". %var%") || (tok && tok->str() == "[")) + if (tok->strAt(1) == "(") { - if (tok->str() != "[") - tok = tok->tokAt(2); - else + tok = tok->next()->link()->next(); + } + else + { + // variable.. + tok = tok->tokAt(2); + while (Token::Match(tok, ". %var%") || (tok && tok->str() == "[")) { - // goto "]" - tok = tok->next(); - while (tok && !Token::Match(tok, "++|--|(|[|]")) - tok = tok->next(); - if (!tok) - break; - else if (tok->str() == "]") - tok = tok->next(); + if (tok->str() != "[") + tok = tok->tokAt(2); else - break; + { + // goto "]" + tok = tok->next(); + while (tok && !Token::Match(tok, "++|--|(|[|]")) + tok = tok->next(); + if (!tok) + break; + else if (tok->str() == "]") + tok = tok->next(); + else + break; + } } } if (!tok) @@ -4526,15 +4533,18 @@ void Tokenizer::simplifyCompoundAssignment() // simplify the compound assignment.. tok->str("="); tok->insertToken(op); - Token *tokend = 0; + std::stack tokend; for (const Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous()) { tok->insertToken(tok2->str()); tok->next()->varId(tok2->varId()); - if (Token::simpleMatch(tok->next(), "]")) - tokend = tok->next(); - else if (Token::simpleMatch(tok->next(), "[")) - Token::createMutualLinks(tok->next(), tokend); + if (Token::Match(tok->next(), "]|)")) + tokend.push(tok->next()); + else if (Token::Match(tok->next(), "(|[")) + { + Token::createMutualLinks(tok->next(), tokend.top()); + tokend.pop(); + } } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index ce530e8d5..c3fc78ef6 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4694,6 +4694,7 @@ private: ASSERT_EQUALS("{ x = x >> y ; }", tokenizeAndStringify("{ x >>= y;}")); ASSERT_EQUALS("; * p = * p + y ;", tokenizeAndStringify("; *p += y;")); + ASSERT_EQUALS("; * ( p [ 0 ] ) = * ( p [ 0 ] ) + y ;", tokenizeAndStringify("; *(p[0]) += y;")); ASSERT_EQUALS("case 0 : x = x + y ; break ;", tokenizeAndStringify("case 0: x += y; break;"));