From 913670d254c7dd402927c9994bac28a027451a0f Mon Sep 17 00:00:00 2001 From: anuraggarg011 Date: Tue, 4 Sep 2012 13:41:14 +0200 Subject: [PATCH] Fixed #3304 (simple cases) --- lib/tokenize.cpp | 37 +++++++++++++++++++++++++++---------- test/testsimplifytokens.cpp | 13 ++++++++++++- test/testtokenize.cpp | 4 ++-- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 1525f2d81..237a4ee96 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3333,19 +3333,34 @@ bool Tokenizer::simplifyTokenList() } // Replace "&str[num]" => "(str + num)" - //TODO: fix the fails testrunner reports: - //1) - //test/teststl.cpp:805: Assertion failed. - //Expected: - //"[test.cpp:7]: (error) Invalid pointer 'first' after push_back / push_front\n". - //Actual: - //"". - /*for (Token *tok = list.front(); tok; tok = tok->next()) { - if (!Token::Match(tok, "%var%") && !Token::Match(tok, "%num%") + std::set pod; + for (const Token *tok = list.front(); tok; tok = tok->next()) { + if (tok->isStandardType()) { + while (tok && (tok->str() == "*" || tok->isName())) { + if (tok->varId() > 0) { + pod.insert(tok->varId()); + break; + } + tok = tok->next(); + } + } + } + + for (Token *tok = list.front(); tok; tok = tok->next()) { + 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(); + + if (tok->next()->varId()) { + if (pod.find(tok->next()->varId()) == pod.end()) { + tok = tok->tokAt(5); + continue; + } + } + // '&' => '(' tok->str("("); @@ -3359,7 +3374,9 @@ bool Tokenizer::simplifyTokenList() tok->str(")"); Token::createMutualLinks(tok->tokAt(-4), tok); } - }*/ + } + + removeRedundantAssignment(); simplifyRealloc(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 688a31ad2..30cb7e46c 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -421,6 +421,7 @@ private: TEST_CASE(consecutiveBraces); TEST_CASE(undefinedSizeArray); + TEST_CASE(simplifyCase3304); } std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Unspecified) { @@ -6701,7 +6702,6 @@ private: const char expected[] = "void f ( ) " "{" " int a [ 10 ] ;" - " int * p ; p = & a [ 0 ] ;" " * a = 0 ; " "}"; ASSERT_EQUALS(expected, tok(code)); @@ -7884,6 +7884,17 @@ private: ASSERT_EQUALS("int * * * * x ;", tok("int * * x [][];")); ASSERT_EQUALS("void f ( int x [ ] , double y [ ] ) { }", tok("void f(int x[], double y[]) { }")); } + + void simplifyCase3304() { // ticket #3304 + const char code[] = "void foo() {\n" + " int a[10];\n" + " memset(&a[4], 0, 20*sizeof(int));\n" + "}"; + ASSERT_EQUALS("void foo ( ) {" + " int a [ 10 ] ;" + " memset ( a + 4 , 0 , 80 ) ;" + " }", tok(code, true)); + } }; REGISTER_TEST(TestSimplifyTokens) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index df3085edb..73a10d81d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5728,8 +5728,8 @@ private: } void removeRedundantAssignment() { - ASSERT_EQUALS("void f ( ) { int * q ; }", tokenizeAndStringify("void f() { int *p, *q; p = q; }", true)); - ASSERT_EQUALS("void f ( ) { int * q ; }", tokenizeAndStringify("void f() { int *p = 0, *q; p = q; }", true)); + ASSERT_EQUALS("void f ( ) { }", tokenizeAndStringify("void f() { int *p, *q; p = q; }", true)); + ASSERT_EQUALS("void f ( ) { }", tokenizeAndStringify("void f() { int *p = 0, *q; p = q; }", true)); ASSERT_EQUALS("int f ( int * x ) { return * x ; }", tokenizeAndStringify("int f(int *x) { return *x; }", true)); }