From b80a954f5520a4f9ba0a9f49c1925ad2f1dd1769 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 21 Oct 2009 21:08:00 +0200 Subject: [PATCH] Fixed #844 (Tokenizer: Simplify 'sizeof *ptr' correctly) --- src/tokenize.cpp | 16 ++++++++++++---- test/testsimplifytokens.cpp | 31 ++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 2f851bb193e..d07db7c5992 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1585,16 +1585,24 @@ void Tokenizer::simplifySizeof() else if (Token::Match(tok, "sizeof ( * %var% )") || Token::Match(tok, "sizeof ( %var% [ %num% ] )")) { // Some default value.. - unsigned int sz = 100; + unsigned int sz = 0; unsigned int varid = tok->tokAt((tok->tokAt(2)->str() == "*") ? 3 : 2)->varId(); if (varid != 0) { // Try to locate variable declaration.. - const Token *decltok = Token::findmatch(_tokens, "%type% %varid% [", varid); - if (decltok) + const Token *decltok = Token::findmatch(_tokens, "%varid%", varid); + if (Token::findmatch(decltok->previous(), "%type% %var% [")) { - sz = sizeOfType(decltok); + sz = sizeOfType(decltok->previous()); + } + else if (Token::findmatch(decltok->previous(), "* %var% [")) + { + sz = sizeOfType(decltok->previous()); + } + else if (Token::findmatch(decltok->tokAt(-2), "%type% * %var%")) + { + sz = sizeOfType(decltok->tokAt(-2)); } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 869fb876a1b..7d436dd3dbb 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -67,6 +67,7 @@ class TestSimplifyTokens : public TestFixture TEST_CASE(sizeof9); TEST_CASE(sizeof10); TEST_CASE(sizeof11); + TEST_CASE(sizeof12); TEST_CASE(casting); TEST_CASE(template1); @@ -549,11 +550,8 @@ class TestSimplifyTokens : public TestFixture void sizeof1() { - const char code1[] = " struct ABC *abc = malloc(sizeof(*abc)); "; - const char code2[] = " struct ABC *abc = malloc(100); "; - const char code3[] = " struct ABC *abc = malloc(sizeof *abc ); "; - ASSERT_EQUALS(tok(code1), tok(code2)); - ASSERT_EQUALS(tok(code2), tok(code3)); + ASSERT_EQUALS("struct ABC * abc ; abc = malloc ( 100 ) ;", tok("struct ABC *abc = malloc(sizeof(*abc));")); + ASSERT_EQUALS("struct ABC * abc ; abc = malloc ( 100 ) ;", tok("struct ABC *abc = malloc(sizeof *abc );")); } @@ -597,9 +595,10 @@ class TestSimplifyTokens : public TestFixture void sizeof5() { const char code[] = - "for (int i = 0; i < sizeof(g_ReservedNames[0]); i++)" + "const char * names[2];" + "for (int i = 0; i < sizeof(names[0]); i++)" "{}"; - ASSERT_EQUALS("for ( int i = 0 ; i < 100 ; i ++ ) { }", sizeof_(code)); + ASSERT_EQUALS("const char * names [ 2 ] ; for ( int i = 0 ; i < 4 ; i ++ ) { }", sizeof_(code)); } void sizeof6() @@ -821,6 +820,24 @@ class TestSimplifyTokens : public TestFixture ASSERT_EQUALS(expected, tok(code)); } + void sizeof12() + { + // ticket #827 + const char code[] = "void f()\n" + "{\n" + " int *p;\n" + " (sizeof *p);\n" + "}"; + + const char expected[] = "void f ( ) " + "{" + " int * p ;" + " 4 ; " + "}"; + + ASSERT_EQUALS(expected, tok(code)); + } + void casting() { {