Improve creation of link() for Token class. Tokenizer::simplifyTokenList() should now return

code where Token::link() actually works.
This commit is contained in:
Reijo Tomperi 2009-03-15 01:39:45 +02:00
parent 6eadbaae04
commit c385b3e045
3 changed files with 27 additions and 5 deletions

View File

@ -1491,7 +1491,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const char clas
// Allocate.. // Allocate..
if (indent == 0 || Token::Match(tok, (std::string(varname) + " =").c_str())) if (indent == 0 || Token::Match(tok, (std::string(varname) + " =").c_str()))
{ {
AllocType alloc = GetAllocationType(tok->tokAt((indent>0) ? 2 : 3)); AllocType alloc = GetAllocationType(tok->tokAt((indent > 0) ? 2 : 3));
if (alloc != No) if (alloc != No)
{ {
if (Alloc != No && Alloc != alloc) if (Alloc != No && Alloc != alloc)

View File

@ -521,7 +521,7 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[])
// New type.. // New type..
std::vector<std::string> types2; std::vector<std::string> types2;
s = ""; s = "";
for (const Token *tok3 = tok2->tokAt(2); tok3->str()!=">"; tok3 = tok3->next()) for (const Token *tok3 = tok2->tokAt(2); tok3->str() != ">"; tok3 = tok3->next())
{ {
if (tok3->str() != ",") if (tok3->str() != ",")
types2.push_back(tok3->str()); types2.push_back(tok3->str());
@ -574,7 +574,7 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[])
s = name + " < " + type2 + " >"; s = name + " < " + type2 + " >";
for (std::string::size_type pos = s.find(","); pos != std::string::npos; pos = s.find(",", pos + 2)) for (std::string::size_type pos = s.find(","); pos != std::string::npos; pos = s.find(",", pos + 2))
{ {
s.insert(pos+1, " "); s.insert(pos + 1, " ");
s.insert(pos, " "); s.insert(pos, " ");
} }
for (Token *tok4 = tok2; tok4; tok4 = tok4->next()) for (Token *tok4 = tok2; tok4; tok4 = tok4->next())
@ -725,11 +725,16 @@ void Tokenizer::simplifyNamespaces()
} }
} }
void Tokenizer::simplifyTokenList() bool Tokenizer::createLinks()
{ {
std::list<Token*> links; std::list<Token*> links;
for (Token *token = _tokens; token; token = token->next()) for (Token *token = _tokens; token; token = token->next())
{ {
if (token->link())
{
token->link(0);
}
if (token->str() == "{") if (token->str() == "{")
{ {
links.push_back(token); links.push_back(token);
@ -739,7 +744,7 @@ void Tokenizer::simplifyTokenList()
if (links.size() == 0) if (links.size() == 0)
{ {
// Error, { and } don't match. // Error, { and } don't match.
break; return false;
} }
token->link(links.back()); token->link(links.back());
@ -751,8 +756,15 @@ void Tokenizer::simplifyTokenList()
if (links.size() > 0) if (links.size() > 0)
{ {
// Error, { and } don't match. // Error, { and } don't match.
return false;
} }
return true;
}
void Tokenizer::simplifyTokenList()
{
createLinks();
simplifyNamespaces(); simplifyNamespaces();
@ -1240,6 +1252,8 @@ void Tokenizer::simplifyTokenList()
modified |= simplifyRedundantParanthesis(); modified |= simplifyRedundantParanthesis();
modified |= simplifyCalculations(); modified |= simplifyCalculations();
} }
createLinks();
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -176,6 +176,14 @@ private:
void InsertTokens(Token *dest, Token *src, unsigned int n); void InsertTokens(Token *dest, Token *src, unsigned int n);
/**
* Setup links for tokens so that one can call Token::link().
*
* @return false if there was a mismatch with tokens, this
* should mean that source code was not valid.
*/
bool createLinks();
Token *_tokensBack; Token *_tokensBack;
std::map<std::string, unsigned int> _typeSize; std::map<std::string, unsigned int> _typeSize;
std::vector<std::string> _files; std::vector<std::string> _files;