2012-01-01 21:55:05 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2019-02-09 07:24:06 +01:00
|
|
|
* Copyright (C) 2007-2019 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"
|
2012-01-09 20:33:11 +01:00
|
|
|
#include "mathlib.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "settings.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>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <stack>
|
|
|
|
#include <utility>
|
2012-01-01 21:55:05 +01:00
|
|
|
|
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 {
|
|
|
|
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:
|
2018-06-17 19:24:40 +02:00
|
|
|
explicit FindName(const std::string &name) : mName(name) {}
|
|
|
|
bool operator()(const TemplateSimplifier::TokenAndName &tokenAndName) const {
|
|
|
|
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:
|
|
|
|
explicit FindFullName(const std::string &fullName) : mFullName(fullName) {}
|
|
|
|
bool operator()(const TemplateSimplifier::TokenAndName &tokenAndName) const {
|
|
|
|
return tokenAndName.fullName == mFullName;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
const std::string mFullName;
|
|
|
|
};
|
2018-01-05 15:27:43 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 20:36:23 +01:00
|
|
|
TemplateSimplifier::TokenAndName::TokenAndName(Token *tok, const std::string &s) :
|
|
|
|
token(tok), scope(s), name(tok ? tok->str() : ""), fullName(s.empty() ? name : (s + " :: " + name)),
|
|
|
|
nameToken(nullptr), paramEnd(nullptr), flags(0)
|
|
|
|
{
|
|
|
|
if (token)
|
|
|
|
token->templateSimplifierPointer(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
TemplateSimplifier::TokenAndName::TokenAndName(Token *tok, const std::string &s, const Token *nt, const Token *pe) :
|
|
|
|
token(tok), scope(s), name(nt->str()), fullName(s.empty() ? name : (s + " :: " + name)),
|
|
|
|
nameToken(nt), paramEnd(pe), flags(0)
|
2018-09-01 11:26:10 +02:00
|
|
|
{
|
2019-01-07 06:55:22 +01:00
|
|
|
// only set flags for declaration
|
|
|
|
if (token && nameToken && paramEnd) {
|
2019-01-27 07:46:27 +01:00
|
|
|
isSpecialization(Token::simpleMatch(token, "template < >"));
|
|
|
|
isPartialSpecialization(!isSpecialization() && nameToken->strAt(1) == "<");
|
2019-01-18 21:12:39 +01:00
|
|
|
isAlias(paramEnd->strAt(1) == "using");
|
|
|
|
isClass(Token::Match(paramEnd->next(), "class|struct|union %name% <|{|:|;"));
|
|
|
|
const Token *tok1 = nameToken->next();
|
|
|
|
if (tok1->str() == "<")
|
|
|
|
tok1 = tok1->findClosingBracket()->next();
|
|
|
|
isFunction(tok1->str() == "(");
|
|
|
|
isVariable(!isClass() && Token::Match(tok1, "=|;"));
|
|
|
|
if (isVariable())
|
|
|
|
isForwardDeclaration(tok1->str() == ";");
|
|
|
|
else {
|
|
|
|
if (isFunction())
|
|
|
|
tok1 = tok1->link()->next();
|
|
|
|
tok1 = Token::findmatch(tok1, "{|;");
|
|
|
|
if (tok1)
|
|
|
|
isForwardDeclaration(tok1->str() == ";");
|
|
|
|
}
|
2019-01-24 07:21:22 +01:00
|
|
|
// check for member function and adjust scope
|
|
|
|
if (isFunction() && nameToken->strAt(-1) == "::") {
|
|
|
|
const Token * start = nameToken;
|
|
|
|
|
|
|
|
while (Token::Match(start->tokAt(-2), "%name% ::") ||
|
|
|
|
(Token::simpleMatch(start->tokAt(-2), "> ::") &&
|
2019-02-11 07:45:03 +01:00
|
|
|
start->tokAt(-2)->findOpeningBracket() &&
|
2019-01-24 07:21:22 +01:00
|
|
|
Token::Match(start->tokAt(-2)->findOpeningBracket()->previous(), "%name% <"))) {
|
|
|
|
if (start->strAt(-2) == ">")
|
|
|
|
start = start->tokAt(-2)->findOpeningBracket()->previous();
|
|
|
|
else
|
|
|
|
start = start->tokAt(-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start && start != nameToken) {
|
|
|
|
if (!scope.empty())
|
|
|
|
scope += " ::";
|
|
|
|
while (start && start->next() != nameToken) {
|
|
|
|
if (start->str() == "<")
|
|
|
|
start = start->findClosingBracket();
|
|
|
|
else {
|
|
|
|
if (!scope.empty())
|
|
|
|
scope += " ";
|
|
|
|
scope += start->str();
|
|
|
|
}
|
|
|
|
start = start->next();
|
|
|
|
}
|
|
|
|
if (start)
|
|
|
|
fullName = scope.empty() ? name : (scope + " :: " + name);
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 06:55:22 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 13:51:45 +01:00
|
|
|
if (token)
|
|
|
|
token->templateSimplifierPointer(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
TemplateSimplifier::TokenAndName::TokenAndName(const TokenAndName& otherTok) :
|
2019-01-14 08:29:32 +01:00
|
|
|
token(otherTok.token), scope(otherTok.scope), name(otherTok.name), fullName(otherTok.fullName),
|
2019-01-07 06:55:22 +01:00
|
|
|
nameToken(otherTok.nameToken), paramEnd(otherTok.paramEnd), flags(otherTok.flags)
|
2018-12-21 13:51:45 +01:00
|
|
|
{
|
|
|
|
if (token)
|
|
|
|
token->templateSimplifierPointer(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
TemplateSimplifier::TokenAndName::~TokenAndName()
|
|
|
|
{
|
|
|
|
if (token)
|
|
|
|
token->templateSimplifierPointers().erase(this);
|
2018-09-01 11:26:10 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 10:05:10 +01:00
|
|
|
TemplateSimplifier::TemplateSimplifier(Tokenizer *tokenizer)
|
2019-02-05 08:52:23 +01:00
|
|
|
: mTokenizer(tokenizer), mTokenList(tokenizer->list), mSettings(tokenizer->mSettings), mErrorLogger(tokenizer->mErrorLogger)
|
2018-09-01 11:26:10 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TemplateSimplifier::~TemplateSimplifier()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplateSimplifier::cleanupAfterSimplify()
|
2012-01-01 21:55:05 +01:00
|
|
|
{
|
|
|
|
bool goback = false;
|
2018-09-01 11:26:10 +02:00
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
2012-01-01 21:55:05 +01:00
|
|
|
if (goback) {
|
|
|
|
tok = tok->previous();
|
|
|
|
goback = false;
|
|
|
|
}
|
|
|
|
if (tok->str() == "(")
|
|
|
|
tok = tok->link();
|
|
|
|
|
2015-01-31 10:50:39 +01:00
|
|
|
else if (Token::Match(tok, "template < > %name%")) {
|
2013-09-29 18:59:27 +02:00
|
|
|
const Token *end = tok;
|
|
|
|
while (end) {
|
|
|
|
if (end->str() == ";")
|
|
|
|
break;
|
|
|
|
if (end->str() == "{") {
|
|
|
|
end = end->link()->next();
|
|
|
|
break;
|
|
|
|
}
|
2015-03-14 23:02:10 +01:00
|
|
|
if (!Token::Match(end, "%name%|::|<|>|,")) {
|
2014-02-16 10:32:10 +01:00
|
|
|
end = nullptr;
|
2013-09-29 18:59:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
end = end->next();
|
|
|
|
}
|
|
|
|
if (end) {
|
|
|
|
Token::eraseTokens(tok,end);
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-01 21:55:05 +01:00
|
|
|
else if (Token::Match(tok, "%type% <") &&
|
|
|
|
(!tok->previous() || tok->previous()->str() == ";")) {
|
|
|
|
const Token *tok2 = tok->tokAt(2);
|
|
|
|
std::string type;
|
2016-08-24 14:10:39 +02:00
|
|
|
while (Token::Match(tok2, "%type%|%num% ,")) {
|
2012-01-01 21:55:05 +01:00
|
|
|
type += tok2->str() + ",";
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
2016-08-24 14:10:39 +02:00
|
|
|
if (Token::Match(tok2, "%type%|%num% > (")) {
|
2012-01-01 21:55:05 +01:00
|
|
|
type += tok2->str();
|
|
|
|
tok->str(tok->str() + "<" + type + ">");
|
|
|
|
Token::eraseTokens(tok, tok2->tokAt(2));
|
2018-09-01 11:26:10 +02:00
|
|
|
if (tok == mTokenList.front())
|
2012-01-01 21:55:05 +01:00
|
|
|
goback = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 21:37:32 +01: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::..
|
2017-01-23 12:50:36 +01:00
|
|
|
while (Token::simpleMatch(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..
|
|
|
|
if (Token::Match(tok, "%type% <")) {
|
|
|
|
// 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()) {
|
2018-10-28 17:37:19 +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 <"))
|
|
|
|
;
|
|
|
|
else if (level == 0)
|
|
|
|
inclevel = true;
|
|
|
|
else if (tok2->next() && tok2->next()->isStandardType())
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
unsigned int level = 0;
|
|
|
|
|
|
|
|
while (tok) {
|
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)
|
|
|
|
if (Token::Match(tok, "%type% . . .")) {
|
2014-05-09 22:20:22 +02:00
|
|
|
tok = tok->tokAt(4);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2014-05-23 23:38:40 +02:00
|
|
|
// Skip casts
|
|
|
|
if (tok->str() == "(") {
|
|
|
|
tok = tok->link();
|
2014-05-24 12:32:44 +02:00
|
|
|
if (tok)
|
2014-05-23 23:38:40 +02:00
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
|
2012-01-02 21:43:38 +01:00
|
|
|
// skip std::
|
2012-09-12 20:41:10 +02:00
|
|
|
if (tok && 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
|
|
|
|
if (tok->str() == "<") {
|
|
|
|
++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
|
|
|
// ,/>
|
2014-09-14 11:26:16 +02:00
|
|
|
while (Token::Match(tok, ">|>>")) {
|
2012-01-02 21:43:38 +01:00
|
|
|
if (level == 0)
|
2015-04-03 20:25:22 +02:00
|
|
|
return ((tok->str() == ">") ? numberOfParameters : 0);
|
2012-01-02 21:43:38 +01:00
|
|
|
--level;
|
2012-08-26 09:49:38 +02:00
|
|
|
if (tok->str() == ">>") {
|
|
|
|
if (level == 0)
|
|
|
|
return numberOfParameters;
|
|
|
|
--level;
|
|
|
|
}
|
2012-01-02 21:43:38 +01:00
|
|
|
tok = tok->next();
|
2012-11-04 10:49:25 +01:00
|
|
|
|
2017-11-05 17:59:34 +01:00
|
|
|
if (Token::simpleMatch(tok,"("))
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
|
|
|
int indentlevel = 0;
|
|
|
|
unsigned int countgt = 0; // Counter for ">"
|
|
|
|
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
|
|
|
|
|
|
|
|
if (tok2->str() == "(") {
|
|
|
|
tok2 = tok2->link();
|
|
|
|
} else if (tok2->str() == ")") { // garbage code! (#3504)
|
2018-10-14 16:57:07 +02:00
|
|
|
eraseTokens(tok,tok2);
|
2018-09-01 11:26:10 +02:00
|
|
|
deleteToken(tok);
|
2012-07-29 16:01:05 +02:00
|
|
|
return false;
|
2012-01-02 21:46:43 +01:00
|
|
|
}
|
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
else if (tok2->str() == "{") {
|
|
|
|
tok2 = tok2->link()->next();
|
|
|
|
if (tok2 && tok2->str() == ";" && tok2->next())
|
2018-10-24 19:32:37 +02:00
|
|
|
tok2 = tok2->next();
|
|
|
|
eraseTokens(tok, tok2);
|
2018-09-01 11:26:10 +02:00
|
|
|
deleteToken(tok);
|
2012-07-29 16:01:05 +02:00
|
|
|
return true;
|
|
|
|
} else if (tok2->str() == "}") { // garbage code! (#3449)
|
2018-10-14 16:57:07 +02:00
|
|
|
eraseTokens(tok,tok2);
|
2018-09-01 11:26:10 +02:00
|
|
|
deleteToken(tok);
|
2012-07-29 16:01:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-01-02 21:46:43 +01:00
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
// Count ">"
|
|
|
|
if (tok2->str() == ">")
|
|
|
|
countgt++;
|
2012-01-02 21:46:43 +01:00
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
// don't remove constructor
|
|
|
|
if (tok2->str() == "explicit" ||
|
2014-04-26 18:31:56 +02:00
|
|
|
(countgt == 1 && Token::Match(tok2->previous(), "> %type% (") &&
|
2016-01-03 22:52:24 +01:00
|
|
|
Tokenizer::startOfExecutableScope(tok2->linkAt(1)))) {
|
2018-10-14 16:57:07 +02:00
|
|
|
eraseTokens(tok, tok2);
|
2018-09-01 11:26:10 +02:00
|
|
|
deleteToken(tok);
|
2012-07-29 16:01:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-31 11:30:26 +02:00
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
if (tok2->str() == ";") {
|
|
|
|
tok2 = tok2->next();
|
2018-10-14 16:57:07 +02:00
|
|
|
eraseTokens(tok, tok2);
|
2018-09-01 11:26:10 +02:00
|
|
|
deleteToken(tok);
|
2012-07-29 16:01:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-31 11:30:26 +02:00
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
if (tok2->str() == "<")
|
|
|
|
++indentlevel;
|
2012-01-02 21:46:43 +01:00
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
else if (indentlevel >= 2 && tok2->str() == ">")
|
|
|
|
--indentlevel;
|
2012-01-02 21:46:43 +01:00
|
|
|
|
2018-09-26 06:23:12 +02:00
|
|
|
else if (Token::Match(tok2, "> class|struct|union %name% [,)]")) {
|
2012-07-29 16:01:05 +02:00
|
|
|
tok2 = tok2->next();
|
2018-10-14 16:57:07 +02:00
|
|
|
eraseTokens(tok, tok2);
|
2018-09-01 11:26:10 +02:00
|
|
|
deleteToken(tok);
|
2012-07-29 16:01:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2017-12-26 10:55:18 +01:00
|
|
|
/// TODO: This is copy pasted from Tokenizer. We should reuse this code.
|
|
|
|
namespace {
|
|
|
|
struct ScopeInfo2 {
|
2018-04-27 22:36:30 +02:00
|
|
|
ScopeInfo2(const std::string &name_, const Token *bodyEnd_) : name(name_), bodyEnd(bodyEnd_) {}
|
2017-12-26 10:55:18 +01:00
|
|
|
const std::string name;
|
2018-04-27 22:36:30 +02:00
|
|
|
const Token * const bodyEnd;
|
2019-01-23 08:53:01 +01:00
|
|
|
std::set<std::string> usingNamespaces;
|
2017-12-26 10:55:18 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
static std::string getScopeName(const std::list<ScopeInfo2> &scopeInfo)
|
2017-12-23 08:20:24 +01:00
|
|
|
{
|
2017-12-26 10:55:18 +01:00
|
|
|
std::string ret;
|
2019-02-05 08:52:23 +01:00
|
|
|
for (const ScopeInfo2 &i : scopeInfo) {
|
|
|
|
if (!i.name.empty())
|
|
|
|
ret += (ret.empty() ? "" : " :: ") + i.name;
|
|
|
|
}
|
2017-12-26 10:55:18 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-05 08:52:23 +01:00
|
|
|
static void setScopeInfo(Token *tok, std::list<ScopeInfo2> *scopeInfo, bool all = false)
|
2017-12-26 10:55:18 +01:00
|
|
|
{
|
2018-04-27 22:36:30 +02:00
|
|
|
while (tok->str() == "}" && !scopeInfo->empty() && tok == scopeInfo->back().bodyEnd)
|
2017-12-26 10:55:18 +01:00
|
|
|
scopeInfo->pop_back();
|
2019-01-14 20:36:23 +01:00
|
|
|
if (!Token::Match(tok, "namespace|class|struct|union %name% {|:|::")) {
|
2019-01-23 08:53:01 +01:00
|
|
|
// check for using namespace
|
|
|
|
if (Token::Match(tok, "using namespace %name% ;|::")) {
|
|
|
|
const Token * tok1 = tok->tokAt(2);
|
|
|
|
std::string nameSpace;
|
|
|
|
while (tok1 && tok1->str() != ";") {
|
|
|
|
if (!nameSpace.empty())
|
|
|
|
nameSpace += " ";
|
|
|
|
nameSpace += tok1->str();
|
|
|
|
tok1 = tok1->next();
|
|
|
|
}
|
|
|
|
scopeInfo->back().usingNamespaces.insert(nameSpace);
|
|
|
|
}
|
2019-01-14 20:36:23 +01:00
|
|
|
// check for member function
|
2019-01-23 08:53:01 +01:00
|
|
|
else if (tok->str() == "{") {
|
2019-02-05 08:52:23 +01:00
|
|
|
bool added = false;
|
2019-01-14 20:36:23 +01:00
|
|
|
Token *tok1 = tok;
|
|
|
|
while (Token::Match(tok1->previous(), "const|volatile|final|override|&|&&|noexcept"))
|
|
|
|
tok1 = tok1->previous();
|
2019-02-11 07:45:03 +01:00
|
|
|
if (tok1 && tok1->previous() && tok1->strAt(-1) == ")") {
|
2019-01-14 20:36:23 +01:00
|
|
|
tok1 = tok1->linkAt(-1);
|
|
|
|
if (Token::Match(tok1->previous(), "throw|noexcept")) {
|
|
|
|
tok1 = tok1->previous();
|
|
|
|
while (Token::Match(tok1->previous(), "const|volatile|final|override|&|&&|noexcept"))
|
|
|
|
tok1 = tok1->previous();
|
|
|
|
if (tok1->strAt(-1) != ")")
|
|
|
|
return;
|
|
|
|
} else if (Token::Match(tok->tokAt(-2), ":|, %name%")) {
|
|
|
|
tok1 = tok1->tokAt(-2);
|
|
|
|
if (tok1->strAt(-1) != ")")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tok1->strAt(-1) == ">")
|
|
|
|
tok1 = tok1->previous()->findOpeningBracket();
|
2019-02-11 07:45:03 +01:00
|
|
|
if (tok1 && Token::Match(tok1->tokAt(-3), "%name% :: %name%")) {
|
2019-01-14 20:36:23 +01:00
|
|
|
tok1 = tok1->tokAt(-2);
|
|
|
|
std::string scope = tok1->strAt(-1);
|
|
|
|
while (Token::Match(tok1->tokAt(-2), ":: %name%")) {
|
|
|
|
scope = tok1->strAt(-3) + " :: " + scope;
|
|
|
|
tok1 = tok1->tokAt(-2);
|
|
|
|
}
|
|
|
|
scopeInfo->emplace_back(scope, tok->link());
|
2019-02-05 08:52:23 +01:00
|
|
|
added = true;
|
2019-01-14 20:36:23 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-05 08:52:23 +01:00
|
|
|
|
|
|
|
if (all && !added)
|
|
|
|
scopeInfo->emplace_back("", tok->link());
|
2019-01-14 20:36:23 +01:00
|
|
|
}
|
2017-12-26 10:55:18 +01:00
|
|
|
return;
|
2019-01-14 20:36:23 +01:00
|
|
|
}
|
|
|
|
|
2017-12-26 10:55:18 +01:00
|
|
|
tok = tok->next();
|
|
|
|
std::string classname = tok->str();
|
|
|
|
while (Token::Match(tok, "%name% :: %name%")) {
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
classname += " :: " + tok->str();
|
|
|
|
}
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok && tok->str() == ":") {
|
2019-01-02 07:15:45 +01:00
|
|
|
while (tok && !Token::Match(tok, ";|{"))
|
|
|
|
tok = tok->next();
|
2017-12-26 10:55:18 +01:00
|
|
|
}
|
|
|
|
if (tok && tok->str() == "{") {
|
2018-04-11 09:44:35 +02:00
|
|
|
scopeInfo->emplace_back(classname,tok->link());
|
2017-12-26 10:55:18 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-23 22:11:30 +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;
|
2017-12-26 10:55:18 +01:00
|
|
|
std::list<ScopeInfo2> scopeInfo;
|
2018-09-01 11:26:10 +02:00
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
2019-01-14 20:36:23 +01:00
|
|
|
if (Token::Match(tok, "{|}|namespace|class|struct|union")) {
|
2018-06-02 13:49:14 +02:00
|
|
|
setScopeInfo(tok, &scopeInfo);
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-27 22:29:45 +01:00
|
|
|
if (!Token::simpleMatch(tok, "template <"))
|
|
|
|
continue;
|
|
|
|
// Some syntax checks, see #6865
|
|
|
|
if (!tok->tokAt(2))
|
|
|
|
syntaxError(tok->next());
|
|
|
|
if (tok->strAt(2)=="typename" &&
|
2018-05-30 17:09:31 +02:00
|
|
|
!Token::Match(tok->tokAt(3), "%name%|.|,|=|>"))
|
2017-12-27 22:29:45 +01:00
|
|
|
syntaxError(tok->next());
|
|
|
|
codeWithTemplates = true;
|
2018-09-23 20:24:51 +02:00
|
|
|
const Token * const parmEnd = tok->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) {
|
|
|
|
TokenAndName decl(tok, getScopeName(scopeInfo), parmEnd->tokAt(namepos), parmEnd);
|
|
|
|
if (decl.isForwardDeclaration()) {
|
|
|
|
// Declaration => add to mTemplateForwardDeclarations
|
|
|
|
mTemplateForwardDeclarations.emplace_back(decl);
|
|
|
|
} else {
|
|
|
|
// Implementation => add to mTemplateDeclarations
|
|
|
|
mTemplateDeclarations.emplace_back(decl);
|
|
|
|
}
|
|
|
|
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());
|
|
|
|
|
|
|
|
mTemplateInstantiations.emplace_back(token, scope);
|
|
|
|
}
|
2012-01-02 22:05:27 +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())
|
|
|
|
functionNameMap.insert(std::make_pair(decl.name, &decl));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & decl : mTemplateForwardDeclarations) {
|
|
|
|
if (decl.isFunction())
|
|
|
|
functionNameMap.insert(std::make_pair(decl.name, &decl));
|
|
|
|
}
|
|
|
|
|
2017-12-26 10:55:18 +01:00
|
|
|
std::list<ScopeInfo2> scopeList;
|
2019-01-05 11:14:09 +01:00
|
|
|
const Token *skip = nullptr;
|
2012-01-02 22:05:27 +01:00
|
|
|
|
2019-01-23 08:53:01 +01:00
|
|
|
scopeList.emplace_back("", nullptr);
|
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
2019-01-23 08:53:01 +01:00
|
|
|
if (Token::Match(tok, "{|}|namespace|class|struct|union") ||
|
|
|
|
Token::Match(tok, "using namespace %name% ;|::")) {
|
2018-06-02 13:49:14 +02:00
|
|
|
setScopeInfo(tok, &scopeList);
|
|
|
|
continue;
|
|
|
|
}
|
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-01-18 21:12:39 +01:00
|
|
|
const bool isUsing = tok->strAt(1) == "using";
|
2019-01-05 11:14:09 +01:00
|
|
|
if (tok->strAt(-1) == "<") {
|
|
|
|
// Don't ignore user specialization but don't consider it an instantiation.
|
|
|
|
// Instantiations in return type, function parameters, and executable code
|
|
|
|
// are not ignored.
|
|
|
|
unsigned int pos = getTemplateNamePosition(tok);
|
|
|
|
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
|
|
|
|
const Token *tok2 = Token::findmatch(tok, "{|;");
|
|
|
|
if (tok2 && tok2->str() == "{")
|
|
|
|
tok = tok2->link();
|
2019-01-18 21:12:39 +01:00
|
|
|
else if (!isUsing && tok2 && tok2->str() == ";")
|
|
|
|
tok = const_cast<Token *>(tok2);
|
2019-01-05 11:14:09 +01:00
|
|
|
}
|
2019-02-26 06:41:04 +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% ::|<")) {
|
|
|
|
std::string scopeName = getScopeName(scopeList);
|
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%")) {
|
2019-01-23 08:53:01 +01:00
|
|
|
// ignore redundant namespaces
|
|
|
|
if (scopeName.find(tok->str()) == std::string::npos)
|
|
|
|
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
|
|
|
|
|
|
|
// look for function instantiation with type deduction
|
|
|
|
// fixme: only single argument functions supported
|
|
|
|
if (tok->strAt(1) == "(") {
|
|
|
|
std::string fullName = qualification + (qualification.empty() ? "" : " :: ") + tok->str();
|
|
|
|
// get all declarations with this name
|
|
|
|
for (auto pos = functionNameMap.lower_bound(tok->str());
|
|
|
|
pos != functionNameMap.upper_bound(tok->str()); ++pos) {
|
|
|
|
// look for declaration with same qualification
|
|
|
|
if (pos->second->fullName == fullName) {
|
|
|
|
// make sure it is a single argument function
|
|
|
|
if (Token::Match(pos->second->token->tokAt(2), "typename|class %name% >") &&
|
|
|
|
Token::Match(pos->second->nameToken->tokAt(2), "const| %type% &| %name%| )") &&
|
|
|
|
Token::Match(tok->tokAt(2), "%num%|%str%|%char%|%bool% )")) {
|
|
|
|
tok->insertToken(">");
|
|
|
|
switch (tok->tokAt(3)->tokType()) {
|
|
|
|
case Token::eBoolean:
|
|
|
|
tok->insertToken("bool");
|
|
|
|
break;
|
|
|
|
case Token::eChar:
|
|
|
|
if (tok->tokAt(3)->isLong())
|
|
|
|
tok->insertToken("wchar_t");
|
|
|
|
else
|
|
|
|
tok->insertToken("char");
|
|
|
|
break;
|
|
|
|
case Token::eString:
|
|
|
|
tok->insertToken("*");
|
|
|
|
if (tok->tokAt(4)->isLong())
|
|
|
|
tok->insertToken("wchar_t");
|
|
|
|
else
|
|
|
|
tok->insertToken("char");
|
|
|
|
tok->insertToken("const");
|
|
|
|
break;
|
|
|
|
case Token::eNumber: {
|
|
|
|
MathLib::value num(tok->strAt(3));
|
|
|
|
if (num.isFloat()) {
|
|
|
|
// MathLib::getSuffix doesn't work for floating point numbers
|
|
|
|
char suffix = tok->strAt(3).back();
|
|
|
|
if (suffix == 'f' || suffix == 'F')
|
|
|
|
tok->insertToken("float");
|
|
|
|
else if (suffix == 'l' || suffix == 'L') {
|
|
|
|
tok->insertToken("double");
|
|
|
|
tok->insertToken("long");
|
|
|
|
} 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");
|
|
|
|
tok->insertToken("long");
|
|
|
|
} else if (suffix.find('L') != std::string::npos)
|
|
|
|
tok->insertToken("long");
|
|
|
|
else
|
|
|
|
tok->insertToken("int");
|
|
|
|
if (suffix.find('U') != std::string::npos)
|
|
|
|
tok->insertToken("unsigned");
|
|
|
|
}
|
|
|
|
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
|
2013-07-31 10:30:20 +02:00
|
|
|
const 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()) {
|
2015-01-31 10:50:39 +01:00
|
|
|
if (Token::Match(tok2, ", %name% <") &&
|
2018-09-01 11:26:10 +02:00
|
|
|
templateParameters(tok2->tokAt(2))) {
|
2019-03-03 07:40:55 +01:00
|
|
|
addInstantiation(tok2->next(), getScopeName(scopeList));
|
2018-11-02 14:49:40 +01:00
|
|
|
} else if (Token::Match(tok2->next(), "class|struct"))
|
|
|
|
const_cast<Token *>(tok2)->deleteNext();
|
2012-01-02 22:05:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add outer template..
|
2018-09-01 11:26:10 +02:00
|
|
|
if (templateParameters(tok->next())) {
|
2017-12-30 09:00:19 +01:00
|
|
|
const std::string scopeName1(scopeName);
|
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();
|
2019-01-14 20:36:23 +01:00
|
|
|
const std::list<TokenAndName>::const_iterator it = std::find_if(mTemplateDeclarations.begin(), mTemplateDeclarations.end(), 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-03-03 07:40:55 +01: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;
|
|
|
|
for (const auto & nameSpace : scopeList.back().usingNamespaces) {
|
|
|
|
std::string fullNameSpace = scopeName + (scopeName.empty()?"":" :: ") +
|
|
|
|
nameSpace + (qualification.empty()?"":" :: ") + qualification;
|
|
|
|
std::string newFullName = fullNameSpace + " :: " + tok->str();
|
|
|
|
const std::list<TokenAndName>::const_iterator it1 = std::find_if(mTemplateDeclarations.begin(), mTemplateDeclarations.end(), FindFullName(newFullName));
|
|
|
|
if (it1 != mTemplateDeclarations.end()) {
|
|
|
|
// insert using namespace into token stream
|
|
|
|
std::string::size_type offset = 0;
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while ((pos = nameSpace.substr(offset).find(' ')) != std::string::npos) {
|
|
|
|
qualificationTok->insertToken(nameSpace.substr(offset, pos), "", true);
|
|
|
|
offset = offset + pos + 1;
|
|
|
|
}
|
|
|
|
qualificationTok->insertToken(nameSpace.substr(offset), "", true);
|
|
|
|
qualificationTok->insertToken("::", "", true);
|
2019-03-03 07:40:55 +01: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-03-03 07:40:55 +01:00
|
|
|
addInstantiation(tok, getScopeName(scopeList));
|
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
|
|
|
{
|
2018-12-30 21:16:11 +01:00
|
|
|
for (TokenAndName &template1 : mTemplateDeclarations) {
|
2012-01-03 22:35:06 +01:00
|
|
|
// template parameters with default value has syntax such as:
|
|
|
|
// x = y
|
|
|
|
// this list will contain all the '=' tokens for such arguments
|
|
|
|
std::list<Token *> eq;
|
2014-05-08 10:15:15 +02:00
|
|
|
// and this set the position of parameters with a default value
|
|
|
|
std::set<std::size_t> defaultedArgPos;
|
2012-01-03 22:35:06 +01:00
|
|
|
|
|
|
|
// parameter number. 1,2,3,..
|
|
|
|
std::size_t templatepar = 1;
|
|
|
|
|
2014-05-08 10:15:15 +02:00
|
|
|
// parameter depth
|
|
|
|
std::size_t templateParmDepth = 0;
|
|
|
|
|
2012-01-03 22:35:06 +01:00
|
|
|
// the template classname. This will be empty for template functions
|
|
|
|
std::string classname;
|
|
|
|
|
|
|
|
// Scan template declaration..
|
2018-07-15 14:45:15 +02:00
|
|
|
for (Token *tok = template1.token; tok; tok = tok->next()) {
|
2014-05-08 10:15:15 +02:00
|
|
|
if (Token::simpleMatch(tok, "template < >")) { // Ticket #5762: Skip specialization tokens
|
|
|
|
tok = tok->tokAt(2);
|
2015-05-01 17:13:02 +02:00
|
|
|
if (0 == templateParmDepth)
|
|
|
|
break;
|
2014-05-08 10:15:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-17 21:30:34 +02:00
|
|
|
if (tok->str() == "(") { // Ticket #6835
|
|
|
|
tok = tok->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-08 10:15:15 +02:00
|
|
|
if (tok->str() == "<" && templateParameters(tok))
|
|
|
|
++templateParmDepth;
|
|
|
|
|
2012-01-03 22:35:06 +01:00
|
|
|
// end of template parameters?
|
2015-03-14 23:02:10 +01:00
|
|
|
if (tok->str() == ">") {
|
2018-09-26 06:23:12 +02:00
|
|
|
if (Token::Match(tok, "> class|struct|union %name%"))
|
2012-01-03 22:35:06 +01:00
|
|
|
classname = tok->strAt(2);
|
2015-05-03 15:21:58 +02:00
|
|
|
if (templateParmDepth<2)
|
2014-05-08 10:15:15 +02:00
|
|
|
break;
|
2015-05-03 15:21:58 +02:00
|
|
|
else
|
|
|
|
--templateParmDepth;
|
2012-01-03 22:35:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// next template parameter
|
2014-05-23 23:38:40 +02:00
|
|
|
if (tok->str() == "," && (1 == templateParmDepth)) // Ticket #5823: Properly count parameters
|
2012-01-03 22:35:06 +01:00
|
|
|
++templatepar;
|
|
|
|
|
2014-05-08 10:15:15 +02:00
|
|
|
// default parameter value?
|
|
|
|
else if (Token::Match(tok, "= !!>")) {
|
|
|
|
if (defaultedArgPos.insert(templatepar).second) {
|
|
|
|
eq.push_back(tok);
|
|
|
|
} else {
|
|
|
|
// Ticket #5605: Syntax error (two equal signs for the same parameter), bail out
|
|
|
|
eq.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-03 22:35:06 +01:00
|
|
|
}
|
|
|
|
if (eq.empty() || classname.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// iterate through all template instantiations
|
2018-09-01 11:26:10 +02:00
|
|
|
for (const TokenAndName &templateInst : mTemplateInstantiations) {
|
2018-07-15 14:45:15 +02:00
|
|
|
Token *tok = templateInst.token;
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2015-12-03 13:04:55 +01:00
|
|
|
if (!Token::simpleMatch(tok, (classname + " <").c_str()))
|
2012-01-03 22:35:06 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// count the parameters..
|
2014-05-23 23:38:40 +02:00
|
|
|
tok = tok->next();
|
2018-09-01 11:26:10 +02:00
|
|
|
const unsigned int usedpar = templateParameters(tok);
|
2014-05-23 23:38:40 +02:00
|
|
|
tok = tok->findClosingBracket();
|
2012-01-03 22:35:06 +01:00
|
|
|
|
|
|
|
if (tok && tok->str() == ">") {
|
|
|
|
tok = tok->previous();
|
|
|
|
std::list<Token *>::const_iterator it = eq.begin();
|
|
|
|
for (std::size_t i = (templatepar - eq.size()); it != eq.end() && i < usedpar; ++i)
|
|
|
|
++it;
|
|
|
|
while (it != eq.end()) {
|
2014-09-06 20:39:04 +02:00
|
|
|
int indentlevel = 0;
|
2012-01-03 22:35:06 +01:00
|
|
|
tok->insertToken(",");
|
|
|
|
tok = tok->next();
|
|
|
|
const Token *from = (*it)->next();
|
|
|
|
std::stack<Token *> links;
|
2017-01-08 09:38:29 +01:00
|
|
|
while (from && (!links.empty() || indentlevel || !Token::Match(from, ",|>"))) {
|
2014-09-06 20:39:04 +02:00
|
|
|
if (from->str() == "<")
|
|
|
|
++indentlevel;
|
|
|
|
else if (from->str() == ">")
|
|
|
|
--indentlevel;
|
2013-09-24 06:43:03 +02:00
|
|
|
tok->insertToken(from->str(), from->originalName());
|
2012-01-03 22:35:06 +01:00
|
|
|
tok = tok->next();
|
|
|
|
if (Token::Match(tok, "(|["))
|
|
|
|
links.push(tok);
|
|
|
|
else if (!links.empty() && Token::Match(tok, ")|]")) {
|
|
|
|
Token::createMutualLinks(links.top(), tok);
|
|
|
|
links.pop();
|
|
|
|
}
|
|
|
|
from = from->next();
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 14:45:15 +02:00
|
|
|
for (Token * const eqtok : eq) {
|
2013-11-21 20:27:24 +01:00
|
|
|
Token *tok2;
|
|
|
|
int indentlevel = 0;
|
2012-01-15 12:14:47 +01:00
|
|
|
for (tok2 = eqtok->next(); tok2; tok2 = tok2->next()) {
|
2015-05-01 17:13:02 +02:00
|
|
|
if (Token::Match(tok2, ";|)|}|]")) { // bail out #6607
|
|
|
|
tok2 = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Token::Match(tok2, "(|{|["))
|
2012-01-15 12:14:47 +01:00
|
|
|
tok2 = tok2->link();
|
2013-11-21 20:27:24 +01:00
|
|
|
else if (Token::Match(tok2, "%type% <") && templateParameters(tok2->next())) {
|
2018-09-01 11:26:10 +02:00
|
|
|
std::list<TokenAndName>::iterator ti = std::find_if(mTemplateInstantiations.begin(),
|
|
|
|
mTemplateInstantiations.end(),
|
2018-01-05 15:27:43 +01:00
|
|
|
FindToken(tok2));
|
2018-09-01 11:26:10 +02:00
|
|
|
if (ti != mTemplateInstantiations.end())
|
|
|
|
mTemplateInstantiations.erase(ti);
|
2013-11-21 20:27:24 +01:00
|
|
|
++indentlevel;
|
|
|
|
} else if (indentlevel > 0 && tok2->str() == ">")
|
|
|
|
--indentlevel;
|
2015-03-14 23:02:10 +01:00
|
|
|
else if (indentlevel == 0 && Token::Match(tok2, ",|>"))
|
2013-11-21 20:27:24 +01:00
|
|
|
break;
|
|
|
|
if (indentlevel < 0)
|
2012-01-15 12:14:47 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-01-18 01:39:53 +01:00
|
|
|
// something went wrong, don't call eraseTokens()
|
|
|
|
// with a nullptr "end" parameter (=all remaining tokens).
|
|
|
|
if (!tok2)
|
|
|
|
continue;
|
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
// don't strip args from uninstantiated templates
|
|
|
|
std::list<TokenAndName>::iterator ti2 = std::find_if(mTemplateInstantiations.begin(),
|
|
|
|
mTemplateInstantiations.end(),
|
|
|
|
FindName(template1.name));
|
|
|
|
|
|
|
|
if (ti2 == mTemplateInstantiations.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
eraseTokens(eqtok, tok2);
|
2012-01-15 12:14:47 +01:00
|
|
|
eqtok->deleteThis();
|
2018-12-30 21:16:11 +01:00
|
|
|
|
|
|
|
// update parameter end pointer
|
|
|
|
template1.paramEnd = template1.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
|
|
|
{
|
2018-09-01 11:26:10 +02:00
|
|
|
std::list<TokenAndName>::iterator it1, it2;
|
|
|
|
for (it1 = mTemplateInstantiations.begin(); it1 != mTemplateInstantiations.end();) {
|
|
|
|
TokenAndName &templateAlias = *it1;
|
2017-12-29 22:47:07 +01:00
|
|
|
++it1;
|
|
|
|
Token *startToken = templateAlias.token;
|
|
|
|
while (Token::Match(startToken->tokAt(-2), "%name% :: %name%"))
|
|
|
|
startToken = startToken->tokAt(-2);
|
|
|
|
if (!Token::Match(startToken->tokAt(-4), "> using %name% = %name% ::|<"))
|
|
|
|
continue;
|
|
|
|
const std::string aliasName(startToken->strAt(-2));
|
|
|
|
const Token * const aliasToken1 = startToken;
|
|
|
|
|
|
|
|
// Get start token for alias
|
|
|
|
startToken = startToken->tokAt(-5);
|
|
|
|
while (Token::Match(startToken, "%name%|<|>|>>|,"))
|
|
|
|
startToken = startToken->previous();
|
2019-01-31 23:57:37 +01:00
|
|
|
// handle case where 'template' is first token
|
|
|
|
if (!startToken) {
|
|
|
|
if (!Token::simpleMatch(mTokenList.front(), "template <"))
|
|
|
|
continue;
|
|
|
|
} else if (!Token::Match(startToken, "[;{}] template <"))
|
2017-12-29 22:47:07 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// alias parameters..
|
|
|
|
std::vector<const Token *> aliasParameters;
|
2019-01-31 23:57:37 +01:00
|
|
|
getTemplateParametersInDeclaration(startToken ? startToken->tokAt(3) : mTokenList.front()->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..
|
|
|
|
const Token *endToken = nullptr;
|
2018-09-01 11:26:10 +02:00
|
|
|
for (it2 = it1; it2 != mTemplateInstantiations.end(); ++it2) {
|
|
|
|
TokenAndName &aliasUsage = *it2;
|
2017-12-29 22:47:07 +01:00
|
|
|
if (aliasUsage.name != aliasName)
|
|
|
|
continue;
|
|
|
|
std::vector<std::pair<Token *, Token *>> args;
|
|
|
|
Token *tok2 = aliasUsage.token->tokAt(2);
|
|
|
|
while (tok2) {
|
|
|
|
Token * const start = tok2;
|
2018-01-05 16:10:13 +01:00
|
|
|
while (tok2 && !Token::Match(tok2, "[,>;{}]")) {
|
|
|
|
if (tok2->link() && Token::Match(tok2, "(|<|["))
|
|
|
|
tok2 = tok2->link();
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!tok2 || tok2->str() != ">" || args.size() != aliasParameters.size())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Replace template alias code..
|
|
|
|
aliasUsage.name = templateAlias.name;
|
2018-01-05 22:24:28 +01:00
|
|
|
if (aliasUsage.name.find(' ') == std::string::npos) {
|
2018-10-26 14:20:13 +02:00
|
|
|
const Token *temp = aliasToken1;
|
|
|
|
while (temp && temp != templateAlias.token) {
|
|
|
|
aliasUsage.token->insertToken(temp->str(), "", true);
|
|
|
|
temp = temp->next();
|
|
|
|
}
|
2017-12-29 22:47:07 +01:00
|
|
|
aliasUsage.token->str(templateAlias.token->str());
|
|
|
|
} else {
|
2018-01-07 14:07:34 +01:00
|
|
|
tok2 = TokenList::copyTokens(aliasUsage.token, aliasToken1, templateAlias.token, true);
|
2018-09-01 11:26:10 +02:00
|
|
|
deleteToken(aliasUsage.token);
|
2017-12-29 22:47:07 +01:00
|
|
|
aliasUsage.token = tok2;
|
|
|
|
}
|
|
|
|
tok2 = aliasUsage.token->next(); // the '<'
|
2018-01-05 16:10:13 +01:00
|
|
|
const Token * const endToken1 = templateAlias.token->next()->findClosingBracket();
|
2018-09-23 20:24:51 +02:00
|
|
|
const Token * const endToken2 = TokenList::copyTokens(tok2, templateAlias.token->tokAt(2), endToken1->previous(), false);
|
2018-01-06 08:40:06 +01:00
|
|
|
for (const Token *tok1 = templateAlias.token->next(); tok2 != endToken2; tok1 = tok1->next(), tok2 = tok2->next()) {
|
|
|
|
if (!tok2->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% <")) {
|
2018-09-01 11:26:10 +02:00
|
|
|
const std::list<TokenAndName>::iterator it = std::find_if(mTemplateInstantiations.begin(),
|
|
|
|
mTemplateInstantiations.end(),
|
2018-01-06 08:40:06 +01:00
|
|
|
FindToken(tok1));
|
2018-09-01 11:26:10 +02:00
|
|
|
if (it != mTemplateInstantiations.end())
|
2019-03-03 07:40:55 +01:00
|
|
|
addInstantiation(tok2, it->scope);
|
2018-01-06 08:40:06 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-05 16:10:13 +01:00
|
|
|
const unsigned int argnr = aliasParameterNames[tok2->str()];
|
|
|
|
const Token * const fromStart = args[argnr].first;
|
|
|
|
const Token * const fromEnd = args[argnr].second->previous();
|
|
|
|
Token * const destToken = tok2;
|
2018-01-07 14:07:34 +01:00
|
|
|
tok2 = TokenList::copyTokens(tok2, fromStart, fromEnd, true);
|
2018-01-05 17:06:02 +01:00
|
|
|
if (tok2 == destToken->next())
|
|
|
|
tok2 = destToken;
|
2018-01-05 16:10:13 +01:00
|
|
|
destToken->deleteThis();
|
2017-12-29 22:47:07 +01:00
|
|
|
}
|
2018-01-05 16:10:13 +01:00
|
|
|
|
|
|
|
endToken = endToken1->next();
|
|
|
|
|
2018-01-06 08:40:06 +01:00
|
|
|
// Remove alias usage code (parameters)
|
2018-01-05 16:10:13 +01:00
|
|
|
Token::eraseTokens(tok2, args.back().second);
|
2017-12-29 22:47:07 +01:00
|
|
|
}
|
|
|
|
if (endToken) {
|
2018-01-05 16:10:13 +01:00
|
|
|
// Remove all template instantiations in template alias
|
2019-01-31 23:57:37 +01:00
|
|
|
for (const Token *tok = startToken ? startToken : mTokenList.front(); tok != endToken; tok = tok->next()) {
|
2018-01-05 16:10:13 +01:00
|
|
|
if (!Token::Match(tok, "%name% <"))
|
|
|
|
continue;
|
2018-09-01 11:26:10 +02:00
|
|
|
std::list<TokenAndName>::iterator it = std::find_if(mTemplateInstantiations.begin(),
|
|
|
|
mTemplateInstantiations.end(),
|
2018-01-05 16:10:13 +01:00
|
|
|
FindToken(tok));
|
2018-09-01 11:26:10 +02:00
|
|
|
if (it == mTemplateInstantiations.end())
|
2018-01-05 16:10:13 +01:00
|
|
|
continue;
|
|
|
|
std::list<TokenAndName>::iterator next = it;
|
2018-01-05 17:29:40 +01:00
|
|
|
++next;
|
|
|
|
if (it == it1)
|
|
|
|
it1 = next;
|
2018-09-01 11:26:10 +02:00
|
|
|
mTemplateInstantiations.erase(it,next);
|
2018-01-05 16:10:13 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 23:57:37 +01:00
|
|
|
// find declaration
|
2019-02-01 06:59:49 +01:00
|
|
|
const std::list<TokenAndName>::iterator it3 = std::find_if(mTemplateDeclarations.begin(),
|
2019-01-31 23:57:37 +01:00
|
|
|
mTemplateDeclarations.end(),
|
2019-02-01 06:59:49 +01:00
|
|
|
FindToken(startToken ? startToken->next() : mTokenList.front()));
|
2019-01-31 23:57:37 +01:00
|
|
|
|
|
|
|
if (startToken)
|
|
|
|
eraseTokens(startToken, endToken);
|
|
|
|
else {
|
|
|
|
eraseTokens(mTokenList.front(), endToken);
|
|
|
|
deleteToken(mTokenList.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove declaration
|
2019-02-01 06:59:49 +01:00
|
|
|
if (it3 != mTemplateDeclarations.end())
|
|
|
|
mTemplateDeclarations.erase(it3);
|
2017-12-29 22:47:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 10:55:18 +01:00
|
|
|
bool TemplateSimplifier::instantiateMatch(const Token *instance, const std::size_t numberOfArguments, const char patternAfter[])
|
2012-01-03 22:35:06 +01:00
|
|
|
{
|
2017-12-26 10:55:18 +01:00
|
|
|
// if (!Token::simpleMatch(instance, (name + " <").c_str()))
|
|
|
|
// return false;
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
if (numberOfArguments != templateParameters(instance->next()))
|
2012-01-03 22:35:06 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (patternAfter) {
|
2013-04-12 16:38:12 +02:00
|
|
|
const Token *tok = instance;
|
|
|
|
unsigned int indentlevel = 0;
|
2015-03-14 23:02:10 +01:00
|
|
|
for (tok = instance; tok && (tok->str() != ">" || indentlevel > 0); tok = tok->next()) {
|
2017-12-02 19:18:36 +01:00
|
|
|
if (Token::Match(tok, "<|,|(|:: %name% <") && templateParameters(tok->tokAt(2)) > 0)
|
2013-04-12 16:38:12 +02:00
|
|
|
++indentlevel;
|
|
|
|
if (indentlevel > 0 && tok->str() == ">")
|
|
|
|
--indentlevel;
|
|
|
|
}
|
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-01-12 18:44:48 +01:00
|
|
|
else if (Token::simpleMatch(tok, "decltype (")) {
|
2018-12-29 11:19:53 +01:00
|
|
|
const Token * end = tok->linkAt(1);
|
|
|
|
while (tok && tok != end) {
|
|
|
|
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;
|
|
|
|
while (tok && 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;
|
|
|
|
else if (Token::Match(tok->next(), "%type% <")) {
|
|
|
|
const Token *closing = tok->tokAt(2)->findClosingBracket();
|
|
|
|
if (closing) {
|
|
|
|
if (Token::Match(closing->next(), "=|;"))
|
|
|
|
return true;
|
|
|
|
while (tok && tok->next() && tok->next() != closing) {
|
|
|
|
tok = tok->next();
|
|
|
|
namepos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (Token::Match(tok->next(), "%type% =|;")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
tok = tok->next();
|
|
|
|
namepos++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TemplateSimplifier::getTemplateNamePosition(const Token *tok)
|
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-01-18 21:12:39 +01:00
|
|
|
if (Token::Match(tok, "> class|struct|union %type% :|<|;|{"))
|
2012-01-03 22:49:50 +01:00
|
|
|
namepos = 2;
|
2019-01-31 23:57:37 +01:00
|
|
|
else if (Token::Match(tok, "> using %name% ="))
|
|
|
|
namepos = 2;
|
2019-01-18 21:12:39 +01:00
|
|
|
else if (getTemplateNamePositionTemplateVariable(tok, namepos))
|
|
|
|
;
|
2018-12-22 10:05:10 +01:00
|
|
|
else if (!getTemplateNamePositionTemplateFunction(tok, namepos))
|
|
|
|
return -1; // Name not found
|
2012-01-03 22:35:06 +01:00
|
|
|
|
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;
|
|
|
|
while ((end = templateDeclaration.scope.find(" ", start)) != std::string::npos) {
|
|
|
|
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;
|
2018-11-10 16:41:14 +01:00
|
|
|
if (insert)
|
|
|
|
mTokenList.back()->tokAt(offset)->insertToken(token, "");
|
|
|
|
else
|
|
|
|
mTokenList.addtoken(token, tok->linenr(), tok->fileIndex());
|
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
|
|
|
|
std::string token = templateDeclaration.scope.substr(start, end - start);
|
|
|
|
if (token != tokStart->str() || tok->strAt(-1) != "::") {
|
|
|
|
if (insert) {
|
|
|
|
mTokenList.back()->tokAt(offset)->insertToken(templateDeclaration.scope.substr(start), "");
|
|
|
|
mTokenList.back()->tokAt(offset)->insertToken("::", "");
|
|
|
|
} else {
|
|
|
|
mTokenList.addtoken(templateDeclaration.scope.substr(start), tok->linenr(), tok->fileIndex());
|
|
|
|
mTokenList.addtoken("::", tok->linenr(), tok->fileIndex());
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
std::string scope = templateDeclaration.scope;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Token::simpleMatch(tok->tokAt(offset), scope.c_str()) ;
|
|
|
|
}
|
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
|
|
|
std::list<ScopeInfo2> scopeInfo;
|
|
|
|
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;
|
2018-12-30 21:16:11 +01: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 {
|
|
|
|
newInstantiation(Token *t, const std::string &s) : token(t), scope(s) { }
|
|
|
|
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) {
|
|
|
|
templateDeclaration.token->insertToken(templateDeclarationToken->strAt(1), "", true);
|
|
|
|
templateDeclaration.token->insertToken(newName, "", true);
|
|
|
|
templateDeclaration.token->insertToken(";", "", true);
|
2019-01-18 21:12:39 +01:00
|
|
|
} else if ((isFunction && (copy || isSpecialization)) || (isVariable && !isSpecialization)) {
|
2018-12-17 05:58:48 +01:00
|
|
|
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;
|
2018-12-17 05:58:48 +01:00
|
|
|
Token * start;
|
|
|
|
Token * end;
|
|
|
|
auto it = mTemplateForwardDeclarationsMap.find(dst);
|
|
|
|
if (it != mTemplateForwardDeclarationsMap.end()) {
|
|
|
|
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 {
|
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();
|
|
|
|
else if (isVariable && end->str() == "=")
|
|
|
|
end = const_cast<Token *>(Token::findsimplematch(templateDeclarationNameToken, ";"));
|
2018-12-17 05:58:48 +01:00
|
|
|
}
|
|
|
|
unsigned int typeindentlevel = 0;
|
|
|
|
while (!(typeindentlevel == 0 && Token::Match(end, ";|{|:"))) {
|
|
|
|
if (Token::Match(end, "<|(|{"))
|
|
|
|
++typeindentlevel;
|
|
|
|
else if (Token::Match(end, ">|)|}"))
|
|
|
|
--typeindentlevel;
|
|
|
|
end = end->next();
|
|
|
|
}
|
|
|
|
|
2018-12-29 11:19:53 +01:00
|
|
|
if (isStatic)
|
|
|
|
dst->insertToken("static", "", true);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (itype < typeParametersInDeclaration.size()) {
|
|
|
|
typeindentlevel = 0;
|
2018-12-21 13:51:45 +01:00
|
|
|
for (const Token *typetok = mTypesUsedInTemplateInstantiation[itype].token;
|
2018-12-17 05:58:48 +01:00
|
|
|
typetok && (typeindentlevel > 0 || !Token::Match(typetok, ",|>"));
|
|
|
|
typetok = typetok->next()) {
|
|
|
|
if (Token::simpleMatch(typetok, ". . .")) {
|
|
|
|
typetok = typetok->tokAt(2);
|
|
|
|
continue;
|
2018-11-26 06:23:12 +01:00
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
if (Token::Match(typetok, "%name% <") && templateParameters(typetok->next()) > 0)
|
|
|
|
++typeindentlevel;
|
|
|
|
else if (typeindentlevel > 0 && typetok->str() == ">")
|
|
|
|
--typeindentlevel;
|
|
|
|
dst->insertToken(typetok->str(), typetok->originalName(), true);
|
|
|
|
dst->previous()->isTemplateArg(true);
|
|
|
|
dst->previous()->isSigned(typetok->isSigned());
|
|
|
|
dst->previous()->isUnsigned(typetok->isUnsigned());
|
|
|
|
dst->previous()->isLong(typetok->isLong());
|
|
|
|
}
|
|
|
|
} 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();
|
|
|
|
} else if (start->str() == templateDeclarationNameToken->str()) {
|
2019-01-18 21:12:39 +01:00
|
|
|
if (start->strAt(1) != "<" || Token::Match(start, newName.c_str()) || !inAssignment) {
|
|
|
|
dst->insertToken(newName, "", true);
|
|
|
|
if (start->strAt(1) == "<")
|
|
|
|
start = start->next()->findClosingBracket();
|
2019-02-28 08:30:04 +01:00
|
|
|
} else {
|
2019-01-18 21:12:39 +01:00
|
|
|
dst->insertToken(start->str(), "", true);
|
2019-03-03 07:40:55 +01: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
|
|
|
|
Token * closing = start->next()->findClosingBracket();
|
|
|
|
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
|
|
|
|
for (const auto & inst : mTemplateInstantiations) {
|
2019-01-14 20:36:23 +01:00
|
|
|
if (Token::simpleMatch(inst.token, name.c_str())) {
|
2018-12-17 05:58:48 +01:00
|
|
|
// use the instantiated name
|
|
|
|
dst->insertToken(name, "", true);
|
|
|
|
start = closing;
|
|
|
|
break;
|
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);
|
|
|
|
dst->previous()->isSigned(start->isSigned());
|
|
|
|
dst->previous()->isUnsigned(start->isUnsigned());
|
|
|
|
dst->previous()->isLong(start->isLong());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dst->insertToken(start->str(), start->originalName(), true);
|
|
|
|
dst->previous()->isSigned(start->isSigned());
|
|
|
|
dst->previous()->isUnsigned(start->isUnsigned());
|
|
|
|
dst->previous()->isLong(start->isLong());
|
|
|
|
}
|
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, "]|}|)")) {
|
|
|
|
Token::createMutualLinks(links[start], dst->previous());
|
|
|
|
links.erase(start);
|
2018-11-23 11:36:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
|
|
|
|
start = start->next();
|
|
|
|
}
|
|
|
|
dst->insertToken(";", "", true);
|
2019-01-18 21:12:39 +01:00
|
|
|
|
|
|
|
if (isVariable)
|
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
|
|
|
|
Token * start = templateInstantiation.token;
|
|
|
|
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) {
|
2019-01-14 20:36:23 +01:00
|
|
|
if (Token::Match(tok3, "{|}|namespace|class|struct|union")) {
|
2018-06-02 13:49:14 +02:00
|
|
|
setScopeInfo(tok3, &scopeInfo);
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-25 08:19:46 +01:00
|
|
|
if (inTemplateDefinition) {
|
2019-01-26 07:08:54 +01:00
|
|
|
if (!endOfTemplateDefinition) {
|
|
|
|
if (isVariable)
|
|
|
|
endOfTemplateDefinition = Token::findsimplematch(tok3, ";");
|
|
|
|
else if (tok3->str() == "{")
|
|
|
|
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);
|
2014-11-01 22:07:24 +01:00
|
|
|
if (localTypeParametersInDeclaration.size() != typeParametersInDeclaration.size())
|
|
|
|
inTemplateDefinition = false; // Partial specialization
|
|
|
|
else
|
|
|
|
inTemplateDefinition = true;
|
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% <") &&
|
2018-10-26 14:20:13 +02:00
|
|
|
templateInstantiation.name == tok3->str() &&
|
2018-09-01 11:26:10 +02:00
|
|
|
instantiateMatch(tok3, typeParametersInDeclaration.size(), ":: ~| %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
|
|
|
|
while (tok5 && tok5 != tok3) {
|
2018-09-02 17:49:13 +02:00
|
|
|
// replace name if found
|
2018-10-26 14:20:13 +02:00
|
|
|
if (Token::Match(tok5, "%name% <") && tok5->str() == templateInstantiation.name) {
|
2018-10-14 16:57:07 +02:00
|
|
|
if (copy) {
|
2018-11-05 19:55:21 +01:00
|
|
|
if (!templateDeclaration.scope.empty() && tok5->strAt(-1) != "::")
|
|
|
|
addNamespace(templateDeclaration, tok5);
|
2018-10-14 16:57:07 +02:00
|
|
|
mTokenList.addtoken(newName, tok5->linenr(), tok5->fileIndex());
|
|
|
|
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;
|
|
|
|
if (tok5->isName()) {
|
|
|
|
// 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..
|
|
|
|
if (itype < typeParametersInDeclaration.size()) {
|
|
|
|
unsigned int typeindentlevel = 0;
|
|
|
|
for (const Token *typetok = mTypesUsedInTemplateInstantiation[itype].token;
|
|
|
|
typetok && (typeindentlevel>0 || !Token::Match(typetok, ",|>"));
|
|
|
|
typetok = typetok->next()) {
|
|
|
|
if (Token::simpleMatch(typetok, ". . .")) {
|
|
|
|
typetok = typetok->tokAt(2);
|
|
|
|
} else {
|
|
|
|
if (Token::Match(typetok, "%name% <") && templateParameters(typetok->next()) > 0)
|
|
|
|
++typeindentlevel;
|
|
|
|
else if (typeindentlevel > 0 && typetok->str() == ">")
|
|
|
|
--typeindentlevel;
|
|
|
|
mTokenList.addtoken(typetok, tok5->linenr(), tok5->fileIndex());
|
|
|
|
mTokenList.back()->isTemplateArg(true);
|
|
|
|
added = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!added)
|
|
|
|
mTokenList.addtoken(tok5, tok5->linenr(), tok5->fileIndex());
|
|
|
|
}
|
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) {
|
|
|
|
if (!templateDeclaration.scope.empty() && tok3->strAt(-1) != "::")
|
|
|
|
addNamespace(templateDeclaration, tok3);
|
2018-10-14 16:57:07 +02:00
|
|
|
mTokenList.addtoken(newName, tok3->linenr(), 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
|
|
|
|
|
|
|
std::list<TokenAndName>::iterator it = std::find_if(mTemplateDeclarations.begin(),
|
|
|
|
mTemplateDeclarations.end(),
|
|
|
|
FindToken(startOfTemplateDeclaration));
|
|
|
|
if (it != mTemplateDeclarations.end())
|
|
|
|
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
|
2018-10-26 14:20:13 +02:00
|
|
|
const std::string lastName = (templateInstantiation.name.find(' ') != std::string::npos) ? templateInstantiation.name.substr(templateInstantiation.name.rfind(' ')+1) : templateInstantiation.name;
|
|
|
|
|
2017-12-26 10:55:18 +01:00
|
|
|
|
2012-01-05 21:45:19 +01:00
|
|
|
for (; tok3; tok3 = tok3->next()) {
|
|
|
|
if (tok3->isName()) {
|
|
|
|
// 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..
|
|
|
|
if (itype < typeParametersInDeclaration.size()) {
|
2013-04-07 16:48:42 +02:00
|
|
|
unsigned int typeindentlevel = 0;
|
2018-12-21 13:51:45 +01:00
|
|
|
for (const Token *typetok = mTypesUsedInTemplateInstantiation[itype].token;
|
2015-03-14 23:02:10 +01:00
|
|
|
typetok && (typeindentlevel>0 || !Token::Match(typetok, ",|>"));
|
2012-01-05 21:45:19 +01:00
|
|
|
typetok = typetok->next()) {
|
2015-04-13 17:02:48 +02:00
|
|
|
if (Token::simpleMatch(typetok, ". . .")) {
|
|
|
|
typetok = typetok->tokAt(2);
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-31 10:50:39 +01:00
|
|
|
if (Token::Match(typetok, "%name% <") && templateParameters(typetok->next()) > 0)
|
2013-04-07 16:48:42 +02:00
|
|
|
++typeindentlevel;
|
|
|
|
else if (typeindentlevel > 0 && typetok->str() == ">")
|
|
|
|
--typeindentlevel;
|
2018-10-14 16:57:07 +02:00
|
|
|
if (copy) {
|
|
|
|
mTokenList.addtoken(typetok, tok3->linenr(), tok3->fileIndex());
|
|
|
|
mTokenList.back()->isTemplateArg(true);
|
|
|
|
}
|
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) {
|
|
|
|
mTokenList.addtoken(newName, tok3->linenr(), tok3->fileIndex());
|
|
|
|
tok3 = closingBracket;
|
|
|
|
} else {
|
|
|
|
tok3->str(newName);
|
|
|
|
eraseTokens(tok3, closingBracket->next());
|
|
|
|
}
|
|
|
|
continue;
|
2018-11-07 21:25:42 +01:00
|
|
|
} else if (!templateDeclaration.scope.empty() &&
|
|
|
|
!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 {
|
2018-10-14 16:57:07 +02:00
|
|
|
if (copy) {
|
2018-10-26 14:20:13 +02:00
|
|
|
// add namespace if necessary
|
2018-11-05 19:55:21 +01:00
|
|
|
if (!templateDeclaration.scope.empty() &&
|
|
|
|
(isClass ? tok3->strAt(1) != "(" : true)) {
|
|
|
|
addNamespace(templateDeclaration, tok3);
|
2018-10-26 14:20:13 +02:00
|
|
|
}
|
2018-10-14 16:57:07 +02:00
|
|
|
mTokenList.addtoken(newName, tok3->linenr(), tok3->fileIndex());
|
2019-01-26 07:08:54 +01: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
|
2018-10-14 16:57:07 +02:00
|
|
|
if (copy)
|
|
|
|
mTokenList.addtoken(tok3, tok3->linenr(), tok3->fileIndex());
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-14 20:36:23 +01:00
|
|
|
std::string scope;
|
|
|
|
for (const Token *prev = tok3->tokAt(-2); Token::Match(prev, "%name% ::"); prev = prev->tokAt(-2)) {
|
|
|
|
if (scope.empty())
|
|
|
|
scope = prev->str();
|
|
|
|
else
|
|
|
|
scope = prev->str() + " :: " + scope;
|
|
|
|
}
|
|
|
|
if (copy)
|
2019-03-03 07:40:55 +01:00
|
|
|
newInstantiations.emplace_back(mTokenList.back(), scope);
|
2019-01-14 20:36:23 +01:00
|
|
|
else if (!inTemplateDefinition)
|
2019-03-03 07:40:55 +01:00
|
|
|
newInstantiations.emplace_back(tok3, 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() == "}") {
|
|
|
|
assert(brackets.empty() == false && brackets.top()->str() == "{");
|
|
|
|
Token::createMutualLinks(brackets.top(), mTokenList.back());
|
|
|
|
if (tok3->strAt(1) == ";") {
|
|
|
|
const Token * tokSemicolon = tok3->next();
|
|
|
|
mTokenList.addtoken(tokSemicolon, tokSemicolon->linenr(), tokSemicolon->fileIndex());
|
|
|
|
}
|
|
|
|
brackets.pop();
|
|
|
|
if (brackets.empty()) {
|
|
|
|
inTemplateDefinition = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (tok3->str() == ")") {
|
|
|
|
assert(brackets.empty() == false && brackets.top()->str() == "(");
|
|
|
|
Token::createMutualLinks(brackets.top(), mTokenList.back());
|
|
|
|
brackets.pop();
|
|
|
|
} else if (tok3->str() == "]") {
|
|
|
|
assert(brackets.empty() == false && brackets.top()->str() == "[");
|
|
|
|
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
|
|
|
|
for (const auto & inst : newInstantiations)
|
|
|
|
addInstantiation(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
|
|
|
|
|
|
|
bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
|
|
|
|
{
|
|
|
|
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"
|
|
|
|
if (Token::Match(op, "[/%] 0"))
|
2018-06-01 23:03:53 +02:00
|
|
|
break;
|
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;
|
2016-03-21 21:20:02 +01: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;
|
|
|
|
}
|
|
|
|
|
2019-03-03 07:40:55 +01:00
|
|
|
static Token *skipTernaryOp(Token *tok, Token *backToken)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
bool again = true;
|
|
|
|
|
|
|
|
while (again) {
|
|
|
|
again = false;
|
|
|
|
|
|
|
|
for (Token *tok = start; tok && tok != end; tok = tok->next()) {
|
|
|
|
if (tok->str() == "sizeof") {
|
|
|
|
// sizeof('x')
|
|
|
|
if (Token::Match(tok->next(), "( %char% )")) {
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
std::ostringstream sz;
|
|
|
|
sz << 1;
|
|
|
|
tok->str(sz.str());
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sizeof ("text")
|
|
|
|
else if (Token::Match(tok->next(), "( %str% )")) {
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << (Token::getStrLength(tok) + 1);
|
|
|
|
tok->str(ostr.str());
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok->next(), "( %type% * )")) {
|
|
|
|
tok->str(MathLib::toString(mTokenizer->sizeOfType(tok->tokAt(3))));
|
|
|
|
tok->deleteNext(4);
|
|
|
|
again = true;
|
|
|
|
} else if (Token::simpleMatch(tok->next(), "( * )")) {
|
|
|
|
tok->str(MathLib::toString(mTokenizer->sizeOfType(tok->tokAt(2))));
|
|
|
|
tok->deleteNext(3);
|
|
|
|
again = true;
|
|
|
|
} else if (Token::Match(tok->next(), "( %type% )")) {
|
|
|
|
const unsigned int size = mTokenizer->sizeOfType(tok->tokAt(2));
|
|
|
|
if (size > 0) {
|
|
|
|
tok->str(MathLib::toString(size));
|
|
|
|
tok->deleteNext(3);
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
} else if (tok->strAt(1) == "(") {
|
|
|
|
tok = tok->linkAt(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (simplifyCalculations(start, end))
|
|
|
|
again = true;
|
|
|
|
|
|
|
|
for (Token *tok = start; tok && tok != end; tok = tok->next()) {
|
|
|
|
if (tok->str() == "?" && tok->previous()->isNumber()) {
|
|
|
|
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()) {
|
|
|
|
if (Token::Match(endTok, "(|[|{")) {
|
|
|
|
endTok = endTok->link();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Token *tok = start; tok && tok != end; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "( %num% )") && !Token::Match(tok->previous(), "%name%")) {
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-09 20:33:11 +01:00
|
|
|
// TODO: This is not the correct class for simplifyCalculations(), so it
|
|
|
|
// should be moved away.
|
2019-01-18 21:12:39 +01:00
|
|
|
bool TemplateSimplifier::simplifyCalculations(Token* frontToken, Token *backToken)
|
2012-01-09 20:33:11 +01:00
|
|
|
{
|
2018-05-14 23:06:10 +02:00
|
|
|
bool ret = false;
|
2018-12-21 13:54:59 +01:00
|
|
|
if (!frontToken) {
|
2018-12-21 13:51:45 +01:00
|
|
|
frontToken = mTokenList.front();
|
|
|
|
}
|
2019-01-18 21:12:39 +01:00
|
|
|
for (Token *tok = frontToken; 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 ) ;
|
2015-01-31 10:50:39 +01:00
|
|
|
if ((Token::Match(tok->next(), "( %name% ) ;|)|,|]") ||
|
2015-10-17 18:58:13 +02:00
|
|
|
(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;
|
|
|
|
}
|
|
|
|
|
2012-12-01 01:31:35 +01:00
|
|
|
if (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
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->isNumber()) {
|
2018-06-01 23:03:53 +02:00
|
|
|
if (simplifyNumericCalculations(tok)) {
|
2018-05-31 06:36:59 +02:00
|
|
|
ret = true;
|
2018-06-01 23:03:53 +02:00
|
|
|
Token *prev = tok->tokAt(-2);
|
|
|
|
while (prev && simplifyNumericCalculations(prev)) {
|
|
|
|
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)
|
|
|
|
if (Token::Match(tok->previous(), "[(=,] 0 &&") ||
|
2014-05-19 06:31:38 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-09 20:33:11 +01:00
|
|
|
if (tok->str() == "0") {
|
2015-11-27 15:02:49 +01:00
|
|
|
if ((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;
|
|
|
|
} else if (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;
|
2017-06-08 08:18:40 +02:00
|
|
|
} else if (Token::Match(tok->previous(), "[=[(,] 0 * %name%|%num% ,|]|)|;|=|%cop%") ||
|
2013-03-01 11:43:59 +01:00
|
|
|
Token::Match(tok->previous(), "[=[(,] 0 * (") ||
|
2017-06-08 08:18:40 +02:00
|
|
|
Token::Match(tok->previous(), "return|case 0 *|&& %name%|%num% ,|:|;|=|%cop%") ||
|
2013-03-01 11:43:59 +01:00
|
|
|
Token::Match(tok->previous(), "return|case 0 *|&& (")) {
|
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;
|
2013-03-01 11:43:59 +01:00
|
|
|
} else if (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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->str() == "1") {
|
2013-03-01 11:43:59 +01:00
|
|
|
if (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;
|
2013-03-01 11:43:59 +01:00
|
|
|
} else if (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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:32:56 +02:00
|
|
|
if (Token::Match(tok->tokAt(-2), "%any% * 1") || Token::Match(tok->previous(), "%any% 1 *")) {
|
|
|
|
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..
|
2017-06-07 20:09:06 +02:00
|
|
|
if (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;
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:15:05 +02:00
|
|
|
if (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;
|
|
|
|
}
|
|
|
|
|
2012-12-01 01:31:35 +01:00
|
|
|
if (Token::Match(tok, "%num% %comp% %num%") &&
|
2012-01-09 20:33:11 +01:00
|
|
|
MathLib::isInt(tok->str()) &&
|
|
|
|
MathLib::isInt(tok->strAt(2))) {
|
2013-06-14 18:51:52 +02:00
|
|
|
if (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;
|
|
|
|
}
|
|
|
|
|
2017-12-28 22:10:10 +01:00
|
|
|
const Token * TemplateSimplifier::getTemplateParametersInDeclaration(
|
2014-11-01 22:07:24 +01:00
|
|
|
const Token * tok,
|
|
|
|
std::vector<const Token *> & typeParametersInDeclaration)
|
|
|
|
{
|
|
|
|
typeParametersInDeclaration.clear();
|
|
|
|
for (; tok && tok->str() != ">"; tok = tok->next()) {
|
2018-10-24 14:38:59 +02:00
|
|
|
if (Token::Match(tok, "%name% ,|>|="))
|
2014-11-01 22:07:24 +01:00
|
|
|
typeParametersInDeclaration.push_back(tok);
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
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?
|
|
|
|
for (std::list<const Token *>::const_iterator it = specializations.begin(); it != specializations.end(); ++it) {
|
|
|
|
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();
|
|
|
|
while (declToken != endToken) {
|
|
|
|
if (declToken->str() != instToken->str()) {
|
|
|
|
int nr = 0;
|
|
|
|
while (nr < templateParameters.size() && templateParameters[nr]->str() != declToken->str())
|
|
|
|
++nr;
|
|
|
|
|
|
|
|
if (nr == templateParameters.size())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
declToken = declToken->next();
|
|
|
|
instToken = instToken->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (declToken == endToken && instToken->str() == ">") {
|
|
|
|
// 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;
|
|
|
|
for (Token *tok3 = tok2->tokAt(2); tok3 && (indentlevel > 0 || tok3->str() != ">"); tok3 = tok3->next()) {
|
|
|
|
// #2648 - unhandled parentheses => bail out
|
|
|
|
// #2721 - unhandled [ => bail out
|
|
|
|
if (Token::Match(tok3, "(|[")) {
|
|
|
|
typeForNewName.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!tok3->next()) {
|
|
|
|
typeForNewName.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Token::Match(tok3->tokAt(-2), "<|,|:: %name% <") && templateParameters(tok3) > 0)
|
|
|
|
++indentlevel;
|
|
|
|
else if (indentlevel > 0 && Token::Match(tok3, "> [,>]"))
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel == 0 && Token::Match(tok3->previous(), "[<,]")) {
|
2019-01-14 20:36:23 +01:00
|
|
|
mTypesUsedInTemplateInstantiation.emplace_back(tok3, "");
|
2018-10-14 16:57:07 +02:00
|
|
|
}
|
|
|
|
const bool constconst = tok3->str() == "const" && tok3->strAt(1) == "const";
|
|
|
|
if (!constconst) {
|
|
|
|
typeStringsUsedInTemplateInstantiation.push_back(tok3->str());
|
|
|
|
}
|
|
|
|
// add additional type information
|
|
|
|
if (!constconst && !Token::Match(tok3, "class|struct|enum")) {
|
|
|
|
if (tok3->isUnsigned())
|
|
|
|
typeForNewName += "unsigned";
|
|
|
|
else if (tok3->isSigned())
|
|
|
|
typeForNewName += "signed";
|
|
|
|
if (tok3->isLong())
|
|
|
|
typeForNewName += "long";
|
|
|
|
if (!typeForNewName.empty())
|
|
|
|
typeForNewName += ' ';
|
|
|
|
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;
|
2018-12-17 05:58:48 +01:00
|
|
|
getTemplateParametersInDeclaration(templateDeclaration.token->tokAt(2), typeParametersInDeclaration);
|
2018-06-16 16:10:28 +02: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) {
|
|
|
|
if (numberOfTemplateInstantiations != mTemplateInstantiations.size()) {
|
|
|
|
numberOfTemplateInstantiations = mTemplateInstantiations.size();
|
2012-01-09 20:33:11 +01:00
|
|
|
++recursiveCount;
|
2016-01-30 11:22:44 +01:00
|
|
|
if (recursiveCount > 100) {
|
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
|
2018-05-30 14:40:38 +02:00
|
|
|
if (!Token::Match(instantiation.token, "%name% <"))
|
2018-01-03 23:05:57 +01:00
|
|
|
continue;
|
|
|
|
|
2019-01-14 20:36:23 +01:00
|
|
|
if (instantiation.fullName != templateDeclaration.fullName) {
|
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
|
|
|
|
if (instantiation.name != templateDeclaration.name)
|
|
|
|
continue;
|
2019-03-01 08:18:53 +01:00
|
|
|
|
|
|
|
// scopes must match when present
|
|
|
|
if (!instantiation.scope.empty() && !templateDeclaration.scope.empty())
|
|
|
|
continue;
|
2019-01-14 20:36:23 +01:00
|
|
|
}
|
2017-12-26 10:55:18 +01:00
|
|
|
|
2018-12-30 21:16:11 +01:00
|
|
|
if (!matchSpecialization(templateDeclaration.nameToken, instantiation.token, specializations))
|
2017-12-30 22:14:48 +01:00
|
|
|
continue;
|
|
|
|
|
2018-05-30 14:40:38 +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());
|
2016-02-12 12:05:32 +01:00
|
|
|
#ifdef MAXTIME
|
|
|
|
if (std::time(0) > maxtime)
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
(void)maxtime;
|
|
|
|
#endif
|
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;
|
2018-10-14 16:57:07 +02:00
|
|
|
while (Token::Match(startToken->tokAt(-2), "%name% :: %name%") ||
|
|
|
|
Token::Match(startToken->tokAt(-2), "> :: %name%")) {
|
|
|
|
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") &&
|
2019-02-25 21:01:34 +01:00
|
|
|
(!specialized && !instantiateMatch(tok2, typeParametersInDeclaration.size(), 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
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
if (typeForNewName.empty() || (!typeParametersInDeclaration.empty() && typeParametersInDeclaration.size() != mTypesUsedInTemplateInstantiation.size())) {
|
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);
|
2018-09-01 11:26:10 +02:00
|
|
|
mErrorLogger->reportErr(ErrorLogger::ErrorMessage(callstack, &mTokenList, Severity::debug, "debug",
|
|
|
|
"Failed to instantiate template \"" + instantiation.name + "\". The checking continues anyway.", false));
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
if (typeForNewName.empty())
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// New classname/funcname..
|
2017-12-26 10:55:18 +01:00
|
|
|
const std::string newName(templateDeclaration.name + " < " + typeForNewName + " >");
|
2019-01-14 20:36:23 +01:00
|
|
|
const std::string newFullName(templateDeclaration.scope + (templateDeclaration.scope.empty() ? "" : " :: ") + newName);
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2019-01-14 20:36:23 +01:00
|
|
|
if (expandedtemplates.find(newFullName) == expandedtemplates.end()) {
|
|
|
|
expandedtemplates.insert(newFullName);
|
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;
|
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) {
|
2018-12-30 21:16:11 +01: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());
|
|
|
|
#ifdef MAXTIME
|
|
|
|
if (std::time(0) > maxtime)
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
(void)maxtime;
|
|
|
|
#endif
|
|
|
|
assert(mTokenList.validateToken(tok2)); // that assertion fails on examples from #6021
|
|
|
|
|
|
|
|
Token *startToken = tok2;
|
|
|
|
while (Token::Match(startToken->tokAt(-2), "%name% :: %name%") ||
|
|
|
|
Token::Match(startToken->tokAt(-2), "> :: %name%")) {
|
|
|
|
if (startToken->strAt(-2) == ">") {
|
|
|
|
const Token * tok3 = startToken->tokAt(-2)->findOpeningBracket();
|
|
|
|
if (tok3)
|
|
|
|
startToken = tok3->previous();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
startToken = startToken->tokAt(-2);
|
|
|
|
}
|
|
|
|
|
2018-11-23 11:36:09 +01:00
|
|
|
if (Token::Match(startToken->previous(), ";|{|}|=|const") &&
|
2019-02-25 21:01:34 +01:00
|
|
|
(!specialized && !instantiateMatch(tok2, typeParametersInDeclaration.size(), isfunc ? "(" : isVar ? ";|%op%" : "*|&|::| %name%")))
|
2018-10-14 16:57:07 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2018-12-30 21:16:11 +01: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);
|
|
|
|
mErrorLogger->reportErr(ErrorLogger::ErrorMessage(callstack, &mTokenList, Severity::debug, "debug",
|
|
|
|
"Failed to instantiate template \"" + templateDeclaration.name + "\". The checking continues anyway.", false));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// New classname/funcname..
|
|
|
|
const std::string newName(templateDeclaration.name + " < " + typeForNewName + " >");
|
2019-01-14 20:36:23 +01:00
|
|
|
const std::string newFullName(templateDeclaration.scope + (templateDeclaration.scope.empty() ? "" : " :: ") + newName);
|
|
|
|
|
|
|
|
if (expandedtemplates.find(newFullName) == expandedtemplates.end()) {
|
|
|
|
expandedtemplates.insert(newFullName);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
std::list<std::string>::const_iterator it = strings.begin();
|
|
|
|
const Token *tok = nameTok->tokAt(2);
|
|
|
|
while (tok && it != strings.end() && *it == tok->str()) {
|
|
|
|
tok = tok->next();
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return it == strings.end() && tok && tok->str() == ">";
|
|
|
|
}
|
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
|
|
|
{
|
2017-12-26 10:55:18 +01:00
|
|
|
std::list<ScopeInfo2> scopeInfo;
|
2017-06-08 00:16:43 +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, "{|}|namespace|class|struct|union")) {
|
2018-06-02 13:49:14 +02:00
|
|
|
setScopeInfo(nameTok, &scopeInfo);
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-14 20:36:23 +01:00
|
|
|
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;
|
|
|
|
|
2019-01-14 20:36:23 +01:00
|
|
|
std::set<TemplateSimplifier::TokenAndName*> & pointers = nameTok->templateSimplifierPointers();
|
|
|
|
|
|
|
|
// check if instantiation matches token instantiation from pointer
|
|
|
|
if (pointers.size()) {
|
|
|
|
// check full name
|
|
|
|
if (instantiation.fullName != (*pointers.begin())->fullName) {
|
|
|
|
// FIXME: fallback to just matching name
|
|
|
|
if (nameTok->str() != instantiation.name)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no pointer available look at tokens directly
|
|
|
|
else {
|
|
|
|
// FIXME: fallback to just matching name
|
|
|
|
if (nameTok->str() != instantiation.name)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!matchTemplateParameters(nameTok, typeStringsUsedInTemplateInstantiation))
|
2017-06-08 00:16:43 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// match parameters
|
|
|
|
Token * tok2 = nameTok->tokAt(2);
|
|
|
|
unsigned int typeCountInInstantiation = 1U; // There is always at least one type
|
2018-12-21 13:51:45 +01:00
|
|
|
const Token *typetok = (!mTypesUsedInTemplateInstantiation.empty()) ? mTypesUsedInTemplateInstantiation[0].token : nullptr;
|
2017-06-08 00:16:43 +02:00
|
|
|
unsigned int indentlevel2 = 0; // indentlevel for tokgt
|
|
|
|
while (tok2 && (indentlevel2 > 0 || tok2->str() != ">")) {
|
|
|
|
if (tok2->str() == "<" && templateParameters(tok2) > 0)
|
|
|
|
++indentlevel2;
|
|
|
|
else if (indentlevel2 > 0 && Token::Match(tok2, "> [,>]"))
|
|
|
|
--indentlevel2;
|
|
|
|
else if (indentlevel2 == 0) {
|
|
|
|
if (tok2->str() != ",") {
|
|
|
|
if (!typetok ||
|
|
|
|
tok2->isUnsigned() != typetok->isUnsigned() ||
|
|
|
|
tok2->isSigned() != typetok->isSigned() ||
|
|
|
|
tok2->isLong() != typetok->isLong()) {
|
|
|
|
break;
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 11:25:28 +02:00
|
|
|
typetok = typetok->next();
|
2017-06-08 00:16:43 +02:00
|
|
|
} else {
|
2018-10-14 16:57:07 +02:00
|
|
|
if (typeCountInInstantiation < mTypesUsedInTemplateInstantiation.size())
|
2018-12-21 13:51:45 +01:00
|
|
|
typetok = mTypesUsedInTemplateInstantiation[typeCountInInstantiation++].token;
|
2017-06-08 00:16:43 +02:00
|
|
|
else
|
|
|
|
typetok = nullptr;
|
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
2017-06-08 00:16:43 +02:00
|
|
|
tok2 = tok2->next();
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
2017-06-08 00:16:43 +02:00
|
|
|
|
|
|
|
if (!tok2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// matching template usage => replace tokens..
|
|
|
|
// Foo < int > => Foo<int>
|
2018-10-14 16:57:07 +02:00
|
|
|
if (tok2->str() == ">" && typeCountInInstantiation == mTypesUsedInTemplateInstantiation.size()) {
|
2018-09-23 20:24:51 +02:00
|
|
|
const Token * const nameTok1 = nameTok;
|
2017-06-08 00:16:43 +02:00
|
|
|
nameTok->str(newName);
|
2018-12-21 13:51:45 +01:00
|
|
|
|
2017-12-26 10:55:18 +01:00
|
|
|
for (Token *tok = nameTok1->next(); tok != tok2; tok = tok->next()) {
|
2018-12-21 13:51:45 +01:00
|
|
|
if (tok->isName() && !tok->templateSimplifierPointers().empty()) {
|
2017-12-26 10:55:18 +01:00
|
|
|
std::list<TokenAndName>::iterator ti;
|
2018-09-01 11:26:10 +02:00
|
|
|
for (ti = mTemplateInstantiations.begin(); ti != mTemplateInstantiations.end();) {
|
2018-12-21 13:54:59 +01:00
|
|
|
if (ti->token == tok) {
|
2018-09-01 11:26:10 +02:00
|
|
|
mTemplateInstantiations.erase(ti++);
|
2018-12-21 13:51:45 +01:00
|
|
|
break;
|
|
|
|
} else {
|
2017-12-26 10:55:18 +01:00
|
|
|
++ti;
|
2018-12-21 13:51:45 +01:00
|
|
|
}
|
2017-12-26 10:55:18 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-08 00:16:43 +02:00
|
|
|
}
|
2018-04-11 09:44:35 +02:00
|
|
|
removeTokens.emplace_back(nameTok, tok2->next());
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
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-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
|
|
|
|
for (auto & spec : mTemplateDeclarations) {
|
2019-01-27 07:46:27 +01:00
|
|
|
if (spec.isSpecialization()) {
|
2018-12-29 11:19:53 +01:00
|
|
|
bool found = false;
|
|
|
|
for (auto & decl : mTemplateDeclarations) {
|
2019-01-27 07:46:27 +01:00
|
|
|
if (decl.isSpecialization())
|
2018-12-29 11:19:53 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// make sure the scopes and names match
|
2019-01-24 07:21:22 +01:00
|
|
|
if (spec.fullName == decl.fullName) {
|
2018-12-29 11:19:53 +01:00
|
|
|
// @todo make sure function parameters also match
|
2019-01-27 07:46:27 +01:00
|
|
|
mTemplateSpecializationMap[spec.token] = decl.token;
|
2019-01-07 06:55:22 +01:00
|
|
|
found = true;
|
2018-12-29 11:19:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
for (auto & decl : mTemplateForwardDeclarations) {
|
|
|
|
// make sure the scopes and names match
|
2019-01-24 07:21:22 +01:00
|
|
|
if (spec.fullName == decl.fullName) {
|
2018-12-29 11:19:53 +01:00
|
|
|
// @todo make sure function parameters also match
|
2019-01-27 07:46:27 +01:00
|
|
|
mTemplateSpecializationMap[spec.token] = decl.token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TemplateSimplifier::getPartialSpecializations()
|
|
|
|
{
|
|
|
|
// try to locate a matching declaration for each user defined partial specialization
|
|
|
|
for (auto & spec : mTemplateDeclarations) {
|
|
|
|
if (spec.isPartialSpecialization()) {
|
|
|
|
bool found = false;
|
|
|
|
for (auto & decl : mTemplateDeclarations) {
|
|
|
|
if (decl.isPartialSpecialization())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// make sure the scopes and names match
|
|
|
|
if (spec.fullName == decl.fullName) {
|
|
|
|
// @todo make sure function parameters also match
|
|
|
|
mTemplatePartialSpecializationMap[spec.token] = decl.token;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
for (auto & decl : mTemplateForwardDeclarations) {
|
|
|
|
// make sure the scopes and names match
|
|
|
|
if (spec.fullName == decl.fullName) {
|
|
|
|
// @todo make sure function parameters also match
|
|
|
|
mTemplatePartialSpecializationMap[spec.token] = decl.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;
|
|
|
|
|
2018-12-17 05:58:48 +01:00
|
|
|
getTemplateParametersInDeclaration(forwardDecl.token->tokAt(2), params1);
|
2018-10-24 14:38:59 +02:00
|
|
|
|
|
|
|
for (auto & decl : mTemplateDeclarations) {
|
2019-02-09 08:34:59 +01:00
|
|
|
// skip partializations
|
|
|
|
if (decl.isPartialSpecialization())
|
|
|
|
continue;
|
|
|
|
|
2018-10-24 14:38:59 +02:00
|
|
|
std::vector<const Token *> params2;
|
|
|
|
|
2018-12-17 05:58:48 +01: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-01-24 07:21:22 +01:00
|
|
|
if (forwardDecl.fullName == decl.fullName) {
|
2018-11-23 11:36:09 +01:00
|
|
|
// save forward declaration for lookup later
|
|
|
|
if ((decl.nameToken->strAt(1) == "(" && forwardDecl.nameToken->strAt(1) == "(") ||
|
|
|
|
(decl.nameToken->strAt(1) == "{" && forwardDecl.nameToken->strAt(1) == ";")) {
|
|
|
|
mTemplateForwardDeclarationsMap[decl.token] = forwardDecl.token;
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
break;
|
2018-10-24 14:38:59 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-30 21:16:11 +01:00
|
|
|
|
|
|
|
// update parameter end pointer
|
|
|
|
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: ";
|
|
|
|
if (tokenAndName.token)
|
|
|
|
std::cout << "\"" << tokenAndName.token->str() << "\" " << mTokenList.fileLine(tokenAndName.token);
|
|
|
|
else
|
|
|
|
std::cout << "nullptr";
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << indent << "scope: \"" << tokenAndName.scope << "\"" << std::endl;
|
|
|
|
std::cout << indent << "name: \"" << tokenAndName.name << "\"" << std::endl;
|
|
|
|
std::cout << indent << "fullName: \"" << tokenAndName.fullName << "\"" << std::endl;
|
|
|
|
std::cout << indent << "nameToken: ";
|
|
|
|
if (tokenAndName.nameToken)
|
|
|
|
std::cout << "\"" << tokenAndName.nameToken->str() << "\" " << mTokenList.fileLine(tokenAndName.nameToken);
|
|
|
|
else
|
|
|
|
std::cout << "nullptr";
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << indent << "paramEnd: ";
|
|
|
|
if (tokenAndName.paramEnd)
|
|
|
|
std::cout << "\"" << tokenAndName.paramEnd->str() << "\" " << mTokenList.fileLine(tokenAndName.paramEnd);
|
|
|
|
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-01-14 08:29:32 +01:00
|
|
|
std::cout << std::endl;
|
|
|
|
if (tokenAndName.token && !tokenAndName.paramEnd && tokenAndName.token->strAt(1) == "<") {
|
|
|
|
const Token *end = tokenAndName.token->next()->findClosingBracket();
|
|
|
|
if (end) {
|
|
|
|
const Token *start = tokenAndName.token->next();
|
|
|
|
std::cout << indent << "type: ";
|
|
|
|
while (start && start != end) {
|
|
|
|
std::cout << start->str();
|
|
|
|
start = start->next();
|
|
|
|
}
|
|
|
|
std::cout << end->str() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (mapItem.first == decl.token) {
|
|
|
|
unsigned int forwardIndex = 0;
|
|
|
|
for (const auto & forwardDecl : mTemplateForwardDeclarations) {
|
|
|
|
if (mapItem.second == forwardDecl.token) {
|
|
|
|
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) {
|
|
|
|
if (decl1.isSpecialization() && mapItem.first == decl1.token) {
|
|
|
|
bool found = 0;
|
|
|
|
unsigned int decl2Index = 0;
|
|
|
|
for (const auto & decl2 : mTemplateDeclarations) {
|
|
|
|
if (mapItem.second == decl2.token) {
|
|
|
|
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) {
|
|
|
|
if (mapItem.second == decl2.token) {
|
|
|
|
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) {
|
|
|
|
if (mapItem.first == decl1.token) {
|
|
|
|
bool found = 0;
|
|
|
|
unsigned int decl2Index = 0;
|
|
|
|
for (const auto & decl2 : mTemplateDeclarations) {
|
|
|
|
if (mapItem.second == decl2.token) {
|
|
|
|
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) {
|
|
|
|
if (mapItem.second == decl2.token) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 08:52:23 +01:00
|
|
|
static Token *findSemicolon(Token *tok)
|
|
|
|
{
|
|
|
|
unsigned int level = 0;
|
|
|
|
|
|
|
|
for (; tok && (level > 0 || tok->str() != ";"); tok = tok->next()) {
|
|
|
|
if (tok->str() == "{")
|
|
|
|
++level;
|
|
|
|
else if (level > 0 && tok->str() == "}")
|
|
|
|
--level;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
static bool usingMatch(
|
|
|
|
const Token *nameToken,
|
|
|
|
const std::string &scope,
|
|
|
|
Token **tok,
|
|
|
|
const std::string &scope1,
|
|
|
|
const std::list<ScopeInfo2> &scopeList1)
|
|
|
|
{
|
|
|
|
Token *tok1 = *tok;
|
|
|
|
|
|
|
|
if (tok1 && tok1->str() != nameToken->str())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// skip this using
|
|
|
|
if (tok1 == nameToken) {
|
|
|
|
*tok = findSemicolon(tok1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip other using with this name
|
|
|
|
if (tok1->strAt(-1) == "using") {
|
|
|
|
// fixme: this is wrong
|
|
|
|
// skip to end of scope
|
|
|
|
if (scopeList1.back().bodyEnd)
|
|
|
|
*tok = scopeList1.back().bodyEnd->previous();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(tok1->tokAt(-1), "struct|union|enum")) {
|
|
|
|
// fixme
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get qualification
|
|
|
|
std::string qualification;
|
|
|
|
const Token* tok2 = tok1;
|
|
|
|
std::string::size_type index = scope.size();
|
|
|
|
std::string::size_type new_index = std::string::npos;
|
|
|
|
bool match = true;
|
|
|
|
while (tok2->strAt(-1) == "::") {
|
|
|
|
std::string last;
|
|
|
|
if (match && !scope1.empty()) {
|
|
|
|
new_index = scope1.rfind(' ', index - 1);
|
|
|
|
if (new_index != std::string::npos)
|
|
|
|
last = scope1.substr(new_index, index - new_index);
|
|
|
|
else if (!qualification.empty())
|
|
|
|
last.clear();
|
|
|
|
else
|
|
|
|
last = scope1;
|
|
|
|
} else
|
|
|
|
match = false;
|
|
|
|
if (match && tok2->strAt(-2) == last)
|
|
|
|
index = new_index;
|
|
|
|
else {
|
|
|
|
if (!qualification.empty())
|
|
|
|
qualification = " :: " + qualification;
|
|
|
|
qualification = tok2->strAt(-2) + qualification;
|
|
|
|
}
|
|
|
|
tok2 = tok2->tokAt(-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: check using namespace
|
|
|
|
std::string fullScope1 = scope1;
|
|
|
|
if (!scope1.empty() && !qualification.empty())
|
|
|
|
fullScope1 += " :: ";
|
|
|
|
fullScope1 += qualification;
|
|
|
|
|
|
|
|
if (scope == fullScope1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
std::string newScope1 = scope1;
|
|
|
|
|
|
|
|
// scopes didn't match so try higher scopes
|
|
|
|
while (!newScope1.empty()) {
|
|
|
|
std::string::size_type separator = newScope1.rfind(" :: ", index - 1);
|
|
|
|
if (separator != std::string::npos)
|
|
|
|
newScope1 = newScope1.substr(0, separator);
|
|
|
|
else
|
|
|
|
newScope1.clear();
|
|
|
|
|
|
|
|
std::string newFullScope1 = newScope1;
|
|
|
|
if (!newScope1.empty() && !qualification.empty())
|
|
|
|
newFullScope1 += " :: ";
|
|
|
|
newFullScope1 += qualification;
|
|
|
|
|
|
|
|
if (scope == newFullScope1)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-05 08:52:23 +01:00
|
|
|
bool TemplateSimplifier::simplifyUsing()
|
|
|
|
{
|
|
|
|
bool substitute = false;
|
|
|
|
std::list<ScopeInfo2> scopeList;
|
|
|
|
bool inTemplateDefinition = false;
|
|
|
|
const Token *endOfTemplateDefinition = nullptr;
|
|
|
|
bool isVariable = false;
|
|
|
|
struct Using {
|
|
|
|
Using(Token *start, Token *end) : startTok(start), endTok(end) { }
|
|
|
|
Token *startTok;
|
|
|
|
Token *endTok;
|
|
|
|
};
|
|
|
|
std::list<Using> usingList;
|
|
|
|
|
|
|
|
scopeList.emplace_back("", nullptr);
|
|
|
|
|
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
|
|
|
if (mErrorLogger && !mTokenList.getFiles().empty())
|
|
|
|
mErrorLogger->reportProgress(mTokenList.getFiles()[0], "Tokenize (using)", tok->progressValue());
|
|
|
|
|
|
|
|
if (mSettings->terminated())
|
|
|
|
return substitute;
|
|
|
|
|
|
|
|
if (Token::Match(tok, "{|}|namespace|class|struct|union") ||
|
|
|
|
Token::Match(tok, "using namespace %name% ;|::")) {
|
|
|
|
setScopeInfo(tok, &scopeList);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inTemplateDefinition) {
|
|
|
|
if (!endOfTemplateDefinition) {
|
|
|
|
if (isVariable)
|
|
|
|
endOfTemplateDefinition = findSemicolon(tok);
|
|
|
|
else if (tok->str() == "{")
|
|
|
|
endOfTemplateDefinition = tok->link();
|
|
|
|
}
|
|
|
|
if (tok == endOfTemplateDefinition) {
|
|
|
|
inTemplateDefinition = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->str()=="template") {
|
|
|
|
if (Token::Match(tok->next(), "< !!>"))
|
|
|
|
inTemplateDefinition = true;
|
|
|
|
else
|
|
|
|
inTemplateDefinition = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inTemplateDefinition) {
|
|
|
|
// look for non-template type aliases
|
|
|
|
if (tok->strAt(-1) != ">" &&
|
|
|
|
(Token::Match(tok, "using %name% = ::| %name%") ||
|
|
|
|
(Token::Match(tok, "using %name% [ [") &&
|
|
|
|
Token::Match(tok->linkAt(2), "] ] = ::| %name%")))) {
|
|
|
|
std::list<ScopeInfo2> scopeList1;
|
|
|
|
scopeList1.emplace_back("", nullptr);
|
|
|
|
std::string name = tok->strAt(1);
|
|
|
|
const Token *nameToken = tok->next();
|
|
|
|
std::string scope = getScopeName(scopeList);
|
|
|
|
Token *usingStart = tok;
|
|
|
|
Token *start;
|
|
|
|
if (tok->strAt(2) == "=")
|
|
|
|
start = tok->tokAt(3);
|
|
|
|
else
|
|
|
|
start = tok->linkAt(2)->tokAt(3);
|
|
|
|
Token *usingEnd = findSemicolon(start);
|
|
|
|
if (!usingEnd)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Move struct defined in using out of using.
|
|
|
|
// using T = struct t { }; => struct t { }; using T = struct t;
|
|
|
|
// fixme: this doesn't handle attributes
|
|
|
|
if (Token::Match(start, "struct|union|enum %name%| {")) {
|
|
|
|
if (start->strAt(1) != "{") {
|
|
|
|
Token *structEnd = start->linkAt(2);
|
|
|
|
structEnd->insertToken(";", "");
|
|
|
|
mTokenList.copyTokens(structEnd->next(), tok, start->next());
|
|
|
|
usingStart = structEnd->tokAt(2);
|
|
|
|
nameToken = usingStart->next();
|
|
|
|
if (usingStart->strAt(2) == "=")
|
|
|
|
start = usingStart->tokAt(3);
|
|
|
|
else
|
|
|
|
start = usingStart->linkAt(2)->tokAt(3);
|
|
|
|
usingEnd = findSemicolon(start);
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok = usingStart;
|
|
|
|
} else {
|
|
|
|
Token *structEnd = start->linkAt(1);
|
|
|
|
structEnd->insertToken(";", "");
|
|
|
|
std::string newName;
|
|
|
|
if (structEnd->strAt(2) == ";")
|
|
|
|
newName = name;
|
|
|
|
else
|
|
|
|
newName = "Unnamed" + MathLib::toString(mTokenizer->mUnnamedCount++);
|
|
|
|
mTokenList.copyTokens(structEnd->next(), tok, start);
|
|
|
|
structEnd->tokAt(5)->insertToken(newName, "");
|
|
|
|
start->insertToken(newName, "");
|
|
|
|
|
|
|
|
usingStart = structEnd->tokAt(2);
|
|
|
|
nameToken = usingStart->next();
|
|
|
|
if (usingStart->strAt(2) == "=")
|
|
|
|
start = usingStart->tokAt(3);
|
|
|
|
else
|
|
|
|
start = usingStart->linkAt(2)->tokAt(3);
|
|
|
|
usingEnd = findSemicolon(start);
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok = usingStart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unfortunately we have to start searching from the beginning
|
|
|
|
// of the token stream because templates are instantiated at
|
|
|
|
// the end of the token stream and it may be used before then.
|
|
|
|
std::string scope1;
|
|
|
|
bool skip = false; // don't erase type aliases we can't parse
|
|
|
|
for (Token* tok1 = mTokenList.front(); tok1; tok1 = tok1->next()) {
|
|
|
|
if ((Token::Match(tok1, "{|}|namespace|class|struct|union") && tok1->strAt(-1) != "using") ||
|
|
|
|
Token::Match(tok1, "using namespace %name% ;|::")) {
|
|
|
|
setScopeInfo(tok1, &scopeList1, true);
|
|
|
|
scope1 = getScopeName(scopeList1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
if (!usingMatch(nameToken, scope, &tok1, scope1, scopeList1))
|
|
|
|
continue;
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
// remove the qualification
|
|
|
|
while (tok1->strAt(-1) == "::" && tok1->strAt(-2) == scope) {
|
|
|
|
tok1->deletePrevious();
|
|
|
|
tok1->deletePrevious();
|
|
|
|
}
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
Token * arrayStart = nullptr;
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
// parse the type
|
|
|
|
Token *type = start;
|
|
|
|
if (type->str() == "::") {
|
|
|
|
type = type->next();
|
|
|
|
while (Token::Match(type, "%type% ::"))
|
|
|
|
type = type->tokAt(2);
|
|
|
|
if (Token::Match(type, "%type%"))
|
|
|
|
type = type->next();
|
|
|
|
} else if (Token::Match(type, "%type% ::")) {
|
|
|
|
do {
|
|
|
|
type = type->tokAt(2);
|
|
|
|
} while (Token::Match(type, "%type% ::"));
|
|
|
|
if (Token::Match(type, "%type%"))
|
|
|
|
type = type->next();
|
|
|
|
} else if (Token::Match(type, "%type%")) {
|
|
|
|
while (Token::Match(type, "const|struct|union|enum %type%") ||
|
|
|
|
(type->next() && type->next()->isStandardType()))
|
|
|
|
type = type->next();
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
type = type->next();
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
while (Token::Match(type, "%type%") &&
|
|
|
|
(type->isStandardType() || Token::Match(type, "unsigned|signed"))) {
|
|
|
|
type = type->next();
|
|
|
|
}
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
bool atEnd = false;
|
|
|
|
while (!atEnd) {
|
|
|
|
if (type && type->str() == "::") {
|
|
|
|
type = type->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(type, "%type%") &&
|
|
|
|
type->next() && !Token::Match(type->next(), "[|;|,|(")) {
|
|
|
|
type = type->next();
|
|
|
|
} else if (Token::simpleMatch(type, "const (")) {
|
|
|
|
type = type->next();
|
|
|
|
atEnd = true;
|
2019-02-05 08:52:23 +01:00
|
|
|
} else
|
2019-02-09 08:34:59 +01:00
|
|
|
atEnd = true;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
syntaxError(type);
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
// check for invalid input
|
|
|
|
if (!type)
|
|
|
|
syntaxError(tok1);
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
// check for template
|
|
|
|
if (type->str() == "<") {
|
|
|
|
type = type->findClosingBracket();
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
while (type && Token::Match(type->next(), ":: %type%"))
|
|
|
|
type = type->tokAt(2);
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
if (!type) {
|
|
|
|
syntaxError(tok1);
|
|
|
|
}
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
while (Token::Match(type->next(), "const|volatile"))
|
|
|
|
type = type->next();
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
type = type->next();
|
|
|
|
}
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
// check for pointers and references
|
|
|
|
std::list<std::string> pointers;
|
|
|
|
while (Token::Match(type, "*|&|&&|const")) {
|
|
|
|
pointers.push_back(type->str());
|
|
|
|
type = type->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for array
|
|
|
|
if (type && type->str() == "[") {
|
|
|
|
do {
|
|
|
|
if (!arrayStart)
|
|
|
|
arrayStart = type;
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
bool atEnd = false;
|
|
|
|
while (!atEnd) {
|
|
|
|
while (type->next() && !Token::Match(type->next(), ";|,")) {
|
|
|
|
type = type->next();
|
|
|
|
}
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
if (!type->next())
|
|
|
|
syntaxError(type); // invalid input
|
|
|
|
else if (type->next()->str() == ";")
|
|
|
|
atEnd = true;
|
|
|
|
else if (type->str() == "]")
|
|
|
|
atEnd = true;
|
|
|
|
else
|
2019-02-05 08:52:23 +01:00
|
|
|
type = type->next();
|
|
|
|
}
|
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
type = type->next();
|
|
|
|
} while (type && type->str() == "[");
|
|
|
|
}
|
|
|
|
|
|
|
|
Token* after = tok1->next();
|
|
|
|
// check if type was parsed
|
|
|
|
if (type && type == usingEnd) {
|
|
|
|
// check for array syntax and add type around variable
|
|
|
|
if (arrayStart) {
|
|
|
|
if (Token::Match(tok1->next(), "%name%")) {
|
|
|
|
mTokenList.copyTokens(tok1->next(), arrayStart, usingEnd->previous());
|
|
|
|
mTokenList.copyTokens(tok1, start, arrayStart->previous());
|
|
|
|
tok1->deleteThis();
|
|
|
|
substitute = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// just replace simple type aliases
|
|
|
|
mTokenList.copyTokens(tok1, start, usingEnd->previous());
|
|
|
|
tok1->deleteThis();
|
|
|
|
substitute = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = true;
|
|
|
|
if (mSettings->debugwarnings && mErrorLogger) {
|
|
|
|
std::string str;
|
|
|
|
for (Token *tok3 = usingStart; tok3 && tok3 != usingEnd; tok3 = tok3->next()) {
|
|
|
|
if (!str.empty())
|
|
|
|
str += ' ';
|
|
|
|
str += tok3->str();
|
2019-02-05 08:52:23 +01:00
|
|
|
}
|
2019-02-09 08:34:59 +01:00
|
|
|
str += " ;";
|
|
|
|
std::list<const Token *> callstack(1, usingStart);
|
|
|
|
mErrorLogger->reportErr(ErrorLogger::ErrorMessage(callstack, &mTokenList, Severity::debug, "debug",
|
|
|
|
|
|
|
|
"Failed to parse \'" + str + "\'. The checking continues anyway.", false));
|
2019-02-05 08:52:23 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-09 08:34:59 +01:00
|
|
|
tok1 = after;
|
2019-02-05 08:52:23 +01:00
|
|
|
}
|
2019-02-09 08:34:59 +01:00
|
|
|
|
2019-02-05 08:52:23 +01:00
|
|
|
if (!skip)
|
|
|
|
usingList.emplace_back(usingStart, usingEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete all used type alias definitions
|
|
|
|
for (std::list<Using>::reverse_iterator it = usingList.rbegin(); it != usingList.rend(); ++it) {
|
|
|
|
Token *usingStart = it->startTok;
|
|
|
|
Token *usingEnd = it->endTok;
|
|
|
|
if (usingStart->previous()) {
|
|
|
|
if (usingEnd->next())
|
|
|
|
Token::eraseTokens(usingStart->previous(), usingEnd->next());
|
|
|
|
else {
|
|
|
|
Token::eraseTokens(usingStart, usingEnd);
|
|
|
|
usingEnd->deleteThis();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (usingEnd->next()) {
|
|
|
|
Token::eraseTokens(usingStart, usingEnd->next());
|
|
|
|
usingStart->deleteThis();
|
|
|
|
} else {
|
2019-02-12 09:39:01 +01:00
|
|
|
// this is the only code being checked so leave ';'
|
2019-02-05 08:52:23 +01:00
|
|
|
Token::eraseTokens(usingStart, usingEnd);
|
|
|
|
usingStart->deleteThis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return substitute;
|
|
|
|
}
|
|
|
|
|
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-02-09 08:34:59 +01:00
|
|
|
// Remove "typename" unless used in template arguments..
|
|
|
|
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "typename %name%"))
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, "template <")) {
|
|
|
|
while (tok && tok->str() != ">")
|
|
|
|
tok = tok->next();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
// TODO: 2 is not the ideal number of loops.
|
|
|
|
// We should loop until the number of declarations is 0 but we can't
|
|
|
|
// do that until we instantiate unintstantiated templates with their symbolic types.
|
|
|
|
// That will allow the uninstantiated template code to be removed from the symbol database.
|
|
|
|
// Unfortunately the template simplifier doesn't handle namespaces properly so
|
|
|
|
// the uninstantiated template code in the symbol database can't be removed until #8768
|
|
|
|
// is fixed.
|
2019-02-05 08:52:23 +01:00
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
2019-02-05 08:52:23 +01:00
|
|
|
// it may take more than one pass to simplify type aliases
|
|
|
|
while (simplifyUsing())
|
|
|
|
;
|
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
if (i) {
|
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();
|
2018-10-14 16:57:07 +02:00
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2018-11-10 16:41:14 +01:00
|
|
|
bool hasTemplates = getTemplateDeclarations();
|
|
|
|
|
2019-02-09 08:34:59 +01:00
|
|
|
if (i == 0)
|
2018-11-10 16:41:14 +01:00
|
|
|
codeWithTemplates = hasTemplates;
|
|
|
|
|
|
|
|
// Make sure there is something to simplify.
|
|
|
|
if (mTemplateDeclarations.empty())
|
|
|
|
return;
|
2012-01-09 20:33:11 +01:00
|
|
|
|
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
|
|
|
|
2019-01-14 08:29:32 +01:00
|
|
|
if (mSettings->debugtemplate)
|
|
|
|
printOut("### Template Simplifier pass " + std::to_string(i + 1) + " ###");
|
|
|
|
|
2018-11-10 16:41:14 +01:00
|
|
|
std::set<std::string> expandedtemplates;
|
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
for (std::list<TokenAndName>::reverse_iterator iter1 = mTemplateDeclarations.rbegin(); iter1 != mTemplateDeclarations.rend(); ++iter1) {
|
2017-12-30 22:14:48 +01:00
|
|
|
// get specializations..
|
|
|
|
std::list<const Token *> specializations;
|
2018-09-01 11:26:10 +02:00
|
|
|
for (std::list<TokenAndName>::const_iterator iter2 = mTemplateDeclarations.begin(); iter2 != mTemplateDeclarations.end(); ++iter2) {
|
2019-01-24 07:21:22 +01:00
|
|
|
if (iter1->fullName == iter2->fullName)
|
2018-12-30 21:16:11 +01:00
|
|
|
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(
|
|
|
|
*iter1,
|
|
|
|
specializations,
|
|
|
|
maxtime,
|
|
|
|
expandedtemplates);
|
2012-07-29 16:01:05 +02:00
|
|
|
if (instantiated)
|
2018-09-01 11:26:10 +02:00
|
|
|
mInstantiatedTemplates.push_back(*iter1);
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
for (std::list<TokenAndName>::const_iterator it = mInstantiatedTemplates.begin(); it != mInstantiatedTemplates.end(); ++it) {
|
2018-05-30 14:33:55 +02:00
|
|
|
std::list<TokenAndName>::iterator decl;
|
2018-09-01 11:26:10 +02:00
|
|
|
for (decl = mTemplateDeclarations.begin(); decl != mTemplateDeclarations.end(); ++decl) {
|
2018-05-30 14:33:55 +02:00
|
|
|
if (decl->token == it->token)
|
2017-12-26 10:55:18 +01:00
|
|
|
break;
|
|
|
|
}
|
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 < >"
|
|
|
|
Token * tok = it->token;
|
|
|
|
tok->deleteNext(2);
|
|
|
|
tok->deleteThis();
|
2018-12-20 20:55:27 +01:00
|
|
|
} else {
|
|
|
|
// remove forward declaration if found
|
|
|
|
auto it1 = mTemplateForwardDeclarationsMap.find(it->token);
|
|
|
|
if (it1 != mTemplateForwardDeclarationsMap.end())
|
|
|
|
removeTemplate(it1->second);
|
2018-10-14 16:57:07 +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(),
|
|
|
|
mTemplateDeclarations.end(),
|
|
|
|
FindToken(mMemberFunctionsToDelete.begin()->token));
|
|
|
|
// multiple functions can share the same declaration so make sure it hasn't already been deleted
|
|
|
|
if (it != mTemplateDeclarations.end()) {
|
|
|
|
removeTemplate(it->token);
|
|
|
|
mTemplateDeclarations.erase(it);
|
|
|
|
}
|
2018-09-14 14:16:34 +02:00
|
|
|
mMemberFunctionsToDelete.erase(mMemberFunctionsToDelete.begin());
|
|
|
|
}
|
2018-12-17 05:58:48 +01:00
|
|
|
|
|
|
|
// remove explicit instantiations
|
|
|
|
for (size_t j = 0; j < mExplicitInstantiationsToDelete.size(); ++j) {
|
2018-12-21 13:51:45 +01:00
|
|
|
Token * start = mExplicitInstantiationsToDelete[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
|
|
|
}
|
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);
|
|
|
|
}
|