Fixed ticket #169 (Add Token::link())

http://apps.sourceforge.net/trac/cppcheck/ticket/169
This commit is contained in:
Reijo Tomperi 2009-03-13 23:25:56 +02:00
parent 0a6a0ae46e
commit 116e940214
3 changed files with 59 additions and 18 deletions

View File

@ -34,6 +34,7 @@ Token::Token() :
_varId(0), _varId(0),
_next(0), _next(0),
_previous(0), _previous(0),
_link(0),
_fileIndex(0), _fileIndex(0),
_linenr(0) _linenr(0)
{ {
@ -77,6 +78,7 @@ void Token::deleteThis()
_varId = _next->_varId; _varId = _next->_varId;
_fileIndex = _next->_fileIndex; _fileIndex = _next->_fileIndex;
_linenr = _next->_linenr; _linenr = _next->_linenr;
_link = _next->_link;
deleteNext(); deleteNext();
} }
else if (_previous) else if (_previous)
@ -491,6 +493,16 @@ void Token::linenr(unsigned int linenr)
_linenr = linenr; _linenr = linenr;
} }
void Token::link(Token *link)
{
_link = link;
}
Token *Token::link() const
{
return _link;
}
void Token::printOut(const char *title) const void Token::printOut(const char *title) const
{ {
std::cout << stringifyList(true, title) << std::endl; std::cout << stringifyList(true, title) << std::endl;

View File

@ -195,6 +195,22 @@ public:
*/ */
void deleteThis(); void deleteThis();
/**
* Create link to given token
* @param link The token where this token should link
* to.
*/
void link(Token *link);
/**
* Return token where this token links to.
* Supported links are:
* "{" <-> "}"
*
* @return The token where this token links to.
*/
Token *link() const;
private: private:
void next(Token *next); void next(Token *next);
void previous(Token *previous); void previous(Token *previous);
@ -206,6 +222,7 @@ private:
unsigned int _varId; unsigned int _varId;
Token *_next; Token *_next;
Token *_previous; Token *_previous;
Token *_link;
unsigned int _fileIndex; unsigned int _fileIndex;
unsigned int _linenr; unsigned int _linenr;
}; };

View File

@ -646,27 +646,12 @@ void Tokenizer::simplifyNamespaces()
{ {
// Token is namespace and there is no "using" before it. // Token is namespace and there is no "using" before it.
Token *start = token; Token *start = token;
Token *tok = token->next(); Token *tok = token->tokAt(2);
if (!tok) if (!tok)
return; return;
int indent = 0; tok = tok->link();
for (tok = tok->next(); tok; tok = tok->next()) if (tok && tok->str() == "}")
{
if (tok->str() == "{")
indent++;
else if (tok->str() == "}")
{
indent--;
if (indent == 0)
break;
}
}
if (!tok)
return;
if (tok->str() == "}")
{ {
tok = tok->previous(); tok = tok->previous();
tok->deleteNext(); tok->deleteNext();
@ -695,6 +680,33 @@ void Tokenizer::simplifyNamespaces()
void Tokenizer::simplifyTokenList() void Tokenizer::simplifyTokenList()
{ {
std::list<Token*> links;
for (Token *token = _tokens; token; token = token->next())
{
if (token->str() == "{")
{
links.push_back(token);
}
else if (token->str() == "}")
{
if (links.size() == 0)
{
// Error, { and } don't match.
break;
}
token->link(links.back());
links.back()->link(token);
links.pop_back();
}
}
if (links.size() > 0)
{
// Error, { and } don't match.
}
simplifyNamespaces(); simplifyNamespaces();
// Combine strings // Combine strings