Skip to content

Commit

Permalink
Fixed #6639: Calculate sizeof() of multidimensional arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
PKEuS committed Apr 13, 2015
1 parent 19bba94 commit 4c40664
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/tokenize.cpp
Expand Up @@ -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% ] [,)]") ||
Expand Down
5 changes: 3 additions & 2 deletions test/testsimplifytokens.cpp
Expand Up @@ -893,8 +893,9 @@ class TestSimplifyTokens : public TestFixture {
}

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() {
Expand Down

0 comments on commit 4c40664

Please sign in to comment.