From 3cc9a333c9bf211763da088e2819ea9a0d5a82a4 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Tue, 8 Nov 2011 17:55:04 +0100 Subject: [PATCH] Fixed tokenizer: don't change '*(str+num)' if '*' means a product with a previous variable or number. Ditto for '&str[num]' (still commented). --- lib/tokenize.cpp | 18 ++++++++---------- test/testtokenize.cpp | 12 ++++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 7c2d93e3d..a021c0377 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4154,8 +4154,10 @@ bool Tokenizer::simplifyTokenList() // Replace "*(str + num)" => "str[num]" for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok->next(), "* ( %var% + %num% )") || - Token::Match(tok->next(), "* ( %var% + %var% )")) { + if (!Token::Match(tok, "%var%") && !Token::Match(tok, "%num%") + && !Token::Match(tok, "]|)") + && (Token::Match(tok->next(), "* ( %var% + %num% )") || + Token::Match(tok->next(), "* ( %var% + %var% )"))) { // remove '* (' tok->deleteNext(); tok->deleteNext(); @@ -4178,15 +4180,11 @@ bool Tokenizer::simplifyTokenList() //"[test.cpp:7]: (error) Invalid pointer 'first' after push_back / push_front\n". //Actual: //"". - //2) - //test/testautovariables.cpp:279: Assertion failed. - //Expected: - //"[test.cpp:4]: (error) Return of the address of an auto-variable\n". - //Actual: - //"". /*for (Token *tok = _tokens; tok; tok = tok->next()) { - if ((Token::Match(tok->next(), "& %var% [ %num% ]") || - Token::Match(tok->next(), "& %var% [ %var% ]"))) { + if (!Token::Match(tok, "%var%") && !Token::Match(tok, "%num%") + && !Token::Match(tok, "]|)") + && (Token::Match(tok->next(), "& %var% [ %num% ]") || + Token::Match(tok->next(), "& %var% [ %var% ]"))) { tok = tok->next(); // '&' => '(' tok->str("("); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index b5ce2e201..bd2484e70 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -475,9 +475,21 @@ private: } void tokenize6() { + // "&p[1]" => "p+1" + /* + ASSERT_EQUALS("; x = p + n ;", tokenizeAndStringify("; x = & p [ n ] ;", true)); + ASSERT_EQUALS("; x = ( p + n ) [ m ] ;", tokenizeAndStringify("; x = & p [ n ] [ m ] ;", true)); + ASSERT_EQUALS("; x = y & p [ n ] ;", tokenizeAndStringify("; x = y & p [ n ] ;", true)); + ASSERT_EQUALS("; x = 10 & p [ n ] ;", tokenizeAndStringify("; x = 10 & p [ n ] ;", true)); + ASSERT_EQUALS("; x = y [ 10 ] & p [ n ] ;", tokenizeAndStringify("; x = y [ 10 ] & p [ n ] ;", true)); + ASSERT_EQUALS("; x = ( a + m ) & p [ n ] ;", tokenizeAndStringify("; x = ( a + m ) & p [ n ] ;", true));*/ // "*(p+1)" => "p[1]" ASSERT_EQUALS("; x = p [ 1 ] ;", tokenizeAndStringify("; x = * ( p + 1 ) ;", true)); ASSERT_EQUALS("; x = p [ n ] ;", tokenizeAndStringify("; x = * ( p + n ) ;", true)); + ASSERT_EQUALS("; x = y * ( p + n ) ;", tokenizeAndStringify("; x = y * ( p + n ) ;", true)); + ASSERT_EQUALS("; x = 10 * ( p + n ) ;", tokenizeAndStringify("; x = 10 * ( p + n ) ;", true)); + ASSERT_EQUALS("; x = y [ 10 ] * ( p + n ) ;", tokenizeAndStringify("; x = y [ 10 ] * ( p + n ) ;", true)); + ASSERT_EQUALS("; x = ( a + m ) * ( p + n ) ;", tokenizeAndStringify("; x = ( a + m ) * ( p + n ) ;", true)); } void tokenize7() {