Skip to content

Commit aa8c17a

Browse files
committed
Tokenizer: remove stuff from simplifyTokenList2
1 parent 3612ddb commit aa8c17a

3 files changed

Lines changed: 3 additions & 437 deletions

File tree

lib/tokenize.cpp

Lines changed: 0 additions & 225 deletions
Original file line numberDiff line numberDiff line change
@@ -5380,13 +5380,6 @@ bool Tokenizer::simplifyTokenList2()
53805380

53815381
simplifyIfAndWhileAssign();
53825382

5383-
// replace strlen(str)
5384-
for (Token *tok = list.front(); tok; tok = tok->next()) {
5385-
if (Token::Match(tok, "strlen ( %str% )")) {
5386-
tok->str(MathLib::toString(Token::getStrLength(tok->tokAt(2))));
5387-
tok->deleteNext(3);
5388-
}
5389-
}
53905383

53915384
bool modified = true;
53925385
while (modified) {
@@ -5397,7 +5390,6 @@ bool Tokenizer::simplifyTokenList2()
53975390
modified |= simplifyConditions();
53985391
modified |= simplifyFunctionReturn();
53995392
modified |= simplifyKnownVariables();
5400-
modified |= simplifyStrlen();
54015393

54025394
modified |= removeRedundantConditions();
54035395
modified |= simplifyRedundantParentheses();
@@ -5406,25 +5398,6 @@ bool Tokenizer::simplifyTokenList2()
54065398
validate();
54075399
}
54085400

5409-
// simplify redundant loops
5410-
simplifyWhile0();
5411-
removeRedundantFor();
5412-
5413-
// Remove redundant parentheses in return..
5414-
for (Token *tok = list.front(); tok; tok = tok->next()) {
5415-
while (Token::simpleMatch(tok, "return (")) {
5416-
Token *tok2 = tok->next()->link();
5417-
if (Token::simpleMatch(tok2, ") ;")) {
5418-
tok->deleteNext();
5419-
tok2->deleteThis();
5420-
} else {
5421-
break;
5422-
}
5423-
}
5424-
}
5425-
5426-
simplifyReturnStrncat();
5427-
54285401
simplifyComma();
54295402

54305403
removeRedundantSemicolons();
@@ -6195,90 +6168,6 @@ bool Tokenizer::removeRedundantConditions()
61956168
return ret;
61966169
}
61976170

6198-
void Tokenizer::removeRedundantFor()
6199-
{
6200-
for (Token *tok = list.front(); tok; tok = tok->next()) {
6201-
if (Token::Match(tok, "[;{}] for ( %name% = %num% ; %name% < %num% ; ++| %name% ++| ) {") ||
6202-
Token::Match(tok, "[;{}] for ( %type% %name% = %num% ; %name% < %num% ; ++| %name% ++| ) {")) {
6203-
// Same variable name..
6204-
const Token* varTok = tok->tokAt(3);
6205-
const bool type = varTok->next()->isName();
6206-
if (type)
6207-
varTok = varTok->next();
6208-
const std::string varname(varTok->str());
6209-
const int varid(varTok->varId());
6210-
if (varname != varTok->strAt(4))
6211-
continue;
6212-
const Token *vartok2 = tok->linkAt(2)->previous();
6213-
if (vartok2->str() == "++")
6214-
vartok2 = vartok2->previous();
6215-
else if (vartok2->strAt(-1) != "++")
6216-
continue;
6217-
if (varname != vartok2->str())
6218-
continue;
6219-
6220-
// Check that the difference of the numeric values is 1
6221-
const MathLib::bigint num1(MathLib::toLongNumber(varTok->strAt(2)));
6222-
const MathLib::bigint num2(MathLib::toLongNumber(varTok->strAt(6)));
6223-
if (num1 + 1 != num2)
6224-
continue;
6225-
6226-
// check how loop variable is used in loop..
6227-
bool read = false;
6228-
bool write = false;
6229-
const Token* end = tok->linkAt(2)->next()->link();
6230-
for (const Token *tok2 = tok->linkAt(2); tok2 != end; tok2 = tok2->next()) {
6231-
if (tok2->str() == varname) {
6232-
if (tok2->previous()->isArithmeticalOp() &&
6233-
tok2->next() &&
6234-
(tok2->next()->isArithmeticalOp() || tok2->next()->str() == ";")) {
6235-
read = true;
6236-
} else {
6237-
read = write = true;
6238-
break;
6239-
}
6240-
}
6241-
}
6242-
6243-
// Simplify loop if loop variable isn't written
6244-
if (!write) {
6245-
Token* bodyBegin = tok->linkAt(2)->next();
6246-
// remove "for ("
6247-
tok->deleteNext(2);
6248-
6249-
// If loop variable is read then keep assignment before
6250-
// loop body..
6251-
if (type) {
6252-
tok->insertToken("{");
6253-
Token::createMutualLinks(tok->next(), bodyBegin->link());
6254-
bodyBegin->deleteThis();
6255-
tok = tok->tokAt(6);
6256-
} else if (read) {
6257-
// goto ";"
6258-
tok = tok->tokAt(4);
6259-
} else {
6260-
// remove "x = 0 ;"
6261-
tok->deleteNext(4);
6262-
}
6263-
6264-
// remove "x < 1 ; x ++ )"
6265-
tok->deleteNext(7);
6266-
6267-
if (!type) {
6268-
// Add assignment after the loop body so the loop variable
6269-
// get the correct end value
6270-
Token *tok2 = tok->next()->link();
6271-
tok2->insertToken(";");
6272-
tok2->insertToken(MathLib::toString(num2));
6273-
tok2->insertToken("=");
6274-
tok2->insertToken(varname);
6275-
tok2->next()->varId(varid);
6276-
}
6277-
}
6278-
}
6279-
}
6280-
}
6281-
62826171

62836172
void Tokenizer::removeRedundantSemicolons()
62846173
{
@@ -10163,59 +10052,6 @@ std::string Tokenizer::simplifyString(const std::string &source)
1016310052
return str;
1016410053
}
1016510054

10166-
void Tokenizer::simplifyWhile0()
10167-
{
10168-
for (Token *tok = list.front(); tok; tok = tok->next()) {
10169-
// while (0)
10170-
const bool while0(Token::Match(tok->previous(), "[{};] while ( 0|false )"));
10171-
10172-
// for (0) - not banal, ticket #3140
10173-
const bool for0((Token::Match(tok->previous(), "[{};] for ( %name% = %num% ; %name% < %num% ;") &&
10174-
tok->strAt(2) == tok->strAt(6) && tok->strAt(4) == tok->strAt(8)) ||
10175-
(Token::Match(tok->previous(), "[{};] for ( %type% %name% = %num% ; %name% < %num% ;") &&
10176-
tok->strAt(3) == tok->strAt(7) && tok->strAt(5) == tok->strAt(9)));
10177-
10178-
if (!while0 && !for0)
10179-
continue;
10180-
10181-
if (while0 && tok->previous()->str() == "}") {
10182-
// find "do"
10183-
Token *tok2 = tok->previous()->link();
10184-
tok2 = tok2->previous();
10185-
if (tok2 && tok2->str() == "do") {
10186-
const bool flowmatch = Token::findmatch(tok2, "continue|break", tok) != nullptr;
10187-
// delete "do ({)"
10188-
tok2->deleteThis();
10189-
if (!flowmatch)
10190-
tok2->deleteThis();
10191-
10192-
// delete "(}) while ( 0 ) (;)"
10193-
tok = tok->previous();
10194-
tok->deleteNext(4); // while ( 0 )
10195-
if (tok->next() && tok->next()->str() == ";")
10196-
tok->deleteNext(); // ;
10197-
if (!flowmatch)
10198-
tok->deleteThis(); // }
10199-
10200-
continue;
10201-
}
10202-
}
10203-
10204-
// remove "while (0) { .. }"
10205-
if (Token::simpleMatch(tok->next()->link(), ") {")) {
10206-
Token *end = tok->next()->link(), *old_prev = tok->previous();
10207-
end = end->next()->link();
10208-
if (Token::Match(tok, "for ( %name% ="))
10209-
old_prev = end->link();
10210-
eraseDeadCode(old_prev, end->next());
10211-
if (old_prev && old_prev->next())
10212-
tok = old_prev->next();
10213-
else
10214-
break;
10215-
}
10216-
}
10217-
}
10218-
1021910055
void Tokenizer::simplifyFunctionTryCatch()
1022010056
{
1022110057
if (!isCPP())
@@ -11821,53 +11657,6 @@ void Tokenizer::removeUnnecessaryQualification()
1182111657
}
1182211658
}
1182311659

11824-
void Tokenizer::simplifyReturnStrncat()
11825-
{
11826-
for (Token *tok = list.front(); tok; tok = tok->next()) {
11827-
if (Token::simpleMatch(tok, "return strncat (") &&
11828-
Token::simpleMatch(tok->linkAt(2), ") ;") &&
11829-
tok->strAt(3) != ")" && tok->strAt(3) != ",") {
11830-
11831-
//first argument
11832-
Token *tok2 = tok->tokAt(3);
11833-
11834-
//check if there are at least three arguments
11835-
for (int i = 0; i < 2; ++i) {
11836-
tok2 = tok2->nextArgument();
11837-
if (!tok2) {
11838-
tok = tok->linkAt(2)->next();
11839-
break;
11840-
}
11841-
}
11842-
if (!tok2)
11843-
continue;
11844-
11845-
tok2 = tok2->nextArgument();
11846-
//we want only three arguments
11847-
if (tok2) {
11848-
tok = tok->linkAt(2)->next();
11849-
continue;
11850-
}
11851-
11852-
// Remove 'return'
11853-
tok->deleteThis();
11854-
11855-
// Add 'return arg1 ;' after 'strncat(arg1, arg2, arg3);'
11856-
tok = tok->next();
11857-
11858-
tok2 = tok->link()->next();
11859-
tok2->insertToken(";");
11860-
11861-
//the last token of the first argument before ','
11862-
const Token * const end = tok->next()->nextArgument()->tokAt(-2);
11863-
11864-
//all the first argument is copied
11865-
TokenList::copyTokens(tok2, tok->next(), end);
11866-
tok2->insertToken("return");
11867-
}
11868-
}
11869-
}
11870-
1187111660
void Tokenizer::printUnknownTypes() const
1187211661
{
1187311662
if (!mSymbolDatabase)
@@ -12036,20 +11825,6 @@ void Tokenizer::simplifyMathExpressions()
1203611825
}
1203711826
}
1203811827

12039-
bool Tokenizer::simplifyStrlen()
12040-
{
12041-
// replace strlen(str)
12042-
bool modified=false;
12043-
for (Token *tok = list.front(); tok; tok = tok->next()) {
12044-
if (Token::Match(tok, "strlen ( %str% )")) {
12045-
tok->str(MathLib::toString(Token::getStrLength(tok->tokAt(2))));
12046-
tok->deleteNext(3);
12047-
modified=true;
12048-
}
12049-
}
12050-
return modified;
12051-
}
12052-
1205311828
void Tokenizer::prepareTernaryOpForAST()
1205411829
{
1205511830
// http://en.cppreference.com/w/cpp/language/operator_precedence says about ternary operator:

lib/tokenize.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,6 @@ class CPPCHECKLIB Tokenizer {
410410
*/
411411
bool removeRedundantConditions();
412412

413-
/**
414-
* Remove redundant for:
415-
* "for (x=0;x<1;x++) { }" => "{ x = 1; }"
416-
*/
417-
void removeRedundantFor();
418-
419-
420413
/**
421414
* Reduces "; ;" to ";", except in "( ; ; )"
422415
*/
@@ -531,11 +524,6 @@ class CPPCHECKLIB Tokenizer {
531524

532525
private:
533526

534-
/**
535-
* simplify "while (0)"
536-
*/
537-
void simplifyWhile0();
538-
539527
/**
540528
* Simplify while(func(f))
541529
*/
@@ -762,12 +750,6 @@ class CPPCHECKLIB Tokenizer {
762750
std::map<nonneg int, std::map<std::string, nonneg int>>& structMembers,
763751
nonneg int *varId_);
764752

765-
/**
766-
* Simplify e.g. 'return(strncat(temp,"a",1));' into
767-
* strncat(temp,"a",1); return temp;
768-
*/
769-
void simplifyReturnStrncat();
770-
771753
/**
772754
* Output list of unknown types.
773755
*/

0 commit comments

Comments
 (0)