From 6e164ae7ed48ee511b7c90db04cca3521cc5ef92 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Thu, 26 Jan 2012 17:07:10 +0100 Subject: [PATCH] Fixed ticket #3557 (Tokenizer: simplification of '[]' doesn't work well): extract undefined size array simplification and handle multiple arrays and combos between pointers and arrays, don't handle the definitions as arguments of function. --- lib/tokenize.cpp | 37 ++++++++++++++++++++++++++++++------- lib/tokenize.h | 5 +++++ test/testsimplifytokens.cpp | 11 +++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e8e05e139..ad88d7b15 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3598,13 +3598,7 @@ bool Tokenizer::simplifyTokenList() simplifySizeof(); - // change array to pointer.. - for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok, "%type% %var% [ ] [,;=]")) { - tok->next()->deleteNext(2); - tok->insertToken("*"); - } - } + simplifyUndefinedSizeArray(); // Replace constants.. for (Token *tok = _tokens; tok; tok = tok->next()) { @@ -4732,6 +4726,35 @@ bool Tokenizer::simplifyQuestionMark() return ret; } +void Tokenizer::simplifyUndefinedSizeArray() +{ + for (Token *tok = _tokens; tok; tok = tok->next()) { + if (Token::Match(tok, "%type%")) { + Token *tok2 = tok->next(); + while (tok2 && tok2->str() == "*") + tok2 = tok2->next(); + if (!Token::Match(tok2, "%var% [ ]")) + continue; + + tok = tok2->previous(); + Token *end = tok2->next(); + unsigned int count = 0; + while (Token::Match(end, "[ ] [;=[]")) { + end = end->tokAt(2); + ++count; + } + if (Token::Match(end, "[;=]")) { + do { + tok2->deleteNext(2); + tok->insertToken("*"); + } while (--count); + tok = end; + } else + tok = tok->tokAt(3); + } + } +} + void Tokenizer::simplifyCasts() { for (Token *tok = _tokens; tok; tok = tok->next()) { diff --git a/lib/tokenize.h b/lib/tokenize.h index ddb483651..7d1f89056 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -349,6 +349,11 @@ public: */ void simplifyCasts(); + /** + * Change (multiple) arrays to (multiple) pointers. + */ + void simplifyUndefinedSizeArray(); + /** * A simplify function that replaces a variable with its value in cases * when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)" diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 41662dd33..af3381451 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -402,6 +402,8 @@ private: TEST_CASE(removeRedundantFor); TEST_CASE(consecutiveBraces); + + TEST_CASE(undefinedSizeArray); } std::string tok(std::string code, bool simplify = true, Settings::PlatformType type = Settings::Unspecified) { @@ -7623,6 +7625,15 @@ private: ASSERT_EQUALS("void f ( ) { for ( ; ; ) { } }", tok("void f () { for(;;){} }", true)); ASSERT_EQUALS("void f ( ) { { scope_lock lock ; foo ( ) ; } { scope_lock lock ; bar ( ) ; } }", tok("void f () { {scope_lock lock; foo();} {scope_lock lock; bar();} }", true)); } + + void undefinedSizeArray() { + ASSERT_EQUALS("int * x ;", tok("int x [];")); + ASSERT_EQUALS("int * * x ;", tok("int x [][];")); + ASSERT_EQUALS("int * * x ;", tok("int * x [];")); + ASSERT_EQUALS("int * * * x ;", tok("int * x [][];")); + ASSERT_EQUALS("int * * * * x ;", tok("int * * x [][];")); + ASSERT_EQUALS("void f ( int x [ ] , double y [ ] ) { }", tok("void f(int x[], double y[]) { }")); + } }; REGISTER_TEST(TestSimplifyTokens)