From fce6f11ed8f0e1c2e6fe9b9cfd6b771e4f5deb24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 4 Nov 2010 19:02:54 +0100 Subject: [PATCH] Tokenizer: simplify 'a[b-1]+=1' better --- lib/tokenize.cpp | 23 ++++++++++++++++++----- test/testtokenize.cpp | 1 + 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 45ddb8183..71e861afb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4390,11 +4390,24 @@ void Tokenizer::simplifyCompoundAssignment() // variable.. tok = tok->tokAt(2); - while (Token::Match(tok, ". %var%")) - tok = tok->tokAt(2); - while (Token::Match(tok, "[ %any% ]")) - tok = tok->tokAt(3); - + while (Token::Match(tok, ". %var%") || (tok && tok->str() == "[")) + { + if (tok->str() != "[") + tok = tok->tokAt(2); + else + { + // 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) break; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 480527b02..dd7e06c62 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4645,6 +4645,7 @@ private: ASSERT_EQUALS("; x . y = x . y + 1 ;", tokenizeAndStringify("; x.y += 1;")); ASSERT_EQUALS("; x [ 0 ] = x [ 0 ] + 1 ;", tokenizeAndStringify("; x[0] += 1;")); + ASSERT_EQUALS("; x [ y - 1 ] = x [ y - 1 ] + 1 ;", tokenizeAndStringify("; x[y-1] += 1;")); } void simplifyAssignmentInFunctionCall()