Fixed #6639: Calculate sizeof() of multidimensional arrays
This commit is contained in:
parent
19bba94282
commit
4c40664861
|
@ -3018,12 +3018,20 @@ bool Tokenizer::simplifySizeof()
|
|||
|
||||
else if (Token::Match(tok->previous(), "%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)
|
||||
continue;
|
||||
|
||||
sizeOfVar[varId] = size * static_cast<unsigned int>(MathLib::toLongNumber(tok->strAt(2)));
|
||||
declTokOfVar[varId] = tok;
|
||||
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;
|
||||
}
|
||||
tok = tok2;
|
||||
}
|
||||
|
||||
else if (Token::Match(tok->previous(), "%type% %name% [ %num% ] [,)]") ||
|
||||
|
|
|
@ -893,8 +893,9 @@ private:
|
|||
}
|
||||
|
||||
code = "char i[2][20];\n"
|
||||
"sizeof(i[1]);";
|
||||
ASSERT_EQUALS("char i [ 2 ] [ 20 ] ; 20 ;", tok(code));
|
||||
"sizeof(i[1]);\n"
|
||||
"sizeof(i);";
|
||||
ASSERT_EQUALS("char i [ 2 ] [ 20 ] ; 20 ; 40 ;", tok(code));
|
||||
}
|
||||
|
||||
void sizeof5() {
|
||||
|
|
Loading…
Reference in New Issue