diff --git a/lib/token.cpp b/lib/token.cpp index 8e7e5ca2e..db386e4d2 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -33,6 +33,7 @@ Token::Token(Token **t) : _next(0), _previous(0), _link(0), + _scope(0), _str(""), _varId(0), _fileIndex(0), @@ -193,6 +194,7 @@ void Token::deleteThis() _fileIndex = _next->_fileIndex; _linenr = _next->_linenr; _link = _next->_link; + _scope = _next->_scope; if (_link) _link->link(this); @@ -211,6 +213,7 @@ void Token::deleteThis() _fileIndex = _previous->_fileIndex; _linenr = _previous->_linenr; _link = _previous->_link; + _scope = _previous->_scope; if (_link) _link->link(this); diff --git a/lib/token.h b/lib/token.h index 36861d7bc..c1fb6c8d0 100644 --- a/lib/token.h +++ b/lib/token.h @@ -24,6 +24,8 @@ #include #include "config.h" +class Scope; + /// @addtogroup Core /// @{ @@ -383,6 +385,21 @@ public: return _link; } + /** + * Associate this token with given scope + * @param scope Scope to be associated + */ + void scope(Scope* scope) { + _scope = scope; + } + + /** + * Returns a pointer to the scope containing this token. + */ + Scope* scope() const { + return _scope; + } + /** * Links two elements against each other. **/ @@ -473,6 +490,9 @@ private: Token *_next; Token *_previous; Token *_link; + + Scope* _scope; + std::string _str; unsigned int _varId; unsigned int _fileIndex; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 28b55184d..9d2c116c8 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3233,8 +3233,7 @@ void Tokenizer::simplifySizeof() bool Tokenizer::simplifyTokenList() { // clear the _functionList so it can't contain dead pointers - delete _symbolDatabase; - _symbolDatabase = NULL; + deleteSymbolDatabase(); // simplify references simplifyReference(); @@ -8874,12 +8873,59 @@ void Tokenizer::simplifyQtSignalsSlots() const SymbolDatabase *Tokenizer::getSymbolDatabase() const { - if (!_symbolDatabase) + if (!_symbolDatabase) { _symbolDatabase = new SymbolDatabase(this, _settings, _errorLogger); + // Set scope pointers + for (std::list::iterator scope = _symbolDatabase->scopeList.begin(); scope != _symbolDatabase->scopeList.end(); ++scope) { + Token* start = const_cast(scope->classStart); + Token* end = const_cast(scope->classEnd); + if (scope->type == Scope::eGlobal) { + start = const_cast(list.front()); + end = const_cast(list.back()); + } + if (start && end) { + start->scope(&*scope); + end->scope(&*scope); + } + if (start != end && start->next() != end) { + for (Token* tok = start->next(); tok != end; tok = tok->next()) { + if (tok->str() == "{") { + bool break2 = false; + for (std::list::const_iterator innerScope = scope->nestedList.begin(); innerScope != scope->nestedList.end(); ++innerScope) { + if (tok == (*innerScope)->classStart) { // Is begin of inner scope + tok = tok->link(); + if (!tok || tok->next() == end || !tok->next()) { + break2 = true; + break; + } + tok = tok->next(); + break; + } + } + if (break2) + break; + } + tok->scope(&*scope); + } + } + } + } + return _symbolDatabase; } +void Tokenizer::deleteSymbolDatabase() +{ + // Clear scope pointers + for (Token* tok = list.front(); tok != list.back(); tok = tok->next()) { + tok->scope(0); + } + + delete _symbolDatabase; + _symbolDatabase = 0; +} + void Tokenizer::simplifyOperatorName() { for (Token *tok = list.front(); tok; tok = tok->next()) { diff --git a/lib/tokenize.h b/lib/tokenize.h index 732381c6f..517592335 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -695,6 +695,7 @@ public: } const SymbolDatabase *getSymbolDatabase() const; + void deleteSymbolDatabase(); Token *deleteInvalidTypedef(Token *typeDef);