diff --git a/src/tokenize.cpp b/src/tokenize.cpp index be49e0b5f..490cb55e3 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -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; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index e2e43edfb..89422beb3 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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)