diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index a59f82e59..1540f8c7e 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1551,7 +1551,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) --innerIndentlevel; } while (innerIndentlevel == 0 && Token::Match(tok3, "[{};] if|ifv|else { continue ; }")) { - Token::eraseTokens(tok3, 5); + tok3->deleteNext(5); if (Token::simpleMatch(tok3->next(), "else")) tok3->deleteNext(); } @@ -1559,8 +1559,8 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) if (Token::simpleMatch(tok2, "while1 { if { dealloc ; return ; } }")) { tok2->str(";"); - Token::eraseTokens(tok2, 3); - Token::eraseTokens(tok2->tokAt(4), 2); + tok2->deleteNext(3); + tok2->tokAt(4)->deleteNext(2); } } } @@ -1575,7 +1575,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Replace "{ }" with ";" if (Token::simpleMatch(tok2->next(), "{ }")) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); tok2->insertToken(";"); done = false; } @@ -1600,7 +1600,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // outer/inner if blocks. Remove outer condition.. else if (Token::Match(tok2->next(), "if|if(var) { if return use ; }")) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); tok2->tokAt(4)->deleteNext(); done = false; } @@ -1615,19 +1615,19 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Reduce "if X ; else X ;" => "X ;" else if (Token::Match(tok2->next(), "if %var% ; else %var% ;") && tok2->strAt(2) == tok2->strAt(5)) { - Token::eraseTokens(tok2, 4); + tok2->deleteNext(4); done = false; } // Reduce "if continue ; if continue ;" => "if continue ;" else if (Token::simpleMatch(tok2->next(), "if continue ; if continue ;")) { - Token::eraseTokens(tok2, 3); + tok2->deleteNext(3); done = false; } // Reduce "if return ; alloc ;" => "alloc ;" else if (Token::Match(tok2, "[;{}] if return ; alloc|return ;")) { - Token::eraseTokens(tok2, 3); + tok2->deleteNext(3); done = false; } @@ -1636,31 +1636,31 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Remove "if" tok2->deleteNext(); // Remove "; else return" - Token::eraseTokens(tok2->next(), 3); + tok2->next()->deleteNext(3); done = false; } // Reduce "if ; else %var% ;" => "if %var% ;" else if (Token::Match(tok2->next(), "if ; else %var% ;")) { - Token::eraseTokens(tok2->next(), 2); + tok2->next()->deleteNext(2); done = false; } // Reduce "if ; else" => "if" else if (Token::simpleMatch(tok2->next(), "if ; else")) { - Token::eraseTokens(tok2->next(), 2); + tok2->next()->deleteNext(2); done = false; } // Reduce "if return ; else|if return|continue ;" => "if return ;" else if (Token::Match(tok2->next(), "if return ; else|if return|continue|break ;")) { - Token::eraseTokens(tok2->tokAt(3), 3); + tok2->tokAt(3)->deleteNext(3); done = false; } // Reduce "if continue|break ; else|if return ;" => "if return ;" else if (Token::Match(tok2->next(), "if continue|break ; if|else return ;")) { - Token::eraseTokens(tok2->next(), 3); + tok2->next()->deleteNext(3); done = false; } @@ -1673,7 +1673,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Delete "if { dealloc|assign|use ; return ; }" else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }") && !Token::findmatch(tok, "if {| alloc ;")) { - Token::eraseTokens(tok2, 7); + tok2->deleteNext(7); if (Token::simpleMatch(tok2->next(), "else")) tok2->deleteNext(); done = false; @@ -1682,7 +1682,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Remove "if { dealloc ; callfunc ; } !!else|return" else if (Token::Match(tok2->next(), "if { dealloc|assign ; callfunc ; }") && !Token::Match(tok2->tokAt(8), "else|return")) { - Token::eraseTokens(tok2, 7); + tok2->deleteNext(7); done = false; } @@ -1691,7 +1691,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Reduce "alloc while(!var) alloc ;" => "alloc ;" if (Token::Match(tok2, "[;{}] alloc ; while(!var) alloc ;")) { - Token::eraseTokens(tok2, 3); + tok2->deleteNext(3); done = false; } @@ -1722,7 +1722,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Reduce "if(!var) exit ;" => ";" if (Token::simpleMatch(tok2, "; if(!var) exit ;")) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); done = false; } @@ -1737,7 +1737,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) tok2->str("if(var)"); // remove the "; else" - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); } else { // remove the "if*" tok2->deleteNext(); @@ -1762,13 +1762,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Reduce "while1 if break ;" => ";" if (Token::simpleMatch(tok2, "while1 if break ;")) { tok2->str(";"); - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); done = false; } // Delete if block: "alloc; if return use ;" if (Token::Match(tok2, "alloc ; if return use ; !!else")) { - Token::eraseTokens(tok2, 4); + tok2->deleteNext(4); done = false; } @@ -1793,33 +1793,33 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Remove the "if break|continue ;" that follows "dealloc ; alloc ;" if (! _settings->experimental && Token::Match(tok2, "dealloc ; alloc ; if break|continue ;")) { - Token::eraseTokens(tok2->tokAt(3), 2); + tok2->tokAt(3)->deleteNext(2); done = false; } // if break ; break ; => break ; if (Token::Match(tok2->previous(), "[;{}] if break ; break ;")) { - Token::eraseTokens(tok2, 3); + tok2->deleteNext(3); done = false; } // Reduce "do { dealloc ; alloc ; } while(var) ;" => ";" if (Token::simpleMatch(tok2->next(), "do { dealloc ; alloc ; } while(var) ;")) { - Token::eraseTokens(tok2, 8); + tok2->deleteNext(8); done = false; } // Reduce "do { alloc ; } " => "alloc ;" /** @todo If the loop "do { alloc ; }" can be executed twice, reduce it to "loop alloc ;" */ if (Token::simpleMatch(tok2->next(), "do { alloc ; }")) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); tok2->tokAt(2)->deleteNext(); done = false; } // Reduce "loop break ; => ";" if (Token::Match(tok2->next(), "loop break|continue ;")) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); done = false; } @@ -1831,19 +1831,19 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Reduce "loop if break|continue ; !!else" => ";" if (Token::Match(tok2->next(), "loop if break|continue ; !!else")) { - Token::eraseTokens(tok2, 3); + tok2->deleteNext(3); done = false; } // Reduce "loop { if break|continue ; !!else" => "loop {" if (Token::Match(tok2, "loop { if break|continue ; !!else")) { - Token::eraseTokens(tok2->next(), 3); + tok2->next()->deleteNext(3); done = false; } // Replace "do ; loop ;" with ";" if (Token::simpleMatch(tok2, "; loop ;")) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); done = false; } @@ -1875,7 +1875,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // loop { use ; callfunc ; } => use ; // assume that the "callfunc" is not noreturn if (Token::simpleMatch(tok2, "loop { use ; callfunc ; }")) { - Token::eraseTokens(tok2, 6); + tok2->deleteNext(6); tok2->str("use"); tok2->insertToken(";"); done = false; @@ -1883,7 +1883,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // Delete if block in "alloc ; if(!var) return ;" if (Token::simpleMatch(tok2, "alloc ; if(!var) return ;")) { - Token::eraseTokens(tok2, 3); + tok2->deleteNext(3); done = false; } @@ -1902,13 +1902,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // malloc - realloc => alloc ; dealloc ; alloc ; // Reduce "[;{}] alloc ; dealloc ; alloc ;" => "[;{}] alloc ;" if (Token::Match(tok2, "[;{}] alloc ; dealloc ; alloc ;")) { - Token::eraseTokens(tok2, 4); + tok2->deleteNext(4); done = false; } // use; dealloc; => dealloc; if (Token::Match(tok2, "[;{}] use ; dealloc ;")) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); done = false; } @@ -1921,28 +1921,26 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) // use; if| use; => use; while (Token::Match(tok2, "[;{}] use ; if| use ;")) { Token *t = tok2->tokAt(2); - Token::eraseTokens(t, 2); - if (t->next() && t->next()->str() == ";") - t->deleteNext(); + t->deleteNext(2+(t->str()=="if")); done = false; } // Delete first part in "use ; return use ;" if (Token::Match(tok2, "[;{}] use ; return use ;")) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); done = false; } // try/catch if (Token::simpleMatch(tok2, "try ; catch exit ;")) { - Token::eraseTokens(tok2, 3); + tok2->deleteNext(3); tok2->deleteThis(); done = false; } // Delete second case in "case ; case ;" while (Token::simpleMatch(tok2, "case ; case ;")) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); done = false; } diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 194e0c27b..95152fc43 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1341,7 +1341,7 @@ void Preprocessor::simplifyCondition(const std::map &c tok->str("0"); else continue; - Token::eraseTokens(tok, 3); + tok->deleteNext(3); continue; } @@ -1414,7 +1414,7 @@ void Preprocessor::simplifyCondition(const std::map &c for (Token *tok = const_cast(tokenizer.tokens()); tok; tok = tok->next()) { while (Token::Match(tok, "(|%oror% %any% %oror% 1")) { - Token::eraseTokens(tok, 2); + tok->deleteNext(2); if (tok->tokAt(-3)) tok = tok->tokAt(-3); } diff --git a/lib/token.cpp b/lib/token.cpp index 9870c83d2..c985707af 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -123,7 +123,7 @@ std::string Token::strValue() const void Token::deleteNext(unsigned long index) { - while(_next && index--) { + while (_next && index--) { Token *n = _next; _next = n->next(); delete n; @@ -869,16 +869,6 @@ void Token::eraseTokens(Token *begin, const Token *end) } } -void Token::eraseTokens(Token *tok, unsigned long index) -{ - if (!tok) - return; - - while (tok->next() && index--) { - tok->deleteNext(); - } -} - void Token::createMutualLinks(Token *begin, Token *end) { assert(begin != NULL); diff --git a/lib/token.h b/lib/token.h index 1d9757db8..c9a1c9374 100644 --- a/lib/token.h +++ b/lib/token.h @@ -279,16 +279,6 @@ public: */ static void eraseTokens(Token *begin, const Token *end); - /** - * Delete tokens after tok 'index' times. It's a replacement - * of the consecutive calls to 'deleteNext', more efficient - * than calling 'Token::eraseTokens(tok, tok->tokAt(index+1))'. - * - * @param tok Tokens after this will be erased. - * @param index How many times it should remove the next token. - */ - static void eraseTokens(Token *tok, unsigned long index); - /** * Insert new token after this token. This function will handle * relations between next and previous token also. diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 414f7caa7..e85fd184e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -936,7 +936,7 @@ void Tokenizer::simplifyTypedef() bool undefinedStruct = false; if (Token::Match(tok, "typedef enum|struct %type% %type% ;") && tok->strAt(2) == tok->strAt(3)) { if (tok->next()->str() == "enum") { - Token::eraseTokens(tok, 3); + tok->deleteNext(3); tok->deleteThis(); if (tok->next()) tok->deleteThis(); @@ -1429,7 +1429,7 @@ void Tokenizer::simplifyTypedef() } for (std::size_t i = classLevel; i < spaceInfo.size(); ++i) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); } simplifyType = true; } else if ((inScope && !exitThisScope) || inMemberFunc) { @@ -1437,7 +1437,7 @@ void Tokenizer::simplifyTypedef() // Don't replace this typename if it's preceded by "::" unless it's a namespace if (!spaceInfo.empty() && (tok2->strAt(-2) == spaceInfo[0].className) && spaceInfo[0].isNamespace) { tok2 = tok2->tokAt(-3); - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); tok2 = tok2->next(); simplifyType = true; } @@ -1910,7 +1910,7 @@ bool Tokenizer::tokenize(std::istream &code, (tok->isName() || tok->isNumber()) && (tok->tokAt(2)->isName() || tok->tokAt(2)->isNumber())) { tok->str(tok->str() + tok->strAt(2)); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); if (tok->previous()) tok = tok->previous(); } @@ -1943,7 +1943,7 @@ bool Tokenizer::tokenize(std::istream &code, const bool isJava(_files[0].find(".java") != std::string::npos); for (Token *tok = _tokens; tok; tok = tok->next()) { if (isJava && Token::Match(tok, ") throws %var% {")) { - Token::eraseTokens(tok, 2); + tok->deleteNext(2); } else if (tok->str() == "private") tok->str("private:"); else if (tok->str() == "protected") @@ -2791,7 +2791,7 @@ std::set Tokenizer::simplifyTemplatesExpandSpecialized() tok2->str(name); // delete the "template < >" - Token::eraseTokens(tok, 2); + tok->deleteNext(2); tok->deleteThis(); // Use this special template in the code.. @@ -3101,7 +3101,7 @@ void Tokenizer::simplifyTemplatesInstantiate(const Token *tok, if (Token::Match(tok2, "%var% < sizeof ( %type% ) >") && tok2->tokAt(4)->isStandardType()) { Token * const tok3 = tok2->next(); const unsigned int sz = sizeOfType(tok3->tokAt(3)); - Token::eraseTokens(tok3, 4); + tok3->deleteNext(4); tok3->insertToken(MathLib::toString(sz)); } @@ -4047,7 +4047,7 @@ void Tokenizer::simplifySizeof() continue; if (Token::simpleMatch(tok->next(), ". . .")) { - Token::eraseTokens(tok, 3); + tok->deleteNext(3); } // sizeof 'x' @@ -4162,7 +4162,7 @@ void Tokenizer::simplifySizeof() if (Token::simpleMatch(tok->next(), "( * )")) { tok->str(MathLib::toString(sizeOfType(tok->tokAt(2)))); - Token::eraseTokens(tok, 3); + tok->deleteNext(3); } // sizeof( a ) @@ -4182,7 +4182,7 @@ void Tokenizer::simplifySizeof() unsigned int size = sizeOfType(tok->tokAt(2)); if (size > 0) { tok->str(MathLib::toString(size)); - Token::eraseTokens(tok, 3); + tok->deleteNext(3); } } @@ -4264,7 +4264,7 @@ bool Tokenizer::simplifyTokenList() // change array to pointer.. for (Token *tok = _tokens; tok; tok = tok->next()) { if (Token::Match(tok, "%type% %var% [ ] [,;=]")) { - Token::eraseTokens(tok->next(), 2); + tok->next()->deleteNext(2); tok->insertToken("*"); } } @@ -4309,7 +4309,7 @@ bool Tokenizer::simplifyTokenList() && (Token::Match(tok->next(), "* ( %var% + %num% )") || Token::Match(tok->next(), "* ( %var% + %var% )"))) { // remove '* (' - Token::eraseTokens(tok, 2); + tok->deleteNext(2); tok = tok->tokAt(2); // '+'->'[' @@ -4358,14 +4358,14 @@ bool Tokenizer::simplifyTokenList() const unsigned int varid(tok->next()->varId()); // Delete the "%var% =" - Token::eraseTokens(tok, 2); + tok->deleteNext(2); // Change function name "realloc" to "free" tok->next()->str("free"); // delete the ", 0" tok = tok->tokAt(3); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); // goto the ";" tok = tok->tokAt(2); @@ -4384,7 +4384,7 @@ bool Tokenizer::simplifyTokenList() tok->str("malloc"); // delete "0 ," - Token::eraseTokens(tok->next(), 2); + tok->next()->deleteNext(2); } } @@ -4429,7 +4429,7 @@ bool Tokenizer::simplifyTokenList() std::ostringstream ostr; ostr << Token::getStrLength(tok->tokAt(2)); tok->str(ostr.str()); - Token::eraseTokens(tok, 3); + tok->deleteNext(3); } } @@ -4542,11 +4542,12 @@ void Tokenizer::removeRedundantAssignment() if (!localvars.empty()) { for (Token *tok2 = tok->next(); tok2 && tok2 != end;) { if (Token::Match(tok2, "[;{}] %type% %var% ;") && localvars.find(tok2->tokAt(2)->varId()) != localvars.end()) { - Token::eraseTokens(tok2, 3); - } else if (Token::Match(tok2, "[;{}] %type% * %var% ;") && localvars.find(tok2->tokAt(3)->varId()) != localvars.end()) { - Token::eraseTokens(tok2, 4); - } else if (Token::Match(tok2, "[;{}] %var% = %any% ;") && localvars.find(tok2->next()->varId()) != localvars.end()) { - Token::eraseTokens(tok2, 4); + tok2->deleteNext(3); + } else if ((Token::Match(tok2, "[;{}] %type% * %var% ;") && + localvars.find(tok2->tokAt(3)->varId()) != localvars.end()) || + (Token::Match(tok2, "[;{}] %var% = %any% ;") && + localvars.find(tok2->next()->varId()) != localvars.end())) { + tok2->deleteNext(4); } else tok2 = tok2->next(); } @@ -4677,7 +4678,7 @@ bool Tokenizer::removeRedundantConditions() else tok->str(";"); - Token::eraseTokens(tok, 4); + tok->deleteNext(4); } ret = true; @@ -4701,7 +4702,7 @@ bool Tokenizer::removeRedundantConditions() else tok->str(";"); - Token::eraseTokens(tok, 4); + tok->deleteNext(4); } ret = true; @@ -4760,7 +4761,7 @@ void Tokenizer::removeRedundantFor() // Simplify loop if loop variable isn't written if (!write) { // remove "for (" - Token::eraseTokens(tok, 2); + tok->deleteNext(2); // If loop variable is read then keep assignment before // loop body.. @@ -4769,11 +4770,11 @@ void Tokenizer::removeRedundantFor() tok = tok->tokAt(4); } else { // remove "x = 0 ;" - Token::eraseTokens(tok, 4); + tok->deleteNext(4); } // remove "x < 1 ; x ++ )" - Token::eraseTokens(tok, 7); + tok->deleteNext(7); // Add assignment after the loop body so the loop variable // get the correct end value @@ -4799,7 +4800,7 @@ void Tokenizer::removeRedundantSemicolons() if (Token::simpleMatch(tok, "; ;")) { tok->deleteNext(); } else if (Token::simpleMatch(tok, "; { ; }")) { - Token::eraseTokens(tok, 3); + tok->deleteNext(3); } else { break; } @@ -5129,11 +5130,11 @@ void Tokenizer::simplifyConditionOperator() if (isPointer) { tok = tok->previous(); - Token::eraseTokens(tok, tok->tokAt(10)); + tok->deleteNext(9); } else if (isReturn) - Token::eraseTokens(tok, tok->tokAt(7)); + tok->deleteNext(6); else - Token::eraseTokens(tok, tok->tokAt(9)); + tok->deleteNext(8); Token *starttok = 0; @@ -5192,14 +5193,14 @@ bool Tokenizer::simplifyConditions() if (Token::simpleMatch(tok, "( true &&") || Token::simpleMatch(tok, "&& true &&") || Token::simpleMatch(tok->next(), "&& true )")) { - Token::eraseTokens(tok, 2); + tok->deleteNext(2); ret = true; } else if (Token::simpleMatch(tok, "( false ||") || Token::simpleMatch(tok, "|| false ||") || Token::simpleMatch(tok->next(), "|| false )")) { - Token::eraseTokens(tok, 2); + tok->deleteNext(2); ret = true; } @@ -5287,7 +5288,7 @@ bool Tokenizer::simplifyConditions() if (! cmp.empty()) { tok = tok->next(); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); tok->str(result ? "true" : "false"); ret = true; @@ -5359,7 +5360,7 @@ bool Tokenizer::simplifyQuestionMark() // delete the condition token and the "?" tok = tok->tokAt(-2); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); // delete operator after the : if (end) { @@ -5606,8 +5607,7 @@ void Tokenizer:: simplifyFunctionPointers() // ok simplify this function pointer cast to an ordinary pointer cast tok1->deleteNext(); tok1->next()->deleteNext(); - const Token *tok2 = tok1->linkAt(2); - Token::eraseTokens(tok1->next(), tok2 ? tok2->next() : 0); + Token::eraseTokens(tok1->next(), tok1->linkAt(2)->next()); continue; } @@ -5632,8 +5632,7 @@ void Tokenizer:: simplifyFunctionPointers() // ok simplify this function pointer to an ordinary pointer tok->deleteNext(); tok->tokAt(2)->deleteNext(); - const Token *tok2 = tok->linkAt(3); - Token::eraseTokens(tok->tokAt(2), tok2 ? tok2->next() : 0); + Token::eraseTokens(tok->tokAt(2), tok->linkAt(3)->next()); } } @@ -5655,7 +5654,7 @@ bool Tokenizer::simplifyFunctionReturn() if (Token::Match(tok2, pattern.c_str())) { tok2 = tok2->next(); tok2->str(tok->strAt(5)); - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); ret = true; } } @@ -6304,28 +6303,28 @@ void Tokenizer::simplifyIfNot() tok = tok->previous(); tok->insertToken("!"); tok = tok->tokAt(4); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); } else if (Token::Match(tok, "* %var% == 0|false")) { tok = tok->previous(); tok->insertToken("!"); tok = tok->tokAt(3); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); } } else if (tok->link() && Token::Match(tok, ") == 0|false")) { // if( foo(x) == 0 ) if (Token::Match(tok->link()->tokAt(-2), "( %var%")) { - Token::eraseTokens(tok, 2); + tok->deleteNext(2); tok->link()->previous()->insertToken(tok->link()->previous()->str().c_str()); tok->link()->tokAt(-2)->str("!"); } // if( (x) == 0 ) else if (Token::simpleMatch(tok->link()->previous(), "(")) { - Token::eraseTokens(tok, 2); + tok->deleteNext(2); tok->link()->insertToken("("); tok->link()->str("!"); Token *temp = tok->link(); @@ -6350,7 +6349,7 @@ void Tokenizer::simplifyIfNotNull() if (Token::simpleMatch(tok->tokAt(2), (varname + " != 0 ) ;").c_str()) || Token::simpleMatch(tok->tokAt(2), ("0 != " + varname + " ) ;").c_str())) { tok = tok->tokAt(-2); - Token::eraseTokens(tok, tok->tokAt(9)); + tok->deleteNext(8); } } continue; @@ -6979,7 +6978,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign break; } if (!structname.empty()) { - Token::eraseTokens(tok3, 2); + tok3->deleteNext(2); } if (Token::Match(valueToken, "& %var% ;")) { tok3->insertToken("&"); @@ -7044,7 +7043,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign // array usage if (Token::Match(tok3, ("[(,] " + structname + " %varid% [|%op%").c_str(), varid)) { if (!structname.empty()) { - Token::eraseTokens(tok3, 2); + tok3->deleteNext(2); } tok3 = tok3->next(); tok3->str(value); @@ -7061,7 +7060,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign if (value[0] == '\"') break; if (!structname.empty()) { - Token::eraseTokens(tok3, 2); + tok3->deleteNext(2); } tok3 = tok3->next(); tok3->str(value); @@ -7122,7 +7121,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value)) { const std::string op(tok3->strAt(2)); if (Token::Match(tok3, "[{};] %any% %any% ;")) { - Token::eraseTokens(tok3, 3); + tok3->deleteNext(3); } else { tok3 = tok3->next(); tok3->str(value); @@ -7143,7 +7142,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign (*tok2)->tokAt(2)->str(value); (*tok2)->tokAt(2)->varId(valueVarId); if (Token::Match(tok3, "[;{}] %any% %any% ;")) { - Token::eraseTokens(tok3, 3); + tok3->deleteNext(3); } else { tok3->deleteNext(); tok3->next()->str(value); @@ -7324,7 +7323,7 @@ void Tokenizer::simplifyReference() } } - Token::eraseTokens(tok2, tok2->tokAt(7+(tok->strAt(6)==")"))); + tok2->deleteNext(6+(tok->strAt(6)==")")); } } } @@ -7372,7 +7371,7 @@ bool Tokenizer::simplifyCalculations() if (Token::Match(tok->tokAt(-4), "[;{}] %var% = %var% [+-|] 0 ;") && tok->strAt(-3) == tok->previous()->str()) { tok = tok->tokAt(-3); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); tok->deleteThis(); } tok->deleteNext(); @@ -7446,7 +7445,7 @@ bool Tokenizer::simplifyCalculations() result = (op1 > op2) ? "1" : "0"; tok->str(result); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); ret = true; } } @@ -7465,7 +7464,7 @@ bool Tokenizer::simplifyCalculations() ss << result; tok->str(ss.str()); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); } } @@ -7500,7 +7499,7 @@ bool Tokenizer::simplifyCalculations() if (!result.empty()) { ret = true; tok->str(result); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); continue; } } @@ -7525,7 +7524,7 @@ bool Tokenizer::simplifyCalculations() else tok->str(MathLib::calculate(tok->str(), tok->strAt(2), tok->next()->str()[0], this)); - Token::eraseTokens(tok, tok->tokAt(3)); + tok->deleteNext(2); // evaluate "2 + 2 - 2 - 2" // as (((2 + 2) - 2) - 2) = 0 @@ -7617,7 +7616,7 @@ void Tokenizer::simplifyGoto() const std::string name(tok->str()); - Token::eraseTokens(tok, 2); + tok->deleteNext(2); tok->deleteThis(); // This label is at the end of the function.. replace all matching goto statements.. @@ -7626,7 +7625,7 @@ void Tokenizer::simplifyGoto() if (token->next()->str() == name) { // Delete the "goto name;" token = token->previous(); - Token::eraseTokens(token, 3); + token->deleteNext(3); // Insert the statements.. bool ret = false; // is there return @@ -8116,7 +8115,7 @@ void Tokenizer::simplifyEnum() } if (hasClass) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); } simplify = false; @@ -8202,7 +8201,7 @@ void Tokenizer::simplifyEnum() } if (hasClass) { - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); } simplify = false; @@ -8406,9 +8405,9 @@ void Tokenizer::eraseDeadCode(Token *begin, const Token *end) if (indentlevel == indentcase) ++indentlevel; if (!checklabel || !indentswitch) { - Token::eraseTokens(tok, 3 + (tok->next()->str() == "case")); + tok->deleteNext(3+(tok->next()->str()=="case")); } else { - tok = tok->tokAt(3 + (tok->next()->str() == "case")); + tok = tok->tokAt(3+(tok->next()->str()=="case")); } } else if (Token::Match(tok, "[{};] %var% : ;") && tok->next()->str() != "default") { if (checklabel) { @@ -8428,7 +8427,7 @@ void Tokenizer::eraseDeadCode(Token *begin, const Token *end) if (Token::Match(tok2->next(), "{|[|(")) { tok2 = tok2->next()->link(); } else if (Token::Match(tok2, "[{};] case %any% : ;") || Token::Match(tok2, "[{};] default : ;")) { - Token::eraseTokens(tok2, 3 + (tok2->next()->str() == "case")); + tok2->deleteNext(3+(tok2->next()->str()=="case")); } else if (tok2->next()->str() == "}") { --indentlevel2; if (indentlevel2 <= indentcase) @@ -9004,7 +9003,7 @@ void Tokenizer::simplifyWhile0() // delete "(}) while ( 0 ) (;)" tok = tok->previous(); - Token::eraseTokens(tok, 4); // while ( 0 ) + tok->deleteNext(4); // while ( 0 ) if (tok->next() && tok->next()->str() == ";") tok->deleteNext(); // ; if (!flowmatch) @@ -9044,7 +9043,8 @@ void Tokenizer::simplifyErrNoInWhile() tok1 = tok1->previous(); // erase "&& errno == EINTR" - Token::eraseTokens(tok1->previous(), endpar); + Token::eraseTokens(tok1, endpar); + tok1->deleteThis(); // tok is invalid.. move to endpar tok = endpar; @@ -9321,7 +9321,7 @@ void Tokenizer::simplifyAsm() Token *partok = tok->tokAt(2); if (partok->str() != "(") partok = partok->next(); - Token::eraseTokens(tok, partok->link() ? partok->link()->next() : NULL); + Token::eraseTokens(tok, partok->link()->next()); } else if (Token::simpleMatch(tok->next(), "__asm")) { @@ -9366,10 +9366,10 @@ void Tokenizer::simplifyBitfields() } } else if (Token::Match(tok, ";|{|}|public:|protected:|private: const| %type% : %any% ;") && tok->next()->str() != "default") { - bool offset = (tok->next()->str() == "const"); + unsigned char offset = (tok->next()->str() == "const") ? 1 : 0; if (tok->strAt(3 + offset) != "{") { - Token::eraseTokens(tok, 4+offset); + tok->deleteNext(4+offset); tok = tok->previous(); } } @@ -9407,7 +9407,8 @@ void Tokenizer::simplifyBuiltinExpect() if (parlevel == 1 && tok2->str() == ",") { if (Token::Match(tok2, ", %num% )")) { tok->deleteNext(); - Token::eraseTokens(tok2->previous(), tok2->tokAt(2)); + tok2->deleteNext(); + tok2->deleteThis(); } break; } @@ -9417,7 +9418,7 @@ void Tokenizer::simplifyBuiltinExpect() tok->linkAt(2)->deleteThis(); // remove "likely|unlikely (" - Token::eraseTokens(tok, 2); + tok->deleteNext(2); } } } @@ -9434,9 +9435,9 @@ void Tokenizer::simplifyMicrosoftMFC() for (Token *tok = _tokens; tok; tok = tok->next()) { if (Token::simpleMatch(tok->next(), "DECLARE_MESSAGE_MAP ( )")) { - Token::eraseTokens(tok, 3); + tok->deleteNext(3); } else if (Token::Match(tok->next(), "DECLARE_DYNAMIC|DECLARE_DYNAMIC_CLASS|DECLARE_DYNCREATE ( %any% )")) { - Token::eraseTokens(tok, 4); + tok->deleteNext(4); } } } @@ -9673,7 +9674,7 @@ void Tokenizer::simplifyQtSignalsSlots() } else if (Token::Match(tok2->next(), "public|protected|private slots|Q_SLOTS :")) { tok2 = tok2->next(); tok2->str(tok2->str() + ":"); - Token::eraseTokens(tok2, 2); + tok2->deleteNext(2); tok2 = tok2->previous(); } else if (Token::Match(tok2->next(), "signals|Q_SIGNALS :")) { tok2 = tok2->next(); @@ -9742,7 +9743,7 @@ void Tokenizer::simplifyOperatorName() if (par && Token::Match(par->link(), ") const| [=;{),]")) { tok->str("operator" + op); - Token::eraseTokens(tok,par); + Token::eraseTokens(tok, par); } } }