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% ] [[;=]") ||
|
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();
|
||||||
declTokOfVar[varId] = tok;
|
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% ] [,)]") ||
|
else if (Token::Match(tok->previous(), "%type% %name% [ %num% ] [,)]") ||
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
Loading…
Reference in New Issue