2012-01-01 21:55:05 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2012-01-01 21:55:05 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "templatesimplifier.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
|
|
|
#include "errorlogger.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "errortypes.h"
|
2012-01-09 20:33:11 +01:00
|
|
|
#include "mathlib.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "settings.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "standards.h"
|
2012-01-01 21:55:05 +01:00
|
|
|
#include "token.h"
|
2014-04-26 18:31:56 +02:00
|
|
|
#include "tokenize.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "tokenlist.h"
|
|
|
|
|
2012-07-24 21:21:05 +02:00
|
|
|
#include <algorithm>
|
2012-01-03 22:35:06 +01:00
|
|
|
#include <cassert>
|
2014-07-02 16:16:19 +02:00
|
|
|
#include <iostream>
|
2019-02-14 11:48:59 +01:00
|
|
|
#include <map>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <memory>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <stack>
|
|
|
|
#include <utility>
|
2012-01-01 21:55:05 +01:00
|
|
|
|
2021-04-20 15:39:04 +02:00
|
|
|
static Token *skipRequires(Token *tok)
|
|
|
|
{
|
|
|
|
if (!Token::simpleMatch(tok, "requires"))
|
|
|
|
return tok;
|
|
|
|
|
|
|
|
while (Token::Match(tok, "%oror%|&&|requires %name%|(")) {
|
|
|
|
Token *after = tok->next();
|
|
|
|
if (after->str() == "(") {
|
|
|
|
tok = after->link()->next();
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-20 16:46:05 +02:00
|
|
|
if (Token::simpleMatch(after, "requires (") && Token::simpleMatch(after->linkAt(1), ") {")) {
|
2021-04-20 15:39:04 +02:00
|
|
|
tok = after->linkAt(1)->linkAt(1)->next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (Token::Match(after, "%name% :: %name%"))
|
|
|
|
after = after->tokAt(2);
|
|
|
|
if (Token::Match(after, "%name% <")) {
|
|
|
|
after = after->next()->findClosingBracket();
|
|
|
|
tok = after ? after->next() : nullptr;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2018-01-05 15:27:43 +01:00
|
|
|
namespace {
|
|
|
|
class FindToken {
|
|
|
|
public:
|
2018-06-17 19:24:40 +02:00
|
|
|
explicit FindToken(const Token *token) : mToken(token) {}
|
|
|
|
bool operator()(const TemplateSimplifier::TokenAndName &tokenAndName) const {
|
2019-08-10 08:42:12 +02:00
|
|
|
return tokenAndName.token() == mToken;
|
2018-01-05 15:27:43 +01:00
|
|
|
}
|
|
|
|
private:
|
2018-06-17 19:24:40 +02:00
|
|
|
const Token * const mToken;
|
2018-01-05 15:27:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class FindName {
|
|
|
|
public:
|
2022-07-28 22:51:45 +02:00
|
|
|
explicit FindName(std::string name) : mName(std::move(name)) {}
|
2018-06-17 19:24:40 +02:00
|
|
|
bool operator()(const TemplateSimplifier::TokenAndName &tokenAndName) const {
|
2019-08-10 08:42:12 +02:00
|
|
|
return tokenAndName.name() == mName;
|
2018-01-05 15:27:43 +01:00
|
|
|
}
|
|
|
|
private:
|
2018-06-17 19:24:40 +02:00
|
|
|
const std::string mName;
|
2018-01-05 15:27:43 +01:00
|
|
|
};
|
2019-01-14 08:29:32 +01:00
|
|
|
|
|
|
|
class FindFullName {
|
|
|
|
public:
|
2022-07-28 22:51:45 +02:00
|
|
|
explicit FindFullName(std::string fullName) : mFullName(std::move(fullName)) {}
|
2019-01-14 08:29:32 +01:00
|
|
|
bool operator()(const TemplateSimplifier::TokenAndName &tokenAndName) const {
|
2019-08-10 08:42:12 +02:00
|
|
|
return tokenAndName.fullName() == mFullName;
|
2019-01-14 08:29:32 +01:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
const std::string mFullName;
|
|
|
|
};
|
2018-01-05 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
2022-07-28 22:51:45 +02:00
|
|
|
TemplateSimplifier::TokenAndName::TokenAndName(Token *token, std::string scope) :
|
|
|
|
mToken(token), mScope(std::move(scope)), mName(mToken ? mToken->str() : ""),
|
2019-08-10 08:42:12 +02:00
|
|
|
mFullName(mScope.empty() ? mName : (mScope + " :: " + mName)),
|
|
|
|
mNameToken(nullptr), mParamEnd(nullptr), mFlags(0)
|
2019-01-14 20:36:23 +01:00
|
|
|
{
|
2020-12-01 09:43:16 +01:00
|
|
|
if (mToken) {
|
|
|
|
if (mToken->strAt(1) == "<") {
|
|
|
|
const Token *end = mToken->next()->findClosingBracket();
|
|
|
|
if (end && end->strAt(1) == "(") {
|
|
|
|
isFunction(true);
|
|
|
|
}
|
|
|
|
}
|
2019-08-10 08:42:12 +02:00
|
|
|
mToken->templateSimplifierPointer(this);
|
2020-12-01 09:43:16 +01:00
|
|
|
}
|
2019-01-14 20:36:23 +01:00
|
|
|
}
|
|
|
|
|
2022-07-28 22:51:45 +02:00
|
|
|
TemplateSimplifier::TokenAndName::TokenAndName(Token *token, std::string scope, const Token *nameToken, const Token *paramEnd) :
|
|
|
|
mToken(token), mScope(std::move(scope)), mName(nameToken->str()),
|
2019-08-10 08:42:12 +02:00
|
|
|
mFullName(mScope.empty() ? mName : (mScope + " :: " + mName)),
|
|
|
|
mNameToken(nameToken), mParamEnd(paramEnd), mFlags(0)
|
2018-09-01 11:26:10 +02:00
|
|
|
{
|
2019-01-07 06:55:22 +01:00
|
|
|
// only set flags for declaration
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mToken && mNameToken && mParamEnd) {
|
|
|
|
isSpecialization(Token::simpleMatch(mToken, "template < >"));
|
2019-07-24 19:20:19 +02:00
|
|
|
|
|
|
|
if (!isSpecialization()) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (Token::simpleMatch(mToken->next()->findClosingBracket(), "> template <")) {
|
|
|
|
const Token * temp = mNameToken->tokAt(-2);
|
2019-07-24 19:20:19 +02:00
|
|
|
while (Token::Match(temp, ">|%name% ::")) {
|
|
|
|
if (temp->str() == ">")
|
|
|
|
temp = temp->findOpeningBracket()->previous();
|
|
|
|
else
|
|
|
|
temp = temp->tokAt(-2);
|
|
|
|
}
|
|
|
|
isPartialSpecialization(temp->strAt(1) == "<");
|
|
|
|
} else
|
2019-08-10 08:42:12 +02:00
|
|
|
isPartialSpecialization(mNameToken->strAt(1) == "<");
|
2019-07-24 19:20:19 +02:00
|
|
|
}
|
|
|
|
|
2019-08-10 08:42:12 +02:00
|
|
|
isAlias(mParamEnd->strAt(1) == "using");
|
2019-06-01 10:52:29 +02:00
|
|
|
|
|
|
|
if (isAlias() && isPartialSpecialization()) {
|
2019-08-10 08:42:12 +02:00
|
|
|
throw InternalError(mToken, "partial specialization of alias templates is not permitted", InternalError::SYNTAX);
|
2019-06-01 10:52:29 +02:00
|
|
|
}
|
|
|
|
if (isAlias() && isSpecialization()) {
|
2019-08-10 08:42:12 +02:00
|
|
|
throw InternalError(mToken, "explicit specialization of alias templates is not permitted", InternalError::SYNTAX);
|
2019-06-01 10:52:29 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 09:56:33 +02:00
|
|
|
isFriend(mParamEnd->strAt(1) == "friend");
|
|
|
|
const Token *next = mParamEnd->next();
|
|
|
|
if (isFriend())
|
|
|
|
next = next->next();
|
|
|
|
|
|
|
|
isClass(Token::Match(next, "class|struct|union %name% <|{|:|;|::"));
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mToken->strAt(1) == "<" && !isSpecialization()) {
|
|
|
|
const Token *end = mToken->next()->findClosingBracket();
|
2021-04-17 21:56:13 +02:00
|
|
|
isVariadic(end && Token::findmatch(mToken->tokAt(2), "%name% ...", end));
|
2019-05-23 20:53:26 +02:00
|
|
|
}
|
2019-08-10 08:42:12 +02:00
|
|
|
const Token *tok1 = mNameToken->next();
|
2019-03-23 10:42:41 +01:00
|
|
|
if (tok1->str() == "<") {
|
|
|
|
const Token *closing = tok1->findClosingBracket();
|
|
|
|
if (closing)
|
|
|
|
tok1 = closing->next();
|
|
|
|
else
|
2019-08-10 08:42:12 +02:00
|
|
|
throw InternalError(mToken, "unsupported syntax", InternalError::SYNTAX);
|
2019-03-23 10:42:41 +01:00
|
|
|
}
|
2019-01-18 21:12:39 +01:00
|
|
|
isFunction(tok1->str() == "(");
|
2019-09-01 09:56:33 +02:00
|
|
|
isVariable(!isClass() && !isAlias() && !isFriend() && Token::Match(tok1, "=|;"));
|
|
|
|
if (!isFriend()) {
|
|
|
|
if (isVariable())
|
|
|
|
isForwardDeclaration(tok1->str() == ";");
|
|
|
|
else if (!isAlias()) {
|
|
|
|
if (isFunction())
|
|
|
|
tok1 = tok1->link()->next();
|
|
|
|
while (tok1 && !Token::Match(tok1, ";|{")) {
|
|
|
|
if (tok1->str() == "<")
|
|
|
|
tok1 = tok1->findClosingBracket();
|
|
|
|
else if (Token::Match(tok1, "(|[") && tok1->link())
|
|
|
|
tok1 = tok1->link();
|
|
|
|
if (tok1)
|
|
|
|
tok1 = tok1->next();
|
|
|
|
}
|
2019-06-17 21:18:27 +02:00
|
|
|
if (tok1)
|
2019-09-01 09:56:33 +02:00
|
|
|
isForwardDeclaration(tok1->str() == ";");
|
2019-06-17 21:18:27 +02:00
|
|
|
}
|
2019-01-18 21:12:39 +01:00
|
|
|
}
|
2019-06-09 08:11:59 +02:00
|
|
|
// check for member class or function and adjust scope
|
2020-12-06 22:08:42 +01:00
|
|
|
if ((isFunction() || isClass()) &&
|
|
|
|
(mNameToken->strAt(-1) == "::" || Token::simpleMatch(mNameToken->tokAt(-2), ":: ~"))) {
|
2019-08-10 08:42:12 +02:00
|
|
|
const Token * start = mNameToken;
|
2020-12-06 22:08:42 +01:00
|
|
|
if (start->strAt(-1) == "~")
|
|
|
|
start = start->previous();
|
|
|
|
const Token *end = start;
|
2019-01-24 07:21:22 +01:00
|
|
|
|
2020-05-21 08:47:48 +02:00
|
|
|
while (start && (Token::Match(start->tokAt(-2), "%name% ::") ||
|
2020-05-22 08:48:28 +02:00
|
|
|
(Token::simpleMatch(start->tokAt(-2), "> ::") &&
|
|
|
|
start->tokAt(-2)->findOpeningBracket() &&
|
|
|
|
Token::Match(start->tokAt(-2)->findOpeningBracket()->previous(), "%name% <")))) {
|
2019-01-24 07:21:22 +01:00
|
|
|
if (start->strAt(-2) == ">")
|
|
|
|
start = start->tokAt(-2)->findOpeningBracket()->previous();
|
|
|
|
else
|
|
|
|
start = start->tokAt(-2);
|
|
|
|
}
|
|
|
|
|
2020-12-06 22:08:42 +01:00
|
|
|
if (start && start != end) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (!mScope.empty())
|
|
|
|
mScope += " ::";
|
2020-12-06 22:08:42 +01:00
|
|
|
while (start && start->next() != end) {
|
2019-01-24 07:21:22 +01:00
|
|
|
if (start->str() == "<")
|
|
|
|
start = start->findClosingBracket();
|
|
|
|
else {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (!mScope.empty())
|
|
|
|
mScope += " ";
|
|
|
|
mScope += start->str();
|
2019-01-24 07:21:22 +01:00
|
|
|
}
|
|
|
|
start = start->next();
|
|
|
|
}
|
|
|
|
if (start)
|
2019-08-10 08:42:12 +02:00
|
|
|
mFullName = mScope.empty() ? mName : (mScope + " :: " + mName);
|
2019-01-24 07:21:22 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-07 06:55:22 +01:00
|
|
|
}
|
|
|
|
|
2019-08-10 08:42:12 +02:00
|
|
|
// make sure at most only one family flag is set
|
|
|
|
assert(isClass() ? !(isFunction() || isVariable()) : true);
|
|
|
|
assert(isFunction() ? !(isClass() || isVariable()) : true);
|
|
|
|
assert(isVariable() ? !(isClass() || isFunction()) : true);
|
|
|
|
|
|
|
|
if (mToken)
|
|
|
|
mToken->templateSimplifierPointer(this);
|
2018-12-21 13:51:45 +01:00
|
|
|
}
|
|
|
|
|
2019-08-10 08:42:12 +02:00
|
|
|
TemplateSimplifier::TokenAndName::TokenAndName(const TokenAndName& other) :
|
|
|
|
mToken(other.mToken), mScope(other.mScope), mName(other.mName), mFullName(other.mFullName),
|
|
|
|
mNameToken(other.mNameToken), mParamEnd(other.mParamEnd), mFlags(other.mFlags)
|
2018-12-21 13:51:45 +01:00
|
|
|
{
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mToken)
|
|
|
|
mToken->templateSimplifierPointer(this);
|
2018-12-21 13:51:45 +01:00
|
|
|
}
|
|
|
|
|
2023-03-07 12:24:01 +01:00
|
|
|
TemplateSimplifier::TokenAndName::TokenAndName(TokenAndName&& other) NOEXCEPT :
|
2022-08-21 17:02:03 +02:00
|
|
|
mToken(other.mToken), mScope(std::move(other.mScope)), mName(std::move(other.mName)), mFullName(std::move(other.mFullName)),
|
|
|
|
mNameToken(other.mNameToken), mParamEnd(other.mParamEnd), mFlags(other.mFlags)
|
|
|
|
{
|
|
|
|
if (mToken)
|
|
|
|
mToken->templateSimplifierPointer(this);
|
|
|
|
}
|
|
|
|
|
2018-12-21 13:51:45 +01:00
|
|
|
TemplateSimplifier::TokenAndName::~TokenAndName()
|
|
|
|
{
|
2020-05-15 09:02:59 +02:00
|
|
|
if (mToken && mToken->templateSimplifierPointers())
|
|
|
|
mToken->templateSimplifierPointers()->erase(this);
|
2018-09-01 11:26:10 +02:00
|
|
|
}
|
|
|
|
|
2023-03-08 18:59:34 +01:00
|
|
|
std::string TemplateSimplifier::TokenAndName::dump(const std::vector<std::string>& fileNames) const {
|
|
|
|
std::string ret = " <TokenAndName name=\"" + mName + "\" file=\"" + ErrorLogger::toxml(fileNames.at(mToken->fileIndex())) + "\" line=\"" + std::to_string(mToken->linenr()) + "\">\n";
|
|
|
|
for (const Token* tok = mToken; tok && !Token::Match(tok, "[;{}]"); tok = tok->next())
|
2023-03-09 21:58:33 +01:00
|
|
|
ret += " <template-token str=\"" + ErrorLogger::toxml(tok->str()) + "\"/>\n";
|
2023-03-08 18:59:34 +01:00
|
|
|
return ret + " </TokenAndName>\n";
|
|
|
|
}
|
|
|
|
|
2019-06-01 10:52:29 +02:00
|
|
|
const Token * TemplateSimplifier::TokenAndName::aliasStartToken() const
|
|
|
|
{
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mParamEnd)
|
|
|
|
return mParamEnd->tokAt(4);
|
2019-06-01 10:52:29 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token * TemplateSimplifier::TokenAndName::aliasEndToken() const
|
|
|
|
{
|
|
|
|
if (aliasStartToken())
|
|
|
|
return Token::findsimplematch(aliasStartToken(), ";");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TemplateSimplifier::TokenAndName::isAliasToken(const Token *tok) const
|
|
|
|
{
|
|
|
|
const Token *end = aliasEndToken();
|
|
|
|
|
|
|
|
for (const Token *tok1 = aliasStartToken(); tok1 != end; tok1 = tok1->next()) {
|
|
|
|
if (tok1 == tok)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-07 12:12:31 +01:00
|
|
|
TemplateSimplifier::TemplateSimplifier(Tokenizer &tokenizer)
|
|
|
|
: mTokenizer(tokenizer), mTokenList(mTokenizer.list), mSettings(*mTokenizer.mSettings),
|
|
|
|
mErrorLogger(mTokenizer.mErrorLogger), mChanged(false)
|
2021-08-07 20:51:18 +02:00
|
|
|
{}
|
2018-09-01 11:26:10 +02:00
|
|
|
|
|
|
|
TemplateSimplifier::~TemplateSimplifier()
|
2021-08-07 20:51:18 +02:00
|
|
|
{}
|
2018-09-01 11:26:10 +02:00
|
|
|
|
|
|
|
void TemplateSimplifier::checkComplicatedSyntaxErrorsInTemplates()
|
2012-01-02 21:37:32 +01:00
|
|
|
{
|
|
|
|
// check for more complicated syntax errors when using templates..
|
2018-09-01 11:26:10 +02:00
|
|
|
for (const Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
2013-03-14 18:52:22 +01:00
|
|
|
// skip executing scopes (ticket #3183)..
|
2015-05-28 21:38:20 +02:00
|
|
|
if (Token::simpleMatch(tok, "( {")) {
|
2013-03-14 18:52:22 +01:00
|
|
|
tok = tok->link();
|
2015-05-28 21:38:20 +02:00
|
|
|
if (!tok)
|
2015-08-19 19:11:53 +02:00
|
|
|
syntaxError(nullptr);
|
2015-05-28 21:38:20 +02:00
|
|
|
}
|
2012-01-02 21:37:32 +01:00
|
|
|
// skip executing scopes..
|
2014-04-26 18:31:56 +02:00
|
|
|
const Token *start = Tokenizer::startOfExecutableScope(tok);
|
|
|
|
if (start) {
|
|
|
|
tok = start->link();
|
2012-01-02 21:37:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip executing scopes (ticket #1985)..
|
2012-01-21 19:55:32 +01:00
|
|
|
else if (Token::simpleMatch(tok, "try {")) {
|
2012-01-02 21:37:32 +01:00
|
|
|
tok = tok->next()->link();
|
|
|
|
while (Token::simpleMatch(tok, "} catch (")) {
|
|
|
|
tok = tok->linkAt(2);
|
|
|
|
if (Token::simpleMatch(tok, ") {"))
|
|
|
|
tok = tok->next()->link();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 21:38:20 +02:00
|
|
|
if (!tok)
|
2015-08-19 19:11:53 +02:00
|
|
|
syntaxError(nullptr);
|
2012-01-02 21:37:32 +01:00
|
|
|
// not start of statement?
|
|
|
|
if (tok->previous() && !Token::Match(tok, "[;{}]"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// skip starting tokens.. ;;; typedef typename foo::bar::..
|
2019-08-27 06:37:58 +02:00
|
|
|
while (Token::Match(tok, ";|{"))
|
2012-01-02 21:37:32 +01:00
|
|
|
tok = tok->next();
|
|
|
|
while (Token::Match(tok, "typedef|typename"))
|
|
|
|
tok = tok->next();
|
|
|
|
while (Token::Match(tok, "%type% ::"))
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// template variable or type..
|
2022-01-12 22:04:28 +01:00
|
|
|
if (Token::Match(tok, "%type% <") && !Token::simpleMatch(tok, "template")) {
|
2012-01-02 21:37:32 +01:00
|
|
|
// these are used types..
|
|
|
|
std::set<std::string> usedtypes;
|
|
|
|
|
|
|
|
// parse this statement and see if the '<' and '>' are matching
|
|
|
|
unsigned int level = 0;
|
2018-10-28 17:16:31 +01:00
|
|
|
for (const Token *tok2 = tok; tok2 && !Token::simpleMatch(tok2, ";"); tok2 = tok2->next()) {
|
2022-01-12 22:04:28 +01:00
|
|
|
if (Token::simpleMatch(tok2, "{") &&
|
|
|
|
(!Token::Match(tok2->previous(), ">|%type%") || Token::simpleMatch(tok2->link(), "} ;")))
|
2018-10-28 17:16:31 +01:00
|
|
|
break;
|
2012-01-02 21:37:32 +01:00
|
|
|
if (tok2->str() == "(")
|
|
|
|
tok2 = tok2->link();
|
|
|
|
else if (tok2->str() == "<") {
|
|
|
|
bool inclevel = false;
|
|
|
|
if (Token::simpleMatch(tok2->previous(), "operator <"))
|
|
|
|
;
|
2019-08-27 06:37:58 +02:00
|
|
|
else if (level == 0 && Token::Match(tok2->previous(), "%type%")) {
|
|
|
|
// @todo add better expression detection
|
2020-12-11 07:01:20 +01:00
|
|
|
if (!(Token::Match(tok2->next(), "*| %type%|%num% ;") ||
|
|
|
|
Token::Match(tok2->next(), "*| %type% . %type% ;"))) {
|
2019-08-27 06:37:58 +02:00
|
|
|
inclevel = true;
|
2020-12-11 07:01:20 +01:00
|
|
|
}
|
2020-08-26 07:00:58 +02:00
|
|
|
} else if (tok2->next() && tok2->next()->isStandardType() && !Token::Match(tok2->tokAt(2), "(|{"))
|
2012-01-02 21:37:32 +01:00
|
|
|
inclevel = true;
|
|
|
|
else if (Token::simpleMatch(tok2, "< typename"))
|
|
|
|
inclevel = true;
|
|
|
|
else if (Token::Match(tok2->tokAt(-2), "<|, %type% <") && usedtypes.find(tok2->previous()->str()) != usedtypes.end())
|
|
|
|
inclevel = true;
|
|
|
|
else if (Token::Match(tok2, "< %type%") && usedtypes.find(tok2->next()->str()) != usedtypes.end())
|
|
|
|
inclevel = true;
|
|
|
|
else if (Token::Match(tok2, "< %type%")) {
|
|
|
|
// is the next token a type and not a variable/constant?
|
|
|
|
// assume it's a type if there comes another "<"
|
|
|
|
const Token *tok3 = tok2->next();
|
|
|
|
while (Token::Match(tok3, "%type% ::"))
|
|
|
|
tok3 = tok3->tokAt(2);
|
|
|
|
if (Token::Match(tok3, "%type% <"))
|
|
|
|
inclevel = true;
|
2019-09-20 12:35:01 +02:00
|
|
|
} else if (tok2->strAt(-1) == ">")
|
|
|
|
syntaxError(tok);
|
2012-01-02 21:37:32 +01:00
|
|
|
|
|
|
|
if (inclevel) {
|
|
|
|
++level;
|
|
|
|
if (Token::Match(tok2->tokAt(-2), "<|, %type% <"))
|
|
|
|
usedtypes.insert(tok2->previous()->str());
|
|
|
|
}
|
|
|
|
} else if (tok2->str() == ">") {
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
} else if (tok2->str() == ">>") {
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
}
|
|
|
|
}
|
2015-08-19 19:11:53 +02:00
|
|
|
if (level > 0)
|
2015-06-23 20:53:57 +02:00
|
|
|
syntaxError(tok);
|
2012-01-02 21:37:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 21:43:38 +01:00
|
|
|
|
|
|
|
unsigned int TemplateSimplifier::templateParameters(const Token *tok)
|
|
|
|
{
|
2012-08-26 09:49:38 +02:00
|
|
|
unsigned int numberOfParameters = 1;
|
2012-01-02 21:43:38 +01:00
|
|
|
|
|
|
|
if (!tok)
|
|
|
|
return 0;
|
|
|
|
if (tok->str() != "<")
|
|
|
|
return 0;
|
2019-03-31 16:29:28 +02:00
|
|
|
if (Token::Match(tok->previous(), "%var% <"))
|
|
|
|
return 0;
|
2012-01-02 21:43:38 +01:00
|
|
|
tok = tok->next();
|
2019-09-26 10:31:19 +02:00
|
|
|
if (!tok || tok->str() == ">")
|
2019-06-28 11:14:20 +02:00
|
|
|
return 0;
|
2012-01-02 21:43:38 +01:00
|
|
|
|
|
|
|
unsigned int level = 0;
|
|
|
|
|
|
|
|
while (tok) {
|
2019-04-04 06:07:49 +02:00
|
|
|
// skip template template
|
|
|
|
if (level == 0 && Token::simpleMatch(tok, "template <")) {
|
|
|
|
const Token *closing = tok->next()->findClosingBracket();
|
2019-06-28 11:14:20 +02:00
|
|
|
if (closing) {
|
|
|
|
if (closing->str() == ">>")
|
|
|
|
return numberOfParameters;
|
2019-04-04 06:07:49 +02:00
|
|
|
tok = closing->next();
|
2022-02-18 12:59:21 +01:00
|
|
|
if (!tok)
|
|
|
|
syntaxError(tok);
|
2020-08-25 07:14:44 +02:00
|
|
|
if (Token::Match(tok, ">|>>|>>="))
|
2019-06-28 11:14:20 +02:00
|
|
|
return numberOfParameters;
|
|
|
|
else if (tok->str() == ",") {
|
|
|
|
++numberOfParameters;
|
|
|
|
tok = tok->next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
2019-04-04 06:07:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-14 22:27:31 +02:00
|
|
|
// skip const/volatile
|
|
|
|
if (Token::Match(tok, "const|volatile"))
|
2012-04-27 20:51:55 +02:00
|
|
|
tok = tok->next();
|
|
|
|
|
2012-04-27 18:02:07 +02:00
|
|
|
// skip struct/union
|
2012-09-12 20:41:10 +02:00
|
|
|
if (Token::Match(tok, "struct|union"))
|
2012-04-27 18:02:07 +02:00
|
|
|
tok = tok->next();
|
|
|
|
|
2012-09-19 16:10:13 +02:00
|
|
|
// Skip '&'
|
2015-01-31 10:50:39 +01:00
|
|
|
if (Token::Match(tok, "& ::| %name%"))
|
2012-09-19 16:10:13 +02:00
|
|
|
tok = tok->next();
|
|
|
|
|
2014-12-14 13:50:32 +01:00
|
|
|
// Skip variadic types (Ticket #5774, #6059, #6172)
|
2019-09-04 08:07:30 +02:00
|
|
|
if (Token::simpleMatch(tok, "...")) {
|
2019-07-07 10:20:43 +02:00
|
|
|
if ((tok->previous()->isName() && !Token::Match(tok->tokAt(-2), "<|,|::")) ||
|
2022-10-22 00:28:33 +02:00
|
|
|
(!tok->previous()->isName() && !Token::Match(tok->previous(), ">|&|&&|*")))
|
2019-06-28 11:14:20 +02:00
|
|
|
return 0; // syntax error
|
2019-09-04 08:07:30 +02:00
|
|
|
tok = tok->next();
|
2019-06-28 11:14:20 +02:00
|
|
|
if (!tok)
|
|
|
|
return 0;
|
|
|
|
if (tok->str() == ">") {
|
|
|
|
if (level == 0)
|
|
|
|
return numberOfParameters;
|
|
|
|
--level;
|
2020-08-25 07:14:44 +02:00
|
|
|
} else if (tok->str() == ">>" || tok->str() == ">>=") {
|
2019-06-28 11:14:20 +02:00
|
|
|
if (level == 1)
|
|
|
|
return numberOfParameters;
|
2019-07-07 10:20:43 +02:00
|
|
|
level -= 2;
|
2019-06-28 11:14:20 +02:00
|
|
|
} else if (tok->str() == "," && level == 0) {
|
|
|
|
++numberOfParameters;
|
|
|
|
tok = tok->next();
|
|
|
|
continue;
|
|
|
|
}
|
2014-05-09 22:20:22 +02:00
|
|
|
}
|
|
|
|
|
2014-10-04 20:49:57 +02:00
|
|
|
// Skip '=', '?', ':'
|
2016-11-26 17:08:36 +01:00
|
|
|
if (Token::Match(tok, "=|?|:"))
|
2014-05-08 10:15:15 +02:00
|
|
|
tok = tok->next();
|
|
|
|
if (!tok)
|
|
|
|
return 0;
|
|
|
|
|
2019-05-27 20:51:52 +02:00
|
|
|
// Skip links
|
|
|
|
if (Token::Match(tok, "(|{")) {
|
2014-05-23 23:38:40 +02:00
|
|
|
tok = tok->link();
|
2014-05-24 12:32:44 +02:00
|
|
|
if (tok)
|
2014-05-23 23:38:40 +02:00
|
|
|
tok = tok->next();
|
2019-04-09 20:07:13 +02:00
|
|
|
if (!tok)
|
|
|
|
return 0;
|
2019-03-25 14:56:51 +01:00
|
|
|
if (tok->str() == ">" && level == 0)
|
|
|
|
return numberOfParameters;
|
2020-08-25 07:14:44 +02:00
|
|
|
else if ((tok->str() == ">>" || tok->str() == ">>=") && level == 1)
|
2019-06-28 11:14:20 +02:00
|
|
|
return numberOfParameters;
|
2019-11-29 09:44:27 +01:00
|
|
|
else if (tok->str() == ",") {
|
|
|
|
if (level == 0)
|
|
|
|
++numberOfParameters;
|
2019-03-25 14:56:51 +01:00
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
continue;
|
2014-05-23 23:38:40 +02:00
|
|
|
}
|
|
|
|
|
2012-01-02 21:43:38 +01:00
|
|
|
// skip std::
|
2021-01-05 16:49:08 +01:00
|
|
|
if (tok->str() == "::")
|
2012-04-15 18:15:36 +02:00
|
|
|
tok = tok->next();
|
2015-01-31 10:50:39 +01:00
|
|
|
while (Token::Match(tok, "%name% ::")) {
|
2012-01-02 21:43:38 +01:00
|
|
|
tok = tok->tokAt(2);
|
2015-04-01 12:43:03 +02:00
|
|
|
if (tok && tok->str() == "*") // Ticket #5759: Class member pointer as a template argument; skip '*'
|
2014-05-08 10:15:15 +02:00
|
|
|
tok = tok->next();
|
|
|
|
}
|
2012-01-02 21:43:38 +01:00
|
|
|
if (!tok)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// num/type ..
|
2017-02-11 10:13:41 +01:00
|
|
|
if (!tok->isNumber() && tok->tokType() != Token::eChar && !tok->isName() && !tok->isOp())
|
2012-01-02 21:43:38 +01:00
|
|
|
return 0;
|
|
|
|
tok = tok->next();
|
2012-04-26 10:58:35 +02:00
|
|
|
if (!tok)
|
|
|
|
return 0;
|
2012-01-02 21:43:38 +01:00
|
|
|
|
2012-04-27 18:02:07 +02:00
|
|
|
// * / const
|
2016-03-18 11:09:41 +01:00
|
|
|
while (Token::Match(tok, "*|&|&&|const"))
|
2012-01-02 21:43:38 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
2012-09-19 11:03:40 +02:00
|
|
|
if (!tok)
|
|
|
|
return 0;
|
|
|
|
|
2012-08-12 14:19:56 +02:00
|
|
|
// Function pointer or prototype..
|
2014-09-14 11:35:04 +02:00
|
|
|
while (Token::Match(tok, "(|[")) {
|
2015-08-19 19:11:53 +02:00
|
|
|
if (!tok->link())
|
2015-06-25 20:48:05 +02:00
|
|
|
syntaxError(tok);
|
2015-08-19 19:11:53 +02:00
|
|
|
|
2012-08-12 14:19:56 +02:00
|
|
|
tok = tok->link()->next();
|
2014-09-14 11:35:04 +02:00
|
|
|
while (Token::Match(tok, "const|volatile")) // Ticket #5786: Skip function cv-qualifiers
|
2014-05-14 22:27:31 +02:00
|
|
|
tok = tok->next();
|
|
|
|
}
|
2013-01-03 08:44:32 +01:00
|
|
|
if (!tok)
|
|
|
|
return 0;
|
2012-04-15 11:47:22 +02:00
|
|
|
|
2012-01-02 21:43:38 +01:00
|
|
|
// inner template
|
2022-12-07 09:12:46 +01:00
|
|
|
if (tok->str() == "<" && tok->previous()->isName()) {
|
2012-01-02 21:43:38 +01:00
|
|
|
++level;
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
|
2012-09-19 11:03:40 +02:00
|
|
|
if (!tok)
|
|
|
|
return 0;
|
2012-11-04 10:49:25 +01:00
|
|
|
|
2012-01-02 21:43:38 +01:00
|
|
|
// ,/>
|
2020-08-25 07:14:44 +02:00
|
|
|
while (Token::Match(tok, ">|>>|>>=")) {
|
2012-01-02 21:43:38 +01:00
|
|
|
if (level == 0)
|
2019-03-31 16:29:28 +02:00
|
|
|
return tok->str() == ">" && !Token::Match(tok->next(), "%num%") ? numberOfParameters : 0;
|
2012-01-02 21:43:38 +01:00
|
|
|
--level;
|
2020-08-25 07:14:44 +02:00
|
|
|
if (tok->str() == ">>" || tok->str() == ">>=") {
|
2012-08-26 09:49:38 +02:00
|
|
|
if (level == 0)
|
2019-03-31 16:29:28 +02:00
|
|
|
return !Token::Match(tok->next(), "%num%") ? numberOfParameters : 0;
|
2012-08-26 09:49:38 +02:00
|
|
|
--level;
|
|
|
|
}
|
2012-01-02 21:43:38 +01:00
|
|
|
tok = tok->next();
|
2012-11-04 10:49:25 +01:00
|
|
|
|
2021-05-23 10:40:09 +02:00
|
|
|
if (Token::Match(tok, "(|["))
|
2017-11-05 17:59:34 +01:00
|
|
|
tok = tok->link()->next();
|
2012-11-04 10:49:25 +01:00
|
|
|
|
2012-04-15 11:47:22 +02:00
|
|
|
if (!tok)
|
|
|
|
return 0;
|
2012-01-02 21:43:38 +01:00
|
|
|
}
|
2012-11-04 10:49:25 +01:00
|
|
|
|
2012-01-02 21:43:38 +01:00
|
|
|
if (tok->str() != ",")
|
2012-08-26 09:49:38 +02:00
|
|
|
continue;
|
|
|
|
if (level == 0)
|
|
|
|
++numberOfParameters;
|
2012-01-02 21:43:38 +01:00
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-02 21:46:43 +01:00
|
|
|
|
2019-07-07 18:33:33 +02:00
|
|
|
const Token *TemplateSimplifier::findTemplateDeclarationEnd(const Token *tok)
|
|
|
|
{
|
|
|
|
return const_cast<const Token *>(findTemplateDeclarationEnd(const_cast<Token *>(tok)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *TemplateSimplifier::findTemplateDeclarationEnd(Token *tok)
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok, "template <")) {
|
|
|
|
tok = tok->next()->findClosingBracket();
|
|
|
|
if (tok)
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Token * tok2 = tok;
|
2020-12-04 08:04:16 +01:00
|
|
|
bool in_init = false;
|
2019-07-07 18:33:33 +02:00
|
|
|
while (tok2 && !Token::Match(tok2, ";|{")) {
|
|
|
|
if (tok2->str() == "<")
|
|
|
|
tok2 = tok2->findClosingBracket();
|
|
|
|
else if (Token::Match(tok2, "(|[") && tok2->link())
|
|
|
|
tok2 = tok2->link();
|
2020-12-04 08:04:16 +01:00
|
|
|
else if (tok2->str() == ":")
|
|
|
|
in_init = true;
|
|
|
|
else if (in_init && Token::Match(tok2, "%name% (|{")) {
|
|
|
|
tok2 = tok2->linkAt(1);
|
|
|
|
if (tok2->strAt(1) == "{")
|
|
|
|
in_init = false;
|
|
|
|
}
|
2019-07-07 18:33:33 +02:00
|
|
|
if (tok2)
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
if (tok2 && tok2->str() == "{") {
|
|
|
|
tok = tok2->link();
|
|
|
|
if (tok && tok->strAt(1) == ";")
|
|
|
|
tok = tok->next();
|
|
|
|
} else if (tok2 && tok2->str() == ";")
|
|
|
|
tok = tok2;
|
|
|
|
else
|
|
|
|
tok = nullptr;
|
|
|
|
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
void TemplateSimplifier::eraseTokens(Token *begin, const Token *end)
|
|
|
|
{
|
|
|
|
if (!begin || begin == end)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (begin->next() && begin->next() != end) {
|
|
|
|
begin->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplateSimplifier::deleteToken(Token *tok)
|
|
|
|
{
|
|
|
|
if (tok->next())
|
|
|
|
tok->next()->deletePrevious();
|
|
|
|
else
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
bool TemplateSimplifier::removeTemplate(Token *tok)
|
2012-01-02 21:46:43 +01:00
|
|
|
{
|
2012-07-29 16:01:05 +02:00
|
|
|
if (!Token::simpleMatch(tok, "template <"))
|
|
|
|
return false;
|
|
|
|
|
2020-12-04 08:04:16 +01:00
|
|
|
Token *end = findTemplateDeclarationEnd(tok);
|
|
|
|
if (end && end->next()) {
|
|
|
|
eraseTokens(tok, end->next());
|
|
|
|
deleteToken(tok);
|
|
|
|
return true;
|
2012-07-29 16:01:05 +02:00
|
|
|
}
|
2012-02-18 08:51:09 +01:00
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-18 08:51:09 +01:00
|
|
|
|
2018-11-10 16:41:14 +01:00
|
|
|
bool TemplateSimplifier::getTemplateDeclarations()
|
2017-12-26 10:55:18 +01:00
|
|
|
{
|
2018-11-10 16:41:14 +01:00
|
|
|
bool codeWithTemplates = false;
|
2018-09-01 11:26:10 +02:00
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
2017-12-27 22:29:45 +01:00
|
|
|
if (!Token::simpleMatch(tok, "template <"))
|
|
|
|
continue;
|
2019-04-03 06:02:38 +02:00
|
|
|
// ignore template template parameter
|
2019-10-30 12:12:47 +01:00
|
|
|
if (tok->strAt(-1) == "<" || tok->strAt(-1) == ",")
|
2019-04-03 06:02:38 +02:00
|
|
|
continue;
|
2019-07-24 19:20:19 +02:00
|
|
|
// ignore nested template
|
|
|
|
if (tok->strAt(-1) == ">")
|
|
|
|
continue;
|
|
|
|
// skip to last nested template parameter
|
|
|
|
const Token *tok1 = tok;
|
2020-02-05 16:20:43 +01:00
|
|
|
while (tok1 && tok1->next()) {
|
2019-07-24 19:20:19 +02:00
|
|
|
const Token *closing = tok1->next()->findClosingBracket();
|
2020-02-05 16:20:43 +01:00
|
|
|
if (!Token::simpleMatch(closing, "> template <"))
|
|
|
|
break;
|
2019-07-24 19:20:19 +02:00
|
|
|
tok1 = closing->next();
|
|
|
|
}
|
2021-04-20 15:39:04 +02:00
|
|
|
if (!Token::Match(tok, "%any% %any%"))
|
2019-07-24 19:20:19 +02:00
|
|
|
syntaxError(tok);
|
2017-12-27 22:29:45 +01:00
|
|
|
if (tok->strAt(2)=="typename" &&
|
2019-09-04 08:07:30 +02:00
|
|
|
!Token::Match(tok->tokAt(3), "%name%|...|,|=|>"))
|
2017-12-27 22:29:45 +01:00
|
|
|
syntaxError(tok->next());
|
|
|
|
codeWithTemplates = true;
|
2019-07-24 19:20:19 +02:00
|
|
|
const Token * const parmEnd = tok1->next()->findClosingBracket();
|
2017-12-27 22:29:45 +01:00
|
|
|
for (const Token *tok2 = parmEnd; tok2; tok2 = tok2->next()) {
|
2019-02-11 07:45:03 +01:00
|
|
|
if (tok2->str() == "(" && tok2->link())
|
2017-12-27 22:29:45 +01:00
|
|
|
tok2 = tok2->link();
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
break;
|
2018-12-29 11:19:53 +01:00
|
|
|
// skip decltype(...)
|
2019-01-12 18:44:48 +01:00
|
|
|
else if (Token::simpleMatch(tok2, "decltype ("))
|
2018-12-29 11:19:53 +01:00
|
|
|
tok2 = tok2->linkAt(1);
|
2019-01-18 21:12:39 +01:00
|
|
|
else if (Token::Match(tok2, "{|=|;")) {
|
|
|
|
const int namepos = getTemplateNamePosition(parmEnd);
|
|
|
|
if (namepos > 0) {
|
2019-07-31 09:19:27 +02:00
|
|
|
TokenAndName decl(tok, tok->scopeInfo()->name, parmEnd->tokAt(namepos), parmEnd);
|
2019-01-18 21:12:39 +01:00
|
|
|
if (decl.isForwardDeclaration()) {
|
|
|
|
// Declaration => add to mTemplateForwardDeclarations
|
2019-09-28 20:22:46 +02:00
|
|
|
mTemplateForwardDeclarations.emplace_back(std::move(decl));
|
2019-01-18 21:12:39 +01:00
|
|
|
} else {
|
|
|
|
// Implementation => add to mTemplateDeclarations
|
2019-09-28 20:22:46 +02:00
|
|
|
mTemplateDeclarations.emplace_back(std::move(decl));
|
2019-01-18 21:12:39 +01:00
|
|
|
}
|
2020-12-04 08:04:16 +01:00
|
|
|
Token *end = findTemplateDeclarationEnd(tok);
|
|
|
|
if (end)
|
|
|
|
tok = end;
|
2019-01-18 21:12:39 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-01-02 22:05:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-10 16:41:14 +01:00
|
|
|
return codeWithTemplates;
|
2012-01-02 22:05:27 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 07:40:55 +01:00
|
|
|
void TemplateSimplifier::addInstantiation(Token *token, const std::string &scope)
|
|
|
|
{
|
|
|
|
simplifyTemplateArgs(token->tokAt(2), token->next()->findClosingBracket());
|
|
|
|
|
2020-09-04 07:08:28 +02:00
|
|
|
TokenAndName instantiation(token, scope);
|
|
|
|
|
|
|
|
// check if instantiation already exists before adding it
|
2022-12-30 15:13:47 +01:00
|
|
|
const std::list<TokenAndName>::const_iterator it = std::find(mTemplateInstantiations.cbegin(),
|
|
|
|
mTemplateInstantiations.cend(),
|
|
|
|
instantiation);
|
2020-09-04 07:08:28 +02:00
|
|
|
|
2022-12-30 15:13:47 +01:00
|
|
|
if (it == mTemplateInstantiations.cend())
|
2022-09-10 11:25:15 +02:00
|
|
|
mTemplateInstantiations.emplace_back(std::move(instantiation));
|
2019-03-03 07:40:55 +01:00
|
|
|
}
|
2012-01-02 22:05:27 +01:00
|
|
|
|
2020-11-17 06:51:32 +01:00
|
|
|
static void getFunctionArguments(const Token *nameToken, std::vector<const Token *> &args)
|
|
|
|
{
|
2020-12-01 09:43:16 +01:00
|
|
|
const Token *argToken;
|
|
|
|
|
|
|
|
if (nameToken->strAt(1) == "(")
|
|
|
|
argToken = nameToken->tokAt(2);
|
|
|
|
else if (nameToken->strAt(1) == "<") {
|
|
|
|
const Token *end = nameToken->next()->findClosingBracket();
|
|
|
|
if (end)
|
|
|
|
argToken = end->tokAt(2);
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
return;
|
2020-11-17 06:51:32 +01:00
|
|
|
|
|
|
|
if (argToken->str() == ")")
|
|
|
|
return;
|
|
|
|
|
|
|
|
args.push_back(argToken);
|
|
|
|
|
|
|
|
while ((argToken = argToken->nextArgumentBeforeCreateLinks2()))
|
|
|
|
args.push_back(argToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool areAllParamsTypes(const std::vector<const Token *> ¶ms)
|
|
|
|
{
|
|
|
|
if (params.empty())
|
|
|
|
return false;
|
|
|
|
|
2022-12-30 15:13:47 +01:00
|
|
|
return std::all_of(params.cbegin(), params.cend(), [](const Token* param) {
|
2022-10-16 13:46:26 +02:00
|
|
|
return Token::Match(param->previous(), "typename|class %name% ,|>");
|
|
|
|
});
|
2020-11-17 06:51:32 +01:00
|
|
|
}
|
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
void TemplateSimplifier::getTemplateInstantiations()
|
2012-01-02 22:05:27 +01:00
|
|
|
{
|
2019-02-14 11:48:59 +01:00
|
|
|
std::multimap<std::string, const TokenAndName *> functionNameMap;
|
|
|
|
|
|
|
|
for (const auto & decl : mTemplateDeclarations) {
|
|
|
|
if (decl.isFunction())
|
2019-08-10 08:42:12 +02:00
|
|
|
functionNameMap.insert(std::make_pair(decl.name(), &decl));
|
2019-02-14 11:48:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & decl : mTemplateForwardDeclarations) {
|
|
|
|
if (decl.isFunction())
|
2019-08-10 08:42:12 +02:00
|
|
|
functionNameMap.insert(std::make_pair(decl.name(), &decl));
|
2019-02-14 11:48:59 +01:00
|
|
|
}
|
|
|
|
|
2019-01-05 11:14:09 +01:00
|
|
|
const Token *skip = nullptr;
|
2012-01-02 22:05:27 +01:00
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
2019-06-30 09:12:18 +02:00
|
|
|
|
2012-01-02 22:05:27 +01:00
|
|
|
// template definition.. skip it
|
|
|
|
if (Token::simpleMatch(tok, "template <")) {
|
2013-07-31 10:30:20 +02:00
|
|
|
tok = tok->next()->findClosingBracket();
|
2012-01-02 22:05:27 +01:00
|
|
|
if (!tok)
|
|
|
|
break;
|
2019-06-01 10:52:29 +02:00
|
|
|
|
2019-01-18 21:12:39 +01:00
|
|
|
const bool isUsing = tok->strAt(1) == "using";
|
2019-06-01 10:52:29 +02:00
|
|
|
if (isUsing && Token::Match(tok->tokAt(2), "%name% <")) {
|
2019-08-08 21:05:28 +02:00
|
|
|
// Can't have specialized type alias so ignore it
|
2019-06-16 10:09:38 +02:00
|
|
|
Token *tok2 = Token::findsimplematch(tok->tokAt(3), ";");
|
2019-06-01 10:52:29 +02:00
|
|
|
if (tok2)
|
2019-06-16 10:09:38 +02:00
|
|
|
tok = tok2;
|
2019-06-01 10:52:29 +02:00
|
|
|
} else if (tok->strAt(-1) == "<") {
|
2019-01-05 11:14:09 +01:00
|
|
|
// Don't ignore user specialization but don't consider it an instantiation.
|
|
|
|
// Instantiations in return type, function parameters, and executable code
|
|
|
|
// are not ignored.
|
2022-10-02 07:12:40 +02:00
|
|
|
const unsigned int pos = getTemplateNamePosition(tok);
|
2019-01-05 11:14:09 +01:00
|
|
|
if (pos > 0)
|
|
|
|
skip = tok->tokAt(pos);
|
|
|
|
} else {
|
|
|
|
// #7914
|
|
|
|
// Ignore template instantiations within template definitions: they will only be
|
|
|
|
// handled if the definition is actually instantiated
|
2019-07-07 18:33:33 +02:00
|
|
|
|
|
|
|
Token * tok2 = findTemplateDeclarationEnd(tok->next());
|
|
|
|
if (tok2)
|
2019-06-16 10:09:38 +02:00
|
|
|
tok = tok2;
|
2019-01-05 11:14:09 +01:00
|
|
|
}
|
2019-06-01 10:52:29 +02:00
|
|
|
} else if (Token::Match(tok, "template using %name% <")) {
|
2019-08-08 21:05:28 +02:00
|
|
|
// Can't have specialized type alias so ignore it
|
2019-06-16 10:09:38 +02:00
|
|
|
Token *tok2 = Token::findsimplematch(tok->tokAt(3), ";");
|
2019-06-01 10:52:29 +02:00
|
|
|
if (tok2)
|
2019-06-16 10:09:38 +02:00
|
|
|
tok = tok2;
|
2019-06-01 10:52:29 +02:00
|
|
|
} else if (Token::Match(tok, "using %name% <")) {
|
2019-08-08 21:05:28 +02:00
|
|
|
// Can't have specialized type alias so ignore it
|
2019-06-16 10:09:38 +02:00
|
|
|
Token *tok2 = Token::findsimplematch(tok->tokAt(2), ";");
|
2019-06-01 10:52:29 +02:00
|
|
|
if (tok2)
|
2019-06-16 10:09:38 +02:00
|
|
|
tok = tok2;
|
2019-12-12 20:50:20 +01:00
|
|
|
} else if (Token::Match(tok->previous(), "(|{|}|;|=|>|<<|:|.|*|&|return|<|,|!|[ %name% ::|<|(") ||
|
2017-12-26 10:55:18 +01:00
|
|
|
Token::Match(tok->previous(), "%type% %name% ::|<") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "[,:] private|protected|public %name% ::|<")) {
|
2019-07-31 09:19:27 +02:00
|
|
|
std::string scopeName = tok->scopeInfo()->name;
|
2019-01-23 08:53:01 +01:00
|
|
|
std::string qualification;
|
|
|
|
Token * qualificationTok = tok;
|
2017-12-26 10:55:18 +01:00
|
|
|
while (Token::Match(tok, "%name% :: %name%")) {
|
2020-09-07 17:45:19 +02:00
|
|
|
qualification += (qualification.empty() ? "" : " :: ") + tok->str();
|
2017-12-26 10:55:18 +01:00
|
|
|
tok = tok->tokAt(2);
|
|
|
|
}
|
2019-02-14 11:48:59 +01:00
|
|
|
|
2020-11-17 06:51:32 +01:00
|
|
|
// skip specialization
|
|
|
|
if (tok == skip) {
|
|
|
|
skip = nullptr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-14 11:48:59 +01:00
|
|
|
// look for function instantiation with type deduction
|
|
|
|
if (tok->strAt(1) == "(") {
|
2020-11-17 06:51:32 +01:00
|
|
|
std::vector<const Token *> instantiationArgs;
|
|
|
|
getFunctionArguments(tok, instantiationArgs);
|
|
|
|
|
2020-11-10 21:19:04 +01:00
|
|
|
std::string fullName;
|
|
|
|
if (!qualification.empty())
|
|
|
|
fullName = qualification + " :: " + tok->str();
|
|
|
|
else if (!scopeName.empty())
|
|
|
|
fullName = scopeName + " :: " + tok->str();
|
|
|
|
else
|
|
|
|
fullName = tok->str();
|
2020-11-17 06:51:32 +01:00
|
|
|
|
2019-02-14 11:48:59 +01:00
|
|
|
// get all declarations with this name
|
2022-06-08 16:58:57 +02:00
|
|
|
auto range = functionNameMap.equal_range(tok->str());
|
|
|
|
for (auto pos = range.first; pos != range.second; ++pos) {
|
2020-11-24 07:21:37 +01:00
|
|
|
// look for declaration with same qualification or constructor with same qualification
|
|
|
|
if (pos->second->fullName() == fullName ||
|
|
|
|
(pos->second->scope() == fullName && tok->str() == pos->second->name())) {
|
2020-11-17 06:51:32 +01:00
|
|
|
std::vector<const Token *> templateParams;
|
|
|
|
getTemplateParametersInDeclaration(pos->second->token()->tokAt(2), templateParams);
|
|
|
|
|
|
|
|
// todo: handle more than one template parameter
|
|
|
|
if (templateParams.size() != 1 || !areAllParamsTypes(templateParams))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<const Token *> declarationParams;
|
|
|
|
getFunctionArguments(pos->second->nameToken(), declarationParams);
|
|
|
|
|
|
|
|
// function argument counts must match
|
|
|
|
if (instantiationArgs.empty() || instantiationArgs.size() != declarationParams.size())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
size_t match = 0;
|
|
|
|
size_t argMatch = 0;
|
|
|
|
for (size_t i = 0; i < declarationParams.size(); ++i) {
|
|
|
|
// fixme: only type deducton from literals is supported
|
2022-10-02 07:12:40 +02:00
|
|
|
const bool isArgLiteral = Token::Match(instantiationArgs[i], "%num%|%str%|%char%|%bool% ,|)");
|
2020-11-17 06:51:32 +01:00
|
|
|
if (isArgLiteral && Token::Match(declarationParams[i], "const| %type% &| %name%| ,|)")) {
|
|
|
|
match++;
|
|
|
|
|
|
|
|
// check if parameter types match
|
|
|
|
if (templateParams[0]->str() == declarationParams[i]->str())
|
|
|
|
argMatch = i;
|
|
|
|
else {
|
|
|
|
// todo: check if non-template args match for function overloads
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match == declarationParams.size()) {
|
|
|
|
const Token *arg = instantiationArgs[argMatch];
|
2019-02-14 11:48:59 +01:00
|
|
|
tok->insertToken(">");
|
2020-11-17 06:51:32 +01:00
|
|
|
switch (arg->tokType()) {
|
2019-02-14 11:48:59 +01:00
|
|
|
case Token::eBoolean:
|
|
|
|
tok->insertToken("bool");
|
|
|
|
break;
|
|
|
|
case Token::eChar:
|
2020-11-17 06:51:32 +01:00
|
|
|
if (arg->isLong())
|
2019-02-14 11:48:59 +01:00
|
|
|
tok->insertToken("wchar_t");
|
|
|
|
else
|
|
|
|
tok->insertToken("char");
|
|
|
|
break;
|
|
|
|
case Token::eString:
|
|
|
|
tok->insertToken("*");
|
2020-11-17 06:51:32 +01:00
|
|
|
if (arg->isLong())
|
2019-02-14 11:48:59 +01:00
|
|
|
tok->insertToken("wchar_t");
|
|
|
|
else
|
|
|
|
tok->insertToken("char");
|
|
|
|
tok->insertToken("const");
|
|
|
|
break;
|
|
|
|
case Token::eNumber: {
|
2020-11-17 06:51:32 +01:00
|
|
|
MathLib::value num(arg->str());
|
2019-02-14 11:48:59 +01:00
|
|
|
if (num.isFloat()) {
|
|
|
|
// MathLib::getSuffix doesn't work for floating point numbers
|
2022-10-02 07:12:40 +02:00
|
|
|
const char suffix = arg->str().back();
|
2019-02-14 11:48:59 +01:00
|
|
|
if (suffix == 'f' || suffix == 'F')
|
|
|
|
tok->insertToken("float");
|
|
|
|
else if (suffix == 'l' || suffix == 'L') {
|
|
|
|
tok->insertToken("double");
|
2020-12-15 22:28:58 +01:00
|
|
|
tok->next()->isLong(true);
|
2019-02-14 11:48:59 +01:00
|
|
|
} else
|
|
|
|
tok->insertToken("double");
|
|
|
|
} else if (num.isInt()) {
|
|
|
|
std::string suffix = MathLib::getSuffix(tok->strAt(3));
|
|
|
|
if (suffix.find("LL") != std::string::npos) {
|
|
|
|
tok->insertToken("long");
|
2020-12-15 22:28:58 +01:00
|
|
|
tok->next()->isLong(true);
|
2019-02-14 11:48:59 +01:00
|
|
|
} else if (suffix.find('L') != std::string::npos)
|
|
|
|
tok->insertToken("long");
|
|
|
|
else
|
|
|
|
tok->insertToken("int");
|
|
|
|
if (suffix.find('U') != std::string::npos)
|
2020-12-15 22:28:58 +01:00
|
|
|
tok->next()->isUnsigned(true);
|
2019-02-14 11:48:59 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tok->insertToken("<");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 20:36:23 +01:00
|
|
|
if (!Token::Match(tok, "%name% <") ||
|
|
|
|
Token::Match(tok, "const_cast|dynamic_cast|reinterpret_cast|static_cast"))
|
2017-12-26 10:55:18 +01:00
|
|
|
continue;
|
2012-01-02 22:05:27 +01:00
|
|
|
|
2019-01-05 11:14:09 +01:00
|
|
|
if (tok == skip) {
|
|
|
|
skip = nullptr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-02 22:05:27 +01:00
|
|
|
// Add inner template instantiations first => go to the ">"
|
|
|
|
// and then parse backwards, adding all seen instantiations
|
2019-06-16 10:09:38 +02:00
|
|
|
Token *tok2 = tok->next()->findClosingBracket();
|
2012-01-02 22:05:27 +01:00
|
|
|
|
|
|
|
// parse backwards and add template instantiations
|
2017-12-26 10:55:18 +01:00
|
|
|
// TODO
|
2012-01-02 22:05:27 +01:00
|
|
|
for (; tok2 && tok2 != tok; tok2 = tok2->previous()) {
|
2019-06-28 11:14:20 +02:00
|
|
|
if (Token::Match(tok2, ",|< %name% <") &&
|
|
|
|
(tok2->strAt(3) == ">" || templateParameters(tok2->tokAt(2)))) {
|
2019-07-31 09:19:27 +02:00
|
|
|
addInstantiation(tok2->next(), tok->scopeInfo()->name);
|
2018-11-02 14:49:40 +01:00
|
|
|
} else if (Token::Match(tok2->next(), "class|struct"))
|
2019-06-16 10:09:38 +02:00
|
|
|
tok2->deleteNext();
|
2012-01-02 22:05:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add outer template..
|
2019-05-19 19:19:57 +02:00
|
|
|
if (templateParameters(tok->next()) || tok->strAt(2) == ">") {
|
2018-01-05 15:27:43 +01:00
|
|
|
while (true) {
|
2019-01-23 08:53:01 +01:00
|
|
|
const std::string fullName = scopeName + (scopeName.empty()?"":" :: ") +
|
|
|
|
qualification + (qualification.empty()?"":" :: ") + tok->str();
|
2022-12-30 15:13:47 +01:00
|
|
|
const std::list<TokenAndName>::const_iterator it = std::find_if(mTemplateDeclarations.cbegin(), mTemplateDeclarations.cend(), FindFullName(fullName));
|
2018-09-01 11:26:10 +02:00
|
|
|
if (it != mTemplateDeclarations.end()) {
|
2019-01-23 08:53:01 +01:00
|
|
|
// full name matches
|
2019-08-10 08:42:12 +02:00
|
|
|
addInstantiation(tok, it->scope());
|
2018-01-05 15:27:43 +01:00
|
|
|
break;
|
|
|
|
} else {
|
2019-01-23 08:53:01 +01:00
|
|
|
// full name doesn't match so try with using namespaces if available
|
|
|
|
bool found = false;
|
2019-07-31 09:19:27 +02:00
|
|
|
for (const auto & nameSpace : tok->scopeInfo()->usingNamespaces) {
|
2019-01-23 08:53:01 +01:00
|
|
|
std::string fullNameSpace = scopeName + (scopeName.empty()?"":" :: ") +
|
|
|
|
nameSpace + (qualification.empty()?"":" :: ") + qualification;
|
|
|
|
std::string newFullName = fullNameSpace + " :: " + tok->str();
|
2022-12-30 15:13:47 +01:00
|
|
|
const std::list<TokenAndName>::const_iterator it1 = std::find_if(mTemplateDeclarations.cbegin(), mTemplateDeclarations.cend(), FindFullName(newFullName));
|
2019-01-23 08:53:01 +01:00
|
|
|
if (it1 != mTemplateDeclarations.end()) {
|
|
|
|
// insert using namespace into token stream
|
|
|
|
std::string::size_type offset = 0;
|
|
|
|
std::string::size_type pos = 0;
|
2023-02-24 21:05:26 +01:00
|
|
|
while ((pos = nameSpace.find(' ', offset)) != std::string::npos) {
|
|
|
|
qualificationTok->insertToken(nameSpace.substr(offset, pos - offset), emptyString, true);
|
|
|
|
offset = pos + 1;
|
2019-01-23 08:53:01 +01:00
|
|
|
}
|
2022-07-10 10:57:29 +02:00
|
|
|
qualificationTok->insertToken(nameSpace.substr(offset), emptyString, true);
|
|
|
|
qualificationTok->insertToken("::", emptyString, true);
|
2019-08-10 08:42:12 +02:00
|
|
|
addInstantiation(tok, it1->scope());
|
2019-01-23 08:53:01 +01:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
|
2017-12-30 09:00:19 +01:00
|
|
|
if (scopeName.empty()) {
|
2019-03-01 08:18:53 +01:00
|
|
|
if (!qualification.empty())
|
2019-03-03 07:40:55 +01:00
|
|
|
addInstantiation(tok, qualification);
|
2019-03-01 08:18:53 +01:00
|
|
|
else
|
2019-07-31 09:19:27 +02:00
|
|
|
addInstantiation(tok, tok->scopeInfo()->name);
|
2017-12-30 09:00:19 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
const std::string::size_type pos = scopeName.rfind(" :: ");
|
|
|
|
scopeName = (pos == std::string::npos) ? std::string() : scopeName.substr(0,pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 22:05:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-03 22:35:06 +01:00
|
|
|
|
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
void TemplateSimplifier::useDefaultArgumentValues()
|
2012-01-03 22:35:06 +01:00
|
|
|
{
|
2019-05-28 21:32:37 +02:00
|
|
|
for (TokenAndName &declaration : mTemplateDeclarations)
|
|
|
|
useDefaultArgumentValues(declaration);
|
2014-05-08 10:15:15 +02:00
|
|
|
|
2019-05-28 21:32:37 +02:00
|
|
|
for (TokenAndName &declaration : mTemplateForwardDeclarations)
|
|
|
|
useDefaultArgumentValues(declaration);
|
2019-05-19 19:19:57 +02:00
|
|
|
}
|
2015-07-17 21:30:34 +02:00
|
|
|
|
2019-05-28 21:32:37 +02:00
|
|
|
void TemplateSimplifier::useDefaultArgumentValues(TokenAndName &declaration)
|
2019-05-19 19:19:57 +02:00
|
|
|
{
|
2019-05-28 21:32:37 +02:00
|
|
|
// Ticket #5762: Skip specialization tokens
|
2019-09-01 09:56:33 +02:00
|
|
|
if (declaration.isSpecialization() || declaration.isAlias() || declaration.isFriend())
|
2019-05-28 21:32:37 +02:00
|
|
|
return;
|
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
// template parameters with default value has syntax such as:
|
|
|
|
// x = y
|
|
|
|
// this list will contain all the '=' tokens for such arguments
|
2021-01-06 23:06:50 +01:00
|
|
|
struct Default {
|
|
|
|
Token *eq;
|
|
|
|
Token *end;
|
|
|
|
};
|
|
|
|
std::list<Default> eq;
|
2019-05-19 19:19:57 +02:00
|
|
|
// and this set the position of parameters with a default value
|
|
|
|
std::set<std::size_t> defaultedArgPos;
|
2014-05-08 10:15:15 +02:00
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
// parameter number. 1,2,3,..
|
|
|
|
std::size_t templatepar = 1;
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
// parameter depth
|
|
|
|
std::size_t templateParmDepth = 0;
|
|
|
|
|
2019-05-28 21:32:37 +02:00
|
|
|
// map type parameter name to index
|
|
|
|
std::map<std::string, unsigned int> typeParameterNames;
|
2019-05-19 19:19:57 +02:00
|
|
|
|
|
|
|
// Scan template declaration..
|
2021-08-01 10:31:36 +02:00
|
|
|
for (Token *tok = declaration.token()->next(); tok; tok = tok->next()) {
|
|
|
|
if (Token::simpleMatch(tok, "template <")) {
|
|
|
|
Token* end = tok->next()->findClosingBracket();
|
|
|
|
if (end)
|
|
|
|
tok = end;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-01 10:52:29 +02:00
|
|
|
if (tok->link() && Token::Match(tok, "{|(|[")) { // Ticket #6835
|
2019-05-19 19:19:57 +02:00
|
|
|
tok = tok->link();
|
2012-01-03 22:35:06 +01:00
|
|
|
continue;
|
2019-05-19 19:19:57 +02:00
|
|
|
}
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2019-08-04 10:24:44 +02:00
|
|
|
if (tok->str() == "<" &&
|
|
|
|
(tok->strAt(1) == ">" || (tok->previous()->isName() &&
|
|
|
|
typeParameterNames.find(tok->strAt(-1)) == typeParameterNames.end())))
|
2019-05-19 19:19:57 +02:00
|
|
|
++templateParmDepth;
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
// end of template parameters?
|
|
|
|
if (tok->str() == ">") {
|
2021-01-06 23:06:50 +01:00
|
|
|
if (templateParmDepth<2) {
|
|
|
|
if (!eq.empty())
|
|
|
|
eq.back().end = tok;
|
2019-05-19 19:19:57 +02:00
|
|
|
break;
|
2021-01-06 23:06:50 +01:00
|
|
|
} else
|
2019-05-19 19:19:57 +02:00
|
|
|
--templateParmDepth;
|
|
|
|
}
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2019-05-28 21:32:37 +02:00
|
|
|
// map type parameter name to index
|
2019-08-04 10:24:44 +02:00
|
|
|
if (Token::Match(tok, "typename|class|%type% %name% ,|>"))
|
2019-05-28 21:32:37 +02:00
|
|
|
typeParameterNames[tok->strAt(1)] = templatepar - 1;
|
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
// next template parameter
|
2021-01-06 23:06:50 +01:00
|
|
|
if (tok->str() == "," && (1 == templateParmDepth)) { // Ticket #5823: Properly count parameters
|
|
|
|
if (!eq.empty())
|
|
|
|
eq.back().end = tok;
|
2019-05-19 19:19:57 +02:00
|
|
|
++templatepar;
|
2021-01-06 23:06:50 +01:00
|
|
|
}
|
2019-05-19 19:19:57 +02:00
|
|
|
|
|
|
|
// default parameter value?
|
|
|
|
else if (Token::Match(tok, "= !!>")) {
|
|
|
|
if (defaultedArgPos.insert(templatepar).second) {
|
2022-07-28 22:53:59 +02:00
|
|
|
eq.emplace_back(Default{tok, nullptr});
|
2019-05-19 19:19:57 +02:00
|
|
|
} else {
|
|
|
|
// Ticket #5605: Syntax error (two equal signs for the same parameter), bail out
|
|
|
|
eq.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 21:32:37 +02:00
|
|
|
if (eq.empty())
|
2019-05-19 19:19:57 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// iterate through all template instantiations
|
2019-05-28 21:32:37 +02:00
|
|
|
for (const TokenAndName &instantiation : mTemplateInstantiations) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (declaration.fullName() != instantiation.fullName())
|
2019-05-19 19:19:57 +02:00
|
|
|
continue;
|
|
|
|
|
2019-05-28 21:32:37 +02:00
|
|
|
// instantiation arguments..
|
|
|
|
std::vector<std::vector<const Token *>> instantiationArgs;
|
|
|
|
std::size_t index = 0;
|
2019-08-10 08:42:12 +02:00
|
|
|
const Token *end = instantiation.token()->next()->findClosingBracket();
|
2019-05-28 21:32:37 +02:00
|
|
|
if (!end)
|
|
|
|
continue;
|
2019-08-10 08:42:12 +02:00
|
|
|
if (end != instantiation.token()->tokAt(2))
|
2019-06-01 10:52:29 +02:00
|
|
|
instantiationArgs.resize(1);
|
2019-08-10 08:42:12 +02:00
|
|
|
for (const Token *tok1 = instantiation.token()->tokAt(2); tok1 && tok1 != end; tok1 = tok1->next()) {
|
2019-06-01 10:52:29 +02:00
|
|
|
if (tok1->link() && Token::Match(tok1, "{|(|[")) {
|
2019-05-28 21:32:37 +02:00
|
|
|
const Token *endLink = tok1->link();
|
|
|
|
do {
|
|
|
|
instantiationArgs[index].push_back(tok1);
|
|
|
|
tok1 = tok1->next();
|
|
|
|
} while (tok1 && tok1 != endLink);
|
2019-07-02 11:40:57 +02:00
|
|
|
instantiationArgs[index].push_back(tok1);
|
2019-08-04 10:24:44 +02:00
|
|
|
} else if (tok1->str() == "<" &&
|
|
|
|
(tok1->strAt(1) == ">" || (tok1->previous()->isName() &&
|
|
|
|
typeParameterNames.find(tok1->strAt(-1)) == typeParameterNames.end()))) {
|
2019-05-28 21:32:37 +02:00
|
|
|
const Token *endLink = tok1->findClosingBracket();
|
|
|
|
do {
|
|
|
|
instantiationArgs[index].push_back(tok1);
|
|
|
|
tok1 = tok1->next();
|
|
|
|
} while (tok1 && tok1 != endLink);
|
2019-07-02 11:40:57 +02:00
|
|
|
instantiationArgs[index].push_back(tok1);
|
2019-05-28 21:32:37 +02:00
|
|
|
} else if (tok1->str() == ",") {
|
|
|
|
++index;
|
|
|
|
instantiationArgs.resize(index + 1);
|
|
|
|
} else
|
|
|
|
instantiationArgs[index].push_back(tok1);
|
|
|
|
}
|
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
// count the parameters..
|
2019-08-10 08:42:12 +02:00
|
|
|
Token *tok = instantiation.token()->next();
|
2019-06-01 10:52:29 +02:00
|
|
|
unsigned int usedpar = templateParameters(tok);
|
2019-08-04 10:24:44 +02:00
|
|
|
Token *instantiationEnd = tok->findClosingBracket();
|
|
|
|
tok = instantiationEnd;
|
2019-05-19 19:19:57 +02:00
|
|
|
|
|
|
|
if (tok && tok->str() == ">") {
|
|
|
|
tok = tok->previous();
|
2022-12-30 15:13:47 +01:00
|
|
|
std::list<Default>::const_iterator it = eq.cbegin();
|
|
|
|
for (std::size_t i = (templatepar - eq.size()); it != eq.cend() && i < usedpar; ++i)
|
2019-05-19 19:19:57 +02:00
|
|
|
++it;
|
2019-06-01 10:52:29 +02:00
|
|
|
int count = 0;
|
2022-12-30 15:13:47 +01:00
|
|
|
while (it != eq.cend()) {
|
2021-01-06 23:06:50 +01:00
|
|
|
// check for end
|
|
|
|
if (!it->end) {
|
2023-03-07 12:12:31 +01:00
|
|
|
if (mSettings.debugwarnings) {
|
2021-01-06 23:06:50 +01:00
|
|
|
const std::list<const Token*> locationList(1, it->eq);
|
2023-03-07 12:12:31 +01:00
|
|
|
const ErrorMessage errmsg(locationList, &mTokenizer.list,
|
2021-01-06 23:06:50 +01:00
|
|
|
Severity::debug,
|
|
|
|
"noparamend",
|
|
|
|
"TemplateSimplifier couldn't find end of template parameter.",
|
2021-02-24 22:00:06 +01:00
|
|
|
Certainty::normal);
|
2023-03-07 12:12:31 +01:00
|
|
|
if (mErrorLogger && mSettings.severity.isEnabled(Severity::debug))
|
2022-02-22 09:51:44 +01:00
|
|
|
mErrorLogger->reportErr(errmsg);
|
2021-01-06 23:06:50 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-06-01 10:52:29 +02:00
|
|
|
if ((usedpar + count) && usedpar <= (instantiationArgs.size() + count)) {
|
2012-01-03 22:35:06 +01:00
|
|
|
tok->insertToken(",");
|
|
|
|
tok = tok->next();
|
2019-05-19 19:19:57 +02:00
|
|
|
}
|
|
|
|
std::stack<Token *> links;
|
2021-01-06 23:06:50 +01:00
|
|
|
for (const Token* from = it->eq->next(); from && from != it->end; from = from->next()) {
|
2019-05-28 21:32:37 +02:00
|
|
|
auto entry = typeParameterNames.find(from->str());
|
|
|
|
if (entry != typeParameterNames.end() && entry->second < instantiationArgs.size()) {
|
|
|
|
for (const Token *tok1 : instantiationArgs[entry->second]) {
|
|
|
|
tok->insertToken(tok1->str(), tok1->originalName());
|
|
|
|
tok = tok->next();
|
2019-06-01 10:52:29 +02:00
|
|
|
|
|
|
|
if (Token::Match(tok, "(|[|{"))
|
|
|
|
links.push(tok);
|
|
|
|
else if (!links.empty() && Token::Match(tok, ")|]|}")) {
|
|
|
|
Token::createMutualLinks(links.top(), tok);
|
|
|
|
links.pop();
|
|
|
|
}
|
2019-05-28 21:32:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tok->insertToken(from->str(), from->originalName());
|
|
|
|
tok = tok->next();
|
2019-06-01 10:52:29 +02:00
|
|
|
|
|
|
|
if (Token::Match(tok, "(|[|{"))
|
|
|
|
links.push(tok);
|
|
|
|
else if (!links.empty() && Token::Match(tok, ")|]|}")) {
|
|
|
|
Token::createMutualLinks(links.top(), tok);
|
|
|
|
links.pop();
|
|
|
|
}
|
2012-01-03 22:35:06 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-19 19:19:57 +02:00
|
|
|
++it;
|
2019-06-01 10:52:29 +02:00
|
|
|
count++;
|
|
|
|
usedpar++;
|
2012-01-03 22:35:06 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-04 10:24:44 +02:00
|
|
|
|
2019-08-10 08:42:12 +02:00
|
|
|
simplifyTemplateArgs(instantiation.token()->next(), instantiationEnd);
|
2019-05-19 19:19:57 +02:00
|
|
|
}
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2021-01-06 23:06:50 +01:00
|
|
|
for (const auto & entry : eq) {
|
|
|
|
Token *const eqtok = entry.eq;
|
2019-05-19 19:19:57 +02:00
|
|
|
Token *tok2;
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (tok2 = eqtok->next(); tok2; tok2 = tok2->next()) {
|
|
|
|
if (Token::Match(tok2, ";|)|}|]")) { // bail out #6607
|
|
|
|
tok2 = nullptr;
|
|
|
|
break;
|
2012-01-15 12:14:47 +01:00
|
|
|
}
|
2019-05-19 19:19:57 +02:00
|
|
|
if (Token::Match(tok2, "(|{|["))
|
|
|
|
tok2 = tok2->link();
|
2019-07-01 07:01:14 +02:00
|
|
|
else if (Token::Match(tok2, "%type% <") && (tok2->strAt(2) == ">" || templateParameters(tok2->next()))) {
|
2022-10-02 07:12:40 +02:00
|
|
|
const std::list<TokenAndName>::iterator ti = std::find_if(mTemplateInstantiations.begin(),
|
|
|
|
mTemplateInstantiations.end(),
|
|
|
|
FindToken(tok2));
|
2019-05-19 19:19:57 +02:00
|
|
|
if (ti != mTemplateInstantiations.end())
|
|
|
|
mTemplateInstantiations.erase(ti);
|
|
|
|
++indentlevel;
|
|
|
|
} else if (indentlevel > 0 && tok2->str() == ">")
|
|
|
|
--indentlevel;
|
|
|
|
else if (indentlevel == 0 && Token::Match(tok2, ",|>"))
|
|
|
|
break;
|
|
|
|
if (indentlevel < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// something went wrong, don't call eraseTokens()
|
|
|
|
// with a nullptr "end" parameter (=all remaining tokens).
|
|
|
|
if (!tok2)
|
|
|
|
continue;
|
2015-01-18 01:39:53 +01:00
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
// don't strip args from uninstantiated templates
|
2022-10-02 07:12:40 +02:00
|
|
|
const std::list<TokenAndName>::iterator ti2 = std::find_if(mTemplateInstantiations.begin(),
|
|
|
|
mTemplateInstantiations.end(),
|
|
|
|
FindName(declaration.name()));
|
2018-10-14 16:57:07 +02:00
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
if (ti2 == mTemplateInstantiations.end())
|
|
|
|
continue;
|
2018-10-14 16:57:07 +02:00
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
eraseTokens(eqtok, tok2);
|
|
|
|
eqtok->deleteThis();
|
2018-12-30 21:16:11 +01:00
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
// update parameter end pointer
|
2019-08-10 08:42:12 +02:00
|
|
|
declaration.paramEnd(declaration.token()->next()->findClosingBracket());
|
2012-01-03 22:35:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
void TemplateSimplifier::simplifyTemplateAliases()
|
2017-12-29 22:47:07 +01:00
|
|
|
{
|
2019-06-12 07:44:48 +02:00
|
|
|
for (std::list<TokenAndName>::iterator it1 = mTemplateDeclarations.begin(); it1 != mTemplateDeclarations.end();) {
|
2022-10-02 07:12:40 +02:00
|
|
|
const TokenAndName &aliasDeclaration = *it1;
|
2017-12-29 22:47:07 +01:00
|
|
|
|
2019-06-12 07:44:48 +02:00
|
|
|
if (!aliasDeclaration.isAlias()) {
|
|
|
|
++it1;
|
2019-06-01 10:52:29 +02:00
|
|
|
continue;
|
2019-06-12 07:44:48 +02:00
|
|
|
}
|
2019-06-01 10:52:29 +02:00
|
|
|
|
2017-12-29 22:47:07 +01:00
|
|
|
// alias parameters..
|
|
|
|
std::vector<const Token *> aliasParameters;
|
2019-08-10 08:42:12 +02:00
|
|
|
getTemplateParametersInDeclaration(aliasDeclaration.token()->tokAt(2), aliasParameters);
|
2017-12-29 22:47:07 +01:00
|
|
|
std::map<std::string, unsigned int> aliasParameterNames;
|
|
|
|
for (unsigned int argnr = 0; argnr < aliasParameters.size(); ++argnr)
|
|
|
|
aliasParameterNames[aliasParameters[argnr]->str()] = argnr;
|
|
|
|
|
|
|
|
// Look for alias usages..
|
2019-06-12 07:44:48 +02:00
|
|
|
bool found = false;
|
|
|
|
for (std::list<TokenAndName>::iterator it2 = mTemplateInstantiations.begin(); it2 != mTemplateInstantiations.end();) {
|
2022-10-02 07:12:40 +02:00
|
|
|
const TokenAndName &aliasUsage = *it2;
|
2019-08-10 08:42:12 +02:00
|
|
|
if (!aliasUsage.token() || aliasUsage.fullName() != aliasDeclaration.fullName()) {
|
2019-06-12 07:44:48 +02:00
|
|
|
++it2;
|
2017-12-29 22:47:07 +01:00
|
|
|
continue;
|
2019-06-12 07:44:48 +02:00
|
|
|
}
|
2019-06-01 10:52:29 +02:00
|
|
|
|
|
|
|
// don't recurse
|
2019-08-10 08:42:12 +02:00
|
|
|
if (aliasDeclaration.isAliasToken(aliasUsage.token())) {
|
2019-06-12 07:44:48 +02:00
|
|
|
++it2;
|
2019-06-01 10:52:29 +02:00
|
|
|
continue;
|
2019-06-12 07:44:48 +02:00
|
|
|
}
|
2019-06-01 10:52:29 +02:00
|
|
|
|
2017-12-29 22:47:07 +01:00
|
|
|
std::vector<std::pair<Token *, Token *>> args;
|
2019-08-10 08:42:12 +02:00
|
|
|
Token *tok2 = aliasUsage.token()->tokAt(2);
|
2017-12-29 22:47:07 +01:00
|
|
|
while (tok2) {
|
|
|
|
Token * const start = tok2;
|
2018-01-05 16:10:13 +01:00
|
|
|
while (tok2 && !Token::Match(tok2, "[,>;{}]")) {
|
2019-03-30 06:53:17 +01:00
|
|
|
if (tok2->link() && Token::Match(tok2, "(|["))
|
2018-01-05 16:10:13 +01:00
|
|
|
tok2 = tok2->link();
|
2020-02-09 11:19:36 +01:00
|
|
|
else if (tok2->str() == "<") {
|
2019-03-30 06:53:17 +01:00
|
|
|
tok2 = tok2->findClosingBracket();
|
2020-02-09 11:19:36 +01:00
|
|
|
if (!tok2)
|
|
|
|
break;
|
|
|
|
}
|
2017-12-29 22:47:07 +01:00
|
|
|
tok2 = tok2->next();
|
2018-01-05 16:10:13 +01:00
|
|
|
}
|
2017-12-29 22:47:07 +01:00
|
|
|
|
2018-04-11 09:44:35 +02:00
|
|
|
args.emplace_back(start, tok2);
|
2017-12-29 22:47:07 +01:00
|
|
|
if (tok2 && tok2->str() == ",") {
|
|
|
|
tok2 = tok2->next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-17 07:38:07 +02:00
|
|
|
if (!tok2 || tok2->str() != ">" ||
|
|
|
|
(!aliasDeclaration.isVariadic() && (args.size() != aliasParameters.size())) ||
|
|
|
|
(aliasDeclaration.isVariadic() && (args.size() < aliasParameters.size()))) {
|
2019-06-12 07:44:48 +02:00
|
|
|
++it2;
|
2017-12-29 22:47:07 +01:00
|
|
|
continue;
|
|
|
|
}
|
2019-06-01 10:52:29 +02:00
|
|
|
|
2019-06-23 10:55:09 +02:00
|
|
|
mChanged = true;
|
|
|
|
|
2019-06-12 07:44:48 +02:00
|
|
|
// copy template-id from declaration to after instantiation
|
2019-08-10 08:42:12 +02:00
|
|
|
Token * dst = aliasUsage.token()->next()->findClosingBracket();
|
2019-06-12 07:44:48 +02:00
|
|
|
Token * end = TokenList::copyTokens(dst, aliasDeclaration.aliasStartToken(), aliasDeclaration.aliasEndToken()->previous(), false)->next();
|
|
|
|
|
|
|
|
// replace parameters
|
|
|
|
for (Token *tok1 = dst->next(); tok1 != end; tok1 = tok1->next()) {
|
|
|
|
if (!tok1->isName())
|
|
|
|
continue;
|
|
|
|
if (aliasParameterNames.find(tok1->str()) != aliasParameterNames.end()) {
|
|
|
|
const unsigned int argnr = aliasParameterNames[tok1->str()];
|
|
|
|
const Token * const fromStart = args[argnr].first;
|
|
|
|
const Token * const fromEnd = args[argnr].second->previous();
|
2019-07-23 21:28:24 +02:00
|
|
|
Token *temp = TokenList::copyTokens(tok1, fromStart, fromEnd, true);
|
|
|
|
const bool tempOK(temp && temp != tok1->next());
|
2019-06-12 07:44:48 +02:00
|
|
|
tok1->deleteThis();
|
2019-07-23 21:28:24 +02:00
|
|
|
if (tempOK)
|
|
|
|
tok1 = temp; // skip over inserted parameters
|
2019-06-12 07:44:48 +02:00
|
|
|
} else if (tok1->str() == "typename")
|
|
|
|
tok1->deleteThis();
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new instantiations
|
|
|
|
for (Token *tok1 = dst->next(); tok1 != end; tok1 = tok1->next()) {
|
|
|
|
if (!tok1->isName())
|
2018-01-05 16:10:13 +01:00
|
|
|
continue;
|
2018-01-06 08:40:06 +01:00
|
|
|
if (aliasParameterNames.find(tok2->str()) == aliasParameterNames.end()) {
|
|
|
|
// Create template instance..
|
|
|
|
if (Token::Match(tok1, "%name% <")) {
|
2022-12-30 15:13:47 +01:00
|
|
|
const std::list<TokenAndName>::const_iterator it = std::find_if(mTemplateInstantiations.cbegin(),
|
|
|
|
mTemplateInstantiations.cend(),
|
|
|
|
FindToken(tok1));
|
|
|
|
if (it != mTemplateInstantiations.cend())
|
2019-08-10 08:42:12 +02:00
|
|
|
addInstantiation(tok2, it->scope());
|
2018-01-06 08:40:06 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-05 16:10:13 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 07:44:48 +02:00
|
|
|
// erase the instantiation tokens
|
2019-08-10 08:42:12 +02:00
|
|
|
eraseTokens(aliasUsage.token()->previous(), dst->next());
|
2019-06-12 07:44:48 +02:00
|
|
|
found = true;
|
|
|
|
|
|
|
|
// erase this instantiation
|
|
|
|
it2 = mTemplateInstantiations.erase(it2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
Token *end = const_cast<Token *>(aliasDeclaration.aliasEndToken());
|
|
|
|
|
|
|
|
// remove declaration tokens
|
2019-08-10 08:42:12 +02:00
|
|
|
if (aliasDeclaration.token()->previous())
|
|
|
|
eraseTokens(aliasDeclaration.token()->previous(), end->next() ? end->next() : end);
|
2019-01-31 23:57:37 +01:00
|
|
|
else {
|
2019-06-12 07:44:48 +02:00
|
|
|
eraseTokens(mTokenList.front(), end->next() ? end->next() : end);
|
2019-01-31 23:57:37 +01:00
|
|
|
deleteToken(mTokenList.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove declaration
|
2019-06-12 07:44:48 +02:00
|
|
|
it1 = mTemplateDeclarations.erase(it1);
|
2019-05-24 10:44:08 +02:00
|
|
|
} else
|
2019-06-12 07:44:48 +02:00
|
|
|
++it1;
|
2017-12-29 22:47:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 21:56:13 +02:00
|
|
|
bool TemplateSimplifier::instantiateMatch(const Token *instance, const std::size_t numberOfArguments, bool variadic, const char patternAfter[])
|
2012-01-03 22:35:06 +01:00
|
|
|
{
|
2019-08-17 07:38:07 +02:00
|
|
|
assert(instance->strAt(1) == "<");
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2021-04-17 21:56:13 +02:00
|
|
|
auto n = templateParameters(instance->next());
|
|
|
|
if (variadic ? (n + 1 < numberOfArguments) : (numberOfArguments != n))
|
2012-01-03 22:35:06 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (patternAfter) {
|
2019-08-17 07:38:07 +02:00
|
|
|
const Token *tok = instance->next()->findClosingBracket();
|
2012-01-03 22:35:06 +01:00
|
|
|
if (!tok || !Token::Match(tok->next(), patternAfter))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing mismatching was found..
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-22 10:05:10 +01:00
|
|
|
// Utility function for TemplateSimplifier::getTemplateNamePosition, that works on template functions
|
|
|
|
bool TemplateSimplifier::getTemplateNamePositionTemplateFunction(const Token *tok, int &namepos)
|
2017-04-30 08:59:47 +02:00
|
|
|
{
|
2018-12-22 10:05:10 +01:00
|
|
|
namepos = 1;
|
2018-09-14 14:16:34 +02:00
|
|
|
while (tok && tok->next()) {
|
|
|
|
if (Token::Match(tok->next(), ";|{"))
|
|
|
|
return false;
|
2018-12-29 11:19:53 +01:00
|
|
|
// skip decltype(...)
|
2019-07-24 19:20:19 +02:00
|
|
|
else if (Token::simpleMatch(tok->next(), "decltype (")) {
|
|
|
|
const Token * end = tok->linkAt(2)->previous();
|
2020-06-29 22:13:06 +02:00
|
|
|
while (tok->next() && tok != end) {
|
2018-12-29 11:19:53 +01:00
|
|
|
tok = tok->next();
|
|
|
|
namepos++;
|
|
|
|
}
|
|
|
|
} else if (Token::Match(tok->next(), "%type% <")) {
|
2018-09-14 14:16:34 +02:00
|
|
|
const Token *closing = tok->tokAt(2)->findClosingBracket();
|
2018-12-22 10:05:10 +01:00
|
|
|
if (closing) {
|
2018-12-29 11:19:53 +01:00
|
|
|
if (closing->strAt(1) == "(" && Tokenizer::isFunctionHead(closing->next(), ";|{|:", true))
|
2018-12-22 10:05:10 +01:00
|
|
|
return true;
|
2020-06-29 22:13:06 +02:00
|
|
|
while (tok->next() && tok->next() != closing) {
|
2018-09-14 14:16:34 +02:00
|
|
|
tok = tok->next();
|
|
|
|
namepos++;
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 11:19:53 +01:00
|
|
|
} else if (Token::Match(tok->next(), "%type% (") && Tokenizer::isFunctionHead(tok->tokAt(2), ";|{|:", true)) {
|
2018-12-22 10:05:10 +01:00
|
|
|
return true;
|
2018-09-14 14:16:34 +02:00
|
|
|
}
|
|
|
|
tok = tok->next();
|
|
|
|
namepos++;
|
2017-04-30 08:59:47 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-18 21:12:39 +01:00
|
|
|
bool TemplateSimplifier::getTemplateNamePositionTemplateVariable(const Token *tok, int &namepos)
|
|
|
|
{
|
|
|
|
namepos = 1;
|
|
|
|
while (tok && tok->next()) {
|
|
|
|
if (Token::Match(tok->next(), ";|{|(|using"))
|
|
|
|
return false;
|
2019-06-28 11:14:20 +02:00
|
|
|
// skip decltype(...)
|
|
|
|
else if (Token::simpleMatch(tok->next(), "decltype (")) {
|
|
|
|
const Token * end = tok->linkAt(2);
|
2020-06-29 22:13:06 +02:00
|
|
|
while (tok->next() && tok != end) {
|
2019-06-28 11:14:20 +02:00
|
|
|
tok = tok->next();
|
|
|
|
namepos++;
|
|
|
|
}
|
|
|
|
} else if (Token::Match(tok->next(), "%type% <")) {
|
2019-01-18 21:12:39 +01:00
|
|
|
const Token *closing = tok->tokAt(2)->findClosingBracket();
|
|
|
|
if (closing) {
|
|
|
|
if (Token::Match(closing->next(), "=|;"))
|
|
|
|
return true;
|
2020-06-29 22:13:06 +02:00
|
|
|
while (tok->next() && tok->next() != closing) {
|
2019-01-18 21:12:39 +01:00
|
|
|
tok = tok->next();
|
|
|
|
namepos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (Token::Match(tok->next(), "%type% =|;")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
tok = tok->next();
|
|
|
|
namepos++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-09 08:11:59 +02:00
|
|
|
bool TemplateSimplifier::getTemplateNamePositionTemplateClass(const Token *tok, int &namepos)
|
|
|
|
{
|
2019-09-01 09:56:33 +02:00
|
|
|
if (Token::Match(tok, "> friend| class|struct|union %type% :|<|;|{|::")) {
|
|
|
|
namepos = tok->strAt(1) == "friend" ? 3 : 2;
|
|
|
|
tok = tok->tokAt(namepos);
|
2019-06-09 08:11:59 +02:00
|
|
|
while (Token::Match(tok, "%type% :: %type%") ||
|
|
|
|
(Token::Match(tok, "%type% <") && Token::Match(tok->next()->findClosingBracket(), "> :: %type%"))) {
|
|
|
|
if (tok->strAt(1) == "::") {
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
namepos += 2;
|
|
|
|
} else {
|
|
|
|
const Token *end = tok->next()->findClosingBracket();
|
|
|
|
if (!end || !end->tokAt(2)) {
|
|
|
|
// syntax error
|
|
|
|
namepos = -1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
end = end->tokAt(2);
|
|
|
|
do {
|
|
|
|
tok = tok->next();
|
|
|
|
namepos += 1;
|
|
|
|
} while (tok && tok != end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-18 21:12:39 +01:00
|
|
|
int TemplateSimplifier::getTemplateNamePosition(const Token *tok)
|
2012-01-03 22:49:50 +01:00
|
|
|
{
|
2019-06-28 11:14:20 +02:00
|
|
|
assert(tok && tok->str() == ">");
|
2019-06-09 08:11:59 +02:00
|
|
|
|
2019-05-25 10:04:01 +02:00
|
|
|
auto it = mTemplateNamePos.find(tok);
|
2023-03-07 12:12:31 +01:00
|
|
|
if (!mSettings.debugtemplate && it != mTemplateNamePos.end()) {
|
2019-05-25 10:04:01 +02:00
|
|
|
return it->second;
|
|
|
|
}
|
2012-01-03 22:49:50 +01:00
|
|
|
// get the position of the template name
|
2018-12-22 10:05:10 +01:00
|
|
|
int namepos = 0;
|
2019-06-09 08:11:59 +02:00
|
|
|
if (getTemplateNamePositionTemplateClass(tok, namepos))
|
|
|
|
;
|
2019-09-11 19:31:15 +02:00
|
|
|
else if (Token::Match(tok, "> using %name% =")) {
|
|
|
|
// types may not be defined in alias template declarations
|
|
|
|
if (!Token::Match(tok->tokAt(4), "class|struct|union|enum %name%| {"))
|
|
|
|
namepos = 2;
|
|
|
|
} else if (getTemplateNamePositionTemplateVariable(tok, namepos))
|
2019-01-18 21:12:39 +01:00
|
|
|
;
|
2018-12-22 10:05:10 +01:00
|
|
|
else if (!getTemplateNamePositionTemplateFunction(tok, namepos))
|
2019-05-25 10:04:01 +02:00
|
|
|
namepos = -1; // Name not found
|
|
|
|
mTemplateNamePos[tok] = namepos;
|
2012-01-03 22:49:50 +01:00
|
|
|
return namepos;
|
|
|
|
}
|
2012-01-05 21:45:19 +01:00
|
|
|
|
2018-11-05 19:55:21 +01:00
|
|
|
void TemplateSimplifier::addNamespace(const TokenAndName &templateDeclaration, const Token *tok)
|
|
|
|
{
|
2018-11-10 16:41:14 +01:00
|
|
|
// find start of qualification
|
|
|
|
const Token * tokStart = tok;
|
|
|
|
int offset = 0;
|
|
|
|
while (Token::Match(tokStart->tokAt(-2), "%name% ::")) {
|
|
|
|
tokStart = tokStart->tokAt(-2);
|
|
|
|
offset -= 2;
|
|
|
|
}
|
|
|
|
// decide if namespace needs to be inserted in or appended to token list
|
|
|
|
const bool insert = tokStart != tok;
|
|
|
|
|
2018-11-05 19:55:21 +01:00
|
|
|
std::string::size_type start = 0;
|
|
|
|
std::string::size_type end = 0;
|
2019-08-17 07:38:07 +02:00
|
|
|
bool inTemplate = false;
|
|
|
|
int level = 0;
|
2023-02-08 21:01:51 +01:00
|
|
|
while ((end = templateDeclaration.scope().find(' ', start)) != std::string::npos) {
|
2019-08-10 08:42:12 +02:00
|
|
|
std::string token = templateDeclaration.scope().substr(start, end - start);
|
2019-01-24 07:21:22 +01:00
|
|
|
// done if scopes overlap
|
|
|
|
if (token == tokStart->str() && tok->strAt(-1) != "::")
|
|
|
|
break;
|
2019-08-17 07:38:07 +02:00
|
|
|
if (token == "<") {
|
|
|
|
inTemplate = true;
|
|
|
|
++level;
|
|
|
|
}
|
|
|
|
if (inTemplate) {
|
|
|
|
if (insert)
|
2019-08-17 16:02:58 +02:00
|
|
|
mTokenList.back()->tokAt(offset)->str(mTokenList.back()->strAt(offset) + token);
|
2019-08-17 07:38:07 +02:00
|
|
|
else
|
|
|
|
mTokenList.back()->str(mTokenList.back()->str() + token);
|
|
|
|
if (token == ">") {
|
|
|
|
--level;
|
|
|
|
if (level == 0)
|
|
|
|
inTemplate = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (insert)
|
2022-07-10 10:57:29 +02:00
|
|
|
mTokenList.back()->tokAt(offset)->insertToken(token, emptyString);
|
2019-08-17 07:38:07 +02:00
|
|
|
else
|
2020-12-08 10:34:23 +01:00
|
|
|
mTokenList.addtoken(token, tok->linenr(), tok->column(), tok->fileIndex());
|
2019-08-17 07:38:07 +02:00
|
|
|
}
|
2018-11-05 19:55:21 +01:00
|
|
|
start = end + 1;
|
|
|
|
}
|
2019-01-24 07:21:22 +01:00
|
|
|
// don't add if it already exists
|
2019-08-10 08:42:12 +02:00
|
|
|
std::string token = templateDeclaration.scope().substr(start, end - start);
|
2019-01-24 07:21:22 +01:00
|
|
|
if (token != tokStart->str() || tok->strAt(-1) != "::") {
|
|
|
|
if (insert) {
|
2019-08-17 07:38:07 +02:00
|
|
|
if (!inTemplate)
|
2022-07-10 10:57:29 +02:00
|
|
|
mTokenList.back()->tokAt(offset)->insertToken(templateDeclaration.scope().substr(start), emptyString);
|
2019-08-17 07:38:07 +02:00
|
|
|
else
|
2019-08-17 16:02:58 +02:00
|
|
|
mTokenList.back()->tokAt(offset)->str(mTokenList.back()->strAt(offset) + templateDeclaration.scope().substr(start));
|
2022-07-10 10:57:29 +02:00
|
|
|
mTokenList.back()->tokAt(offset)->insertToken("::", emptyString);
|
2019-01-24 07:21:22 +01:00
|
|
|
} else {
|
2019-08-17 07:38:07 +02:00
|
|
|
if (!inTemplate)
|
2020-12-08 10:34:23 +01:00
|
|
|
mTokenList.addtoken(templateDeclaration.scope().substr(start), tok->linenr(), tok->column(), tok->fileIndex());
|
2019-08-17 07:38:07 +02:00
|
|
|
else
|
|
|
|
mTokenList.back()->str(mTokenList.back()->str() + templateDeclaration.scope().substr(start));
|
2020-12-08 10:34:23 +01:00
|
|
|
mTokenList.addtoken("::", tok->linenr(), tok->column(), tok->fileIndex());
|
2019-01-24 07:21:22 +01:00
|
|
|
}
|
2018-11-10 16:41:14 +01:00
|
|
|
}
|
2018-11-05 19:55:21 +01:00
|
|
|
}
|
|
|
|
|
2019-01-07 06:55:22 +01:00
|
|
|
bool TemplateSimplifier::alreadyHasNamespace(const TokenAndName &templateDeclaration, const Token *tok)
|
2018-11-05 19:55:21 +01:00
|
|
|
{
|
2021-01-02 19:10:25 +01:00
|
|
|
const std::string& scope = templateDeclaration.scope();
|
2018-11-05 19:55:21 +01:00
|
|
|
|
|
|
|
// get the length in tokens of the namespace
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
int offset = -2;
|
|
|
|
|
|
|
|
while ((pos = scope.find("::", pos)) != std::string::npos) {
|
|
|
|
offset -= 2;
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
|
2020-05-26 20:13:56 +02:00
|
|
|
return Token::simpleMatch(tok->tokAt(offset), scope.c_str(), scope.size());
|
2018-11-05 19:55:21 +01:00
|
|
|
}
|
2012-01-05 21:45:19 +01:00
|
|
|
|
2012-09-12 20:46:56 +02:00
|
|
|
void TemplateSimplifier::expandTemplate(
|
2018-10-26 14:20:13 +02:00
|
|
|
const TokenAndName &templateDeclaration,
|
|
|
|
const TokenAndName &templateInstantiation,
|
2016-10-31 17:24:15 +01:00
|
|
|
const std::vector<const Token *> &typeParametersInDeclaration,
|
2012-01-05 21:45:19 +01:00
|
|
|
const std::string &newName,
|
2018-10-14 16:57:07 +02:00
|
|
|
bool copy)
|
2012-01-05 21:45:19 +01:00
|
|
|
{
|
2017-12-26 10:55:18 +01:00
|
|
|
bool inTemplateDefinition = false;
|
2018-09-14 14:16:34 +02:00
|
|
|
const Token *startOfTemplateDeclaration = nullptr;
|
2017-12-25 08:19:46 +01:00
|
|
|
const Token *endOfTemplateDefinition = nullptr;
|
2019-08-10 08:42:12 +02:00
|
|
|
const Token * const templateDeclarationNameToken = templateDeclaration.nameToken();
|
|
|
|
const Token * const templateDeclarationToken = templateDeclaration.paramEnd();
|
2019-01-07 06:55:22 +01:00
|
|
|
const bool isClass = templateDeclaration.isClass();
|
|
|
|
const bool isFunction = templateDeclaration.isFunction();
|
2019-01-27 07:46:27 +01:00
|
|
|
const bool isSpecialization = templateDeclaration.isSpecialization();
|
2019-01-18 21:12:39 +01:00
|
|
|
const bool isVariable = templateDeclaration.isVariable();
|
2019-03-03 07:40:55 +01:00
|
|
|
struct newInstantiation {
|
2022-07-28 22:51:45 +02:00
|
|
|
newInstantiation(Token *t, std::string s) : token(t), scope(std::move(s)) {}
|
2019-03-03 07:40:55 +01:00
|
|
|
Token *token;
|
|
|
|
std::string scope;
|
|
|
|
};
|
|
|
|
std::vector<newInstantiation> newInstantiations;
|
2018-10-26 14:20:13 +02:00
|
|
|
|
2018-11-23 11:36:09 +01:00
|
|
|
// add forward declarations
|
2018-10-26 14:20:13 +02:00
|
|
|
if (copy && isClass) {
|
2022-07-10 10:57:29 +02:00
|
|
|
templateDeclaration.token()->insertToken(templateDeclarationToken->strAt(1), emptyString, true);
|
|
|
|
templateDeclaration.token()->insertToken(newName, emptyString, true);
|
|
|
|
templateDeclaration.token()->insertToken(";", emptyString, true);
|
2019-06-09 08:11:59 +02:00
|
|
|
} else if ((isFunction && (copy || isSpecialization)) ||
|
|
|
|
(isVariable && !isSpecialization) ||
|
2019-08-10 08:42:12 +02:00
|
|
|
(isClass && isSpecialization && mTemplateSpecializationMap.find(templateDeclaration.token()) != mTemplateSpecializationMap.end())) {
|
|
|
|
Token * dst = templateDeclaration.token();
|
2019-01-18 21:12:39 +01:00
|
|
|
Token * dstStart = dst->previous();
|
2018-12-29 11:19:53 +01:00
|
|
|
bool isStatic = false;
|
|
|
|
std::string scope;
|
2023-02-08 21:07:16 +01:00
|
|
|
const Token * start;
|
|
|
|
const Token * end;
|
2018-12-17 05:58:48 +01:00
|
|
|
auto it = mTemplateForwardDeclarationsMap.find(dst);
|
2021-01-09 20:00:35 +01:00
|
|
|
if (!isSpecialization && it != mTemplateForwardDeclarationsMap.end()) {
|
2018-12-17 05:58:48 +01:00
|
|
|
dst = it->second;
|
2019-01-18 21:12:39 +01:00
|
|
|
dstStart = dst->previous();
|
2018-12-17 05:58:48 +01:00
|
|
|
const Token * temp1 = dst->tokAt(1)->findClosingBracket();
|
|
|
|
const Token * temp2 = temp1->tokAt(getTemplateNamePosition(temp1));
|
|
|
|
start = temp1->next();
|
|
|
|
end = temp2->linkAt(1)->next();
|
2018-11-23 11:36:09 +01:00
|
|
|
} else {
|
2021-01-09 20:00:35 +01:00
|
|
|
if (it != mTemplateForwardDeclarationsMap.end()) {
|
2022-12-30 15:13:47 +01:00
|
|
|
const std::list<TokenAndName>::const_iterator it1 = std::find_if(mTemplateForwardDeclarations.cbegin(),
|
|
|
|
mTemplateForwardDeclarations.cend(),
|
|
|
|
FindToken(it->second));
|
|
|
|
if (it1 != mTemplateForwardDeclarations.cend())
|
2021-01-09 20:00:35 +01:00
|
|
|
mMemberFunctionsToDelete.push_back(*it1);
|
|
|
|
}
|
|
|
|
|
2019-01-27 07:46:27 +01:00
|
|
|
auto it2 = mTemplateSpecializationMap.find(dst);
|
|
|
|
if (it2 != mTemplateSpecializationMap.end()) {
|
2018-12-29 11:19:53 +01:00
|
|
|
dst = it2->second;
|
2019-01-18 21:12:39 +01:00
|
|
|
dstStart = dst->previous();
|
2018-12-29 11:19:53 +01:00
|
|
|
isStatic = dst->next()->findClosingBracket()->strAt(1) == "static";
|
|
|
|
const Token * temp = templateDeclarationNameToken;
|
|
|
|
while (Token::Match(temp->tokAt(-2), "%name% ::")) {
|
|
|
|
scope.insert(0, temp->strAt(-2) + " :: ");
|
|
|
|
temp = temp->tokAt(-2);
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
start = templateDeclarationToken->next();
|
2019-01-18 21:12:39 +01:00
|
|
|
end = templateDeclarationNameToken->next();
|
|
|
|
if (end->str() == "<")
|
|
|
|
end = end->findClosingBracket()->next();
|
|
|
|
if (end->str() == "(")
|
|
|
|
end = end->link()->next();
|
2019-06-28 11:14:20 +02:00
|
|
|
else if (isVariable && end->str() == "=") {
|
2023-02-08 21:07:16 +01:00
|
|
|
const Token *temp = end->next();
|
2019-06-28 11:14:20 +02:00
|
|
|
while (temp && temp->str() != ";") {
|
|
|
|
if (temp->link() && Token::Match(temp, "{|[|("))
|
|
|
|
temp = temp->link();
|
|
|
|
temp = temp->next();
|
|
|
|
}
|
|
|
|
end = temp;
|
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
}
|
|
|
|
unsigned int typeindentlevel = 0;
|
2019-10-30 12:12:47 +01:00
|
|
|
while (end && !(typeindentlevel == 0 && Token::Match(end, ";|{|:"))) {
|
2018-12-17 05:58:48 +01:00
|
|
|
if (Token::Match(end, "<|(|{"))
|
|
|
|
++typeindentlevel;
|
|
|
|
else if (Token::Match(end, ">|)|}"))
|
|
|
|
--typeindentlevel;
|
|
|
|
end = end->next();
|
|
|
|
}
|
|
|
|
|
2020-11-24 07:21:37 +01:00
|
|
|
if (isStatic) {
|
2022-07-10 10:57:29 +02:00
|
|
|
dst->insertToken("static", emptyString, true);
|
2020-11-24 07:21:37 +01:00
|
|
|
if (start) {
|
|
|
|
dst->previous()->linenr(start->linenr());
|
|
|
|
dst->previous()->column(start->column());
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 11:19:53 +01:00
|
|
|
|
2018-12-17 05:58:48 +01:00
|
|
|
std::map<const Token *, Token *> links;
|
2019-01-18 21:12:39 +01:00
|
|
|
bool inAssignment = false;
|
2018-12-17 05:58:48 +01:00
|
|
|
while (start && start != end) {
|
2019-01-18 21:12:39 +01:00
|
|
|
if (isVariable && start->str() == "=")
|
|
|
|
inAssignment = true;
|
2018-12-17 05:58:48 +01:00
|
|
|
unsigned int itype = 0;
|
|
|
|
while (itype < typeParametersInDeclaration.size() && typeParametersInDeclaration[itype]->str() != start->str())
|
|
|
|
++itype;
|
|
|
|
|
2021-04-22 22:23:01 +02:00
|
|
|
if (itype < typeParametersInDeclaration.size() && itype < mTypesUsedInTemplateInstantiation.size() &&
|
2019-06-01 10:52:29 +02:00
|
|
|
(!isVariable || !Token::Match(typeParametersInDeclaration[itype]->previous(), "<|, %type% >|,"))) {
|
2018-12-17 05:58:48 +01:00
|
|
|
typeindentlevel = 0;
|
2019-06-03 07:06:04 +02:00
|
|
|
std::stack<Token *> brackets1; // holds "(" and "{" tokens
|
2020-04-05 13:51:58 +02:00
|
|
|
bool pointerType = false;
|
|
|
|
Token * const dst1 = dst->previous();
|
2021-04-17 21:56:13 +02:00
|
|
|
const bool isVariadicTemplateArg = templateDeclaration.isVariadic() && itype + 1 == typeParametersInDeclaration.size();
|
|
|
|
if (isVariadicTemplateArg && Token::Match(start, "%name% ... %name%"))
|
|
|
|
start = start->tokAt(2);
|
2021-09-15 20:28:58 +02:00
|
|
|
const std::string endStr(isVariadicTemplateArg ? ">" : ",>");
|
2021-03-18 09:15:21 +01:00
|
|
|
for (const Token *typetok = mTypesUsedInTemplateInstantiation[itype].token();
|
2021-09-15 20:28:58 +02:00
|
|
|
typetok && (typeindentlevel > 0 || endStr.find(typetok->str()[0]) == std::string::npos);
|
2018-12-17 05:58:48 +01:00
|
|
|
typetok = typetok->next()) {
|
2020-04-05 13:51:58 +02:00
|
|
|
if (typeindentlevel == 0 && typetok->str() == "*")
|
|
|
|
pointerType = true;
|
2019-09-04 08:07:30 +02:00
|
|
|
if (Token::simpleMatch(typetok, "..."))
|
2018-12-17 05:58:48 +01:00
|
|
|
continue;
|
2019-07-01 07:01:14 +02:00
|
|
|
if (Token::Match(typetok, "%name% <") && (typetok->strAt(2) == ">" || templateParameters(typetok->next())))
|
2018-12-17 05:58:48 +01:00
|
|
|
++typeindentlevel;
|
|
|
|
else if (typeindentlevel > 0 && typetok->str() == ">")
|
|
|
|
--typeindentlevel;
|
2019-08-03 08:15:06 +02:00
|
|
|
else if (typetok->str() == "(")
|
|
|
|
++typeindentlevel;
|
|
|
|
else if (typetok->str() == ")")
|
|
|
|
--typeindentlevel;
|
2018-12-17 05:58:48 +01:00
|
|
|
dst->insertToken(typetok->str(), typetok->originalName(), true);
|
2020-11-24 07:21:37 +01:00
|
|
|
dst->previous()->linenr(start->linenr());
|
|
|
|
dst->previous()->column(start->column());
|
2019-06-03 07:06:04 +02:00
|
|
|
Token *previous = dst->previous();
|
|
|
|
previous->isTemplateArg(true);
|
|
|
|
previous->isSigned(typetok->isSigned());
|
|
|
|
previous->isUnsigned(typetok->isUnsigned());
|
|
|
|
previous->isLong(typetok->isLong());
|
2019-06-09 08:10:57 +02:00
|
|
|
if (Token::Match(previous, "{|(|[")) {
|
2019-06-03 07:06:04 +02:00
|
|
|
brackets1.push(previous);
|
|
|
|
} else if (previous->str() == "}") {
|
|
|
|
assert(brackets1.empty() == false);
|
|
|
|
assert(brackets1.top()->str() == "{");
|
|
|
|
Token::createMutualLinks(brackets1.top(), previous);
|
|
|
|
brackets1.pop();
|
|
|
|
} else if (previous->str() == ")") {
|
|
|
|
assert(brackets1.empty() == false);
|
|
|
|
assert(brackets1.top()->str() == "(");
|
|
|
|
Token::createMutualLinks(brackets1.top(), previous);
|
|
|
|
brackets1.pop();
|
2019-06-09 08:10:57 +02:00
|
|
|
} else if (previous->str() == "]") {
|
|
|
|
assert(brackets1.empty() == false);
|
|
|
|
assert(brackets1.top()->str() == "[");
|
|
|
|
Token::createMutualLinks(brackets1.top(), previous);
|
|
|
|
brackets1.pop();
|
2019-06-03 07:06:04 +02:00
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
}
|
2020-04-05 13:51:58 +02:00
|
|
|
if (pointerType && Token::simpleMatch(dst1, "const")) {
|
|
|
|
dst->insertToken("const", dst1->originalName(), true);
|
2020-11-24 07:21:37 +01:00
|
|
|
dst->previous()->linenr(start->linenr());
|
|
|
|
dst->previous()->column(start->column());
|
2020-04-05 13:51:58 +02:00
|
|
|
dst1->deleteThis();
|
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
} else {
|
2018-12-29 11:19:53 +01:00
|
|
|
if (isSpecialization && !copy && !scope.empty() && Token::Match(start, (scope + templateDeclarationNameToken->str()).c_str())) {
|
|
|
|
// skip scope
|
|
|
|
while (start->strAt(1) != templateDeclarationNameToken->str())
|
|
|
|
start = start->next();
|
2019-08-02 07:53:39 +02:00
|
|
|
} else if (start->str() == templateDeclarationNameToken->str() &&
|
2019-08-10 08:42:12 +02:00
|
|
|
!(templateDeclaration.isFunction() && templateDeclaration.scope().empty() &&
|
2019-08-02 07:53:39 +02:00
|
|
|
(start->strAt(-1) == "." || Token::simpleMatch(start->tokAt(-2), ". template")))) {
|
2019-01-18 21:12:39 +01:00
|
|
|
if (start->strAt(1) != "<" || Token::Match(start, newName.c_str()) || !inAssignment) {
|
2022-07-10 10:57:29 +02:00
|
|
|
dst->insertToken(newName, emptyString, true);
|
2020-11-24 07:21:37 +01:00
|
|
|
dst->previous()->linenr(start->linenr());
|
|
|
|
dst->previous()->column(start->column());
|
2019-01-18 21:12:39 +01:00
|
|
|
if (start->strAt(1) == "<")
|
|
|
|
start = start->next()->findClosingBracket();
|
2019-02-28 08:30:04 +01:00
|
|
|
} else {
|
2022-07-10 10:57:29 +02:00
|
|
|
dst->insertToken(start->str(), emptyString, true);
|
2020-11-24 07:21:37 +01:00
|
|
|
dst->previous()->linenr(start->linenr());
|
|
|
|
dst->previous()->column(start->column());
|
2019-08-10 08:42:12 +02:00
|
|
|
newInstantiations.emplace_back(dst->previous(), templateDeclaration.scope());
|
2019-02-28 08:30:04 +01:00
|
|
|
}
|
2018-11-23 11:36:09 +01:00
|
|
|
} else {
|
2018-12-17 05:58:48 +01:00
|
|
|
// check if type is a template
|
|
|
|
if (start->strAt(1) == "<") {
|
|
|
|
// get the instantiated name
|
2023-02-08 21:07:16 +01:00
|
|
|
const Token * closing = start->next()->findClosingBracket();
|
2019-04-02 09:00:11 +02:00
|
|
|
if (closing) {
|
|
|
|
std::string name;
|
|
|
|
const Token * type = start;
|
|
|
|
while (type && type != closing->next()) {
|
|
|
|
if (!name.empty())
|
|
|
|
name += " ";
|
|
|
|
name += type->str();
|
|
|
|
type = type->next();
|
|
|
|
}
|
|
|
|
// check if type is instantiated
|
2022-12-30 15:13:47 +01:00
|
|
|
if (std::any_of(mTemplateInstantiations.cbegin(), mTemplateInstantiations.cend(), [&](const TokenAndName& inst) {
|
2022-10-16 13:46:26 +02:00
|
|
|
return Token::simpleMatch(inst.token(), name.c_str(), name.size());
|
|
|
|
})) {
|
|
|
|
// use the instantiated name
|
|
|
|
dst->insertToken(name, "", true);
|
|
|
|
dst->previous()->linenr(start->linenr());
|
|
|
|
dst->previous()->column(start->column());
|
|
|
|
start = closing;
|
2018-11-27 06:17:04 +01:00
|
|
|
}
|
2018-11-23 11:36:09 +01:00
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
// just copy the token if it wasn't instantiated
|
2019-01-14 20:36:23 +01:00
|
|
|
if (start != closing) {
|
|
|
|
dst->insertToken(start->str(), start->originalName(), true);
|
2020-11-24 07:21:37 +01:00
|
|
|
dst->previous()->linenr(start->linenr());
|
|
|
|
dst->previous()->column(start->column());
|
2019-01-14 20:36:23 +01:00
|
|
|
dst->previous()->isSigned(start->isSigned());
|
|
|
|
dst->previous()->isUnsigned(start->isUnsigned());
|
|
|
|
dst->previous()->isLong(start->isLong());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dst->insertToken(start->str(), start->originalName(), true);
|
2020-11-24 07:21:37 +01:00
|
|
|
dst->previous()->linenr(start->linenr());
|
|
|
|
dst->previous()->column(start->column());
|
2019-01-14 20:36:23 +01:00
|
|
|
dst->previous()->isSigned(start->isSigned());
|
|
|
|
dst->previous()->isUnsigned(start->isUnsigned());
|
|
|
|
dst->previous()->isLong(start->isLong());
|
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
}
|
2019-04-01 08:04:47 +02:00
|
|
|
|
|
|
|
if (!start)
|
|
|
|
continue;
|
|
|
|
|
2018-12-17 05:58:48 +01:00
|
|
|
if (start->link()) {
|
|
|
|
if (Token::Match(start, "[|{|(")) {
|
|
|
|
links[start->link()] = dst->previous();
|
|
|
|
} else if (Token::Match(start, "]|}|)")) {
|
2021-08-01 10:31:36 +02:00
|
|
|
std::map<const Token *, Token *>::iterator link = links.find(start);
|
|
|
|
// make sure link is valid
|
|
|
|
if (link != links.end()) {
|
|
|
|
Token::createMutualLinks(link->second, dst->previous());
|
|
|
|
links.erase(start);
|
|
|
|
}
|
2018-11-23 11:36:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
|
|
|
|
start = start->next();
|
|
|
|
}
|
2022-07-10 10:57:29 +02:00
|
|
|
dst->insertToken(";", emptyString, true);
|
2020-11-24 07:21:37 +01:00
|
|
|
dst->previous()->linenr(dst->tokAt(-2)->linenr());
|
|
|
|
dst->previous()->column(dst->tokAt(-2)->column() + 1);
|
2019-01-18 21:12:39 +01:00
|
|
|
|
2019-06-15 09:46:32 +02:00
|
|
|
if (isVariable || isFunction)
|
2019-03-03 07:40:55 +01:00
|
|
|
simplifyTemplateArgs(dstStart, dst);
|
2018-12-17 05:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (copy && (isClass || isFunction)) {
|
|
|
|
// check if this is an explicit instantiation
|
2019-08-10 08:42:12 +02:00
|
|
|
Token * start = templateInstantiation.token();
|
2018-12-17 05:58:48 +01:00
|
|
|
while (start && !Token::Match(start->previous(), "}|;|extern"))
|
|
|
|
start = start->previous();
|
|
|
|
if (Token::Match(start, "template !!<")) {
|
|
|
|
if (start->strAt(-1) == "extern")
|
|
|
|
start = start->previous();
|
2019-01-14 20:36:23 +01:00
|
|
|
mExplicitInstantiationsToDelete.emplace_back(start, "");
|
2018-11-23 11:36:09 +01:00
|
|
|
}
|
2018-10-26 14:20:13 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
for (Token *tok3 = mTokenList.front(); tok3; tok3 = tok3 ? tok3->next() : nullptr) {
|
2017-12-25 08:19:46 +01:00
|
|
|
if (inTemplateDefinition) {
|
2019-01-26 07:08:54 +01:00
|
|
|
if (!endOfTemplateDefinition) {
|
2019-06-28 11:14:20 +02:00
|
|
|
if (isVariable) {
|
|
|
|
Token *temp = tok3->findClosingBracket();
|
|
|
|
if (temp) {
|
|
|
|
while (temp && temp->str() != ";") {
|
|
|
|
if (temp->link() && Token::Match(temp, "{|[|("))
|
|
|
|
temp = temp->link();
|
|
|
|
temp = temp->next();
|
|
|
|
}
|
|
|
|
endOfTemplateDefinition = temp;
|
|
|
|
}
|
|
|
|
} else if (tok3->str() == "{")
|
2019-01-26 07:08:54 +01:00
|
|
|
endOfTemplateDefinition = tok3->link();
|
|
|
|
}
|
2018-09-14 14:16:34 +02:00
|
|
|
if (tok3 == endOfTemplateDefinition) {
|
2017-12-25 08:19:46 +01:00
|
|
|
inTemplateDefinition = false;
|
2018-09-14 14:16:34 +02:00
|
|
|
startOfTemplateDeclaration = nullptr;
|
|
|
|
}
|
2017-12-25 08:19:46 +01:00
|
|
|
}
|
|
|
|
|
2014-11-01 22:07:24 +01:00
|
|
|
if (tok3->str()=="template") {
|
|
|
|
if (tok3->next() && tok3->next()->str()=="<") {
|
2017-12-26 10:55:18 +01:00
|
|
|
std::vector<const Token *> localTypeParametersInDeclaration;
|
2017-12-28 22:10:10 +01:00
|
|
|
getTemplateParametersInDeclaration(tok3->tokAt(2), localTypeParametersInDeclaration);
|
2023-03-17 13:51:55 +01:00
|
|
|
inTemplateDefinition = localTypeParametersInDeclaration.size() == typeParametersInDeclaration.size(); // Partial specialization
|
2017-12-26 10:55:18 +01:00
|
|
|
} else {
|
2014-11-01 22:07:24 +01:00
|
|
|
inTemplateDefinition = false; // Only template instantiation
|
2017-12-26 10:55:18 +01:00
|
|
|
}
|
2018-09-14 14:16:34 +02:00
|
|
|
startOfTemplateDeclaration = tok3;
|
2014-11-01 22:07:24 +01:00
|
|
|
}
|
2017-12-26 10:55:18 +01:00
|
|
|
if (Token::Match(tok3, "(|["))
|
2012-01-05 21:45:19 +01:00
|
|
|
tok3 = tok3->link();
|
|
|
|
|
|
|
|
// Start of template..
|
2017-12-26 10:55:18 +01:00
|
|
|
if (tok3 == templateDeclarationToken) {
|
2012-01-05 21:45:19 +01:00
|
|
|
tok3 = tok3->next();
|
2018-12-29 11:19:53 +01:00
|
|
|
if (tok3->str() == "static")
|
|
|
|
tok3 = tok3->next();
|
2012-01-05 21:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// member function implemented outside class definition
|
2014-11-01 22:07:24 +01:00
|
|
|
else if (inTemplateDefinition &&
|
2017-12-26 10:55:18 +01:00
|
|
|
Token::Match(tok3, "%name% <") &&
|
2019-08-10 08:42:12 +02:00
|
|
|
templateInstantiation.name() == tok3->str() &&
|
2021-04-17 21:56:13 +02:00
|
|
|
instantiateMatch(tok3, typeParametersInDeclaration.size(), templateDeclaration.isVariadic(), ":: ~| %name% (")) {
|
2018-01-06 15:16:03 +01:00
|
|
|
// there must be template..
|
|
|
|
bool istemplate = false;
|
2018-10-14 16:57:07 +02:00
|
|
|
Token * tok5 = nullptr; // start of function return type
|
|
|
|
for (Token *prev = tok3; prev && !Token::Match(prev, "[;{}]"); prev = prev->previous()) {
|
2018-01-06 15:16:03 +01:00
|
|
|
if (prev->str() == "template") {
|
|
|
|
istemplate = true;
|
2018-09-02 08:35:05 +02:00
|
|
|
tok5 = prev;
|
2018-01-06 15:16:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!istemplate)
|
|
|
|
continue;
|
|
|
|
|
2017-12-25 08:19:46 +01:00
|
|
|
const Token *tok4 = tok3->next()->findClosingBracket();
|
|
|
|
while (tok4 && tok4->str() != "(")
|
|
|
|
tok4 = tok4->next();
|
2017-12-26 10:55:18 +01:00
|
|
|
if (!Tokenizer::isFunctionHead(tok4, ":{", true))
|
2017-12-25 08:19:46 +01:00
|
|
|
continue;
|
2018-09-02 08:35:05 +02:00
|
|
|
// find function return type start
|
|
|
|
tok5 = tok5->next()->findClosingBracket();
|
|
|
|
if (tok5)
|
|
|
|
tok5 = tok5->next();
|
|
|
|
// copy return type
|
2019-07-24 19:20:19 +02:00
|
|
|
std::stack<Token *> brackets2; // holds "(" and "{" tokens
|
2018-09-02 08:35:05 +02:00
|
|
|
while (tok5 && tok5 != tok3) {
|
2018-09-02 17:49:13 +02:00
|
|
|
// replace name if found
|
2019-08-10 08:42:12 +02:00
|
|
|
if (Token::Match(tok5, "%name% <") && tok5->str() == templateInstantiation.name()) {
|
2018-10-14 16:57:07 +02:00
|
|
|
if (copy) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (!templateDeclaration.scope().empty() && tok5->strAt(-1) != "::")
|
2018-11-05 19:55:21 +01:00
|
|
|
addNamespace(templateDeclaration, tok5);
|
2020-12-08 10:34:23 +01:00
|
|
|
mTokenList.addtoken(newName, tok5->linenr(), tok5->column(), tok5->fileIndex());
|
2018-10-14 16:57:07 +02:00
|
|
|
tok5 = tok5->next()->findClosingBracket();
|
|
|
|
} else {
|
|
|
|
tok5->str(newName);
|
|
|
|
eraseTokens(tok5, tok5->next()->findClosingBracket()->next());
|
|
|
|
}
|
2019-03-03 19:42:46 +01:00
|
|
|
} else if (copy) {
|
|
|
|
bool added = false;
|
2019-06-28 11:14:20 +02:00
|
|
|
if (tok5->isName() && !Token::Match(tok5, "class|typename|struct") && !tok5->isStandardType()) {
|
2019-03-03 19:42:46 +01:00
|
|
|
// search for this token in the type vector
|
|
|
|
unsigned int itype = 0;
|
|
|
|
while (itype < typeParametersInDeclaration.size() && typeParametersInDeclaration[itype]->str() != tok5->str())
|
|
|
|
++itype;
|
|
|
|
|
|
|
|
// replace type with given type..
|
2021-04-22 22:23:01 +02:00
|
|
|
if (itype < typeParametersInDeclaration.size() && itype < mTypesUsedInTemplateInstantiation.size()) {
|
2019-06-01 10:52:29 +02:00
|
|
|
std::stack<Token *> brackets1; // holds "(" and "{" tokens
|
2021-03-18 09:15:21 +01:00
|
|
|
for (const Token *typetok = mTypesUsedInTemplateInstantiation[itype].token();
|
2022-02-22 09:51:44 +01:00
|
|
|
typetok && !Token::Match(typetok, ",|>");
|
2019-03-03 19:42:46 +01:00
|
|
|
typetok = typetok->next()) {
|
2019-09-04 08:07:30 +02:00
|
|
|
if (!Token::simpleMatch(typetok, "...")) {
|
2019-08-17 07:38:07 +02:00
|
|
|
mTokenList.addtoken(typetok, tok5);
|
2019-06-01 10:52:29 +02:00
|
|
|
Token *back = mTokenList.back();
|
2019-06-09 08:10:57 +02:00
|
|
|
if (Token::Match(back, "{|(|[")) {
|
2019-06-01 10:52:29 +02:00
|
|
|
brackets1.push(back);
|
|
|
|
} else if (back->str() == "}") {
|
|
|
|
assert(brackets1.empty() == false);
|
|
|
|
assert(brackets1.top()->str() == "{");
|
|
|
|
Token::createMutualLinks(brackets1.top(), back);
|
|
|
|
brackets1.pop();
|
|
|
|
} else if (back->str() == ")") {
|
|
|
|
assert(brackets1.empty() == false);
|
|
|
|
assert(brackets1.top()->str() == "(");
|
|
|
|
Token::createMutualLinks(brackets1.top(), back);
|
|
|
|
brackets1.pop();
|
2019-06-09 08:10:57 +02:00
|
|
|
} else if (back->str() == "]") {
|
|
|
|
assert(brackets1.empty() == false);
|
|
|
|
assert(brackets1.top()->str() == "[");
|
|
|
|
Token::createMutualLinks(brackets1.top(), back);
|
|
|
|
brackets1.pop();
|
2019-06-01 10:52:29 +02:00
|
|
|
}
|
|
|
|
back->isTemplateArg(true);
|
2021-03-04 08:43:22 +01:00
|
|
|
back->isUnsigned(typetok->isUnsigned());
|
|
|
|
back->isSigned(typetok->isSigned());
|
|
|
|
back->isLong(typetok->isLong());
|
2019-03-03 19:42:46 +01:00
|
|
|
added = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-24 19:20:19 +02:00
|
|
|
if (!added) {
|
2019-08-17 07:38:07 +02:00
|
|
|
mTokenList.addtoken(tok5);
|
2019-07-24 19:20:19 +02:00
|
|
|
Token *back = mTokenList.back();
|
|
|
|
if (Token::Match(back, "{|(|[")) {
|
|
|
|
brackets2.push(back);
|
|
|
|
} else if (back->str() == "}") {
|
|
|
|
assert(brackets2.empty() == false);
|
|
|
|
assert(brackets2.top()->str() == "{");
|
|
|
|
Token::createMutualLinks(brackets2.top(), back);
|
|
|
|
brackets2.pop();
|
|
|
|
} else if (back->str() == ")") {
|
|
|
|
assert(brackets2.empty() == false);
|
|
|
|
assert(brackets2.top()->str() == "(");
|
|
|
|
Token::createMutualLinks(brackets2.top(), back);
|
|
|
|
brackets2.pop();
|
|
|
|
} else if (back->str() == "]") {
|
|
|
|
assert(brackets2.empty() == false);
|
|
|
|
assert(brackets2.top()->str() == "[");
|
|
|
|
Token::createMutualLinks(brackets2.top(), back);
|
|
|
|
brackets2.pop();
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 19:42:46 +01:00
|
|
|
}
|
2018-10-14 16:57:07 +02:00
|
|
|
|
2018-09-02 08:35:05 +02:00
|
|
|
tok5 = tok5->next();
|
|
|
|
}
|
2018-11-05 19:55:21 +01:00
|
|
|
if (copy) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (!templateDeclaration.scope().empty() && tok3->strAt(-1) != "::")
|
2018-11-05 19:55:21 +01:00
|
|
|
addNamespace(templateDeclaration, tok3);
|
2020-12-08 10:34:23 +01:00
|
|
|
mTokenList.addtoken(newName, tok3->linenr(), tok3->column(), tok3->fileIndex());
|
2018-11-05 19:55:21 +01:00
|
|
|
}
|
2018-10-26 14:20:13 +02:00
|
|
|
|
2014-07-15 21:28:52 +02:00
|
|
|
while (tok3 && tok3->str() != "::")
|
2012-01-05 21:45:19 +01:00
|
|
|
tok3 = tok3->next();
|
2018-09-14 14:16:34 +02:00
|
|
|
|
2022-12-30 15:13:47 +01:00
|
|
|
const std::list<TokenAndName>::const_iterator it = std::find_if(mTemplateDeclarations.cbegin(),
|
|
|
|
mTemplateDeclarations.cend(),
|
|
|
|
FindToken(startOfTemplateDeclaration));
|
|
|
|
if (it != mTemplateDeclarations.cend())
|
2018-09-14 14:16:34 +02:00
|
|
|
mMemberFunctionsToDelete.push_back(*it);
|
2012-01-05 21:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// not part of template.. go on to next token
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2012-09-12 20:41:10 +02:00
|
|
|
std::stack<Token *> brackets; // holds "(", "[" and "{" tokens
|
2012-01-05 21:45:19 +01:00
|
|
|
|
2017-12-26 10:55:18 +01:00
|
|
|
// FIXME use full name matching somehow
|
2019-08-10 08:42:12 +02:00
|
|
|
const std::string lastName = (templateInstantiation.name().find(' ') != std::string::npos) ? templateInstantiation.name().substr(templateInstantiation.name().rfind(' ')+1) : templateInstantiation.name();
|
2018-10-26 14:20:13 +02:00
|
|
|
|
2019-08-17 07:38:07 +02:00
|
|
|
std::stack<const Token *> templates;
|
2012-01-05 21:45:19 +01:00
|
|
|
for (; tok3; tok3 = tok3->next()) {
|
2019-06-28 11:14:20 +02:00
|
|
|
if (tok3->isName() && !Token::Match(tok3, "class|typename|struct") && !tok3->isStandardType()) {
|
2012-01-05 21:45:19 +01:00
|
|
|
// search for this token in the type vector
|
|
|
|
unsigned int itype = 0;
|
|
|
|
while (itype < typeParametersInDeclaration.size() && typeParametersInDeclaration[itype]->str() != tok3->str())
|
|
|
|
++itype;
|
|
|
|
|
|
|
|
// replace type with given type..
|
2021-04-22 22:23:01 +02:00
|
|
|
if (itype < typeParametersInDeclaration.size() && itype < mTypesUsedInTemplateInstantiation.size()) {
|
2013-04-07 16:48:42 +02:00
|
|
|
unsigned int typeindentlevel = 0;
|
2019-06-01 10:52:29 +02:00
|
|
|
std::stack<Token *> brackets1; // holds "(" and "{" tokens
|
2020-04-05 13:51:58 +02:00
|
|
|
Token * const beforeTypeToken = mTokenList.back();
|
|
|
|
bool pointerType = false;
|
2021-04-17 21:56:13 +02:00
|
|
|
const bool isVariadicTemplateArg = templateDeclaration.isVariadic() && itype + 1 == typeParametersInDeclaration.size();
|
|
|
|
if (isVariadicTemplateArg && Token::Match(tok3, "%name% ... %name%"))
|
|
|
|
tok3 = tok3->tokAt(2);
|
2021-09-15 20:28:58 +02:00
|
|
|
const std::string endStr(isVariadicTemplateArg ? ">" : ",>");
|
2023-02-08 21:07:16 +01:00
|
|
|
for (Token *typetok = mTypesUsedInTemplateInstantiation[itype].token();
|
2021-09-15 20:28:58 +02:00
|
|
|
typetok && (typeindentlevel > 0 || endStr.find(typetok->str()[0]) == std::string::npos);
|
2012-01-05 21:45:19 +01:00
|
|
|
typetok = typetok->next()) {
|
2020-04-05 13:51:58 +02:00
|
|
|
if (typeindentlevel == 0 && typetok->str() == "*")
|
|
|
|
pointerType = true;
|
2019-09-04 08:07:30 +02:00
|
|
|
if (Token::simpleMatch(typetok, "..."))
|
2015-04-13 17:02:48 +02:00
|
|
|
continue;
|
2019-08-17 07:38:07 +02:00
|
|
|
if (Token::Match(typetok, "%name% <") &&
|
|
|
|
(typetok->strAt(2) == ">" || templateParameters(typetok->next()))) {
|
|
|
|
brackets1.push(typetok->next());
|
2013-04-07 16:48:42 +02:00
|
|
|
++typeindentlevel;
|
2019-08-17 07:38:07 +02:00
|
|
|
} else if (typeindentlevel > 0 && typetok->str() == ">" && brackets1.top()->str() == "<") {
|
2013-04-07 16:48:42 +02:00
|
|
|
--typeindentlevel;
|
2019-08-17 07:38:07 +02:00
|
|
|
brackets1.pop();
|
2020-06-16 19:05:55 +02:00
|
|
|
} else if (Token::Match(typetok, "const_cast|dynamic_cast|reinterpret_cast|static_cast <")) {
|
|
|
|
brackets1.push(typetok->next());
|
|
|
|
++typeindentlevel;
|
2019-08-17 07:38:07 +02:00
|
|
|
} else if (typetok->str() == "(")
|
2019-08-03 08:15:06 +02:00
|
|
|
++typeindentlevel;
|
|
|
|
else if (typetok->str() == ")")
|
|
|
|
--typeindentlevel;
|
2019-08-17 07:38:07 +02:00
|
|
|
Token *back;
|
2018-10-14 16:57:07 +02:00
|
|
|
if (copy) {
|
2019-08-17 07:38:07 +02:00
|
|
|
mTokenList.addtoken(typetok, tok3);
|
|
|
|
back = mTokenList.back();
|
|
|
|
} else
|
2023-02-08 21:07:16 +01:00
|
|
|
back = typetok;
|
2019-08-17 07:38:07 +02:00
|
|
|
if (Token::Match(back, "{|(|["))
|
|
|
|
brackets1.push(back);
|
|
|
|
else if (back->str() == "}") {
|
|
|
|
assert(brackets1.empty() == false);
|
|
|
|
assert(brackets1.top()->str() == "{");
|
|
|
|
if (copy)
|
2019-06-01 10:52:29 +02:00
|
|
|
Token::createMutualLinks(brackets1.top(), back);
|
2019-08-17 07:38:07 +02:00
|
|
|
brackets1.pop();
|
|
|
|
} else if (back->str() == ")") {
|
|
|
|
assert(brackets1.empty() == false);
|
|
|
|
assert(brackets1.top()->str() == "(");
|
|
|
|
if (copy)
|
2019-06-01 10:52:29 +02:00
|
|
|
Token::createMutualLinks(brackets1.top(), back);
|
2019-08-17 07:38:07 +02:00
|
|
|
brackets1.pop();
|
|
|
|
} else if (back->str() == "]") {
|
|
|
|
assert(brackets1.empty() == false);
|
|
|
|
assert(brackets1.top()->str() == "[");
|
|
|
|
if (copy)
|
2019-06-09 08:10:57 +02:00
|
|
|
Token::createMutualLinks(brackets1.top(), back);
|
2019-08-17 07:38:07 +02:00
|
|
|
brackets1.pop();
|
2018-10-14 16:57:07 +02:00
|
|
|
}
|
2019-08-17 07:38:07 +02:00
|
|
|
if (copy)
|
|
|
|
back->isTemplateArg(true);
|
2012-01-05 21:45:19 +01:00
|
|
|
}
|
2020-04-05 13:51:58 +02:00
|
|
|
if (pointerType && Token::simpleMatch(beforeTypeToken, "const")) {
|
|
|
|
mTokenList.addtoken(beforeTypeToken);
|
|
|
|
beforeTypeToken->deleteThis();
|
|
|
|
}
|
2012-01-05 21:45:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-23 15:41:32 +01:00
|
|
|
// replace name..
|
2018-01-15 22:03:23 +01:00
|
|
|
if (tok3->str() == lastName) {
|
2018-10-26 14:20:13 +02:00
|
|
|
if (Token::simpleMatch(tok3->next(), "<")) {
|
2018-11-07 21:25:42 +01:00
|
|
|
Token *closingBracket = tok3->next()->findClosingBracket();
|
|
|
|
if (closingBracket) {
|
|
|
|
// replace multi token name with single token name
|
|
|
|
if (tok3 == templateDeclarationNameToken ||
|
|
|
|
Token::Match(tok3, newName.c_str())) {
|
2018-10-26 14:20:13 +02:00
|
|
|
if (copy) {
|
2019-08-17 07:38:07 +02:00
|
|
|
mTokenList.addtoken(newName, tok3);
|
2018-10-26 14:20:13 +02:00
|
|
|
tok3 = closingBracket;
|
|
|
|
} else {
|
|
|
|
tok3->str(newName);
|
|
|
|
eraseTokens(tok3, closingBracket->next());
|
|
|
|
}
|
|
|
|
continue;
|
2019-08-10 08:42:12 +02:00
|
|
|
} else if (!templateDeclaration.scope().empty() &&
|
2018-11-07 21:25:42 +01:00
|
|
|
!alreadyHasNamespace(templateDeclaration, tok3) &&
|
2019-01-24 07:21:22 +01:00
|
|
|
!Token::Match(closingBracket->next(), "(|::")) {
|
2018-11-07 21:25:42 +01:00
|
|
|
if (copy)
|
|
|
|
addNamespace(templateDeclaration, tok3);
|
2018-10-26 14:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-01 09:56:33 +02:00
|
|
|
// don't modify friend
|
|
|
|
if (Token::Match(tok3->tokAt(-3), "> friend class|struct|union")) {
|
|
|
|
if (copy)
|
|
|
|
mTokenList.addtoken(tok3);
|
|
|
|
} else if (copy) {
|
2018-10-26 14:20:13 +02:00
|
|
|
// add namespace if necessary
|
2019-08-10 08:42:12 +02:00
|
|
|
if (!templateDeclaration.scope().empty() &&
|
2018-11-05 19:55:21 +01:00
|
|
|
(isClass ? tok3->strAt(1) != "(" : true)) {
|
|
|
|
addNamespace(templateDeclaration, tok3);
|
2018-10-26 14:20:13 +02:00
|
|
|
}
|
2019-08-17 07:38:07 +02:00
|
|
|
mTokenList.addtoken(newName, tok3);
|
2022-04-13 12:25:21 +02:00
|
|
|
} else if (!Token::Match(tok3->next(), ":|{|=|;|[|]"))
|
2018-10-14 16:57:07 +02:00
|
|
|
tok3->str(newName);
|
2017-04-09 16:01:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-01-05 21:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy
|
2019-08-17 07:38:07 +02:00
|
|
|
if (copy)
|
|
|
|
mTokenList.addtoken(tok3);
|
|
|
|
|
|
|
|
// look for template definitions
|
2019-08-17 16:02:58 +02:00
|
|
|
if (Token::simpleMatch(tok3, "template <")) {
|
2019-08-17 07:38:07 +02:00
|
|
|
Token * tok2 = findTemplateDeclarationEnd(tok3);
|
|
|
|
if (tok2)
|
|
|
|
templates.push(tok2);
|
|
|
|
} else if (!templates.empty() && templates.top() == tok3)
|
|
|
|
templates.pop();
|
2018-10-14 16:57:07 +02:00
|
|
|
|
2019-01-14 20:36:23 +01:00
|
|
|
if (Token::Match(tok3, "%type% <") &&
|
|
|
|
!Token::Match(tok3, "template|static_cast|const_cast|reinterpret_cast|dynamic_cast") &&
|
|
|
|
Token::Match(tok3->next()->findClosingBracket(), ">|>>")) {
|
2017-12-26 10:55:18 +01:00
|
|
|
const Token *closingBracket = tok3->next()->findClosingBracket();
|
|
|
|
if (Token::simpleMatch(closingBracket->next(), "&")) {
|
|
|
|
int num = 0;
|
|
|
|
const Token *par = tok3->next();
|
|
|
|
while (num < typeParametersInDeclaration.size() && par != closingBracket) {
|
|
|
|
const std::string pattern("[<,] " + typeParametersInDeclaration[num]->str() + " [,>]");
|
|
|
|
if (!Token::Match(par, pattern.c_str()))
|
|
|
|
break;
|
|
|
|
++num;
|
|
|
|
par = par->tokAt(2);
|
|
|
|
}
|
|
|
|
if (num < typeParametersInDeclaration.size() || par != closingBracket)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-07 19:19:04 +02:00
|
|
|
// don't add instantiations in template definitions
|
|
|
|
if (!templates.empty())
|
|
|
|
continue;
|
|
|
|
|
2019-01-14 20:36:23 +01:00
|
|
|
std::string scope;
|
2019-09-02 06:51:19 +02:00
|
|
|
const Token *prev = tok3;
|
|
|
|
for (; Token::Match(prev->tokAt(-2), "%name% ::"); prev = prev->tokAt(-2)) {
|
2019-01-14 20:36:23 +01:00
|
|
|
if (scope.empty())
|
2019-09-02 06:51:19 +02:00
|
|
|
scope = prev->strAt(-2);
|
2019-01-14 20:36:23 +01:00
|
|
|
else
|
2019-09-02 06:51:19 +02:00
|
|
|
scope = prev->strAt(-2) + " :: " + scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for global scope
|
|
|
|
if (prev->strAt(-1) != "::") {
|
|
|
|
// adjust for current scope
|
|
|
|
std::string token_scope = tok3->scopeInfo()->name;
|
2022-10-02 07:12:40 +02:00
|
|
|
const std::string::size_type end = token_scope.find_last_of(" :: ");
|
2019-09-02 06:51:19 +02:00
|
|
|
if (end != std::string::npos) {
|
|
|
|
token_scope.resize(end);
|
|
|
|
if (scope.empty())
|
|
|
|
scope = token_scope;
|
|
|
|
else
|
|
|
|
scope = token_scope + " :: " + scope;
|
|
|
|
}
|
2019-01-14 20:36:23 +01:00
|
|
|
}
|
2019-08-17 07:38:07 +02:00
|
|
|
|
2022-09-07 19:19:04 +02:00
|
|
|
if (copy)
|
2022-09-10 11:25:15 +02:00
|
|
|
newInstantiations.emplace_back(mTokenList.back(), std::move(scope));
|
2022-09-07 19:19:04 +02:00
|
|
|
else if (!inTemplateDefinition)
|
2022-09-10 11:25:15 +02:00
|
|
|
newInstantiations.emplace_back(tok3, std::move(scope));
|
2012-01-05 21:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// link() newly tokens manually
|
2018-10-14 16:57:07 +02:00
|
|
|
else if (copy) {
|
|
|
|
if (tok3->str() == "{") {
|
|
|
|
brackets.push(mTokenList.back());
|
|
|
|
} else if (tok3->str() == "(") {
|
|
|
|
brackets.push(mTokenList.back());
|
|
|
|
} else if (tok3->str() == "[") {
|
|
|
|
brackets.push(mTokenList.back());
|
|
|
|
} else if (tok3->str() == "}") {
|
2019-05-07 10:28:31 +02:00
|
|
|
assert(brackets.empty() == false);
|
|
|
|
assert(brackets.top()->str() == "{");
|
2018-10-14 16:57:07 +02:00
|
|
|
Token::createMutualLinks(brackets.top(), mTokenList.back());
|
|
|
|
if (tok3->strAt(1) == ";") {
|
|
|
|
const Token * tokSemicolon = tok3->next();
|
2020-12-08 10:34:23 +01:00
|
|
|
mTokenList.addtoken(tokSemicolon, tokSemicolon->linenr(), tokSemicolon->column(), tokSemicolon->fileIndex());
|
2018-10-14 16:57:07 +02:00
|
|
|
}
|
|
|
|
brackets.pop();
|
2021-12-16 22:32:44 +01:00
|
|
|
if (brackets.empty() && !Token::Match(tok3, "} >|,|{")) {
|
2018-10-14 16:57:07 +02:00
|
|
|
inTemplateDefinition = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (tok3->str() == ")") {
|
2019-05-07 10:28:31 +02:00
|
|
|
assert(brackets.empty() == false);
|
|
|
|
assert(brackets.top()->str() == "(");
|
2018-10-14 16:57:07 +02:00
|
|
|
Token::createMutualLinks(brackets.top(), mTokenList.back());
|
|
|
|
brackets.pop();
|
|
|
|
} else if (tok3->str() == "]") {
|
2019-05-07 10:28:31 +02:00
|
|
|
assert(brackets.empty() == false);
|
|
|
|
assert(brackets.top()->str() == "[");
|
2018-10-14 16:57:07 +02:00
|
|
|
Token::createMutualLinks(brackets.top(), mTokenList.back());
|
|
|
|
brackets.pop();
|
2012-09-12 20:41:10 +02:00
|
|
|
}
|
2012-01-05 21:45:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(brackets.empty());
|
|
|
|
}
|
2019-03-03 07:40:55 +01:00
|
|
|
|
|
|
|
// add new instantiations
|
2019-09-02 06:51:19 +02:00
|
|
|
for (const auto & inst : newInstantiations) {
|
|
|
|
simplifyTemplateArgs(inst.token->tokAt(2), inst.token->next()->findClosingBracket());
|
|
|
|
// only add recursive instantiation if its arguments are a constant expression
|
2021-09-25 11:56:39 +02:00
|
|
|
if (templateDeclaration.name() != inst.token->str() ||
|
2019-09-02 06:51:19 +02:00
|
|
|
(inst.token->tokAt(2)->isNumber() || inst.token->tokAt(2)->isStandardType()))
|
|
|
|
mTemplateInstantiations.emplace_back(inst.token, inst.scope);
|
|
|
|
}
|
2012-01-05 21:45:19 +01:00
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2015-11-25 16:46:39 +01:00
|
|
|
static bool isLowerThanLogicalAnd(const Token *lower)
|
|
|
|
{
|
|
|
|
return lower->isAssignmentOp() || Token::Match(lower, "}|;|(|[|]|)|,|?|:|%oror%|return|throw|case");
|
|
|
|
}
|
2012-03-27 21:29:50 +02:00
|
|
|
static bool isLowerThanOr(const Token* lower)
|
|
|
|
{
|
2015-11-25 16:46:39 +01:00
|
|
|
return isLowerThanLogicalAnd(lower) || lower->str() == "&&";
|
2012-03-27 21:29:50 +02:00
|
|
|
}
|
|
|
|
static bool isLowerThanXor(const Token* lower)
|
|
|
|
{
|
|
|
|
return isLowerThanOr(lower) || lower->str() == "|";
|
|
|
|
}
|
|
|
|
static bool isLowerThanAnd(const Token* lower)
|
|
|
|
{
|
|
|
|
return isLowerThanXor(lower) || lower->str() == "^";
|
|
|
|
}
|
|
|
|
static bool isLowerThanShift(const Token* lower)
|
|
|
|
{
|
2015-11-28 09:26:44 +01:00
|
|
|
return isLowerThanAnd(lower) || lower->str() == "&";
|
2012-03-27 21:29:50 +02:00
|
|
|
}
|
|
|
|
static bool isLowerThanPlusMinus(const Token* lower)
|
|
|
|
{
|
2015-11-28 09:02:26 +01:00
|
|
|
return isLowerThanShift(lower) || Token::Match(lower, "%comp%|<<|>>");
|
2012-03-27 21:29:50 +02:00
|
|
|
}
|
|
|
|
static bool isLowerThanMulDiv(const Token* lower)
|
|
|
|
{
|
2014-09-14 11:26:16 +02:00
|
|
|
return isLowerThanPlusMinus(lower) || Token::Match(lower, "+|-");
|
2012-03-27 21:29:50 +02:00
|
|
|
}
|
|
|
|
static bool isLowerEqualThanMulDiv(const Token* lower)
|
|
|
|
{
|
|
|
|
return isLowerThanMulDiv(lower) || Token::Match(lower, "[*/%]");
|
|
|
|
}
|
|
|
|
|
2012-08-20 18:10:32 +02:00
|
|
|
|
2019-07-18 14:55:30 +02:00
|
|
|
bool TemplateSimplifier::simplifyNumericCalculations(Token *tok, bool isTemplate)
|
2012-08-20 18:10:32 +02:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
// (1-2)
|
2018-06-01 23:03:53 +02:00
|
|
|
while (tok->tokAt(3) && tok->isNumber() && tok->tokAt(2)->isNumber()) { // %any% %num% %any% %num% %any%
|
|
|
|
const Token *before = tok->previous();
|
|
|
|
if (!before)
|
|
|
|
break;
|
|
|
|
const Token* op = tok->next();
|
|
|
|
const Token* after = tok->tokAt(3);
|
2018-05-29 22:41:37 +02:00
|
|
|
const std::string &num1 = op->previous()->str();
|
|
|
|
const std::string &num2 = op->next()->str();
|
|
|
|
if (Token::Match(before, "* %num% /") && (num2 != "0") && num1 == MathLib::multiply(num2, MathLib::divide(num1, num2))) {
|
2012-08-20 18:10:32 +02:00
|
|
|
// Division where result is a whole number
|
2018-05-29 22:41:37 +02:00
|
|
|
} else if (!((op->str() == "*" && (isLowerThanMulDiv(before) || before->str() == "*") && isLowerEqualThanMulDiv(after)) || // associative
|
|
|
|
(Token::Match(op, "[/%]") && isLowerThanMulDiv(before) && isLowerEqualThanMulDiv(after)) || // NOT associative
|
|
|
|
(Token::Match(op, "[+-]") && isLowerThanMulDiv(before) && isLowerThanMulDiv(after)) || // Only partially (+) associative, but handled later
|
|
|
|
(Token::Match(op, ">>|<<") && isLowerThanShift(before) && isLowerThanPlusMinus(after)) || // NOT associative
|
|
|
|
(op->str() == "&" && isLowerThanShift(before) && isLowerThanShift(after)) || // associative
|
|
|
|
(op->str() == "^" && isLowerThanAnd(before) && isLowerThanAnd(after)) || // associative
|
|
|
|
(op->str() == "|" && isLowerThanXor(before) && isLowerThanXor(after)) || // associative
|
|
|
|
(op->str() == "&&" && isLowerThanOr(before) && isLowerThanOr(after)) ||
|
|
|
|
(op->str() == "||" && isLowerThanLogicalAnd(before) && isLowerThanLogicalAnd(after))))
|
2012-08-20 18:10:32 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Don't simplify "%num% / 0"
|
2019-07-18 14:55:30 +02:00
|
|
|
if (Token::Match(op, "[/%] 0")) {
|
|
|
|
if (isTemplate) throw InternalError(op, "Instantiation error: Divide by zero in template instantiation.", InternalError::INSTANTIATION);
|
|
|
|
else return ret;
|
|
|
|
}
|
2012-08-20 18:10:32 +02:00
|
|
|
|
|
|
|
// Integer operations
|
|
|
|
if (Token::Match(op, ">>|<<|&|^|%or%")) {
|
2015-11-27 14:16:49 +01:00
|
|
|
// Don't simplify if operand is negative, shifting with negative
|
|
|
|
// operand is UB. Bitmasking with negative operand is implementation
|
|
|
|
// defined behaviour.
|
2018-06-01 23:03:53 +02:00
|
|
|
if (MathLib::isNegative(num1) || MathLib::isNegative(num2))
|
|
|
|
break;
|
2015-11-27 14:16:49 +01:00
|
|
|
|
2018-05-29 22:41:37 +02:00
|
|
|
const MathLib::value v1(num1);
|
|
|
|
const MathLib::value v2(num2);
|
2016-03-21 21:20:02 +01:00
|
|
|
|
|
|
|
if (!v1.isInt() || !v2.isInt())
|
2018-06-01 23:03:53 +02:00
|
|
|
break;
|
2016-03-21 21:20:02 +01:00
|
|
|
|
|
|
|
switch (op->str()[0]) {
|
|
|
|
case '<':
|
|
|
|
tok->str((v1 << v2).str());
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
tok->str((v1 >> v2).str());
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
tok->str((v1 & v2).str());
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
tok->str((v1 | v2).str());
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
tok->str((v1 ^ v2).str());
|
2015-11-25 16:46:39 +01:00
|
|
|
break;
|
2020-04-21 17:27:51 +02:00
|
|
|
}
|
2015-11-25 16:46:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Logical operations
|
|
|
|
else if (Token::Match(op, "%oror%|&&")) {
|
2018-05-29 22:41:37 +02:00
|
|
|
const bool op1 = !MathLib::isNullValue(num1);
|
|
|
|
const bool op2 = !MathLib::isNullValue(num2);
|
2018-04-04 21:51:31 +02:00
|
|
|
const bool result = (op->str() == "||") ? (op1 || op2) : (op1 && op2);
|
2015-11-25 16:46:39 +01:00
|
|
|
tok->str(result ? "1" : "0");
|
2012-08-20 18:10:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok->previous(), "- %num% - %num%"))
|
2018-05-29 22:41:37 +02:00
|
|
|
tok->str(MathLib::add(num1, num2));
|
2012-08-20 18:10:32 +02:00
|
|
|
else if (Token::Match(tok->previous(), "- %num% + %num%"))
|
2018-05-29 22:41:37 +02:00
|
|
|
tok->str(MathLib::subtract(num1, num2));
|
2012-08-20 18:10:32 +02:00
|
|
|
else {
|
|
|
|
try {
|
2018-05-29 22:41:37 +02:00
|
|
|
tok->str(MathLib::calculate(num1, num2, op->str()[0]));
|
2012-08-20 18:10:32 +02:00
|
|
|
} catch (InternalError &e) {
|
|
|
|
e.token = tok;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tok->deleteNext(2);
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
2015-11-25 16:46:39 +01:00
|
|
|
|
2012-08-20 18:10:32 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-07-20 12:21:01 +02:00
|
|
|
static Token *skipTernaryOp(Token *tok, const Token *backToken)
|
2019-03-03 07:40:55 +01:00
|
|
|
{
|
|
|
|
unsigned int colonLevel = 1;
|
|
|
|
while (nullptr != (tok = tok->next())) {
|
|
|
|
if (tok->str() == "?") {
|
|
|
|
++colonLevel;
|
|
|
|
} else if (tok->str() == ":") {
|
|
|
|
--colonLevel;
|
|
|
|
if (colonLevel == 0) {
|
|
|
|
tok = tok->next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tok->link() && tok->str() == "(")
|
|
|
|
tok = tok->link();
|
|
|
|
else if (Token::Match(tok->next(), "[{};)]") || tok->next() == backToken)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (colonLevel > 0) // Ticket #5214: Make sure the ':' matches the proper '?'
|
|
|
|
return nullptr;
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplateSimplifier::simplifyTemplateArgs(Token *start, Token *end)
|
|
|
|
{
|
2019-03-25 14:56:51 +01:00
|
|
|
// start could be erased so use the token before start if available
|
|
|
|
Token * first = (start && start->previous()) ? start->previous() : mTokenList.front();
|
2019-03-03 07:40:55 +01:00
|
|
|
bool again = true;
|
|
|
|
|
|
|
|
while (again) {
|
|
|
|
again = false;
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
for (Token *tok = first->next(); tok && tok != end; tok = tok->next()) {
|
2019-03-03 07:40:55 +01:00
|
|
|
if (tok->str() == "sizeof") {
|
|
|
|
// sizeof('x')
|
|
|
|
if (Token::Match(tok->next(), "( %char% )")) {
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
2023-01-16 22:05:33 +01:00
|
|
|
tok->str(std::to_string(1));
|
2019-03-03 07:40:55 +01:00
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sizeof ("text")
|
|
|
|
else if (Token::Match(tok->next(), "( %str% )")) {
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
2023-01-16 22:05:33 +01:00
|
|
|
tok->str(std::to_string(Token::getStrLength(tok) + 1));
|
2019-03-03 07:40:55 +01:00
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok->next(), "( %type% * )")) {
|
2023-03-07 12:12:31 +01:00
|
|
|
tok->str(MathLib::toString(mTokenizer.sizeOfType(tok->tokAt(3))));
|
2019-03-03 07:40:55 +01:00
|
|
|
tok->deleteNext(4);
|
|
|
|
again = true;
|
|
|
|
} else if (Token::simpleMatch(tok->next(), "( * )")) {
|
2023-03-07 12:12:31 +01:00
|
|
|
tok->str(MathLib::toString(mTokenizer.sizeOfType(tok->tokAt(2))));
|
2019-03-03 07:40:55 +01:00
|
|
|
tok->deleteNext(3);
|
|
|
|
again = true;
|
|
|
|
} else if (Token::Match(tok->next(), "( %type% )")) {
|
2023-03-07 12:12:31 +01:00
|
|
|
const unsigned int size = mTokenizer.sizeOfType(tok->tokAt(2));
|
2019-03-03 07:40:55 +01:00
|
|
|
if (size > 0) {
|
|
|
|
tok->str(MathLib::toString(size));
|
|
|
|
tok->deleteNext(3);
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
} else if (tok->strAt(1) == "(") {
|
|
|
|
tok = tok->linkAt(1);
|
|
|
|
}
|
2019-08-04 10:24:44 +02:00
|
|
|
} else if (Token::Match(tok, "%num% %comp% %num%") &&
|
|
|
|
MathLib::isInt(tok->str()) &&
|
|
|
|
MathLib::isInt(tok->strAt(2))) {
|
2019-09-25 12:06:29 +02:00
|
|
|
if ((Token::Match(tok->previous(), "(|&&|%oror%|,") || tok == start) &&
|
2019-08-04 10:24:44 +02:00
|
|
|
(Token::Match(tok->tokAt(3), ")|&&|%oror%|?") || tok->tokAt(3) == end)) {
|
|
|
|
const MathLib::bigint op1(MathLib::toLongNumber(tok->str()));
|
|
|
|
const std::string &cmp(tok->next()->str());
|
|
|
|
const MathLib::bigint op2(MathLib::toLongNumber(tok->strAt(2)));
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
if (cmp == "==")
|
|
|
|
result = (op1 == op2) ? "true" : "false";
|
|
|
|
else if (cmp == "!=")
|
|
|
|
result = (op1 != op2) ? "true" : "false";
|
|
|
|
else if (cmp == "<=")
|
|
|
|
result = (op1 <= op2) ? "true" : "false";
|
|
|
|
else if (cmp == ">=")
|
|
|
|
result = (op1 >= op2) ? "true" : "false";
|
|
|
|
else if (cmp == "<")
|
|
|
|
result = (op1 < op2) ? "true" : "false";
|
|
|
|
else
|
|
|
|
result = (op1 > op2) ? "true" : "false";
|
|
|
|
|
|
|
|
tok->str(result);
|
|
|
|
tok->deleteNext(2);
|
|
|
|
again = true;
|
|
|
|
tok = tok->previous();
|
|
|
|
}
|
2019-03-03 07:40:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
if (simplifyCalculations(first->next(), end))
|
2019-03-03 07:40:55 +01:00
|
|
|
again = true;
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
for (Token *tok = first->next(); tok && tok != end; tok = tok->next()) {
|
2019-07-28 21:52:52 +02:00
|
|
|
if (tok->str() == "?" &&
|
|
|
|
((tok->previous()->isNumber() || tok->previous()->isBoolean()) ||
|
|
|
|
Token::Match(tok->tokAt(-3), "( %bool%|%num% )"))) {
|
2019-03-03 07:40:55 +01:00
|
|
|
const int offset = (tok->previous()->str() == ")") ? 2 : 1;
|
|
|
|
|
|
|
|
// Find the token ":" then go to the next token
|
|
|
|
Token *colon = skipTernaryOp(tok, end);
|
|
|
|
if (!colon || colon->previous()->str() != ":" || !colon->next())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//handle the GNU extension: "x ? : y" <-> "x ? x : y"
|
|
|
|
if (colon->previous() == tok->next())
|
|
|
|
tok->insertToken(tok->strAt(-offset));
|
|
|
|
|
|
|
|
// go back before the condition, if possible
|
|
|
|
tok = tok->tokAt(-2);
|
|
|
|
if (offset == 2) {
|
|
|
|
// go further back before the "("
|
|
|
|
tok = tok->tokAt(-2);
|
|
|
|
//simplify the parentheses
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->next()->deleteNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(tok->next(), "false|0")) {
|
|
|
|
// Use code after colon, remove code before it.
|
|
|
|
Token::eraseTokens(tok, colon);
|
|
|
|
|
|
|
|
tok = tok->next();
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The condition is true. Delete the operator after the ":"..
|
|
|
|
else {
|
|
|
|
// delete the condition token and the "?"
|
|
|
|
tok->deleteNext(2);
|
|
|
|
|
|
|
|
unsigned int ternaryOplevel = 0;
|
|
|
|
for (const Token *endTok = colon; endTok; endTok = endTok->next()) {
|
2019-07-28 21:52:52 +02:00
|
|
|
if (Token::Match(endTok, "(|[|{"))
|
2019-03-03 07:40:55 +01:00
|
|
|
endTok = endTok->link();
|
2019-07-28 21:52:52 +02:00
|
|
|
else if (endTok->str() == "<" && (endTok->strAt(1) == ">" || templateParameters(endTok)))
|
|
|
|
endTok = endTok->findClosingBracket();
|
2019-03-03 07:40:55 +01:00
|
|
|
else if (endTok->str() == "?")
|
|
|
|
++ternaryOplevel;
|
|
|
|
else if (Token::Match(endTok, ")|}|]|;|,|:|>")) {
|
|
|
|
if (endTok->str() == ":" && ternaryOplevel)
|
|
|
|
--ternaryOplevel;
|
|
|
|
else if (endTok->str() == ">" && !end)
|
|
|
|
;
|
|
|
|
else {
|
|
|
|
Token::eraseTokens(colon->tokAt(-2), endTok);
|
|
|
|
again = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
for (Token *tok = first->next(); tok && tok != end; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "( %num%|%bool% )") &&
|
2022-06-08 16:58:57 +02:00
|
|
|
(tok->previous() && !tok->previous()->isName())) {
|
2019-03-03 07:40:55 +01:00
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
2019-03-25 14:56:51 +01:00
|
|
|
again = true;
|
2019-03-03 07:40:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
static bool validTokenStart(bool bounded, const Token *tok, const Token *frontToken, int offset)
|
|
|
|
{
|
|
|
|
if (!bounded)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (frontToken)
|
|
|
|
frontToken = frontToken->previous();
|
|
|
|
|
|
|
|
while (tok && offset <= 0) {
|
|
|
|
if (tok == frontToken)
|
|
|
|
return false;
|
|
|
|
++offset;
|
|
|
|
tok = tok->previous();
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok && offset > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool validTokenEnd(bool bounded, const Token *tok, const Token *backToken, int offset)
|
|
|
|
{
|
|
|
|
if (!bounded)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
while (tok && offset >= 0) {
|
|
|
|
if (tok == backToken)
|
|
|
|
return false;
|
|
|
|
--offset;
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok && offset < 0;
|
|
|
|
}
|
|
|
|
|
2012-01-09 20:33:11 +01:00
|
|
|
// TODO: This is not the correct class for simplifyCalculations(), so it
|
|
|
|
// should be moved away.
|
2019-07-18 14:55:30 +02:00
|
|
|
bool TemplateSimplifier::simplifyCalculations(Token* frontToken, Token *backToken, bool isTemplate)
|
2012-01-09 20:33:11 +01:00
|
|
|
{
|
2018-05-14 23:06:10 +02:00
|
|
|
bool ret = false;
|
2019-03-25 14:56:51 +01:00
|
|
|
const bool bounded = frontToken || backToken;
|
2018-12-21 13:54:59 +01:00
|
|
|
if (!frontToken) {
|
2018-12-21 13:51:45 +01:00
|
|
|
frontToken = mTokenList.front();
|
|
|
|
}
|
2019-03-25 14:56:51 +01:00
|
|
|
for (Token *tok = frontToken; tok && tok != backToken; tok = tok->next()) {
|
2012-01-09 20:33:11 +01:00
|
|
|
// Remove parentheses around variable..
|
|
|
|
// keep parentheses here: dynamic_cast<Fred *>(p);
|
|
|
|
// keep parentheses here: A operator * (int);
|
|
|
|
// keep parentheses here: int ( * ( * f ) ( ... ) ) (int) ;
|
|
|
|
// keep parentheses here: int ( * * ( * compilerHookVector ) (void) ) ( ) ;
|
|
|
|
// keep parentheses here: operator new [] (size_t);
|
|
|
|
// keep parentheses here: Functor()(a ... )
|
|
|
|
// keep parentheses here: ) ( var ) ;
|
2019-03-25 14:56:51 +01:00
|
|
|
if (validTokenEnd(bounded, tok, backToken, 4) &&
|
|
|
|
(Token::Match(tok->next(), "( %name% ) ;|)|,|]") ||
|
|
|
|
(Token::Match(tok->next(), "( %name% ) %cop%") &&
|
|
|
|
(tok->tokAt(2)->varId()>0 ||
|
|
|
|
!Token::Match(tok->tokAt(4), "[*&+-~]")))) &&
|
2012-01-09 20:33:11 +01:00
|
|
|
!tok->isName() &&
|
|
|
|
tok->str() != ">" &&
|
2015-03-02 16:27:00 +01:00
|
|
|
tok->str() != ")" &&
|
|
|
|
tok->str() != "]") {
|
2012-01-09 20:33:11 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
tok = tok->next();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
if (validTokenEnd(bounded, tok, backToken, 3) &&
|
|
|
|
Token::Match(tok->previous(), "(|&&|%oror% %char% %comp% %num% &&|%oror%|)")) {
|
2015-11-28 10:49:08 +01:00
|
|
|
tok->str(MathLib::toString(MathLib::toLongNumber(tok->str())));
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
2019-06-15 09:46:32 +02:00
|
|
|
if (validTokenEnd(bounded, tok, backToken, 5) &&
|
|
|
|
Token::Match(tok, "decltype ( %type% { } )")) {
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
2019-07-28 21:52:52 +02:00
|
|
|
|
|
|
|
if (validTokenEnd(bounded, tok, backToken, 3) &&
|
|
|
|
Token::Match(tok, "decltype ( %bool%|%num% )")) {
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
if (tok->isBoolean())
|
|
|
|
tok->str("bool");
|
|
|
|
else if (MathLib::isFloat(tok->str())) {
|
|
|
|
// MathLib::getSuffix doesn't work for floating point numbers
|
2022-10-02 07:12:40 +02:00
|
|
|
const char suffix = tok->str().back();
|
2019-07-28 21:52:52 +02:00
|
|
|
if (suffix == 'f' || suffix == 'F')
|
|
|
|
tok->str("float");
|
|
|
|
else if (suffix == 'l' || suffix == 'L') {
|
|
|
|
tok->str("double");
|
|
|
|
tok->isLong(true);
|
|
|
|
} else
|
|
|
|
tok->str("double");
|
|
|
|
} else if (MathLib::isInt(tok->str())) {
|
|
|
|
std::string suffix = MathLib::getSuffix(tok->str());
|
|
|
|
if (suffix.find("LL") != std::string::npos) {
|
|
|
|
tok->str("long");
|
|
|
|
tok->isLong(true);
|
|
|
|
} else if (suffix.find('L') != std::string::npos)
|
|
|
|
tok->str("long");
|
|
|
|
else
|
|
|
|
tok->str("int");
|
|
|
|
tok->isUnsigned(suffix.find('U') != std::string::npos);
|
|
|
|
}
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
2019-06-15 09:46:32 +02:00
|
|
|
|
|
|
|
if (validTokenEnd(bounded, tok, backToken, 2) &&
|
2020-08-26 07:00:58 +02:00
|
|
|
(Token::Match(tok, "char|short|int|long { }") ||
|
|
|
|
Token::Match(tok, "char|short|int|long ( )"))) {
|
2019-06-15 09:46:32 +02:00
|
|
|
tok->str("0"); // FIXME add type suffix
|
|
|
|
tok->isSigned(false);
|
|
|
|
tok->isUnsigned(false);
|
|
|
|
tok->isLong(false);
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
if (tok && tok->isNumber()) {
|
|
|
|
if (validTokenEnd(bounded, tok, backToken, 2) &&
|
2019-07-18 14:55:30 +02:00
|
|
|
simplifyNumericCalculations(tok, isTemplate)) {
|
2018-05-31 06:36:59 +02:00
|
|
|
ret = true;
|
2018-06-01 23:03:53 +02:00
|
|
|
Token *prev = tok->tokAt(-2);
|
2019-03-25 14:56:51 +01:00
|
|
|
while (validTokenStart(bounded, tok, frontToken, -2) &&
|
2019-07-18 14:55:30 +02:00
|
|
|
prev && simplifyNumericCalculations(prev, isTemplate)) {
|
2018-06-01 23:03:53 +02:00
|
|
|
tok = prev;
|
|
|
|
prev = prev->tokAt(-2);
|
2018-05-31 06:36:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-14 21:47:03 +02:00
|
|
|
// Remove redundant conditions (0&&x) (1||x)
|
2019-03-25 14:56:51 +01:00
|
|
|
if (validTokenStart(bounded, tok, frontToken, -1) &&
|
|
|
|
validTokenEnd(bounded, tok, backToken, 1) &&
|
|
|
|
(Token::Match(tok->previous(), "[(=,] 0 &&") ||
|
|
|
|
Token::Match(tok->previous(), "[(=,] 1 %oror%"))) {
|
2012-06-14 21:47:03 +02:00
|
|
|
unsigned int par = 0;
|
|
|
|
const Token *tok2 = tok;
|
2018-04-04 21:51:31 +02:00
|
|
|
const bool andAnd = (tok->next()->str() == "&&");
|
2012-10-24 03:09:42 +02:00
|
|
|
for (; tok2; tok2 = tok2->next()) {
|
2016-07-17 15:52:53 +02:00
|
|
|
if (tok2->str() == "(" || tok2->str() == "[")
|
2012-06-14 21:47:03 +02:00
|
|
|
++par;
|
2016-07-17 15:52:53 +02:00
|
|
|
else if (tok2->str() == ")" || tok2->str() == "]") {
|
2012-06-14 21:47:03 +02:00
|
|
|
if (par == 0)
|
|
|
|
break;
|
|
|
|
--par;
|
2015-11-28 15:00:41 +01:00
|
|
|
} else if (par == 0 && isLowerThanLogicalAnd(tok2) && (andAnd || tok2->str() != "||"))
|
2012-06-14 21:47:03 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-11-28 15:00:41 +01:00
|
|
|
if (tok2) {
|
2018-09-01 11:26:10 +02:00
|
|
|
eraseTokens(tok, tok2);
|
2013-06-14 18:51:52 +02:00
|
|
|
ret = true;
|
|
|
|
}
|
2012-06-14 21:47:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
if (tok->str() == "0" && validTokenStart(bounded, tok, frontToken, -1)) {
|
|
|
|
if (validTokenEnd(bounded, tok, backToken, 1) &&
|
|
|
|
((Token::Match(tok->previous(), "[+-] 0 %cop%|;") && isLowerThanMulDiv(tok->next())) ||
|
|
|
|
(Token::Match(tok->previous(), "%or% 0 %cop%|;") && isLowerThanXor(tok->next())))) {
|
2012-01-09 20:33:11 +01:00
|
|
|
tok = tok->previous();
|
2015-01-31 10:50:39 +01:00
|
|
|
if (Token::Match(tok->tokAt(-4), "[;{}] %name% = %name% [+-|] 0 ;") &&
|
2012-01-09 20:33:11 +01:00
|
|
|
tok->strAt(-3) == tok->previous()->str()) {
|
2017-06-07 19:46:02 +02:00
|
|
|
tok = tok->tokAt(-4);
|
|
|
|
tok->deleteNext(5);
|
|
|
|
} else {
|
|
|
|
tok = tok->previous();
|
2012-01-09 20:33:11 +01:00
|
|
|
tok->deleteNext(2);
|
|
|
|
}
|
|
|
|
ret = true;
|
2019-03-25 14:56:51 +01:00
|
|
|
} else if (validTokenEnd(bounded, tok, backToken, 1) &&
|
|
|
|
(Token::Match(tok->previous(), "[=([,] 0 [+|]") ||
|
|
|
|
Token::Match(tok->previous(), "return|case 0 [+|]"))) {
|
2015-03-14 18:46:17 +01:00
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteNext(2);
|
2012-01-09 20:33:11 +01:00
|
|
|
ret = true;
|
2019-03-25 14:56:51 +01:00
|
|
|
} else if ((((Token::Match(tok->previous(), "[=[(,] 0 * %name%|%num% ,|]|)|;|=|%cop%") ||
|
|
|
|
Token::Match(tok->previous(), "return|case 0 *|&& %name%|%num% ,|:|;|=|%cop%")) &&
|
|
|
|
validTokenEnd(bounded, tok, backToken, 3)) ||
|
|
|
|
(((Token::Match(tok->previous(), "[=[(,] 0 * (") ||
|
|
|
|
Token::Match(tok->previous(), "return|case 0 *|&& (")) &&
|
|
|
|
validTokenEnd(bounded, tok, backToken, 2))))) {
|
2012-03-07 20:31:23 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
if (tok->next()->str() == "(")
|
2018-09-01 11:26:10 +02:00
|
|
|
eraseTokens(tok, tok->next()->link());
|
2012-03-07 20:31:23 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
2019-03-25 14:56:51 +01:00
|
|
|
} else if (validTokenEnd(bounded, tok, backToken, 4) &&
|
|
|
|
(Token::Match(tok->previous(), "[=[(,] 0 && *|& %any% ,|]|)|;|=|%cop%") ||
|
|
|
|
Token::Match(tok->previous(), "return|case 0 && *|& %any% ,|:|;|=|%cop%"))) {
|
2012-03-07 20:31:23 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
if (tok->next()->str() == "(")
|
2018-09-01 11:26:10 +02:00
|
|
|
eraseTokens(tok, tok->next()->link());
|
2012-03-07 20:31:23 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
if (tok->str() == "1" && validTokenStart(bounded, tok, frontToken, -1)) {
|
|
|
|
if (validTokenEnd(bounded, tok, backToken, 3) &&
|
|
|
|
(Token::Match(tok->previous(), "[=[(,] 1 %oror% %any% ,|]|)|;|=|%cop%") ||
|
|
|
|
Token::Match(tok->previous(), "return|case 1 %oror% %any% ,|:|;|=|%cop%"))) {
|
2012-03-07 20:31:23 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
if (tok->next()->str() == "(")
|
2018-09-01 11:26:10 +02:00
|
|
|
eraseTokens(tok, tok->next()->link());
|
2012-03-07 20:31:23 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
2019-03-25 14:56:51 +01:00
|
|
|
} else if (validTokenEnd(bounded, tok, backToken, 4) &&
|
|
|
|
(Token::Match(tok->previous(), "[=[(,] 1 %oror% *|& %any% ,|]|)|;|=|%cop%") ||
|
|
|
|
Token::Match(tok->previous(), "return|case 1 %oror% *|& %any% ,|:|;|=|%cop%"))) {
|
2012-03-07 20:31:23 +01:00
|
|
|
tok->deleteNext();
|
2012-01-09 20:33:11 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
if (tok->next()->str() == "(")
|
2018-09-01 11:26:10 +02:00
|
|
|
eraseTokens(tok, tok->next()->link());
|
2012-01-09 20:33:11 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
if ((Token::Match(tok->tokAt(-2), "%any% * 1") &&
|
|
|
|
validTokenStart(bounded, tok, frontToken, -2)) ||
|
|
|
|
(Token::Match(tok->previous(), "%any% 1 *") &&
|
|
|
|
validTokenStart(bounded, tok, frontToken, -1))) {
|
2017-06-07 19:32:56 +02:00
|
|
|
tok = tok->previous();
|
2017-06-08 00:49:46 +02:00
|
|
|
if (tok->str() == "*")
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteNext(2);
|
2012-01-09 20:33:11 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove parentheses around number..
|
2019-03-25 14:56:51 +01:00
|
|
|
if (validTokenStart(bounded, tok, frontToken, -2) &&
|
|
|
|
Token::Match(tok->tokAt(-2), "%op%|< ( %num% )") &&
|
|
|
|
tok->strAt(-2) != ">") {
|
2012-01-09 20:33:11 +01:00
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
if (validTokenStart(bounded, tok, frontToken, -1) &&
|
|
|
|
validTokenEnd(bounded, tok, backToken, 1) &&
|
|
|
|
(Token::Match(tok->previous(), "( 0 [|+]") ||
|
|
|
|
Token::Match(tok->previous(), "[|+-] 0 )"))) {
|
2017-06-07 20:09:06 +02:00
|
|
|
tok = tok->previous();
|
2017-06-08 08:15:05 +02:00
|
|
|
if (Token::Match(tok, "[|+-]"))
|
2012-01-09 20:33:11 +01:00
|
|
|
tok = tok->previous();
|
2017-06-07 20:09:06 +02:00
|
|
|
tok->deleteNext(2);
|
2012-01-09 20:33:11 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:56:51 +01:00
|
|
|
if (validTokenEnd(bounded, tok, backToken, 2) &&
|
|
|
|
Token::Match(tok, "%num% %comp% %num%") &&
|
2012-01-09 20:33:11 +01:00
|
|
|
MathLib::isInt(tok->str()) &&
|
|
|
|
MathLib::isInt(tok->strAt(2))) {
|
2019-03-25 14:56:51 +01:00
|
|
|
if (validTokenStart(bounded, tok, frontToken, -1) &&
|
|
|
|
Token::Match(tok->previous(), "(|&&|%oror%") &&
|
|
|
|
Token::Match(tok->tokAt(3), ")|&&|%oror%|?")) {
|
2012-01-09 20:33:11 +01:00
|
|
|
const MathLib::bigint op1(MathLib::toLongNumber(tok->str()));
|
|
|
|
const std::string &cmp(tok->next()->str());
|
|
|
|
const MathLib::bigint op2(MathLib::toLongNumber(tok->strAt(2)));
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
if (cmp == "==")
|
|
|
|
result = (op1 == op2) ? "1" : "0";
|
|
|
|
else if (cmp == "!=")
|
|
|
|
result = (op1 != op2) ? "1" : "0";
|
|
|
|
else if (cmp == "<=")
|
|
|
|
result = (op1 <= op2) ? "1" : "0";
|
|
|
|
else if (cmp == ">=")
|
|
|
|
result = (op1 >= op2) ? "1" : "0";
|
|
|
|
else if (cmp == "<")
|
|
|
|
result = (op1 < op2) ? "1" : "0";
|
2015-08-15 19:46:31 +02:00
|
|
|
else
|
2012-01-09 20:33:11 +01:00
|
|
|
result = (op1 > op2) ? "1" : "0";
|
|
|
|
|
|
|
|
tok->str(result);
|
|
|
|
tok->deleteNext(2);
|
|
|
|
ret = true;
|
2018-05-14 23:06:10 +02:00
|
|
|
tok = tok->previous();
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 21:29:50 +02:00
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:31:52 +01:00
|
|
|
void TemplateSimplifier::getTemplateParametersInDeclaration(
|
2014-11-01 22:07:24 +01:00
|
|
|
const Token * tok,
|
|
|
|
std::vector<const Token *> & typeParametersInDeclaration)
|
|
|
|
{
|
2019-05-28 21:32:37 +02:00
|
|
|
assert(tok->strAt(-1) == "<");
|
|
|
|
|
2014-11-01 22:07:24 +01:00
|
|
|
typeParametersInDeclaration.clear();
|
2019-04-09 20:07:13 +02:00
|
|
|
const Token *end = tok->previous()->findClosingBracket();
|
2021-03-16 09:31:52 +01:00
|
|
|
bool inDefaultValue = false;
|
2019-04-09 20:07:13 +02:00
|
|
|
for (; tok && tok!= end; tok = tok->next()) {
|
2019-04-04 06:07:49 +02:00
|
|
|
if (Token::simpleMatch(tok, "template <")) {
|
|
|
|
const Token *closing = tok->next()->findClosingBracket();
|
|
|
|
if (closing)
|
|
|
|
tok = closing->next();
|
2019-06-01 10:52:29 +02:00
|
|
|
} else if (tok->link() && Token::Match(tok, "{|(|["))
|
2019-05-28 21:32:37 +02:00
|
|
|
tok = tok->link();
|
2021-03-16 09:31:52 +01:00
|
|
|
else if (Token::Match(tok, "%name% ,|>|=")) {
|
|
|
|
if (!inDefaultValue) {
|
|
|
|
typeParametersInDeclaration.push_back(tok);
|
|
|
|
if (tok->strAt(1) == "=")
|
|
|
|
inDefaultValue = true;
|
|
|
|
}
|
|
|
|
} else if (inDefaultValue) {
|
|
|
|
if (tok->str() == ",")
|
|
|
|
inDefaultValue = false;
|
|
|
|
else if (tok->str() == "<") {
|
|
|
|
const Token *closing = tok->findClosingBracket();
|
|
|
|
if (closing)
|
|
|
|
tok = closing;
|
|
|
|
}
|
|
|
|
}
|
2014-11-01 22:07:24 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
bool TemplateSimplifier::matchSpecialization(
|
|
|
|
const Token *templateDeclarationNameToken,
|
|
|
|
const Token *templateInstantiationNameToken,
|
|
|
|
const std::list<const Token *> & specializations)
|
2017-12-30 22:14:48 +01:00
|
|
|
{
|
|
|
|
// Is there a matching specialization?
|
2022-12-20 20:32:16 +01:00
|
|
|
for (std::list<const Token *>::const_iterator it = specializations.cbegin(); it != specializations.cend(); ++it) {
|
2017-12-30 22:14:48 +01:00
|
|
|
if (!Token::Match(*it, "%name% <"))
|
|
|
|
continue;
|
|
|
|
const Token *startToken = (*it);
|
|
|
|
while (startToken->previous() && !Token::Match(startToken->previous(), "[;{}]"))
|
|
|
|
startToken = startToken->previous();
|
2017-12-30 22:43:44 +01:00
|
|
|
if (!Token::simpleMatch(startToken, "template <"))
|
2017-12-30 22:14:48 +01:00
|
|
|
continue;
|
|
|
|
std::vector<const Token *> templateParameters;
|
2018-09-01 11:26:10 +02:00
|
|
|
getTemplateParametersInDeclaration(startToken->tokAt(2), templateParameters);
|
2017-12-30 22:14:48 +01:00
|
|
|
|
|
|
|
const Token *instToken = templateInstantiationNameToken->tokAt(2);
|
|
|
|
const Token *declToken = (*it)->tokAt(2);
|
|
|
|
const Token * const endToken = (*it)->next()->findClosingBracket();
|
2019-04-01 08:04:47 +02:00
|
|
|
if (!endToken)
|
|
|
|
continue;
|
2017-12-30 22:14:48 +01:00
|
|
|
while (declToken != endToken) {
|
2021-02-28 21:52:14 +01:00
|
|
|
if (declToken->str() != instToken->str() ||
|
|
|
|
declToken->isSigned() != instToken->isSigned() ||
|
|
|
|
declToken->isUnsigned() != instToken->isUnsigned() ||
|
|
|
|
declToken->isLong() != instToken->isLong()) {
|
2017-12-30 22:14:48 +01:00
|
|
|
int nr = 0;
|
|
|
|
while (nr < templateParameters.size() && templateParameters[nr]->str() != declToken->str())
|
|
|
|
++nr;
|
|
|
|
|
|
|
|
if (nr == templateParameters.size())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
declToken = declToken->next();
|
|
|
|
instToken = instToken->next();
|
|
|
|
}
|
|
|
|
|
2019-04-01 08:04:47 +02:00
|
|
|
if (declToken && instToken && declToken == endToken && instToken->str() == ">") {
|
2017-12-30 22:14:48 +01:00
|
|
|
// specialization matches.
|
|
|
|
return templateDeclarationNameToken == *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No specialization matches. Return true if the declaration is not a specialization.
|
2018-12-31 21:29:53 +01:00
|
|
|
return Token::Match(templateDeclarationNameToken, "%name% !!<") &&
|
|
|
|
(templateDeclarationNameToken->str().find('<') == std::string::npos);
|
2017-12-30 22:14:48 +01:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
std::string TemplateSimplifier::getNewName(
|
|
|
|
Token *tok2,
|
|
|
|
std::list<std::string> &typeStringsUsedInTemplateInstantiation)
|
|
|
|
{
|
|
|
|
std::string typeForNewName;
|
|
|
|
unsigned int indentlevel = 0;
|
2019-05-24 10:44:08 +02:00
|
|
|
const Token * endToken = tok2->next()->findClosingBracket();
|
|
|
|
for (Token *tok3 = tok2->tokAt(2); tok3 != endToken && (indentlevel > 0 || tok3->str() != ">"); tok3 = tok3->next()) {
|
2018-10-14 16:57:07 +02:00
|
|
|
// #2721 - unhandled [ => bail out
|
2021-05-23 10:40:09 +02:00
|
|
|
if (tok3->str() == "[" && !Token::Match(tok3->next(), "%num%| ]")) {
|
2018-10-14 16:57:07 +02:00
|
|
|
typeForNewName.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!tok3->next()) {
|
|
|
|
typeForNewName.clear();
|
|
|
|
break;
|
|
|
|
}
|
2019-07-01 07:01:14 +02:00
|
|
|
if (Token::Match(tok3->tokAt(-2), "<|,|:: %name% <") && (tok3->strAt(1) == ">" || templateParameters(tok3)))
|
2018-10-14 16:57:07 +02:00
|
|
|
++indentlevel;
|
2021-04-19 09:17:49 +02:00
|
|
|
else if (indentlevel > 0 && Token::Match(tok3, "> ,|>|::"))
|
2018-10-14 16:57:07 +02:00
|
|
|
--indentlevel;
|
2022-09-01 20:24:01 +02:00
|
|
|
else if (indentlevel == 0 && Token::Match(tok3->previous(), "[<,]")) {
|
2021-03-18 09:15:21 +01:00
|
|
|
mTypesUsedInTemplateInstantiation.emplace_back(tok3, "");
|
2018-10-14 16:57:07 +02:00
|
|
|
}
|
2021-05-23 10:40:09 +02:00
|
|
|
if (Token::Match(tok3, "(|["))
|
2019-07-31 09:18:43 +02:00
|
|
|
++indentlevel;
|
2021-05-23 10:40:09 +02:00
|
|
|
else if (Token::Match(tok3, ")|]"))
|
2019-07-31 09:18:43 +02:00
|
|
|
--indentlevel;
|
2018-10-14 16:57:07 +02:00
|
|
|
const bool constconst = tok3->str() == "const" && tok3->strAt(1) == "const";
|
|
|
|
if (!constconst) {
|
2021-03-04 08:43:22 +01:00
|
|
|
if (tok3->isUnsigned())
|
2022-06-03 15:09:29 +02:00
|
|
|
typeStringsUsedInTemplateInstantiation.emplace_back("unsigned");
|
2021-03-04 08:43:22 +01:00
|
|
|
else if (tok3->isSigned())
|
2022-06-03 15:09:29 +02:00
|
|
|
typeStringsUsedInTemplateInstantiation.emplace_back("signed");
|
2021-03-04 08:43:22 +01:00
|
|
|
if (tok3->isLong())
|
2022-06-03 15:09:29 +02:00
|
|
|
typeStringsUsedInTemplateInstantiation.emplace_back("long");
|
2018-10-14 16:57:07 +02:00
|
|
|
typeStringsUsedInTemplateInstantiation.push_back(tok3->str());
|
|
|
|
}
|
|
|
|
// add additional type information
|
|
|
|
if (!constconst && !Token::Match(tok3, "class|struct|enum")) {
|
|
|
|
if (!typeForNewName.empty())
|
|
|
|
typeForNewName += ' ';
|
2021-02-28 21:52:14 +01:00
|
|
|
if (tok3->isUnsigned())
|
|
|
|
typeForNewName += "unsigned ";
|
|
|
|
else if (tok3->isSigned())
|
|
|
|
typeForNewName += "signed ";
|
|
|
|
if (tok3->isLong()) {
|
|
|
|
typeForNewName += "long ";
|
|
|
|
}
|
2018-10-14 16:57:07 +02:00
|
|
|
typeForNewName += tok3->str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return typeForNewName;
|
|
|
|
}
|
|
|
|
|
2013-02-10 07:43:09 +01:00
|
|
|
bool TemplateSimplifier::simplifyTemplateInstantiations(
|
2017-12-26 10:55:18 +01:00
|
|
|
const TokenAndName &templateDeclaration,
|
2017-12-30 22:14:48 +01:00
|
|
|
const std::list<const Token *> &specializations,
|
2016-02-12 12:05:32 +01:00
|
|
|
const std::time_t maxtime,
|
2012-01-09 20:33:11 +01:00
|
|
|
std::set<std::string> &expandedtemplates)
|
|
|
|
{
|
|
|
|
// this variable is not used at the moment. The intention was to
|
|
|
|
// allow continuous instantiations until all templates has been expanded
|
|
|
|
//bool done = false;
|
|
|
|
|
|
|
|
// Contains tokens such as "T"
|
|
|
|
std::vector<const Token *> typeParametersInDeclaration;
|
2019-08-10 08:42:12 +02:00
|
|
|
getTemplateParametersInDeclaration(templateDeclaration.token()->tokAt(2), typeParametersInDeclaration);
|
2023-03-07 12:12:31 +01:00
|
|
|
const bool printDebug = mSettings.debugwarnings;
|
2019-01-27 07:46:27 +01:00
|
|
|
const bool specialized = templateDeclaration.isSpecialization();
|
2019-01-07 06:55:22 +01:00
|
|
|
const bool isfunc = templateDeclaration.isFunction();
|
2019-01-18 21:12:39 +01:00
|
|
|
const bool isVar = templateDeclaration.isVariable();
|
2012-01-09 20:33:11 +01:00
|
|
|
|
|
|
|
// locate template usage..
|
2018-09-01 11:26:10 +02:00
|
|
|
std::string::size_type numberOfTemplateInstantiations = mTemplateInstantiations.size();
|
2012-01-09 20:33:11 +01:00
|
|
|
unsigned int recursiveCount = 0;
|
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
bool instantiated = false;
|
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
for (const TokenAndName &instantiation : mTemplateInstantiations) {
|
2020-09-28 22:51:13 +02:00
|
|
|
// skip deleted instantiations
|
|
|
|
if (!instantiation.token())
|
|
|
|
continue;
|
2018-09-01 11:26:10 +02:00
|
|
|
if (numberOfTemplateInstantiations != mTemplateInstantiations.size()) {
|
|
|
|
numberOfTemplateInstantiations = mTemplateInstantiations.size();
|
2012-01-09 20:33:11 +01:00
|
|
|
++recursiveCount;
|
2023-03-07 12:12:31 +01:00
|
|
|
if (recursiveCount > mSettings.maxTemplateRecursion) {
|
2020-06-26 12:59:40 +02:00
|
|
|
std::list<std::string> typeStringsUsedInTemplateInstantiation;
|
|
|
|
const std::string typeForNewName = templateDeclaration.name() + "<" + getNewName(instantiation.token(), typeStringsUsedInTemplateInstantiation) + ">";
|
|
|
|
|
2020-06-24 20:30:03 +02:00
|
|
|
const std::list<const Token *> callstack(1, instantiation.token());
|
|
|
|
const ErrorMessage errmsg(callstack,
|
2023-03-07 12:12:31 +01:00
|
|
|
&mTokenizer.list,
|
2020-06-24 20:30:03 +02:00
|
|
|
Severity::information,
|
|
|
|
"templateRecursion",
|
|
|
|
"TemplateSimplifier: max template recursion ("
|
2023-03-07 12:12:31 +01:00
|
|
|
+ MathLib::toString(mSettings.maxTemplateRecursion)
|
2020-06-26 12:59:40 +02:00
|
|
|
+ ") reached for template '"+typeForNewName+"'. You might want to limit Cppcheck recursion.",
|
2021-02-24 22:00:06 +01:00
|
|
|
Certainty::normal);
|
2023-03-07 12:12:31 +01:00
|
|
|
if (mErrorLogger && mSettings.severity.isEnabled(Severity::information))
|
2020-06-24 20:30:03 +02:00
|
|
|
mErrorLogger->reportErr(errmsg);
|
|
|
|
|
2012-01-09 20:33:11 +01:00
|
|
|
// bail out..
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-26 10:55:18 +01:00
|
|
|
|
2018-01-03 23:05:57 +01:00
|
|
|
// already simplified
|
2019-08-10 08:42:12 +02:00
|
|
|
if (!Token::Match(instantiation.token(), "%name% <"))
|
2018-01-03 23:05:57 +01:00
|
|
|
continue;
|
|
|
|
|
2020-11-24 07:21:37 +01:00
|
|
|
if (!((instantiation.fullName() == templateDeclaration.fullName()) ||
|
|
|
|
(instantiation.name() == templateDeclaration.name() &&
|
|
|
|
instantiation.fullName() == templateDeclaration.scope()))) {
|
2019-03-01 08:18:53 +01:00
|
|
|
// FIXME: fallback to not matching scopes until type deduction works
|
2019-01-14 20:36:23 +01:00
|
|
|
|
|
|
|
// names must match
|
2019-08-10 08:42:12 +02:00
|
|
|
if (instantiation.name() != templateDeclaration.name())
|
2019-01-14 20:36:23 +01:00
|
|
|
continue;
|
2019-03-01 08:18:53 +01:00
|
|
|
|
|
|
|
// scopes must match when present
|
2019-08-10 08:42:12 +02:00
|
|
|
if (!instantiation.scope().empty() && !templateDeclaration.scope().empty())
|
2019-03-01 08:18:53 +01:00
|
|
|
continue;
|
2019-01-14 20:36:23 +01:00
|
|
|
}
|
2017-12-26 10:55:18 +01:00
|
|
|
|
2020-12-06 22:08:42 +01:00
|
|
|
// make sure constructors and destructors don't match each other
|
|
|
|
if (templateDeclaration.nameToken()->strAt(-1) == "~" && instantiation.token()->strAt(-1) != "~")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// template families should match
|
|
|
|
if (!instantiation.isFunction() && templateDeclaration.isFunction()) {
|
|
|
|
// there are exceptions
|
|
|
|
if (!Token::simpleMatch(instantiation.token()->tokAt(-2), "decltype ("))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 09:43:16 +01:00
|
|
|
if (templateDeclaration.isFunction() && instantiation.isFunction()) {
|
|
|
|
std::vector<const Token*> declFuncArgs;
|
|
|
|
getFunctionArguments(templateDeclaration.nameToken(), declFuncArgs);
|
|
|
|
std::vector<const Token*> instFuncParams;
|
|
|
|
getFunctionArguments(instantiation.token(), instFuncParams);
|
|
|
|
|
|
|
|
if (declFuncArgs.size() != instFuncParams.size()) {
|
|
|
|
// check for default arguments
|
|
|
|
const Token* tok = templateDeclaration.nameToken()->tokAt(2);
|
|
|
|
const Token* end = templateDeclaration.nameToken()->linkAt(1);
|
|
|
|
size_t count = 0;
|
|
|
|
for (; tok != end; tok = tok->next()) {
|
|
|
|
if (tok->str() == "=")
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instFuncParams.size() < (declFuncArgs.size() - count) || instFuncParams.size() > declFuncArgs.size())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 09:25:18 +02:00
|
|
|
// A global function can't be called through a pointer.
|
2019-08-10 08:42:12 +02:00
|
|
|
if (templateDeclaration.isFunction() && templateDeclaration.scope().empty() &&
|
|
|
|
(instantiation.token()->strAt(-1) == "." ||
|
|
|
|
Token::simpleMatch(instantiation.token()->tokAt(-2), ". template")))
|
2019-07-28 09:25:18 +02:00
|
|
|
continue;
|
|
|
|
|
2019-08-10 08:42:12 +02:00
|
|
|
if (!matchSpecialization(templateDeclaration.nameToken(), instantiation.token(), specializations))
|
2017-12-30 22:14:48 +01:00
|
|
|
continue;
|
|
|
|
|
2019-08-10 08:42:12 +02:00
|
|
|
Token * const tok2 = instantiation.token();
|
2018-09-01 11:26:10 +02:00
|
|
|
if (mErrorLogger && !mTokenList.getFiles().empty())
|
|
|
|
mErrorLogger->reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue());
|
2022-12-30 21:21:05 +01:00
|
|
|
|
|
|
|
if (maxtime > 0 && std::time(nullptr) > maxtime) {
|
2023-03-07 12:12:31 +01:00
|
|
|
if (mSettings.debugwarnings) {
|
2022-12-30 21:21:05 +01:00
|
|
|
ErrorMessage::FileLocation loc;
|
|
|
|
loc.setfile(mTokenList.getFiles()[0]);
|
|
|
|
ErrorMessage errmsg({std::move(loc)},
|
|
|
|
emptyString,
|
|
|
|
Severity::debug,
|
2023-01-10 15:15:27 +01:00
|
|
|
"Template instantiation maximum time exceeded",
|
2022-12-30 21:21:05 +01:00
|
|
|
"templateMaxTime",
|
|
|
|
Certainty::normal);
|
|
|
|
mErrorLogger->reportErr(errmsg);
|
|
|
|
}
|
2016-02-12 12:05:32 +01:00
|
|
|
return false;
|
2022-12-30 21:21:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
assert(mTokenList.validateToken(tok2)); // that assertion fails on examples from #6021
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2017-12-26 10:55:18 +01:00
|
|
|
const Token *startToken = tok2;
|
2020-02-04 16:35:30 +01:00
|
|
|
while (Token::Match(startToken->tokAt(-2), ">|%name% :: %name%")) {
|
2018-10-14 16:57:07 +02:00
|
|
|
if (startToken->strAt(-2) == ">") {
|
|
|
|
const Token * tok3 = startToken->tokAt(-2)->findOpeningBracket();
|
|
|
|
if (tok3)
|
|
|
|
startToken = tok3->previous();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
startToken = startToken->tokAt(-2);
|
|
|
|
}
|
2017-12-26 10:55:18 +01:00
|
|
|
|
2018-11-23 11:36:09 +01:00
|
|
|
if (Token::Match(startToken->previous(), ";|{|}|=|const") &&
|
2021-04-17 21:56:13 +02:00
|
|
|
(!specialized && !instantiateMatch(tok2, typeParametersInDeclaration.size(), templateDeclaration.isVariadic(), isfunc ? "(" : isVar ? ";|%op%|(" : "*|&|::| %name%")))
|
2012-01-09 20:33:11 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// New type..
|
2018-10-14 16:57:07 +02:00
|
|
|
mTypesUsedInTemplateInstantiation.clear();
|
2017-12-26 22:34:39 +01:00
|
|
|
std::list<std::string> typeStringsUsedInTemplateInstantiation;
|
2018-10-14 16:57:07 +02:00
|
|
|
std::string typeForNewName = getNewName(tok2, typeStringsUsedInTemplateInstantiation);
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2019-05-23 20:53:26 +02:00
|
|
|
if ((typeForNewName.empty() && !templateDeclaration.isVariadic()) ||
|
2021-04-17 21:56:13 +02:00
|
|
|
(!typeParametersInDeclaration.empty() && !instantiateMatch(tok2, typeParametersInDeclaration.size(), templateDeclaration.isVariadic(), nullptr))) {
|
2018-09-01 11:26:10 +02:00
|
|
|
if (printDebug && mErrorLogger) {
|
2015-04-05 14:54:24 +02:00
|
|
|
std::list<const Token *> callstack(1, tok2);
|
2020-05-23 07:16:49 +02:00
|
|
|
mErrorLogger->reportErr(ErrorMessage(callstack, &mTokenList, Severity::debug, "debug",
|
2021-02-24 22:00:06 +01:00
|
|
|
"Failed to instantiate template \"" + instantiation.name() + "\". The checking continues anyway.", Certainty::normal));
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
if (typeForNewName.empty())
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// New classname/funcname..
|
2019-08-10 08:42:12 +02:00
|
|
|
const std::string newName(templateDeclaration.name() + " < " + typeForNewName + " >");
|
|
|
|
const std::string newFullName(templateDeclaration.scope() + (templateDeclaration.scope().empty() ? "" : " :: ") + newName);
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2019-05-07 10:26:41 +02:00
|
|
|
if (expandedtemplates.insert(newFullName).second) {
|
2019-01-18 21:12:39 +01:00
|
|
|
expandTemplate(templateDeclaration, instantiation, typeParametersInDeclaration, newName, !specialized && !isVar);
|
2012-07-29 16:01:05 +02:00
|
|
|
instantiated = true;
|
2019-06-23 10:55:09 +02:00
|
|
|
mChanged = true;
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace all these template usages..
|
2019-01-14 20:36:23 +01:00
|
|
|
replaceTemplateUsage(instantiation, typeStringsUsedInTemplateInstantiation, newName);
|
2018-10-14 16:57:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// process uninstantiated templates
|
|
|
|
// TODO: remove the specialized check and handle all uninstantiated templates someday.
|
2018-12-31 17:19:34 +01:00
|
|
|
if (!instantiated && specialized) {
|
2019-08-10 08:42:12 +02:00
|
|
|
Token * tok2 = const_cast<Token *>(templateDeclaration.nameToken());
|
2018-10-14 16:57:07 +02:00
|
|
|
if (mErrorLogger && !mTokenList.getFiles().empty())
|
|
|
|
mErrorLogger->reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue());
|
2022-12-30 21:21:05 +01:00
|
|
|
|
|
|
|
if (maxtime > 0 && std::time(nullptr) > maxtime) {
|
2023-03-07 12:12:31 +01:00
|
|
|
if (mSettings.debugwarnings) {
|
2022-12-30 21:21:05 +01:00
|
|
|
ErrorMessage::FileLocation loc;
|
|
|
|
loc.setfile(mTokenList.getFiles()[0]);
|
|
|
|
ErrorMessage errmsg({std::move(loc)},
|
|
|
|
emptyString,
|
|
|
|
Severity::debug,
|
2023-01-10 15:15:27 +01:00
|
|
|
"Template instantiation maximum time exceeded",
|
2022-12-30 21:21:05 +01:00
|
|
|
"templateMaxTime",
|
|
|
|
Certainty::normal);
|
|
|
|
mErrorLogger->reportErr(errmsg);
|
|
|
|
}
|
2018-10-14 16:57:07 +02:00
|
|
|
return false;
|
2022-12-30 21:21:05 +01:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
assert(mTokenList.validateToken(tok2)); // that assertion fails on examples from #6021
|
|
|
|
|
|
|
|
Token *startToken = tok2;
|
2020-02-04 16:35:30 +01:00
|
|
|
while (Token::Match(startToken->tokAt(-2), ">|%name% :: %name%")) {
|
2018-10-14 16:57:07 +02:00
|
|
|
if (startToken->strAt(-2) == ">") {
|
2023-02-08 21:07:16 +01:00
|
|
|
Token * tok3 = startToken->tokAt(-2)->findOpeningBracket();
|
2018-10-14 16:57:07 +02:00
|
|
|
if (tok3)
|
|
|
|
startToken = tok3->previous();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
startToken = startToken->tokAt(-2);
|
|
|
|
}
|
|
|
|
|
2020-02-19 07:55:04 +01:00
|
|
|
// TODO: re-enable when specialized check is removed
|
|
|
|
// if (Token::Match(startToken->previous(), ";|{|}|=|const") &&
|
|
|
|
// (!specialized && !instantiateMatch(tok2, typeParametersInDeclaration.size(), isfunc ? "(" : isVar ? ";|%op%|(" : "*|&|::| %name%")))
|
|
|
|
// return false;
|
2018-10-14 16:57:07 +02:00
|
|
|
|
|
|
|
// already simplified
|
2018-12-29 11:19:53 +01:00
|
|
|
if (!Token::Match(tok2, "%name% <"))
|
2018-10-14 16:57:07 +02:00
|
|
|
return false;
|
|
|
|
|
2019-08-10 08:42:12 +02:00
|
|
|
if (!matchSpecialization(templateDeclaration.nameToken(), tok2, specializations))
|
2018-10-14 16:57:07 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// New type..
|
|
|
|
mTypesUsedInTemplateInstantiation.clear();
|
|
|
|
std::list<std::string> typeStringsUsedInTemplateInstantiation;
|
|
|
|
std::string typeForNewName = getNewName(tok2, typeStringsUsedInTemplateInstantiation);
|
|
|
|
|
|
|
|
if (typeForNewName.empty()) {
|
|
|
|
if (printDebug && mErrorLogger) {
|
|
|
|
std::list<const Token *> callstack(1, tok2);
|
2020-05-23 07:16:49 +02:00
|
|
|
mErrorLogger->reportErr(ErrorMessage(callstack, &mTokenList, Severity::debug, "debug",
|
2021-02-24 22:00:06 +01:00
|
|
|
"Failed to instantiate template \"" + templateDeclaration.name() + "\". The checking continues anyway.", Certainty::normal));
|
2018-10-14 16:57:07 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// New classname/funcname..
|
2019-08-10 08:42:12 +02:00
|
|
|
const std::string newName(templateDeclaration.name() + " < " + typeForNewName + " >");
|
|
|
|
const std::string newFullName(templateDeclaration.scope() + (templateDeclaration.scope().empty() ? "" : " :: ") + newName);
|
2019-01-14 20:36:23 +01:00
|
|
|
|
2019-05-07 10:26:41 +02:00
|
|
|
if (expandedtemplates.insert(newFullName).second) {
|
2019-01-18 21:12:39 +01:00
|
|
|
expandTemplate(templateDeclaration, templateDeclaration, typeParametersInDeclaration, newName, !specialized && !isVar);
|
2018-10-14 16:57:07 +02:00
|
|
|
instantiated = true;
|
2019-06-23 10:55:09 +02:00
|
|
|
mChanged = true;
|
2018-10-14 16:57:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace all these template usages..
|
2019-01-14 20:36:23 +01:00
|
|
|
replaceTemplateUsage(templateDeclaration, typeStringsUsedInTemplateInstantiation, newName);
|
2017-06-08 00:16:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Template has been instantiated .. then remove the template declaration
|
|
|
|
return instantiated;
|
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2017-12-26 22:34:39 +01:00
|
|
|
static bool matchTemplateParameters(const Token *nameTok, const std::list<std::string> &strings)
|
|
|
|
{
|
2022-12-20 20:32:16 +01:00
|
|
|
std::list<std::string>::const_iterator it = strings.cbegin();
|
2017-12-26 22:34:39 +01:00
|
|
|
const Token *tok = nameTok->tokAt(2);
|
2021-03-04 08:43:22 +01:00
|
|
|
const Token *end = nameTok->next()->findClosingBracket();
|
|
|
|
if (!end)
|
|
|
|
return false;
|
2022-12-20 20:32:16 +01:00
|
|
|
while (tok && tok != end && it != strings.cend()) {
|
2021-03-04 08:43:22 +01:00
|
|
|
if (tok->isUnsigned()) {
|
|
|
|
if (*it != "unsigned")
|
|
|
|
return false;
|
|
|
|
else {
|
|
|
|
++it;
|
2022-12-20 20:32:16 +01:00
|
|
|
if (it == strings.cend())
|
2021-03-04 08:43:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (tok->isSigned()) {
|
|
|
|
if (*it != "signed")
|
|
|
|
return false;
|
|
|
|
else {
|
|
|
|
++it;
|
2022-12-20 20:32:16 +01:00
|
|
|
if (it == strings.cend())
|
2021-03-04 08:43:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tok->isLong()) {
|
|
|
|
if (*it != "long")
|
|
|
|
return false;
|
|
|
|
else {
|
|
|
|
++it;
|
2022-12-20 20:32:16 +01:00
|
|
|
if (it == strings.cend())
|
2021-03-04 08:43:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*it != tok->str())
|
|
|
|
return false;
|
2017-12-26 22:34:39 +01:00
|
|
|
tok = tok->next();
|
|
|
|
++it;
|
|
|
|
}
|
2022-12-20 20:32:16 +01:00
|
|
|
return it == strings.cend() && tok && tok->str() == ">";
|
2017-12-26 22:34:39 +01:00
|
|
|
}
|
2017-06-08 00:16:43 +02:00
|
|
|
|
2018-10-26 14:20:13 +02:00
|
|
|
void TemplateSimplifier::replaceTemplateUsage(
|
2019-01-14 20:36:23 +01:00
|
|
|
const TokenAndName &instantiation,
|
2018-10-26 14:20:13 +02:00
|
|
|
const std::list<std::string> &typeStringsUsedInTemplateInstantiation,
|
|
|
|
const std::string &newName)
|
2017-06-08 00:16:43 +02:00
|
|
|
{
|
2021-08-07 20:51:18 +02:00
|
|
|
std::list<std::pair<Token *, Token *>> removeTokens;
|
2019-01-14 20:36:23 +01:00
|
|
|
for (Token *nameTok = mTokenList.front(); nameTok; nameTok = nameTok->next()) {
|
|
|
|
if (!Token::Match(nameTok, "%name% <") ||
|
|
|
|
Token::Match(nameTok, "template|const_cast|dynamic_cast|reinterpret_cast|static_cast"))
|
2017-12-26 10:55:18 +01:00
|
|
|
continue;
|
|
|
|
|
2020-05-15 09:02:59 +02:00
|
|
|
std::set<TemplateSimplifier::TokenAndName*>* pointers = nameTok->templateSimplifierPointers();
|
2019-01-14 20:36:23 +01:00
|
|
|
|
|
|
|
// check if instantiation matches token instantiation from pointer
|
2020-05-15 09:02:59 +02:00
|
|
|
if (pointers && pointers->size()) {
|
2019-01-14 20:36:23 +01:00
|
|
|
// check full name
|
2020-05-15 09:02:59 +02:00
|
|
|
if (instantiation.fullName() != (*pointers->begin())->fullName()) {
|
2019-01-14 20:36:23 +01:00
|
|
|
// FIXME: fallback to just matching name
|
2019-08-10 08:42:12 +02:00
|
|
|
if (nameTok->str() != instantiation.name())
|
2019-01-14 20:36:23 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no pointer available look at tokens directly
|
|
|
|
else {
|
|
|
|
// FIXME: fallback to just matching name
|
2019-08-10 08:42:12 +02:00
|
|
|
if (nameTok->str() != instantiation.name())
|
2019-01-14 20:36:23 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!matchTemplateParameters(nameTok, typeStringsUsedInTemplateInstantiation))
|
2017-06-08 00:16:43 +02:00
|
|
|
continue;
|
|
|
|
|
2021-03-04 08:43:22 +01:00
|
|
|
Token *tok2 = nameTok->next()->findClosingBracket();
|
2017-06-08 00:16:43 +02:00
|
|
|
|
|
|
|
if (!tok2)
|
|
|
|
break;
|
|
|
|
|
2021-03-04 08:43:22 +01:00
|
|
|
const Token * const nameTok1 = nameTok;
|
|
|
|
nameTok->str(newName);
|
|
|
|
|
2017-06-08 00:16:43 +02:00
|
|
|
// matching template usage => replace tokens..
|
|
|
|
// Foo < int > => Foo<int>
|
2023-02-08 21:07:16 +01:00
|
|
|
for (const Token *tok = nameTok1->next(); tok != tok2; tok = tok->next()) {
|
2021-03-04 08:43:22 +01:00
|
|
|
if (tok->isName() && tok->templateSimplifierPointers() && !tok->templateSimplifierPointers()->empty()) {
|
|
|
|
std::list<TokenAndName>::iterator ti;
|
|
|
|
for (ti = mTemplateInstantiations.begin(); ti != mTemplateInstantiations.end();) {
|
|
|
|
if (ti->token() == tok) {
|
2022-10-02 07:13:31 +02:00
|
|
|
ti = mTemplateInstantiations.erase(ti);
|
2021-03-04 08:43:22 +01:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
++ti;
|
2017-12-26 10:55:18 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-08 00:16:43 +02:00
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
2021-03-04 08:43:22 +01:00
|
|
|
// Fix crash in #9007
|
|
|
|
if (Token::simpleMatch(nameTok->previous(), ">"))
|
|
|
|
mTemplateNamePos.erase(nameTok->previous());
|
|
|
|
removeTokens.emplace_back(nameTok, tok2->next());
|
2012-07-29 16:01:05 +02:00
|
|
|
|
2017-06-08 00:16:43 +02:00
|
|
|
nameTok = tok2;
|
|
|
|
}
|
|
|
|
while (!removeTokens.empty()) {
|
2018-09-01 11:26:10 +02:00
|
|
|
eraseTokens(removeTokens.back().first, removeTokens.back().second);
|
2017-06-08 00:16:43 +02:00
|
|
|
removeTokens.pop_back();
|
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 08:22:34 +02:00
|
|
|
static bool specMatch(
|
|
|
|
const TemplateSimplifier::TokenAndName &spec,
|
|
|
|
const TemplateSimplifier::TokenAndName &decl)
|
|
|
|
{
|
|
|
|
// make sure decl is really a declaration
|
2019-09-01 09:56:33 +02:00
|
|
|
if (decl.isPartialSpecialization() || decl.isSpecialization() || decl.isAlias() || decl.isFriend())
|
2019-06-07 08:22:34 +02:00
|
|
|
return false;
|
|
|
|
|
2020-12-01 09:43:16 +01:00
|
|
|
if (!spec.isSameFamily(decl))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// make sure the scopes and names match
|
|
|
|
if (spec.fullName() == decl.fullName()) {
|
|
|
|
if (spec.isFunction()) {
|
|
|
|
std::vector<const Token*> specArgs;
|
|
|
|
std::vector<const Token*> declArgs;
|
|
|
|
getFunctionArguments(spec.nameToken(), specArgs);
|
|
|
|
getFunctionArguments(decl.nameToken(), declArgs);
|
|
|
|
|
|
|
|
if (specArgs.size() == declArgs.size()) {
|
|
|
|
// @todo make sure function parameters also match
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-06-07 08:22:34 +02:00
|
|
|
}
|
|
|
|
|
2019-01-27 07:46:27 +01:00
|
|
|
void TemplateSimplifier::getSpecializations()
|
2018-12-29 11:19:53 +01:00
|
|
|
{
|
|
|
|
// try to locate a matching declaration for each user defined specialization
|
2022-05-29 17:06:33 +02:00
|
|
|
for (const auto& spec : mTemplateDeclarations) {
|
2019-01-27 07:46:27 +01:00
|
|
|
if (spec.isSpecialization()) {
|
2022-12-30 15:13:47 +01:00
|
|
|
auto it = std::find_if(mTemplateDeclarations.cbegin(), mTemplateDeclarations.cend(), [&](const TokenAndName& decl) {
|
2022-10-16 13:46:26 +02:00
|
|
|
return specMatch(spec, decl);
|
|
|
|
});
|
2022-12-30 15:13:47 +01:00
|
|
|
if (it != mTemplateDeclarations.cend())
|
2022-10-16 13:46:26 +02:00
|
|
|
mTemplateSpecializationMap[spec.token()] = it->token();
|
|
|
|
else {
|
2022-12-30 15:13:47 +01:00
|
|
|
it = std::find_if(mTemplateForwardDeclarations.cbegin(), mTemplateForwardDeclarations.cend(), [&](const TokenAndName& decl) {
|
2022-10-16 13:46:26 +02:00
|
|
|
return specMatch(spec, decl);
|
|
|
|
});
|
2022-12-30 15:13:47 +01:00
|
|
|
if (it != mTemplateForwardDeclarations.cend())
|
2022-10-16 13:46:26 +02:00
|
|
|
mTemplateSpecializationMap[spec.token()] = it->token();
|
2019-01-27 07:46:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplateSimplifier::getPartialSpecializations()
|
|
|
|
{
|
|
|
|
// try to locate a matching declaration for each user defined partial specialization
|
2022-05-29 17:06:33 +02:00
|
|
|
for (const auto& spec : mTemplateDeclarations) {
|
2019-01-27 07:46:27 +01:00
|
|
|
if (spec.isPartialSpecialization()) {
|
2022-12-30 15:13:47 +01:00
|
|
|
auto it = std::find_if(mTemplateDeclarations.cbegin(), mTemplateDeclarations.cend(), [&](const TokenAndName& decl) {
|
2022-10-16 13:46:26 +02:00
|
|
|
return specMatch(spec, decl);
|
|
|
|
});
|
2022-12-30 15:13:47 +01:00
|
|
|
if (it != mTemplateDeclarations.cend())
|
2022-10-16 13:46:26 +02:00
|
|
|
mTemplatePartialSpecializationMap[spec.token()] = it->token();
|
|
|
|
else {
|
2022-12-30 15:13:47 +01:00
|
|
|
it = std::find_if(mTemplateForwardDeclarations.cbegin(), mTemplateForwardDeclarations.cend(), [&](const TokenAndName& decl) {
|
2022-10-16 13:46:26 +02:00
|
|
|
return specMatch(spec, decl);
|
|
|
|
});
|
2022-12-30 15:13:47 +01:00
|
|
|
if (it != mTemplateForwardDeclarations.cend())
|
2022-10-16 13:46:26 +02:00
|
|
|
mTemplatePartialSpecializationMap[spec.token()] = it->token();
|
2018-12-29 11:19:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:38:59 +02:00
|
|
|
void TemplateSimplifier::fixForwardDeclaredDefaultArgumentValues()
|
|
|
|
{
|
|
|
|
// try to locate a matching declaration for each forward declaration
|
2018-11-10 16:41:14 +01:00
|
|
|
for (const auto & forwardDecl : mTemplateForwardDeclarations) {
|
2018-10-24 14:38:59 +02:00
|
|
|
std::vector<const Token *> params1;
|
|
|
|
|
2019-08-10 08:42:12 +02:00
|
|
|
getTemplateParametersInDeclaration(forwardDecl.token()->tokAt(2), params1);
|
2018-10-24 14:38:59 +02:00
|
|
|
|
|
|
|
for (auto & decl : mTemplateDeclarations) {
|
2019-09-01 09:56:33 +02:00
|
|
|
// skip partializations, type aliases and friends
|
|
|
|
if (decl.isPartialSpecialization() || decl.isAlias() || decl.isFriend())
|
2019-02-09 08:34:59 +01:00
|
|
|
continue;
|
|
|
|
|
2018-10-24 14:38:59 +02:00
|
|
|
std::vector<const Token *> params2;
|
|
|
|
|
2019-08-10 08:42:12 +02:00
|
|
|
getTemplateParametersInDeclaration(decl.token()->tokAt(2), params2);
|
2018-10-24 14:38:59 +02:00
|
|
|
|
|
|
|
// make sure the number of arguments match
|
|
|
|
if (params1.size() == params2.size()) {
|
|
|
|
// make sure the scopes and names match
|
2019-08-10 08:42:12 +02:00
|
|
|
if (forwardDecl.fullName() == decl.fullName()) {
|
2018-11-23 11:36:09 +01:00
|
|
|
// save forward declaration for lookup later
|
2019-08-10 08:42:12 +02:00
|
|
|
if ((decl.nameToken()->strAt(1) == "(" && forwardDecl.nameToken()->strAt(1) == "(") ||
|
|
|
|
(decl.nameToken()->strAt(1) == "{" && forwardDecl.nameToken()->strAt(1) == ";")) {
|
|
|
|
mTemplateForwardDeclarationsMap[decl.token()] = forwardDecl.token();
|
2018-11-23 11:36:09 +01:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:38:59 +02:00
|
|
|
for (size_t k = 0; k < params1.size(); k++) {
|
|
|
|
// copy default value to declaration if not present
|
|
|
|
if (params1[k]->strAt(1) == "=" && params2[k]->strAt(1) != "=") {
|
2019-02-09 08:34:59 +01:00
|
|
|
int level = 0;
|
|
|
|
const Token *end = params1[k]->next();
|
|
|
|
while (end && !(level == 0 && Token::Match(end, ",|>"))) {
|
|
|
|
if (Token::Match(end, "{|(|<"))
|
|
|
|
level++;
|
|
|
|
else if (Token::Match(end, "}|)|>"))
|
|
|
|
level--;
|
|
|
|
end = end->next();
|
|
|
|
}
|
|
|
|
if (end)
|
|
|
|
TokenList::copyTokens(const_cast<Token *>(params2[k]), params1[k]->next(), end->previous());
|
2018-10-24 14:38:59 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-30 21:16:11 +01:00
|
|
|
|
|
|
|
// update parameter end pointer
|
2019-08-10 08:42:12 +02:00
|
|
|
decl.paramEnd(decl.token()->next()->findClosingBracket());
|
2018-10-24 14:38:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2019-01-14 08:29:32 +01:00
|
|
|
void TemplateSimplifier::printOut(const TokenAndName &tokenAndName, const std::string &indent) const
|
|
|
|
{
|
|
|
|
std::cout << indent << "token: ";
|
2019-08-10 08:42:12 +02:00
|
|
|
if (tokenAndName.token())
|
|
|
|
std::cout << "\"" << tokenAndName.token()->str() << "\" " << mTokenList.fileLine(tokenAndName.token());
|
2019-01-14 08:29:32 +01:00
|
|
|
else
|
|
|
|
std::cout << "nullptr";
|
|
|
|
std::cout << std::endl;
|
2019-08-10 08:42:12 +02:00
|
|
|
std::cout << indent << "scope: \"" << tokenAndName.scope() << "\"" << std::endl;
|
|
|
|
std::cout << indent << "name: \"" << tokenAndName.name() << "\"" << std::endl;
|
|
|
|
std::cout << indent << "fullName: \"" << tokenAndName.fullName() << "\"" << std::endl;
|
2019-01-14 08:29:32 +01:00
|
|
|
std::cout << indent << "nameToken: ";
|
2019-08-10 08:42:12 +02:00
|
|
|
if (tokenAndName.nameToken())
|
|
|
|
std::cout << "\"" << tokenAndName.nameToken()->str() << "\" " << mTokenList.fileLine(tokenAndName.nameToken());
|
2019-01-14 08:29:32 +01:00
|
|
|
else
|
|
|
|
std::cout << "nullptr";
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << indent << "paramEnd: ";
|
2019-08-10 08:42:12 +02:00
|
|
|
if (tokenAndName.paramEnd())
|
|
|
|
std::cout << "\"" << tokenAndName.paramEnd()->str() << "\" " << mTokenList.fileLine(tokenAndName.paramEnd());
|
2019-01-14 08:29:32 +01:00
|
|
|
else
|
|
|
|
std::cout << "nullptr";
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << indent << "flags: ";
|
|
|
|
if (tokenAndName.isClass())
|
|
|
|
std::cout << " isClass";
|
|
|
|
if (tokenAndName.isFunction())
|
|
|
|
std::cout << " isFunction";
|
|
|
|
if (tokenAndName.isVariable())
|
|
|
|
std::cout << " isVariable";
|
|
|
|
if (tokenAndName.isAlias())
|
|
|
|
std::cout << " isAlias";
|
2019-01-27 07:46:27 +01:00
|
|
|
if (tokenAndName.isSpecialization())
|
|
|
|
std::cout << " isSpecialization";
|
|
|
|
if (tokenAndName.isPartialSpecialization())
|
|
|
|
std::cout << " isPartialSpecialization";
|
2019-01-18 21:12:39 +01:00
|
|
|
if (tokenAndName.isForwardDeclaration())
|
|
|
|
std::cout << " isForwardDeclaration";
|
2019-05-23 20:53:26 +02:00
|
|
|
if (tokenAndName.isVariadic())
|
|
|
|
std::cout << " isVariadic";
|
2019-09-01 09:56:33 +02:00
|
|
|
if (tokenAndName.isFriend())
|
|
|
|
std::cout << " isFriend";
|
2019-01-14 08:29:32 +01:00
|
|
|
std::cout << std::endl;
|
2019-08-10 08:42:12 +02:00
|
|
|
if (tokenAndName.token() && !tokenAndName.paramEnd() && tokenAndName.token()->strAt(1) == "<") {
|
|
|
|
const Token *end = tokenAndName.token()->next()->findClosingBracket();
|
2019-01-14 08:29:32 +01:00
|
|
|
if (end) {
|
2019-08-10 08:42:12 +02:00
|
|
|
const Token *start = tokenAndName.token()->next();
|
2019-01-14 08:29:32 +01:00
|
|
|
std::cout << indent << "type: ";
|
|
|
|
while (start && start != end) {
|
2021-02-28 21:52:14 +01:00
|
|
|
if (start->isUnsigned())
|
|
|
|
std::cout << "unsigned";
|
|
|
|
else if (start->isSigned())
|
|
|
|
std::cout << "signed";
|
|
|
|
if (start->isLong())
|
|
|
|
std::cout << "long";
|
2019-01-14 08:29:32 +01:00
|
|
|
std::cout << start->str();
|
|
|
|
start = start->next();
|
|
|
|
}
|
|
|
|
std::cout << end->str() << std::endl;
|
|
|
|
}
|
2019-08-10 08:42:12 +02:00
|
|
|
} else if (tokenAndName.isAlias() && tokenAndName.paramEnd()) {
|
2019-06-01 10:52:29 +02:00
|
|
|
if (tokenAndName.aliasStartToken()) {
|
|
|
|
std::cout << indent << "aliasStartToken: \"" << tokenAndName.aliasStartToken()->str() << "\" "
|
|
|
|
<< mTokenList.fileLine(tokenAndName.aliasStartToken()) << std::endl;
|
|
|
|
}
|
|
|
|
if (tokenAndName.aliasEndToken()) {
|
|
|
|
std::cout << indent << "aliasEndToken: \"" << tokenAndName.aliasEndToken()->str() << "\" "
|
|
|
|
<< mTokenList.fileLine(tokenAndName.aliasEndToken()) << std::endl;
|
|
|
|
}
|
2019-01-14 08:29:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplateSimplifier::printOut(const std::string & text) const
|
|
|
|
{
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << text << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << "mTemplateDeclarations: " << mTemplateDeclarations.size() << std::endl;
|
|
|
|
int count = 0;
|
|
|
|
for (const auto & decl : mTemplateDeclarations) {
|
|
|
|
std::cout << "mTemplateDeclarations[" << count++ << "]:" << std::endl;
|
|
|
|
printOut(decl);
|
|
|
|
}
|
|
|
|
std::cout << "mTemplateForwardDeclarations: " << mTemplateForwardDeclarations.size() << std::endl;
|
|
|
|
count = 0;
|
|
|
|
for (const auto & decl : mTemplateForwardDeclarations) {
|
|
|
|
std::cout << "mTemplateForwardDeclarations[" << count++ << "]:" << std::endl;
|
|
|
|
printOut(decl);
|
|
|
|
}
|
2019-01-27 07:46:27 +01:00
|
|
|
std::cout << "mTemplateForwardDeclarationsMap: " << mTemplateForwardDeclarationsMap.size() << std::endl;
|
|
|
|
unsigned int mapIndex = 0;
|
|
|
|
for (const auto & mapItem : mTemplateForwardDeclarationsMap) {
|
|
|
|
unsigned int declIndex = 0;
|
|
|
|
for (const auto & decl : mTemplateDeclarations) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mapItem.first == decl.token()) {
|
2019-01-27 07:46:27 +01:00
|
|
|
unsigned int forwardIndex = 0;
|
|
|
|
for (const auto & forwardDecl : mTemplateForwardDeclarations) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mapItem.second == forwardDecl.token()) {
|
2019-01-27 07:46:27 +01:00
|
|
|
std::cout << "mTemplateForwardDeclarationsMap[" << mapIndex << "]:" << std::endl;
|
|
|
|
std::cout << " mTemplateDeclarations[" << declIndex
|
|
|
|
<< "] => mTemplateForwardDeclarations[" << forwardIndex << "]" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
forwardIndex++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
declIndex++;
|
|
|
|
}
|
|
|
|
mapIndex++;
|
|
|
|
}
|
|
|
|
std::cout << "mTemplateSpecializationMap: " << mTemplateSpecializationMap.size() << std::endl;
|
|
|
|
for (const auto & mapItem : mTemplateSpecializationMap) {
|
|
|
|
unsigned int decl1Index = 0;
|
|
|
|
for (const auto & decl1 : mTemplateDeclarations) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (decl1.isSpecialization() && mapItem.first == decl1.token()) {
|
2019-09-25 15:25:19 +02:00
|
|
|
bool found = false;
|
2019-01-27 07:46:27 +01:00
|
|
|
unsigned int decl2Index = 0;
|
|
|
|
for (const auto & decl2 : mTemplateDeclarations) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mapItem.second == decl2.token()) {
|
2019-01-27 07:46:27 +01:00
|
|
|
std::cout << "mTemplateSpecializationMap[" << mapIndex << "]:" << std::endl;
|
|
|
|
std::cout << " mTemplateDeclarations[" << decl1Index
|
|
|
|
<< "] => mTemplateDeclarations[" << decl2Index << "]" << std::endl;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
decl2Index++;
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
decl2Index = 0;
|
|
|
|
for (const auto & decl2 : mTemplateForwardDeclarations) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mapItem.second == decl2.token()) {
|
2019-01-27 07:46:27 +01:00
|
|
|
std::cout << "mTemplateSpecializationMap[" << mapIndex << "]:" << std::endl;
|
|
|
|
std::cout << " mTemplateDeclarations[" << decl1Index
|
|
|
|
<< "] => mTemplateForwardDeclarations[" << decl2Index << "]" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
decl2Index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
decl1Index++;
|
|
|
|
}
|
|
|
|
mapIndex++;
|
|
|
|
}
|
|
|
|
std::cout << "mTemplatePartialSpecializationMap: " << mTemplatePartialSpecializationMap.size() << std::endl;
|
|
|
|
for (const auto & mapItem : mTemplatePartialSpecializationMap) {
|
|
|
|
unsigned int decl1Index = 0;
|
|
|
|
for (const auto & decl1 : mTemplateDeclarations) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mapItem.first == decl1.token()) {
|
2019-09-25 15:25:19 +02:00
|
|
|
bool found = false;
|
2019-01-27 07:46:27 +01:00
|
|
|
unsigned int decl2Index = 0;
|
|
|
|
for (const auto & decl2 : mTemplateDeclarations) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mapItem.second == decl2.token()) {
|
2019-01-27 07:46:27 +01:00
|
|
|
std::cout << "mTemplatePartialSpecializationMap[" << mapIndex << "]:" << std::endl;
|
|
|
|
std::cout << " mTemplateDeclarations[" << decl1Index
|
|
|
|
<< "] => mTemplateDeclarations[" << decl2Index << "]" << std::endl;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
decl2Index++;
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
decl2Index = 0;
|
|
|
|
for (const auto & decl2 : mTemplateForwardDeclarations) {
|
2019-08-10 08:42:12 +02:00
|
|
|
if (mapItem.second == decl2.token()) {
|
2019-01-27 07:46:27 +01:00
|
|
|
std::cout << "mTemplatePartialSpecializationMap[" << mapIndex << "]:" << std::endl;
|
|
|
|
std::cout << " mTemplateDeclarations[" << decl1Index
|
|
|
|
<< "] => mTemplateForwardDeclarations[" << decl2Index << "]" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
decl2Index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
decl1Index++;
|
|
|
|
}
|
|
|
|
mapIndex++;
|
|
|
|
}
|
2019-01-14 08:29:32 +01:00
|
|
|
std::cout << "mTemplateInstantiations: " << mTemplateInstantiations.size() << std::endl;
|
|
|
|
count = 0;
|
|
|
|
for (const auto & decl : mTemplateInstantiations) {
|
|
|
|
std::cout << "mTemplateInstantiations[" << count++ << "]:" << std::endl;
|
|
|
|
printOut(decl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-09 20:33:11 +01:00
|
|
|
void TemplateSimplifier::simplifyTemplates(
|
2016-02-12 12:05:32 +01:00
|
|
|
const std::time_t maxtime,
|
2018-11-10 16:41:14 +01:00
|
|
|
bool &codeWithTemplates)
|
2012-01-09 20:33:11 +01:00
|
|
|
{
|
2019-09-09 21:46:21 +02:00
|
|
|
// convert "sizeof ..." to "sizeof..."
|
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
|
|
|
if (Token::simpleMatch(tok, "sizeof ...")) {
|
|
|
|
tok->str("sizeof...");
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:46:16 +02:00
|
|
|
// Remove "typename" unless used in template arguments or using type alias..
|
2019-02-09 08:34:59 +01:00
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
2019-04-21 06:46:16 +02:00
|
|
|
if (Token::Match(tok, "typename %name%") && !Token::Match(tok->tokAt(-3), "using %name% ="))
|
2019-02-09 08:34:59 +01:00
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, "template <")) {
|
2019-04-04 06:07:49 +02:00
|
|
|
tok = tok->next()->findClosingBracket();
|
2019-02-09 08:34:59 +01:00
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 12:12:31 +01:00
|
|
|
if (mSettings.standards.cpp >= Standards::CPP20) {
|
2021-04-20 15:39:04 +02:00
|
|
|
// Remove concepts/requires
|
|
|
|
// TODO concepts are not removed yet
|
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
|
|
|
if (!Token::Match(tok, ")|>|>> requires %name%|("))
|
|
|
|
continue;
|
|
|
|
Token *end = skipRequires(tok->next());
|
|
|
|
if (end)
|
|
|
|
Token::eraseTokens(tok, end);
|
|
|
|
}
|
2021-05-09 18:47:02 +02:00
|
|
|
|
|
|
|
// explicit(bool)
|
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
2021-05-09 23:02:35 +02:00
|
|
|
if (Token::simpleMatch(tok, "explicit (")) {
|
2022-10-02 07:12:40 +02:00
|
|
|
const bool isFalse = Token::simpleMatch(tok->tokAt(2), "false )");
|
2021-05-09 18:47:02 +02:00
|
|
|
Token::eraseTokens(tok, tok->linkAt(1)->next());
|
|
|
|
if (isFalse)
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 15:39:04 +02:00
|
|
|
}
|
|
|
|
|
2023-03-07 12:12:31 +01:00
|
|
|
mTokenizer.calculateScopes();
|
2019-08-07 08:05:02 +02:00
|
|
|
|
2019-06-23 10:55:09 +02:00
|
|
|
unsigned int passCount = 0;
|
|
|
|
const unsigned int passCountMax = 10;
|
|
|
|
for (; passCount < passCountMax; ++passCount) {
|
|
|
|
if (passCount) {
|
2019-03-26 07:09:56 +01:00
|
|
|
// it may take more than one pass to simplify type aliases
|
2019-06-23 10:55:09 +02:00
|
|
|
bool usingChanged = false;
|
2023-03-07 12:12:31 +01:00
|
|
|
while (mTokenizer.simplifyUsing())
|
2019-06-23 10:55:09 +02:00
|
|
|
usingChanged = true;
|
|
|
|
|
|
|
|
if (!usingChanged && !mChanged)
|
|
|
|
break;
|
2019-03-26 07:09:56 +01:00
|
|
|
|
2019-06-23 10:55:09 +02:00
|
|
|
mChanged = usingChanged;
|
2018-11-10 16:41:14 +01:00
|
|
|
mTemplateDeclarations.clear();
|
|
|
|
mTemplateForwardDeclarations.clear();
|
2018-11-23 11:36:09 +01:00
|
|
|
mTemplateForwardDeclarationsMap.clear();
|
2019-01-27 07:46:27 +01:00
|
|
|
mTemplateSpecializationMap.clear();
|
|
|
|
mTemplatePartialSpecializationMap.clear();
|
2018-10-14 16:57:07 +02:00
|
|
|
mTemplateInstantiations.clear();
|
|
|
|
mInstantiatedTemplates.clear();
|
2018-12-17 05:58:48 +01:00
|
|
|
mExplicitInstantiationsToDelete.clear();
|
2019-05-25 10:04:01 +02:00
|
|
|
mTemplateNamePos.clear();
|
2018-10-14 16:57:07 +02:00
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2022-10-02 07:12:40 +02:00
|
|
|
const bool hasTemplates = getTemplateDeclarations();
|
2018-11-10 16:41:14 +01:00
|
|
|
|
2023-03-08 18:59:34 +01:00
|
|
|
if (passCount == 0) {
|
2018-11-10 16:41:14 +01:00
|
|
|
codeWithTemplates = hasTemplates;
|
2023-03-08 18:59:34 +01:00
|
|
|
mDump.clear();
|
|
|
|
for (const TokenAndName& t: mTemplateDeclarations)
|
|
|
|
mDump += t.dump(mTokenizer.list.getFiles());
|
|
|
|
for (const TokenAndName& t: mTemplateForwardDeclarations)
|
|
|
|
mDump += t.dump(mTokenizer.list.getFiles());
|
|
|
|
if (!mDump.empty())
|
|
|
|
mDump = " <TemplateSimplifier>\n" + mDump + " </TemplateSimplifier>\n";
|
|
|
|
}
|
2018-11-10 16:41:14 +01:00
|
|
|
|
|
|
|
// Make sure there is something to simplify.
|
2019-06-01 10:52:29 +02:00
|
|
|
if (mTemplateDeclarations.empty() && mTemplateForwardDeclarations.empty())
|
2018-11-10 16:41:14 +01:00
|
|
|
return;
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2023-03-07 12:12:31 +01:00
|
|
|
if (mSettings.debugtemplate && mSettings.debugnormal) {
|
2019-08-17 07:38:07 +02:00
|
|
|
std::string title("Template Simplifier pass " + std::to_string(passCount + 1));
|
|
|
|
mTokenList.front()->printOut(title.c_str(), mTokenList.getFiles());
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:38:59 +02:00
|
|
|
// Copy default argument values from forward declaration to declaration
|
|
|
|
fixForwardDeclaredDefaultArgumentValues();
|
|
|
|
|
2018-12-29 11:19:53 +01:00
|
|
|
// Locate user defined specializations.
|
2019-01-27 07:46:27 +01:00
|
|
|
getSpecializations();
|
|
|
|
|
|
|
|
// Locate user defined partial specializations.
|
|
|
|
getPartialSpecializations();
|
2018-12-29 11:19:53 +01:00
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
// Locate possible instantiations of templates..
|
|
|
|
getTemplateInstantiations();
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
// Template arguments with default values
|
|
|
|
useDefaultArgumentValues();
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
simplifyTemplateAliases();
|
2017-12-29 22:47:07 +01:00
|
|
|
|
2023-03-07 12:12:31 +01:00
|
|
|
if (mSettings.debugtemplate)
|
2019-06-23 10:55:09 +02:00
|
|
|
printOut("### Template Simplifier pass " + std::to_string(passCount + 1) + " ###");
|
2019-01-14 08:29:32 +01:00
|
|
|
|
2018-11-10 16:41:14 +01:00
|
|
|
std::set<std::string> expandedtemplates;
|
|
|
|
|
2022-12-30 15:13:47 +01:00
|
|
|
for (std::list<TokenAndName>::const_reverse_iterator iter1 = mTemplateDeclarations.crbegin(); iter1 != mTemplateDeclarations.crend(); ++iter1) {
|
2019-09-01 09:56:33 +02:00
|
|
|
if (iter1->isAlias() || iter1->isFriend())
|
2019-06-01 10:52:29 +02:00
|
|
|
continue;
|
|
|
|
|
2017-12-30 22:14:48 +01:00
|
|
|
// get specializations..
|
|
|
|
std::list<const Token *> specializations;
|
2022-12-20 20:32:16 +01:00
|
|
|
for (std::list<TokenAndName>::const_iterator iter2 = mTemplateDeclarations.cbegin(); iter2 != mTemplateDeclarations.cend(); ++iter2) {
|
2019-09-01 09:56:33 +02:00
|
|
|
if (iter2->isAlias() || iter2->isFriend())
|
2019-06-01 10:52:29 +02:00
|
|
|
continue;
|
|
|
|
|
2019-08-10 08:42:12 +02:00
|
|
|
if (iter1->fullName() == iter2->fullName())
|
|
|
|
specializations.push_back(iter2->nameToken());
|
2017-12-30 22:14:48 +01:00
|
|
|
}
|
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
const bool instantiated = simplifyTemplateInstantiations(
|
2021-08-07 20:51:18 +02:00
|
|
|
*iter1,
|
|
|
|
specializations,
|
|
|
|
maxtime,
|
|
|
|
expandedtemplates);
|
2022-06-28 22:43:34 +02:00
|
|
|
if (instantiated) {
|
2018-09-01 11:26:10 +02:00
|
|
|
mInstantiatedTemplates.push_back(*iter1);
|
2022-06-28 22:43:34 +02:00
|
|
|
mTemplateNamePos.clear(); // positions might be invalid after instantiations
|
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
2022-12-20 20:32:16 +01:00
|
|
|
for (std::list<TokenAndName>::const_iterator it = mInstantiatedTemplates.cbegin(); it != mInstantiatedTemplates.cend(); ++it) {
|
2023-02-07 21:57:59 +01:00
|
|
|
auto decl = std::find_if(mTemplateDeclarations.begin(), mTemplateDeclarations.end(), [&it](const TokenAndName& decl) {
|
|
|
|
return decl.token() == it->token();
|
|
|
|
});
|
2018-09-01 11:26:10 +02:00
|
|
|
if (decl != mTemplateDeclarations.end()) {
|
2019-01-27 07:46:27 +01:00
|
|
|
if (it->isSpecialization()) {
|
2018-10-14 16:57:07 +02:00
|
|
|
// delete the "template < >"
|
2019-08-10 08:42:12 +02:00
|
|
|
Token * tok = it->token();
|
2018-10-14 16:57:07 +02:00
|
|
|
tok->deleteNext(2);
|
|
|
|
tok->deleteThis();
|
2018-12-20 20:55:27 +01:00
|
|
|
} else {
|
|
|
|
// remove forward declaration if found
|
2019-08-10 08:42:12 +02:00
|
|
|
auto it1 = mTemplateForwardDeclarationsMap.find(it->token());
|
2018-12-20 20:55:27 +01:00
|
|
|
if (it1 != mTemplateForwardDeclarationsMap.end())
|
|
|
|
removeTemplate(it1->second);
|
2019-08-10 08:42:12 +02:00
|
|
|
removeTemplate(it->token());
|
2018-12-20 20:55:27 +01:00
|
|
|
}
|
2018-09-01 11:26:10 +02:00
|
|
|
mTemplateDeclarations.erase(decl);
|
2012-07-29 16:01:05 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-14 14:16:34 +02:00
|
|
|
|
|
|
|
// remove out of line member functions
|
|
|
|
while (!mMemberFunctionsToDelete.empty()) {
|
2018-10-24 19:32:37 +02:00
|
|
|
const std::list<TokenAndName>::iterator it = std::find_if(mTemplateDeclarations.begin(),
|
2021-08-07 20:51:18 +02:00
|
|
|
mTemplateDeclarations.end(),
|
2022-12-30 15:13:47 +01:00
|
|
|
FindToken(mMemberFunctionsToDelete.cbegin()->token()));
|
2018-10-24 19:32:37 +02:00
|
|
|
// multiple functions can share the same declaration so make sure it hasn't already been deleted
|
|
|
|
if (it != mTemplateDeclarations.end()) {
|
2019-08-10 08:42:12 +02:00
|
|
|
removeTemplate(it->token());
|
2018-10-24 19:32:37 +02:00
|
|
|
mTemplateDeclarations.erase(it);
|
2021-01-09 20:00:35 +01:00
|
|
|
} else {
|
|
|
|
const std::list<TokenAndName>::iterator it1 = std::find_if(mTemplateForwardDeclarations.begin(),
|
2021-08-07 20:51:18 +02:00
|
|
|
mTemplateForwardDeclarations.end(),
|
2022-12-30 15:13:47 +01:00
|
|
|
FindToken(mMemberFunctionsToDelete.cbegin()->token()));
|
2021-01-09 20:00:35 +01:00
|
|
|
// multiple functions can share the same declaration so make sure it hasn't already been deleted
|
|
|
|
if (it1 != mTemplateForwardDeclarations.end()) {
|
|
|
|
removeTemplate(it1->token());
|
|
|
|
mTemplateForwardDeclarations.erase(it1);
|
|
|
|
}
|
2018-10-24 19:32:37 +02:00
|
|
|
}
|
2018-09-14 14:16:34 +02:00
|
|
|
mMemberFunctionsToDelete.erase(mMemberFunctionsToDelete.begin());
|
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
|
|
|
|
// remove explicit instantiations
|
2022-05-29 17:06:33 +02:00
|
|
|
for (const TokenAndName& j : mExplicitInstantiationsToDelete) {
|
2019-09-19 20:29:33 +02:00
|
|
|
Token * start = j.token();
|
2018-12-17 05:58:48 +01:00
|
|
|
if (start) {
|
|
|
|
Token * end = start->next();
|
|
|
|
while (end && end->str() != ";")
|
|
|
|
end = end->next();
|
|
|
|
if (start->previous())
|
|
|
|
start = start->previous();
|
2019-01-07 06:55:22 +01:00
|
|
|
if (end && end->next())
|
2018-12-17 05:58:48 +01:00
|
|
|
end = end->next();
|
|
|
|
eraseTokens(start, end);
|
|
|
|
}
|
|
|
|
}
|
2012-07-29 16:01:05 +02:00
|
|
|
}
|
2019-06-23 10:55:09 +02:00
|
|
|
|
|
|
|
if (passCount == passCountMax) {
|
2023-03-07 12:12:31 +01:00
|
|
|
if (mSettings.debugwarnings) {
|
2019-06-23 10:55:09 +02:00
|
|
|
const std::list<const Token*> locationList(1, mTokenList.front());
|
2023-03-07 12:12:31 +01:00
|
|
|
const ErrorMessage errmsg(locationList, &mTokenizer.list,
|
2020-05-23 07:16:49 +02:00
|
|
|
Severity::debug,
|
|
|
|
"debug",
|
|
|
|
"TemplateSimplifier: pass count limit hit before simplifications were finished.",
|
2021-02-24 22:00:06 +01:00
|
|
|
Certainty::normal);
|
2019-06-23 10:55:09 +02:00
|
|
|
if (mErrorLogger)
|
|
|
|
mErrorLogger->reportErr(errmsg);
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 15:39:04 +02:00
|
|
|
|
|
|
|
// Tweak uninstantiated C++17 fold expressions (... && args)
|
2023-03-07 12:12:31 +01:00
|
|
|
if (mSettings.standards.cpp >= Standards::CPP17) {
|
2021-04-20 15:39:04 +02:00
|
|
|
bool simplify = false;
|
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == "template")
|
|
|
|
simplify = false;
|
|
|
|
if (tok->str() == "{")
|
|
|
|
simplify = true;
|
|
|
|
if (!simplify || tok->str() != "(")
|
|
|
|
continue;
|
2021-04-21 13:13:11 +02:00
|
|
|
const Token *op = nullptr;
|
|
|
|
const Token *args = nullptr;
|
|
|
|
if (Token::Match(tok, "( ... %op%")) {
|
|
|
|
op = tok->tokAt(2);
|
|
|
|
args = tok->link()->previous();
|
|
|
|
} else if (Token::Match(tok, "( %name% %op% ...")) {
|
|
|
|
op = tok->tokAt(2);
|
|
|
|
args = tok->link()->previous()->isName() ? nullptr : tok->next();
|
|
|
|
} else if (Token::Match(tok->link()->tokAt(-3), "%op% ... )")) {
|
|
|
|
op = tok->link()->tokAt(-2);
|
|
|
|
args = tok->next();
|
|
|
|
} else if (Token::Match(tok->link()->tokAt(-3), "... %op% %name% )")) {
|
|
|
|
op = tok->link()->tokAt(-2);
|
|
|
|
args = tok->next()->isName() ? nullptr : tok->link()->previous();
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string strop = op->str();
|
|
|
|
const std::string strargs = (args && args->isName()) ? args->str() : "";
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, tok->link());
|
|
|
|
tok->insertToken(")");
|
|
|
|
if (!strargs.empty()) {
|
|
|
|
tok->insertToken("...");
|
|
|
|
tok->insertToken(strargs);
|
2021-04-20 15:39:04 +02:00
|
|
|
}
|
2021-04-21 13:13:11 +02:00
|
|
|
tok->insertToken("(");
|
|
|
|
Token::createMutualLinks(tok->next(), tok->link()->previous());
|
|
|
|
tok->insertToken("__cppcheck_fold_" + strop + "__");
|
2021-04-20 15:39:04 +02:00
|
|
|
}
|
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
2015-06-23 20:53:57 +02:00
|
|
|
|
|
|
|
void TemplateSimplifier::syntaxError(const Token *tok)
|
|
|
|
{
|
|
|
|
throw InternalError(tok, "syntax error", InternalError::SYNTAX);
|
|
|
|
}
|