Refactoring: move code for sizeof() simplification to method.

Introduce and use Tokenizer::simplifySizeof() method.

No functional change.
This commit is contained in:
Slava Semushin 2009-09-06 18:22:01 +07:00
parent 587e96322d
commit 572c206755
2 changed files with 113 additions and 105 deletions

View File

@ -1249,112 +1249,8 @@ bool Tokenizer::createLinks()
return true; return true;
} }
void Tokenizer::simplifyTokenList() void Tokenizer::simplifySizeof()
{ {
simplifyNamespaces();
simplifyGoto();
// Combine wide strings
for (Token *tok = _tokens; tok; tok = tok->next())
{
while (tok->str() == "L" && tok->next() && tok->next()->str()[0] == '"')
{
// Combine 'L "string"'
tok->str(tok->next()->str());
tok->deleteNext();
}
}
// Combine strings
for (Token *tok = _tokens; tok; tok = tok->next())
{
while (tok->str()[0] == '"' && tok->next() && tok->next()->str()[0] == '"')
{
// Two strings after each other, combine them
tok->concatStr(tok->next()->str());
tok->deleteNext();
}
}
// Remove unwanted keywords
static const char * const unwantedWords[] = { "unsigned", "unlikely", "likely" };
for (Token *tok = _tokens; tok; tok = tok->next())
{
for (unsigned ui = 0; ui < sizeof(unwantedWords) / sizeof(unwantedWords[0]) && tok->next(); ui++)
{
if (tok->next()->str() == unwantedWords[ui])
{
tok->deleteNext();
break;
}
}
if (Token::simpleMatch(tok->next(), "__builtin_expect ("))
{
unsigned int parlevel = 0;
for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
{
if (tok2->str() == "(")
++parlevel;
else if (tok2->str() == ")")
{
if (parlevel <= 1)
break;
--parlevel;
}
if (parlevel == 1 && tok2->str() == ",")
{
if (Token::Match(tok2, ", %num% )"))
{
tok->deleteNext();
Token::eraseTokens(tok2->previous(), tok2->tokAt(2));
}
break;
}
}
}
}
// Convert + + into + and + - into -
for (Token *tok = _tokens; tok; tok = tok->next())
{
while (tok->next())
{
if (tok->str() == "+")
{
if (tok->next()->str() == "+")
{
tok->deleteNext();
continue;
}
else if (tok->next()->str() == "-")
{
tok->str("-");
tok->deleteNext();
continue;
}
}
else if (tok->str() == "-")
{
if (tok->next()->str() == "-")
{
tok->str("+");
tok->deleteNext();
continue;
}
else if (tok->next()->str() == "+")
{
tok->deleteNext();
continue;
}
}
break;
}
}
// Fill the map _typeSize.. // Fill the map _typeSize..
_typeSize.clear(); _typeSize.clear();
_typeSize["char"] = sizeof(char); _typeSize["char"] = sizeof(char);
@ -1553,6 +1449,113 @@ void Tokenizer::simplifyTokenList()
} }
} }
} }
}
void Tokenizer::simplifyTokenList()
{
simplifyNamespaces();
simplifyGoto();
// Combine wide strings
for (Token *tok = _tokens; tok; tok = tok->next())
{
while (tok->str() == "L" && tok->next() && tok->next()->str()[0] == '"')
{
// Combine 'L "string"'
tok->str(tok->next()->str());
tok->deleteNext();
}
}
// Combine strings
for (Token *tok = _tokens; tok; tok = tok->next())
{
while (tok->str()[0] == '"' && tok->next() && tok->next()->str()[0] == '"')
{
// Two strings after each other, combine them
tok->concatStr(tok->next()->str());
tok->deleteNext();
}
}
// Remove unwanted keywords
static const char * const unwantedWords[] = { "unsigned", "unlikely", "likely" };
for (Token *tok = _tokens; tok; tok = tok->next())
{
for (unsigned ui = 0; ui < sizeof(unwantedWords) / sizeof(unwantedWords[0]) && tok->next(); ui++)
{
if (tok->next()->str() == unwantedWords[ui])
{
tok->deleteNext();
break;
}
}
if (Token::simpleMatch(tok->next(), "__builtin_expect ("))
{
unsigned int parlevel = 0;
for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
{
if (tok2->str() == "(")
++parlevel;
else if (tok2->str() == ")")
{
if (parlevel <= 1)
break;
--parlevel;
}
if (parlevel == 1 && tok2->str() == ",")
{
if (Token::Match(tok2, ", %num% )"))
{
tok->deleteNext();
Token::eraseTokens(tok2->previous(), tok2->tokAt(2));
}
break;
}
}
}
}
// Convert + + into + and + - into -
for (Token *tok = _tokens; tok; tok = tok->next())
{
while (tok->next())
{
if (tok->str() == "+")
{
if (tok->next()->str() == "+")
{
tok->deleteNext();
continue;
}
else if (tok->next()->str() == "-")
{
tok->str("-");
tok->deleteNext();
continue;
}
}
else if (tok->str() == "-")
{
if (tok->next()->str() == "-")
{
tok->str("+");
tok->deleteNext();
continue;
}
else if (tok->next()->str() == "+")
{
tok->deleteNext();
continue;
}
}
break;
}
}
simplifySizeof();
// Replace constants.. // Replace constants..
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())

View File

@ -116,6 +116,11 @@ public:
private: private:
#endif #endif
/**
* Replace sizeof() to appropriate size.
*/
void simplifySizeof();
/** /**
* Simplify variable declarations * Simplify variable declarations
*/ */