Fixed #6170 (Tokenizer: sizeof on array in 2d-array is not handled properly)
This commit is contained in:
parent
67c9356455
commit
f93cf9d1b7
|
@ -2935,6 +2935,7 @@ bool Tokenizer::simplifySizeof()
|
|||
{
|
||||
// Locate variable declarations and calculate the size
|
||||
std::map<unsigned int, unsigned int> sizeOfVar;
|
||||
std::map<unsigned int, Token *> declTokOfVar;
|
||||
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
||||
if (tok->varId() != 0 && sizeOfVar.find(tok->varId()) == sizeOfVar.end()) {
|
||||
const unsigned int varId = tok->varId();
|
||||
|
@ -2948,15 +2949,17 @@ bool Tokenizer::simplifySizeof()
|
|||
}
|
||||
|
||||
sizeOfVar[varId] = size;
|
||||
declTokOfVar[varId] = tok;
|
||||
}
|
||||
|
||||
else if (Token::Match(tok->previous(), "%type% %var% [ %num% ] [;=]") ||
|
||||
Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [;=]")) {
|
||||
else if (Token::Match(tok->previous(), "%type% %var% [ %num% ] [[;=]") ||
|
||||
Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [[;=]")) {
|
||||
const unsigned int size = sizeOfType(tok->previous());
|
||||
if (size == 0)
|
||||
continue;
|
||||
|
||||
sizeOfVar[varId] = size * static_cast<unsigned int>(MathLib::toLongNumber(tok->strAt(2)));
|
||||
declTokOfVar[varId] = tok;
|
||||
}
|
||||
|
||||
else if (Token::Match(tok->previous(), "%type% %var% [ %num% ] [,)]") ||
|
||||
|
@ -2964,6 +2967,7 @@ bool Tokenizer::simplifySizeof()
|
|||
Token tempTok(0);
|
||||
tempTok.str("*");
|
||||
sizeOfVar[varId] = sizeOfType(&tempTok);
|
||||
declTokOfVar[varId] = tok;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3131,16 +3135,24 @@ bool Tokenizer::simplifySizeof()
|
|||
std::size_t sz = 0;
|
||||
|
||||
const unsigned int varid = tok->tokAt((tok->strAt(2) == "*") ? 3 : 2)->varId();
|
||||
if (varid != 0) {
|
||||
if ((varid != 0) && (declTokOfVar.find(varid) != declTokOfVar.end())) {
|
||||
// Try to locate variable declaration..
|
||||
const Token *decltok = Token::findmatch(list.front(), "%varid%", varid);
|
||||
if (Token::Match(decltok->previous(), "%type% %var% [")) {
|
||||
sz = sizeOfType(decltok->previous());
|
||||
} else if (Token::Match(decltok->previous(), "* %var% [")) {
|
||||
const Token *decltok = declTokOfVar[varid];
|
||||
if (Token::Match(decltok->previous(), "%type%|* %var% [")) {
|
||||
sz = sizeOfType(decltok->previous());
|
||||
} else if (Token::Match(decltok->tokAt(-2), "%type% * %var%")) {
|
||||
sz = sizeOfType(decltok->tokAt(-2));
|
||||
}
|
||||
// Multi-dimensional array..
|
||||
if (Token::Match(decltok,"%var% [") && Token::simpleMatch(decltok->linkAt(1), "] [")) {
|
||||
const Token *tok2 = decltok->linkAt(1);
|
||||
while (Token::Match(tok2, "] [ %num% ]")) {
|
||||
sz = sz * MathLib::toLongNumber(tok2->strAt(2));
|
||||
tok2 = tok2->linkAt(3);
|
||||
}
|
||||
if (Token::simpleMatch(tok2, "] ["))
|
||||
sz = 0;
|
||||
}
|
||||
} else if (tok->strAt(3) == "[" && tok->tokAt(2)->isStandardType()) {
|
||||
sz = sizeOfType(tok->tokAt(2));
|
||||
if (sz == 0)
|
||||
|
|
|
@ -65,9 +65,7 @@ private:
|
|||
TEST_CASE(elseif1);
|
||||
TEST_CASE(ifa_ifa); // "if (a) { if (a) .." => "if (a) { if (1) .."
|
||||
|
||||
TEST_CASE(sizeof2);
|
||||
TEST_CASE(sizeof3);
|
||||
TEST_CASE(sizeof4);
|
||||
TEST_CASE(sizeof_array);
|
||||
TEST_CASE(sizeof5);
|
||||
TEST_CASE(sizeof6);
|
||||
TEST_CASE(sizeof7);
|
||||
|
@ -1028,38 +1026,37 @@ private:
|
|||
|
||||
|
||||
|
||||
void sizeof2() {
|
||||
const char code[] = "void foo()\n"
|
||||
void sizeof_array() {
|
||||
const char *code;
|
||||
|
||||
code = "void foo()\n"
|
||||
"{\n"
|
||||
" int i[4];\n"
|
||||
" sizeof(i);\n"
|
||||
" sizeof(*i);\n"
|
||||
"}\n";
|
||||
ASSERT_EQUALS("void foo ( ) { int i [ 4 ] ; 16 ; 4 ; }", tok(code));
|
||||
}
|
||||
|
||||
void sizeof3() {
|
||||
const char code[] = "static int i[4];\n"
|
||||
code = "static int i[4];\n"
|
||||
"void f()\n"
|
||||
"{\n"
|
||||
" int i[10];\n"
|
||||
" sizeof(i);\n"
|
||||
"}\n";
|
||||
ASSERT_EQUALS("static int i [ 4 ] ; void f ( ) { int i [ 10 ] ; 40 ; }", tok(code));
|
||||
}
|
||||
|
||||
void sizeof4() {
|
||||
{
|
||||
const char code[] = "int i[10];\n"
|
||||
code = "int i[10];\n"
|
||||
"sizeof(i[0]);\n";
|
||||
ASSERT_EQUALS("int i [ 10 ] ; 4 ;", tok(code));
|
||||
}
|
||||
|
||||
{
|
||||
const char code[] = "int i[10];\n"
|
||||
code = "int i[10];\n"
|
||||
"sizeof i[0];\n";
|
||||
ASSERT_EQUALS("int i [ 10 ] ; 4 ;", tok(code));
|
||||
}
|
||||
|
||||
code = "char i[2][20];\n"
|
||||
"sizeof(i[1]);";
|
||||
ASSERT_EQUALS("char i [ 2 ] [ 20 ] ; 20 ;", tok(code));
|
||||
}
|
||||
|
||||
void sizeof5() {
|
||||
|
|
Loading…
Reference in New Issue