Fixed varid is 0 bug which happened with sizeof(var[0]) and added testcase for it
This commit is contained in:
parent
e052173308
commit
142a21973a
|
@ -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,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;
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue