diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 87195727e..4d8ccd367 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -796,13 +796,13 @@ void TemplateSimplifier::simplifyTemplateAliases(std::liststr(templateAlias.token->str()); } else { - tok2 = Tokenizer::copyTokens(aliasUsage.token, aliasToken1, templateAlias.token, true); + tok2 = TokenList::copyTokens(aliasUsage.token, aliasToken1, templateAlias.token, true); aliasUsage.token->deleteThis(); aliasUsage.token = tok2; } tok2 = aliasUsage.token->next(); // the '<' const Token * const endToken1 = templateAlias.token->next()->findClosingBracket(); - Token * const endToken2 = Tokenizer::copyTokens(tok2, templateAlias.token->tokAt(2), endToken1->previous(), false); + Token * const endToken2 = TokenList::copyTokens(tok2, templateAlias.token->tokAt(2), endToken1->previous(), false); for (const Token *tok1 = templateAlias.token->next(); tok2 != endToken2; tok1 = tok1->next(), tok2 = tok2->next()) { if (!tok2->isName()) continue; @@ -821,7 +821,7 @@ void TemplateSimplifier::simplifyTemplateAliases(std::listprevious(); Token * const destToken = tok2; - tok2 = Tokenizer::copyTokens(tok2, fromStart, fromEnd, true); + tok2 = TokenList::copyTokens(tok2, fromStart, fromEnd, true); if (tok2 == destToken->next()) tok2 = destToken; destToken->deleteThis(); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index df3259603..7a8926b38 100755 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -197,44 +197,6 @@ unsigned int Tokenizer::sizeOfType(const Token *type) const return it->second; } - -//--------------------------------------------------------------------------- - -Token *Tokenizer::copyTokens(Token *dest, const Token *first, const Token *last, bool one_line) -{ - std::stack links; - Token *tok2 = dest; - unsigned int linenrs = dest->linenr(); - const unsigned int commonFileIndex = dest->fileIndex(); - for (const Token *tok = first; tok != last->next(); tok = tok->next()) { - tok2->insertToken(tok->str()); - tok2 = tok2->next(); - tok2->fileIndex(commonFileIndex); - tok2->linenr(linenrs); - tok2->tokType(tok->tokType()); - tok2->flags(tok->flags()); - tok2->varId(tok->varId()); - - // Check for links and fix them up - if (Token::Match(tok2, "(|[|{")) - links.push(tok2); - else if (Token::Match(tok2, ")|]|}")) { - if (links.empty()) - return tok2; - - Token * link = links.top(); - - tok2->link(link); - link->link(tok2); - - links.pop(); - } - if (!one_line && tok->next()) - linenrs += tok->next()->linenr() - tok->linenr(); - } - return tok2; -} - //--------------------------------------------------------------------------- // check if this statement is a duplicate definition @@ -1310,7 +1272,7 @@ void Tokenizer::simplifyTypedef() } // add remainder of type - tok2 = copyTokens(tok2, typeStart->next(), typeEnd); + tok2 = TokenList::copyTokens(tok2, typeStart->next(), typeEnd); if (!pointers.empty()) { for (std::list::const_iterator iter = pointers.begin(); iter != pointers.end(); ++iter) { @@ -1323,7 +1285,7 @@ void Tokenizer::simplifyTypedef() tok2->insertToken("("); tok2 = tok2->next(); Token *tok3 = tok2; - tok2 = copyTokens(tok2, funcStart, funcEnd); + tok2 = TokenList::copyTokens(tok2, funcStart, funcEnd); if (!inCast) tok2 = processFunc(tok2, inOperator); @@ -1335,7 +1297,7 @@ void Tokenizer::simplifyTypedef() tok2 = tok2->next(); Token::createMutualLinks(tok2, tok3); - tok2 = copyTokens(tok2, argStart, argEnd); + tok2 = TokenList::copyTokens(tok2, argStart, argEnd); if (specStart) { Token *spec = specStart; @@ -1393,7 +1355,7 @@ void Tokenizer::simplifyTypedef() if (!tok2) syntaxError(nullptr); - tok2 = copyTokens(tok2, argStart, argEnd); + tok2 = TokenList::copyTokens(tok2, argStart, argEnd); if (inTemplate) { if (!tok2) syntaxError(nullptr); @@ -1442,13 +1404,13 @@ void Tokenizer::simplifyTypedef() Token::createMutualLinks(tok2, tok4); } - tok2 = copyTokens(tok2, argStart, argEnd); + tok2 = TokenList::copyTokens(tok2, argStart, argEnd); tok2->insertToken(")"); tok2 = tok2->next(); Token::createMutualLinks(tok2, tok3); - tok2 = copyTokens(tok2, argFuncRetStart, argFuncRetEnd); + tok2 = TokenList::copyTokens(tok2, argFuncRetStart, argFuncRetEnd); } else if (ptrToArray || refToArray) { tok2->insertToken("("); tok2 = tok2->next(); @@ -1520,7 +1482,7 @@ void Tokenizer::simplifyTypedef() } } } else if (typeOf) { - tok2 = copyTokens(tok2, argStart, argEnd); + tok2 = TokenList::copyTokens(tok2, argStart, argEnd); } else if (tok2->strAt(2) == "[") { do { if (!tok2->linkAt(2)) @@ -1560,7 +1522,7 @@ void Tokenizer::simplifyTypedef() if (!tok2->next()) syntaxError(tok2); // can't recover so quit - tok2 = copyTokens(tok2, arrayStart, arrayEnd); + tok2 = TokenList::copyTokens(tok2, arrayStart, arrayEnd); if (!tok2->next()) syntaxError(tok2); @@ -9817,7 +9779,7 @@ void Tokenizer::simplifyReturnStrncat() Token *end = tok->next()->nextArgument()->tokAt(-2); //all the first argument is copied - copyTokens(tok2, tok->next(), end); + TokenList::copyTokens(tok2, tok->next(), end); tok2->insertToken("return"); } } diff --git a/lib/tokenize.h b/lib/tokenize.h index 9ee23637d..a914b20c8 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -768,16 +768,6 @@ public: return list.front(); } - /** - * Copy tokens. - * @param dest destination token where copied tokens will be inserted after - * @param first first token to copy - * @param last last token to copy - * @param one_line true=>copy all tokens to the same line as dest. false=>copy all tokens to dest while keeping the 'line breaks' - * @return new location of last token copied - */ - static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true); - /** * Helper function to check whether number is zero (0 or 0.0 or 0E+0) or not? * @param s the string to check diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index fbe2946cc..10d215b48 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -175,6 +175,47 @@ void TokenList::addtoken(const Token * tok, const unsigned int lineno, const uns _back->fileIndex(fileno); _back->flags(tok->flags()); } + + +//--------------------------------------------------------------------------- +// copyTokens - Copy and insert tokens +//--------------------------------------------------------------------------- + +Token *TokenList::copyTokens(Token *dest, const Token *first, const Token *last, bool one_line) +{ + std::stack links; + Token *tok2 = dest; + unsigned int linenrs = dest->linenr(); + const unsigned int commonFileIndex = dest->fileIndex(); + for (const Token *tok = first; tok != last->next(); tok = tok->next()) { + tok2->insertToken(tok->str()); + tok2 = tok2->next(); + tok2->fileIndex(commonFileIndex); + tok2->linenr(linenrs); + tok2->tokType(tok->tokType()); + tok2->flags(tok->flags()); + tok2->varId(tok->varId()); + + // Check for links and fix them up + if (Token::Match(tok2, "(|[|{")) + links.push(tok2); + else if (Token::Match(tok2, ")|]|}")) { + if (links.empty()) + return tok2; + + Token * link = links.top(); + + tok2->link(link); + link->link(tok2); + + links.pop(); + } + if (!one_line && tok->next()) + linenrs += tok->next()->linenr() - tok->linenr(); + } + return tok2; +} + //--------------------------------------------------------------------------- // InsertTokens - Copy and insert tokens //--------------------------------------------------------------------------- diff --git a/lib/tokenlist.h b/lib/tokenlist.h index fb0cba930..af25e9f95 100644 --- a/lib/tokenlist.h +++ b/lib/tokenlist.h @@ -73,6 +73,16 @@ public: static void insertTokens(Token *dest, const Token *src, unsigned int n); + /** + * Copy tokens. + * @param dest destination token where copied tokens will be inserted after + * @param first first token to copy + * @param last last token to copy + * @param one_line true=>copy all tokens to the same line as dest. false=>copy all tokens to dest while keeping the 'line breaks' + * @return new location of last token copied + */ + static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true); + /** * Create tokens from code. * The code must be preprocessed first: