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

@ -685,13 +685,15 @@ void Tokenizer::simplifyTokenList()
int sz = 100;
unsigned int varid = tok->tokAt((tok->tokAt(2)->str() == "*") ? 3 : 2)->varId();
if (varid != 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;
ostr << sz;

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)