diff --git a/testtokenize.cpp b/testtokenize.cpp index c10c6add3..85bb6d146 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -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 ) diff --git a/tokenize.cpp b/tokenize.cpp index e3f67320f..4c2fe8d56 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -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 ); + } + } } }