Fix: sizeof(char*) was not simplified correctly

This commit is contained in:
Reijo Tomperi 2010-04-13 23:17:27 +03:00
parent 4a33b226e6
commit 0912836c25
2 changed files with 41 additions and 16 deletions

View File

@ -2820,10 +2820,9 @@ void Tokenizer::simplifySizeof()
} }
// sizeof(type *) => sizeof(*) // sizeof(type *) => sizeof(*)
if (Token::Match(tok->next(), "( %type% *)")) if (Token::Match(tok->next(), "( %type% * )"))
{ {
tok->next()->deleteNext(); tok->next()->deleteNext();
continue;
} }
if (Token::Match(tok->next(), "( * )")) if (Token::Match(tok->next(), "( * )"))

View File

@ -1095,14 +1095,16 @@ private:
void sizeof18() void sizeof18()
{ {
if (sizeof(short int) == 2)
{ {
std::ostringstream expected;
expected << sizeof(short int);
{ {
const char code[] = "void f()\n" const char code[] = "void f()\n"
"{\n" "{\n"
" sizeof(short int);\n" " sizeof(short int);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 2 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1111,7 +1113,7 @@ private:
"{\n" "{\n"
" sizeof(unsigned short int);\n" " sizeof(unsigned short int);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 2 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1120,7 +1122,7 @@ private:
"{\n" "{\n"
" sizeof(short unsigned int);\n" " sizeof(short unsigned int);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 2 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1129,19 +1131,21 @@ private:
"{\n" "{\n"
" sizeof(signed short int);\n" " sizeof(signed short int);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 2 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
} }
if (sizeof(long long) == 8)
{ {
std::ostringstream expected;
expected << sizeof(long long);
{ {
const char code[] = "void f()\n" const char code[] = "void f()\n"
"{\n" "{\n"
" sizeof(long long);\n" " sizeof(long long);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1150,7 +1154,7 @@ private:
"{\n" "{\n"
" sizeof(signed long long);\n" " sizeof(signed long long);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1159,7 +1163,7 @@ private:
"{\n" "{\n"
" sizeof(unsigned long long);\n" " sizeof(unsigned long long);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1168,7 +1172,7 @@ private:
"{\n" "{\n"
" sizeof(long unsigned long);\n" " sizeof(long unsigned long);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1177,7 +1181,7 @@ private:
"{\n" "{\n"
" sizeof(long long int);\n" " sizeof(long long int);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1186,7 +1190,7 @@ private:
"{\n" "{\n"
" sizeof(signed long long int);\n" " sizeof(signed long long int);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1195,7 +1199,7 @@ private:
"{\n" "{\n"
" sizeof(unsigned long long int);\n" " sizeof(unsigned long long int);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1204,10 +1208,32 @@ private:
"{\n" "{\n"
" sizeof(long unsigned long int);\n" " sizeof(long unsigned long int);\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
} }
{
const char code[] = "void f()\n"
"{\n"
" sizeof(char*);\n"
"}\n";
std::ostringstream expected;
expected << sizeof(int*);
ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str());
}
{
const char code[] = "void f()\n"
"{\n"
" sizeof(unsigned int*);\n"
"}\n";
std::ostringstream expected;
expected << sizeof(int*);
ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code));
ASSERT_EQUALS("", errout.str());
}
} }
void casting() void casting()