Fixed #6639: Calculate sizeof() of multidimensional arrays

This commit is contained in:
PKEuS 2015-04-13 19:32:28 +02:00
parent 19bba94282
commit 4c40664861
2 changed files with 14 additions and 5 deletions

View File

@ -3018,13 +3018,21 @@ bool Tokenizer::simplifySizeof()
else if (Token::Match(tok->previous(), "%type% %name% [ %num% ] [[;=]") || else if (Token::Match(tok->previous(), "%type% %name% [ %num% ] [[;=]") ||
Token::Match(tok->tokAt(-2), "%type% * %name% [ %num% ] [[;=]")) { Token::Match(tok->tokAt(-2), "%type% * %name% [ %num% ] [[;=]")) {
const unsigned int size = sizeOfType(tok->previous()); unsigned int size = sizeOfType(tok->previous());
if (size == 0) if (size == 0)
continue; continue;
sizeOfVar[varId] = size * static_cast<unsigned int>(MathLib::toLongNumber(tok->strAt(2))); Token* tok2 = tok->next();
while (Token::Match(tok2, "[ %num% ]")) {
size *= static_cast<unsigned int>(MathLib::toLongNumber(tok2->strAt(1)));
tok2 = tok2->tokAt(3);
}
if (Token::Match(tok2, "[;=]")) {
sizeOfVar[varId] = size;
declTokOfVar[varId] = tok; declTokOfVar[varId] = tok;
} }
tok = tok2;
}
else if (Token::Match(tok->previous(), "%type% %name% [ %num% ] [,)]") || else if (Token::Match(tok->previous(), "%type% %name% [ %num% ] [,)]") ||
Token::Match(tok->tokAt(-2), "%type% * %name% [ %num% ] [,)]")) { Token::Match(tok->tokAt(-2), "%type% * %name% [ %num% ] [,)]")) {

View File

@ -893,8 +893,9 @@ private:
} }
code = "char i[2][20];\n" code = "char i[2][20];\n"
"sizeof(i[1]);"; "sizeof(i[1]);\n"
ASSERT_EQUALS("char i [ 2 ] [ 20 ] ; 20 ;", tok(code)); "sizeof(i);";
ASSERT_EQUALS("char i [ 2 ] [ 20 ] ; 20 ; 40 ;", tok(code));
} }
void sizeof5() { void sizeof5() {