variable id : set variable ids for struct members

This commit is contained in:
Daniel Marjamäki 2008-12-18 18:56:25 +00:00
parent 744c4928c4
commit c56779c9ae
2 changed files with 44 additions and 0 deletions

View File

@ -57,6 +57,7 @@ private:
TEST_CASE( match2 );
TEST_CASE( varid1 );
TEST_CASE( varid2 );
}
@ -474,6 +475,32 @@ private:
ASSERT_EQUALS( 2, tok->varId() );
}
}
void varid2()
{
const std::string code("void f()\n"
"{\n"
" struct ABC abc;\n"
" abc.a = 3;\n"
" i = abc.a;\n"
"}\n" );
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next() )
{
if ( tok->str() == "abc" )
ASSERT_EQUALS( 1, tok->varId() );
else if ( tok->str() == "a" )
ASSERT_EQUALS( 2, tok->varId() );
else
ASSERT_EQUALS( 0, tok->varId() );
}
}
};
REGISTER_TEST( TestTokenizer )

View File

@ -659,6 +659,23 @@ void Tokenizer::setVarId()
break;
}
}
}
// Struct/Class members
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
{
if ( TOKEN::Match(tok, "%var% . %var%") &&
tok->varId() != 0 &&
tok->tokAt(2)->varId() == 0 )
{
++_varId;
const std::string pattern(std::string("%varid% . ") + tok->strAt(2));
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
{
if ( TOKEN::Match(tok2, pattern.c_str(), 0, 0, tok->varId()) )
tok2->next()->next()->varId( _varId );
}
}
}
}