Fixed varid is 0 bug which happened with sizeof(var[0]) and added testcase for it

This commit is contained in:
Reijo Tomperi 2009-02-03 21:42:50 +00:00
parent e052173308
commit 142a21973a
2 changed files with 30 additions and 6 deletions

View File

@ -471,7 +471,7 @@ void Tokenizer::setVarId()
if (tok != _tokens && !Token::Match(tok, "[;{}(]"))
continue;
if ( Token::Match(tok, "[;{}(] %any%") )
if (Token::Match(tok, "[;{}(] %any%"))
tok = tok->next();
if (!(firstMatch = Token::Match(tok, "%type% *| %var%"))
@ -685,12 +685,14 @@ void Tokenizer::simplifyTokenList()
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% [", varid);
if (decltok)
if (varid != 0)
{
sz = SizeOfType(decltok->strAt(0));
// Try to locate variable declaration..
const Token *decltok = Token::findmatch(_tokens, "%type% %varid% [", varid);
if (decltok)
{
sz = SizeOfType(decltok->strAt(0));
}
}
std::ostringstream ostr;

View File

@ -81,6 +81,7 @@ private:
TEST_CASE(sizeof1);
TEST_CASE(sizeof2);
TEST_CASE(sizeof3);
TEST_CASE(sizeof4);
}
@ -894,6 +895,27 @@ private:
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" int i [ 10 ] ; 4 ;"), ostr.str());
}
void sizeof4()
{
const char code[] =
"for (int i = 0; i < sizeof(g_ReservedNames[0]); i++)"
"{}";
// 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(" for ( int i = 0 ; i < 100 ; i + + ) { }"), ostr.str());
}
};
REGISTER_TEST(TestTokenizer)