Tokenizer: sizeof handling of 'sizeof(var[0])'

This commit is contained in:
Daniel Marjamäki 2009-02-02 18:59:32 +00:00
parent d0af67a1b1
commit 0059ceefb9
2 changed files with 28 additions and 5 deletions

View File

@ -468,9 +468,9 @@ void Tokenizer::setVarId()
unsigned int _varId = 0;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if ( tok != _tokens && !Token::Match(tok, "[;{}(]") )
if (tok != _tokens && !Token::Match(tok, "[;{}(]"))
continue;
if (!(firstMatch = Token::Match(tok->next(), "%type% *| %var%"))
&& !Token::Match(tok->next(), "%type% %type% *| %var%"))
continue;
@ -676,13 +676,15 @@ void Tokenizer::simplifyTokenList()
}
}
else if (Token::Match(tok, "sizeof ( * %var% )"))
else if (Token::Match(tok, "sizeof ( * %var% )") || Token::Match(tok, "sizeof ( %var% [ %num% ] )"))
{
// Some default value..
int sz = 100;
unsigned int varid = tok->tokAt((tok->tokAt(2)->str() == "*") ? 3 : 2)->varId();
// Try to locate variable declaration..
const Token *decltok = Token::findmatch(_tokens, "%type% %varid% [", tok->tokAt(3)->varId());
const Token *decltok = Token::findmatch(_tokens, "%type% %varid% [", varid);
if (decltok)
{
sz = SizeOfType(decltok->strAt(0));
@ -691,8 +693,9 @@ void Tokenizer::simplifyTokenList()
std::ostringstream ostr;
ostr << sz;
tok->str(ostr.str().c_str());
for (int i = 0; i < 4; ++i)
while (tok->next()->str() != ")")
tok->deleteNext();
tok->deleteNext();
}
}

View File

@ -80,6 +80,7 @@ private:
TEST_CASE(sizeof1);
TEST_CASE(sizeof2);
TEST_CASE(sizeof3);
}
@ -874,6 +875,25 @@ private:
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" static int i [ 4 ] ; void f ( ) { int i [ 10 ] ; 40 ; }"), ostr.str());
}
void sizeof3()
{
const char code[] = ";int i[10];\n"
"sizeof(i[0]);\n";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" ; int i [ 10 ] ; 4 ;"), ostr.str());
}
};
REGISTER_TEST(TestTokenizer)