2012-01-01 21:55:05 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2015-01-03 12:14:58 +01:00
|
|
|
* Copyright (C) 2007-2015 Daniel Marjamäki and 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"
|
2012-01-09 20:33:11 +01:00
|
|
|
#include "mathlib.h"
|
2012-01-01 21:55:05 +01:00
|
|
|
#include "token.h"
|
2014-04-26 18:31:56 +02:00
|
|
|
#include "tokenize.h"
|
2012-05-06 10:38:55 +02:00
|
|
|
#include "errorlogger.h"
|
2012-01-09 20:33:11 +01:00
|
|
|
#include "settings.h"
|
2012-07-24 21:21:05 +02:00
|
|
|
#include <algorithm>
|
2012-01-02 21:53:13 +01:00
|
|
|
#include <sstream>
|
2012-01-02 22:05:27 +01:00
|
|
|
#include <list>
|
2012-01-02 21:37:32 +01:00
|
|
|
#include <set>
|
2012-01-03 22:35:06 +01:00
|
|
|
#include <stack>
|
|
|
|
#include <vector>
|
2012-01-02 21:37:32 +01:00
|
|
|
#include <string>
|
2012-01-03 22:35:06 +01:00
|
|
|
#include <cassert>
|
2014-07-02 16:16:19 +02:00
|
|
|
#include <iostream>
|
2012-01-01 21:55:05 +01:00
|
|
|
|
2013-04-07 16:48:42 +02:00
|
|
|
#ifdef GDB_HELPERS
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
static void printlist(const std::list<Token *> &list)
|
|
|
|
{
|
2013-04-07 19:33:46 +02:00
|
|
|
for (std::list<Token *>::const_iterator it = list.begin(); it != list.end(); ++it) {
|
2013-04-07 16:48:42 +02:00
|
|
|
const Token *token = *it;
|
|
|
|
std::cout << " ";
|
|
|
|
while (token && !Token::Match(token, "[{};]")) {
|
|
|
|
std::cout << " " << token->str();
|
|
|
|
token = token->next();
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printvector(const std::vector<const Token *> &v)
|
|
|
|
{
|
2014-07-07 21:25:30 +02:00
|
|
|
for (std::size_t i = 0; i < v.size(); i++) {
|
2013-04-07 16:48:42 +02:00
|
|
|
const Token *token = v[i];
|
|
|
|
std::cout << " " << i << ":";
|
|
|
|
while (token && !Token::Match(token, "[{};]")) {
|
|
|
|
std::cout << " " << token->str();
|
|
|
|
token = token->next();
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-01-01 21:55:05 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void TemplateSimplifier::cleanupAfterSimplify(Token *tokens)
|
|
|
|
{
|
|
|
|
bool goback = false;
|
|
|
|
for (Token *tok = tokens; tok; tok = tok->next()) {
|
|
|
|
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;
|
|
|
|
while (Token::Match(tok2, "%type% ,") || Token::Match(tok2, "%num% ,")) {
|
|
|
|
type += tok2->str() + ",";
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
|
|
|
if (Token::Match(tok2, "%type% > (") || Token::Match(tok2, "%num% > (")) {
|
|
|
|
type += tok2->str();
|
|
|
|
tok->str(tok->str() + "<" + type + ">");
|
|
|
|
Token::eraseTokens(tok, tok2->tokAt(2));
|
|
|
|
if (tok == tokens)
|
|
|
|
goback = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 21:37:32 +01:00
|
|
|
|
|
|
|
|
2015-05-28 21:38:20 +02:00
|
|
|
bool TemplateSimplifier::hasComplicatedSyntaxErrorsInTemplates(const Token *tokens, const Token *& errorToken)
|
2012-01-02 21:37:32 +01:00
|
|
|
{
|
2015-05-28 21:38:20 +02:00
|
|
|
errorToken=nullptr;
|
2012-01-02 21:37:32 +01:00
|
|
|
// check for more complicated syntax errors when using templates..
|
|
|
|
for (const Token *tok = tokens; 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)
|
|
|
|
return true;
|
|
|
|
}
|
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)
|
|
|
|
return true;
|
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::..
|
|
|
|
while (Token::Match(tok, "[;{}]"))
|
|
|
|
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;
|
|
|
|
for (const Token *tok2 = tok; tok2 && !Token::Match(tok2, "[;{}]"); tok2 = tok2->next()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (level > 0) {
|
2015-05-29 18:34:00 +02:00
|
|
|
errorToken=tok;
|
2015-06-23 20:53:57 +02:00
|
|
|
syntaxError(tok);
|
2015-05-29 19:15:41 +02:00
|
|
|
return true;
|
2012-01-02 21:37:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 20:53:57 +02:00
|
|
|
return false;
|
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 '=', '?', ':'
|
|
|
|
if (tok && 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 ..
|
2015-08-14 20:46:13 +02:00
|
|
|
if (!tok->isNumber() && tok->tokType() != Token::eChar && !tok->isName())
|
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
|
2012-08-05 17:11:39 +02: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-06-25 20:48:05 +02:00
|
|
|
if (!tok->link()) {
|
|
|
|
syntaxError(tok);
|
|
|
|
return 0;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// * / &
|
|
|
|
while (Token::Match(tok, "[*&]"))
|
|
|
|
tok = tok->next();
|
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
Token::eraseTokens(tok,tok2);
|
|
|
|
tok->deleteThis();
|
|
|
|
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();
|
|
|
|
Token::eraseTokens(tok, tok2);
|
|
|
|
if (tok2 && tok2->str() == ";" && tok2->next())
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
return true;
|
|
|
|
} else if (tok2->str() == "}") { // garbage code! (#3449)
|
|
|
|
Token::eraseTokens(tok,tok2);
|
|
|
|
tok->deleteThis();
|
|
|
|
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% (") &&
|
|
|
|
Tokenizer::startOfExecutableScope(const_cast<const Token *>(tok2->next()->link())))) {
|
2012-07-29 16:01:05 +02:00
|
|
|
Token::eraseTokens(tok, tok2);
|
|
|
|
tok->deleteThis();
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-31 11:30:26 +02:00
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
if (tok2->str() == ";") {
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::eraseTokens(tok, tok2);
|
|
|
|
tok->deleteThis();
|
|
|
|
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
|
|
|
|
2015-01-31 10:50:39 +01:00
|
|
|
else if (Token::Match(tok2, "> class|struct %name% [,)]")) {
|
2012-07-29 16:01:05 +02:00
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::eraseTokens(tok, tok2);
|
|
|
|
tok->deleteThis();
|
|
|
|
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
|
|
|
|
2012-09-12 20:46:56 +02:00
|
|
|
std::set<std::string> TemplateSimplifier::expandSpecialized(Token *tokens)
|
2012-01-02 21:53:13 +01:00
|
|
|
{
|
|
|
|
std::set<std::string> expandedtemplates;
|
|
|
|
|
|
|
|
// Locate specialized templates..
|
|
|
|
for (Token *tok = tokens; tok; tok = tok->next()) {
|
2012-09-12 20:41:10 +02:00
|
|
|
if (!Token::simpleMatch(tok, "template < >"))
|
2012-01-02 21:53:13 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// what kind of template is this?
|
|
|
|
Token *tok2 = tok->tokAt(3);
|
|
|
|
while (tok2 && (tok2->isName() || tok2->str() == "*"))
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
if (!TemplateSimplifier::templateParameters(tok2))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// unknown template.. bail out
|
|
|
|
if (!tok2->previous()->isName())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tok2 = tok2->previous();
|
|
|
|
std::string s;
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
const Token *tok3 = tok2;
|
2012-10-24 03:27:15 +02:00
|
|
|
for (; tok3 && tok3->str() != ">"; tok3 = tok3->next()) {
|
2012-01-02 21:53:13 +01:00
|
|
|
if (tok3 != tok2)
|
|
|
|
ostr << " ";
|
|
|
|
ostr << tok3->str();
|
|
|
|
}
|
2015-03-15 00:28:33 +01:00
|
|
|
if (!Token::Match(tok3, "> (|{"))
|
2012-01-02 21:53:13 +01:00
|
|
|
continue;
|
|
|
|
s = ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// save search pattern..
|
2015-03-15 00:28:33 +01:00
|
|
|
const std::string pattern(s + " >");
|
2012-01-02 21:53:13 +01:00
|
|
|
|
|
|
|
// remove spaces to create new name
|
2012-07-24 21:21:05 +02:00
|
|
|
s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
|
2012-01-02 21:53:13 +01:00
|
|
|
const std::string name(s + ">");
|
|
|
|
expandedtemplates.insert(name);
|
|
|
|
|
|
|
|
// Rename template..
|
2015-03-15 00:28:33 +01:00
|
|
|
Token::eraseTokens(tok2, Token::findsimplematch(tok2, "<")->findClosingBracket()->next());
|
2012-01-02 21:53:13 +01:00
|
|
|
tok2->str(name);
|
|
|
|
|
|
|
|
// delete the "template < >"
|
|
|
|
tok->deleteNext(2);
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
// Use this special template in the code..
|
2014-02-16 10:32:10 +01:00
|
|
|
while (nullptr != (tok2 = const_cast<Token *>(Token::findmatch(tok2, pattern.c_str())))) {
|
2015-03-15 00:28:33 +01:00
|
|
|
Token::eraseTokens(tok2, Token::findsimplematch(tok2, "<")->findClosingBracket()->next());
|
2012-01-02 21:53:13 +01:00
|
|
|
tok2->str(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return expandedtemplates;
|
|
|
|
}
|
2012-01-02 22:05:27 +01:00
|
|
|
|
2012-09-12 20:46:56 +02:00
|
|
|
std::list<Token *> TemplateSimplifier::getTemplateDeclarations(Token *tokens, bool &codeWithTemplates)
|
2012-01-02 22:05:27 +01:00
|
|
|
{
|
|
|
|
std::list<Token *> templates;
|
|
|
|
for (Token *tok = tokens; tok; tok = tok->next()) {
|
2012-08-13 16:17:33 +02:00
|
|
|
// TODO: handle namespaces. Right now we don't instantiate templates that are defined in namespaces.
|
|
|
|
if (Token::Match(tok, "namespace %type% {"))
|
2012-09-07 12:36:40 +02:00
|
|
|
tok = tok->linkAt(2);
|
2012-08-13 16:17:33 +02:00
|
|
|
|
2012-01-02 22:05:27 +01:00
|
|
|
if (Token::simpleMatch(tok, "template <")) {
|
2014-03-01 14:55:57 +01:00
|
|
|
Token *parmEnd = tok->next()->findClosingBracket();
|
2012-01-02 22:05:27 +01:00
|
|
|
codeWithTemplates = true;
|
|
|
|
|
2014-09-13 16:44:05 +02:00
|
|
|
int indentlevel = 0;
|
2014-03-01 14:55:57 +01:00
|
|
|
for (const Token *tok2 = parmEnd; tok2; tok2 = tok2->next()) {
|
2014-09-13 16:44:05 +02:00
|
|
|
if (tok2->str() == "(")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
--indentlevel;
|
|
|
|
|
|
|
|
if (indentlevel) // In an argument list; move to the next token
|
|
|
|
continue;
|
|
|
|
|
2012-01-02 22:05:27 +01:00
|
|
|
// Just a declaration => ignore this
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
break;
|
|
|
|
// Implementation => add to "templates"
|
|
|
|
if (tok2->str() == "{") {
|
|
|
|
templates.push_back(tok);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return templates;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-12 20:46:56 +02:00
|
|
|
std::list<Token *> TemplateSimplifier::getTemplateInstantiations(Token *tokens)
|
2012-01-02 22:05:27 +01:00
|
|
|
{
|
|
|
|
std::list<Token *> used;
|
|
|
|
|
|
|
|
for (Token *tok = tokens; tok; tok = tok->next()) {
|
|
|
|
// 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;
|
2015-01-31 10:50:39 +01:00
|
|
|
} else if (Token::Match(tok->previous(), "[({};=] %name% <") ||
|
|
|
|
Token::Match(tok->previous(), "%type% %name% <") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "[,:] private|protected|public %name% <")) {
|
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
|
|
|
|
for (; tok2 && tok2 != tok; tok2 = tok2->previous()) {
|
2015-01-31 10:50:39 +01:00
|
|
|
if (Token::Match(tok2, ", %name% <") &&
|
2012-01-02 22:05:27 +01:00
|
|
|
TemplateSimplifier::templateParameters(tok2->tokAt(2))) {
|
|
|
|
used.push_back(tok2->next());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add outer template..
|
|
|
|
if (TemplateSimplifier::templateParameters(tok->next()))
|
|
|
|
used.push_back(tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return used;
|
|
|
|
}
|
2012-01-03 22:35:06 +01:00
|
|
|
|
|
|
|
|
2012-09-12 20:46:56 +02:00
|
|
|
void TemplateSimplifier::useDefaultArgumentValues(const std::list<Token *> &templates,
|
2013-11-21 20:27:24 +01:00
|
|
|
std::list<Token *> * const templateInstantiations)
|
2012-01-03 22:35:06 +01:00
|
|
|
{
|
|
|
|
for (std::list<Token *>::const_iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1) {
|
|
|
|
// 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..
|
|
|
|
for (Token *tok = *iter1; 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() == ">") {
|
|
|
|
if (Token::Match(tok, "> class|struct %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
|
2013-11-21 20:27:24 +01:00
|
|
|
for (std::list<Token *>::const_iterator iter2 = templateInstantiations->begin(); iter2 != templateInstantiations->end(); ++iter2) {
|
2012-01-03 22:35:06 +01:00
|
|
|
Token *tok = *iter2;
|
|
|
|
|
|
|
|
if (!Token::Match(tok, (classname + " < %any%").c_str()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// count the parameters..
|
2014-05-23 23:38:40 +02:00
|
|
|
tok = tok->next();
|
2015-05-03 15:21:58 +02:00
|
|
|
const unsigned int usedpar = TemplateSimplifier::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;
|
2014-09-06 20:39:04 +02:00
|
|
|
while (from && (!links.empty() || (from->str() != "," && (indentlevel || from->str() != ">")))) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::list<Token *>::iterator it = eq.begin(); it != eq.end(); ++it) {
|
2012-01-15 12:14:47 +01:00
|
|
|
Token * const eqtok = *it;
|
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())) {
|
|
|
|
std::list<Token*>::iterator ti = std::find(templateInstantiations->begin(),
|
|
|
|
templateInstantiations->end(),
|
|
|
|
tok2);
|
|
|
|
if (ti != templateInstantiations->end())
|
|
|
|
templateInstantiations->erase(ti);
|
|
|
|
++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;
|
|
|
|
|
2012-01-15 12:14:47 +01:00
|
|
|
Token::eraseTokens(eqtok, tok2);
|
|
|
|
eqtok->deleteThis();
|
2012-01-03 22:35:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-12 20:46:56 +02:00
|
|
|
bool TemplateSimplifier::instantiateMatch(const Token *instance, const std::string &name, std::size_t numberOfArguments, const char patternAfter[])
|
2012-01-03 22:35:06 +01:00
|
|
|
{
|
|
|
|
if (!Token::simpleMatch(instance, (name + " <").c_str()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (numberOfArguments != TemplateSimplifier::templateParameters(instance->next()))
|
|
|
|
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()) {
|
2015-01-31 10:50:39 +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;
|
|
|
|
}
|
|
|
|
|
2012-09-12 20:46:56 +02:00
|
|
|
int TemplateSimplifier::getTemplateNamePosition(const Token *tok)
|
2012-01-03 22:49:50 +01:00
|
|
|
{
|
|
|
|
// get the position of the template name
|
2015-03-07 21:52:12 +01:00
|
|
|
int namepos = 0, starAmpPossiblePosition = 0;
|
2015-03-15 00:28:33 +01:00
|
|
|
if (Token::Match(tok, "> class|struct %type% {|:|<"))
|
2012-01-03 22:49:50 +01:00
|
|
|
namepos = 2;
|
|
|
|
else if (Token::Match(tok, "> %type% *|&| %type% ("))
|
|
|
|
namepos = 2;
|
|
|
|
else if (Token::Match(tok, "> %type% %type% *|&| %type% ("))
|
|
|
|
namepos = 3;
|
2015-03-07 21:52:12 +01:00
|
|
|
else if (Token::Match(tok, "> %type% *|&| %type% :: %type% (")) {
|
|
|
|
namepos = 4;
|
|
|
|
starAmpPossiblePosition = 2;
|
|
|
|
} else if (Token::Match(tok, "> %type% %type% *|&| %type% :: %type% (")) {
|
|
|
|
namepos = 5;
|
|
|
|
starAmpPossiblePosition = 3;
|
|
|
|
} else {
|
2012-01-03 22:49:50 +01:00
|
|
|
// Name not found
|
|
|
|
return -1;
|
|
|
|
}
|
2015-03-07 21:52:12 +01:00
|
|
|
if (Token::Match(tok->tokAt(starAmpPossiblePosition ? starAmpPossiblePosition : namepos), "*|&"))
|
2012-01-03 22:49:50 +01:00
|
|
|
++namepos;
|
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
|
|
|
|
|
|
|
|
2012-09-12 20:46:56 +02:00
|
|
|
void TemplateSimplifier::expandTemplate(
|
2012-05-06 10:38:55 +02:00
|
|
|
TokenList& tokenlist,
|
2012-01-05 21:45:19 +01:00
|
|
|
const Token *tok,
|
|
|
|
const std::string &name,
|
|
|
|
std::vector<const Token *> &typeParametersInDeclaration,
|
|
|
|
const std::string &newName,
|
2013-02-10 07:43:09 +01:00
|
|
|
std::vector<const Token *> &typesUsedInTemplateInstantiation,
|
2012-01-05 21:45:19 +01:00
|
|
|
std::list<Token *> &templateInstantiations)
|
|
|
|
{
|
2014-11-01 22:07:24 +01:00
|
|
|
bool inTemplateDefinition=false;
|
|
|
|
std::vector<const Token *> localTypeParametersInDeclaration;
|
2014-07-15 21:28:52 +02:00
|
|
|
for (const Token *tok3 = tokenlist.front(); tok3; tok3 = tok3 ? tok3->next() : nullptr) {
|
2014-11-01 22:07:24 +01:00
|
|
|
if (tok3->str()=="template") {
|
|
|
|
if (tok3->next() && tok3->next()->str()=="<") {
|
|
|
|
TemplateParametersInDeclaration(tok3->tokAt(2), localTypeParametersInDeclaration);
|
|
|
|
if (localTypeParametersInDeclaration.size() != typeParametersInDeclaration.size())
|
|
|
|
inTemplateDefinition = false; // Partial specialization
|
|
|
|
else
|
|
|
|
inTemplateDefinition = true;
|
|
|
|
} else
|
|
|
|
inTemplateDefinition = false; // Only template instantiation
|
|
|
|
}
|
2014-09-14 11:26:16 +02:00
|
|
|
if (Token::Match(tok3, "{|(|["))
|
2012-01-05 21:45:19 +01:00
|
|
|
tok3 = tok3->link();
|
|
|
|
|
|
|
|
// Start of template..
|
|
|
|
if (tok3 == tok) {
|
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// member function implemented outside class definition
|
2014-11-01 22:07:24 +01:00
|
|
|
else if (inTemplateDefinition &&
|
2015-01-31 10:50:39 +01:00
|
|
|
TemplateSimplifier::instantiateMatch(tok3, name, typeParametersInDeclaration.size(), ":: ~| %name% (")) {
|
2014-03-09 17:54:49 +01:00
|
|
|
tokenlist.addtoken(newName, tok3->linenr(), tok3->fileIndex());
|
2014-07-15 21:28:52 +02:00
|
|
|
while (tok3 && tok3->str() != "::")
|
2012-01-05 21:45:19 +01:00
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// not part of template.. go on to next token
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int indentlevel = 0;
|
2012-09-12 20:41:10 +02:00
|
|
|
std::stack<Token *> brackets; // holds "(", "[" and "{" tokens
|
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;
|
2013-02-10 07:43:09 +01:00
|
|
|
for (const Token *typetok = typesUsedInTemplateInstantiation[itype];
|
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;
|
2012-05-06 10:38:55 +02:00
|
|
|
tokenlist.addtoken(typetok, tok3->linenr(), tok3->fileIndex());
|
2012-01-05 21:45:19 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace name..
|
|
|
|
if (Token::Match(tok3, (name + " !!<").c_str())) {
|
2014-03-09 17:54:49 +01:00
|
|
|
tokenlist.addtoken(newName, tok3->linenr(), tok3->fileIndex());
|
2012-01-05 21:45:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy
|
2012-05-06 10:38:55 +02:00
|
|
|
tokenlist.addtoken(tok3, tok3->linenr(), tok3->fileIndex());
|
2012-01-05 21:45:19 +01:00
|
|
|
if (Token::Match(tok3, "%type% <")) {
|
|
|
|
//if (!Token::simpleMatch(tok3, (name + " <").c_str()))
|
|
|
|
//done = false;
|
2012-05-06 10:38:55 +02:00
|
|
|
templateInstantiations.push_back(tokenlist.back());
|
2012-01-05 21:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// link() newly tokens manually
|
2012-09-12 20:41:10 +02:00
|
|
|
else if (tok3->str() == "{") {
|
|
|
|
brackets.push(tokenlist.back());
|
|
|
|
indentlevel++;
|
2012-01-05 21:45:19 +01:00
|
|
|
} else if (tok3->str() == "(") {
|
2012-05-06 10:38:55 +02:00
|
|
|
brackets.push(tokenlist.back());
|
2012-01-05 21:45:19 +01:00
|
|
|
} else if (tok3->str() == "[") {
|
2012-09-12 20:41:10 +02:00
|
|
|
brackets.push(tokenlist.back());
|
|
|
|
} else if (tok3->str() == "}") {
|
|
|
|
assert(brackets.empty() == false && brackets.top()->str() == "{");
|
|
|
|
Token::createMutualLinks(brackets.top(), tokenlist.back());
|
2014-03-12 06:09:45 +01:00
|
|
|
if (tok3->strAt(1) == ";") {
|
|
|
|
const Token * tokSemicolon = tok3->next();
|
|
|
|
tokenlist.addtoken(tokSemicolon, tokSemicolon->linenr(), tokSemicolon->fileIndex());
|
|
|
|
}
|
2012-09-12 20:41:10 +02:00
|
|
|
brackets.pop();
|
|
|
|
if (indentlevel <= 1 && brackets.empty()) {
|
|
|
|
// there is a bug if indentlevel is 0
|
|
|
|
// the "}" token should only be added if indentlevel is 1 but I add it always intentionally
|
|
|
|
// if indentlevel ever becomes 0, cppcheck will write:
|
|
|
|
// ### Error: Invalid number of character {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--indentlevel;
|
2012-01-05 21:45:19 +01:00
|
|
|
} else if (tok3->str() == ")") {
|
2012-09-12 20:41:10 +02:00
|
|
|
assert(brackets.empty() == false && brackets.top()->str() == "(");
|
2012-05-06 10:38:55 +02:00
|
|
|
Token::createMutualLinks(brackets.top(), tokenlist.back());
|
2012-01-05 21:45:19 +01:00
|
|
|
brackets.pop();
|
|
|
|
} else if (tok3->str() == "]") {
|
2012-09-12 20:41:10 +02:00
|
|
|
assert(brackets.empty() == false && brackets.top()->str() == "[");
|
|
|
|
Token::createMutualLinks(brackets.top(), tokenlist.back());
|
|
|
|
brackets.pop();
|
2012-01-05 21:45:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(brackets.empty());
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2012-03-27 21:29:50 +02:00
|
|
|
static bool isLowerThanOr(const Token* lower)
|
|
|
|
{
|
2012-09-09 16:22:39 +02:00
|
|
|
return lower->isAssignmentOp() || Token::Match(lower, "}|;|(|[|]|)|,|?|:|%oror%|&&|return|throw|case");
|
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)
|
|
|
|
{
|
2012-12-01 01:31:35 +01:00
|
|
|
return isLowerThanAnd(lower) || Token::Match(lower, "%comp%|&");
|
2012-03-27 21:29:50 +02:00
|
|
|
}
|
|
|
|
static bool isLowerThanPlusMinus(const Token* lower)
|
|
|
|
{
|
2014-09-14 11:26:16 +02:00
|
|
|
return isLowerThanShift(lower) || Token::Match(lower, "<<|>>");
|
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, "[*/%]");
|
|
|
|
}
|
|
|
|
|
2014-04-14 20:30:38 +02:00
|
|
|
static std::string ShiftInt(const char cop, const Token* left, const Token* right)
|
2014-04-10 19:22:14 +02:00
|
|
|
{
|
|
|
|
if (cop == '&' || cop == '|' || cop == '^')
|
|
|
|
return MathLib::calculate(left->str(), right->str(), cop);
|
2014-04-15 06:32:25 +02:00
|
|
|
|
2014-06-16 10:58:41 +02:00
|
|
|
const MathLib::bigint leftInt = MathLib::toLongNumber(left->str());
|
|
|
|
const MathLib::bigint rightInt = MathLib::toLongNumber(right->str());
|
|
|
|
const bool rightIntIsPositive = rightInt >= 0;
|
|
|
|
|
2014-04-14 20:30:38 +02:00
|
|
|
if (cop == '<') {
|
2014-09-17 08:54:53 +02:00
|
|
|
const bool leftOperationIsNotLeftShift = left->previous()->str() != "<<";
|
|
|
|
const bool operandIsLeftShift = right->previous()->str() == "<<";
|
|
|
|
|
2014-06-16 10:58:41 +02:00
|
|
|
// Ensure that its not a shift operator as used for streams
|
|
|
|
if (leftOperationIsNotLeftShift && operandIsLeftShift && rightIntIsPositive) {
|
2014-09-17 08:54:53 +02:00
|
|
|
const bool leftIntIsPositive = leftInt >= 0;
|
2014-06-16 10:58:41 +02:00
|
|
|
if (!leftIntIsPositive) { // In case the left integer is negative, e.g. -1000 << 16. Do not simplify.
|
|
|
|
return left->str() + " << " + right->str();
|
|
|
|
}
|
2014-04-10 19:22:14 +02:00
|
|
|
return MathLib::toString(leftInt << rightInt);
|
2014-06-16 10:58:41 +02:00
|
|
|
}
|
|
|
|
} else if (rightIntIsPositive) {
|
2014-04-10 19:22:14 +02:00
|
|
|
return MathLib::toString(leftInt >> rightInt);
|
2014-04-14 20:30:38 +02:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2014-04-10 19:22:14 +02:00
|
|
|
|
2014-04-14 20:30:38 +02:00
|
|
|
static std::string ShiftUInt(const char cop, const Token* left, const Token* right)
|
|
|
|
{
|
|
|
|
if (cop == '&' || cop == '|' || cop == '^')
|
|
|
|
return MathLib::calculate(left->str(), right->str(), cop);
|
2014-04-15 06:32:25 +02:00
|
|
|
|
2014-04-14 20:30:38 +02:00
|
|
|
const MathLib::biguint leftInt=MathLib::toULongNumber(left->str());
|
|
|
|
const MathLib::biguint rightInt=MathLib::toULongNumber(right->str());
|
|
|
|
if (cop == '<') {
|
|
|
|
if (left->previous()->str() != "<<") // Ensure that its not a shift operator as used for streams
|
|
|
|
return MathLib::toString(leftInt << rightInt);
|
|
|
|
} else {
|
|
|
|
return MathLib::toString(leftInt >> rightInt);
|
|
|
|
}
|
2014-04-10 19:22:14 +02:00
|
|
|
return "";
|
|
|
|
}
|
2012-08-20 18:10:32 +02:00
|
|
|
|
|
|
|
bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
// (1-2)
|
|
|
|
while (tok->tokAt(4) && tok->next()->isNumber() && tok->tokAt(3)->isNumber()) { // %any% %num% %any% %num% %any%
|
|
|
|
const Token* op = tok->tokAt(2);
|
|
|
|
const Token* after = tok->tokAt(4);
|
2013-07-28 12:32:18 +02:00
|
|
|
if (Token::Match(tok, "* %num% /") && (tok->strAt(3) != "0") && tok->next()->str() == MathLib::multiply(tok->strAt(3), MathLib::divide(tok->next()->str(), tok->strAt(3)))) {
|
2012-08-20 18:10:32 +02:00
|
|
|
// Division where result is a whole number
|
|
|
|
} else if (!((op->str() == "*" && (isLowerThanMulDiv(tok) || tok->str() == "*") && isLowerEqualThanMulDiv(after)) || // associative
|
|
|
|
(Token::Match(op, "[/%]") && isLowerThanMulDiv(tok) && isLowerEqualThanMulDiv(after)) || // NOT associative
|
|
|
|
(Token::Match(op, "[+-]") && isLowerThanMulDiv(tok) && isLowerThanMulDiv(after)) || // Only partially (+) associative, but handled later
|
|
|
|
(Token::Match(op, ">>|<<") && isLowerThanShift(tok) && isLowerThanPlusMinus(after)) || // NOT associative
|
|
|
|
(op->str() == "&" && isLowerThanShift(tok) && isLowerThanShift(after)) || // associative
|
|
|
|
(op->str() == "^" && isLowerThanAnd(tok) && isLowerThanAnd(after)) || // associative
|
|
|
|
(op->str() == "|" && isLowerThanXor(tok) && isLowerThanXor(after)))) // associative
|
|
|
|
break;
|
|
|
|
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// Don't simplify "%num% / 0"
|
|
|
|
if (Token::Match(op, "[/%] 0"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Integer operations
|
|
|
|
if (Token::Match(op, ">>|<<|&|^|%or%")) {
|
|
|
|
const char cop = op->str()[0];
|
|
|
|
std::string result;
|
2014-04-10 19:22:14 +02:00
|
|
|
if (tok->str().find_first_of("uU") != std::string::npos)
|
2014-04-14 20:30:38 +02:00
|
|
|
result = ShiftUInt(cop, tok, tok->tokAt(2));
|
2014-04-10 19:22:14 +02:00
|
|
|
else
|
2014-04-14 20:30:38 +02:00
|
|
|
result = ShiftInt(cop, tok, tok->tokAt(2));
|
2012-08-20 18:10:32 +02:00
|
|
|
if (!result.empty()) {
|
|
|
|
ret = true;
|
|
|
|
tok->str(result);
|
|
|
|
tok->deleteNext(2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok->previous(), "- %num% - %num%"))
|
|
|
|
tok->str(MathLib::add(tok->str(), tok->strAt(2)));
|
|
|
|
else if (Token::Match(tok->previous(), "- %num% + %num%"))
|
|
|
|
tok->str(MathLib::subtract(tok->str(), tok->strAt(2)));
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
tok->str(MathLib::calculate(tok->str(), tok->strAt(2), op->str()[0]));
|
|
|
|
} catch (InternalError &e) {
|
|
|
|
e.token = tok;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tok->deleteNext(2);
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-09 20:33:11 +01:00
|
|
|
// TODO: This is not the correct class for simplifyCalculations(), so it
|
|
|
|
// should be moved away.
|
|
|
|
bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
|
|
|
|
{
|
2014-05-10 09:37:56 +02:00
|
|
|
bool ret = false, goback = false;
|
2012-01-09 20:33:11 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
2014-05-10 09:37:56 +02:00
|
|
|
if (goback) {
|
|
|
|
tok = tok->previous();
|
|
|
|
goback = false;
|
|
|
|
}
|
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% ) ;|)|,|]") ||
|
|
|
|
(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%|)")) {
|
2013-09-21 18:48:48 +02:00
|
|
|
tok->str(MathLib::toString(tok->str()[1] & 0xff));
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->isNumber()) {
|
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;
|
2012-10-24 03:09:42 +02:00
|
|
|
for (; tok2; tok2 = tok2->next()) {
|
2012-06-14 21:47:03 +02:00
|
|
|
if (tok2->str() == "(")
|
|
|
|
++par;
|
|
|
|
else if (tok2->str() == ")") {
|
|
|
|
if (par == 0)
|
|
|
|
break;
|
|
|
|
--par;
|
2013-06-14 18:51:52 +02:00
|
|
|
} else if (par == 0 && (Token::Match(tok2, "[,;?]")))
|
2012-06-14 21:47:03 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-06-14 18:51:52 +02:00
|
|
|
if (Token::Match(tok2, "[);,?]")) {
|
2012-06-14 21:47:03 +02:00
|
|
|
Token::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") {
|
|
|
|
if (Token::Match(tok->previous(), "[+-|] 0")) {
|
|
|
|
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()) {
|
|
|
|
tok = tok->tokAt(-3);
|
|
|
|
tok->deleteNext(2);
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
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;
|
2015-01-31 10:50:39 +01:00
|
|
|
} else if (Token::Match(tok->previous(), "[=[(,] 0 * %name% ,|]|)|;|=|%cop%") ||
|
2013-03-01 11:43:59 +01:00
|
|
|
Token::Match(tok->previous(), "[=[(,] 0 * %num% ,|]|)|;|%op%") ||
|
|
|
|
Token::Match(tok->previous(), "[=[(,] 0 * (") ||
|
2015-01-31 10:50:39 +01:00
|
|
|
Token::Match(tok->previous(), "return|case 0 *|&& %name% ,|:|;|=|%cop%") ||
|
2013-03-01 11:43:59 +01:00
|
|
|
Token::Match(tok->previous(), "return|case 0 *|&& %num% ,|:|;|%op%") ||
|
|
|
|
Token::Match(tok->previous(), "return|case 0 *|&& (")) {
|
2012-03-07 20:31:23 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
if (tok->next()->str() == "(")
|
|
|
|
Token::eraseTokens(tok, tok->next()->link());
|
|
|
|
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() == "(")
|
|
|
|
Token::eraseTokens(tok, tok->next()->link());
|
|
|
|
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() == "(")
|
|
|
|
Token::eraseTokens(tok, tok->next()->link());
|
|
|
|
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() == "(")
|
|
|
|
Token::eraseTokens(tok, tok->next()->link());
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok->previous(), "* 1") || Token::simpleMatch(tok, "1 *")) {
|
2013-02-28 21:50:29 +01:00
|
|
|
if (tok->previous() && tok->previous()->isConstOp())
|
2012-01-09 20:33:11 +01:00
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove parentheses around number..
|
2014-10-10 13:19:25 +02:00
|
|
|
if (Token::Match(tok->tokAt(-2), "%op%|< ( %num% )") && !tok->tokAt(-2)->isName() && tok->strAt(-2) != ">") {
|
2012-01-09 20:33:11 +01:00
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok->previous(), "( 0 ||") ||
|
|
|
|
Token::simpleMatch(tok->previous(), "|| 0 )") ||
|
|
|
|
Token::simpleMatch(tok->previous(), "( 0 |") ||
|
|
|
|
Token::simpleMatch(tok->previous(), "| 0 )") ||
|
|
|
|
Token::simpleMatch(tok->previous(), "( 1 &&") ||
|
|
|
|
Token::simpleMatch(tok->previous(), "&& 1 )")) {
|
2013-02-28 21:50:29 +01:00
|
|
|
if (tok->previous()->isConstOp())
|
2012-01-09 20:33:11 +01:00
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
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";
|
|
|
|
else if (cmp == ">")
|
|
|
|
result = (op1 > op2) ? "1" : "0";
|
|
|
|
|
|
|
|
tok->str(result);
|
|
|
|
tok->deleteNext(2);
|
|
|
|
ret = true;
|
2014-05-10 09:37:56 +02:00
|
|
|
goback = true;
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 21:29:50 +02:00
|
|
|
}
|
|
|
|
// Division where result is a whole number
|
|
|
|
else if (Token::Match(tok->previous(), "* %num% /") &&
|
|
|
|
tok->str() == MathLib::multiply(tok->strAt(2), MathLib::divide(tok->str(), tok->strAt(2)))) {
|
|
|
|
tok->deleteNext(2);
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
2012-03-27 21:29:50 +02:00
|
|
|
else {
|
2012-08-20 18:10:32 +02:00
|
|
|
ret |= simplifyNumericCalculations(tok);
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-01 22:07:24 +01:00
|
|
|
const Token * TemplateSimplifier::TemplateParametersInDeclaration(
|
|
|
|
const Token * tok,
|
|
|
|
std::vector<const Token *> & typeParametersInDeclaration)
|
|
|
|
{
|
|
|
|
typeParametersInDeclaration.clear();
|
|
|
|
for (; tok && tok->str() != ">"; tok = tok->next()) {
|
2015-01-31 10:50:39 +01: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
|
|
|
|
2013-02-10 07:43:09 +01:00
|
|
|
bool TemplateSimplifier::simplifyTemplateInstantiations(
|
2012-05-06 10:38:55 +02:00
|
|
|
TokenList& tokenlist,
|
2014-05-20 21:55:08 +02:00
|
|
|
ErrorLogger* errorlogger,
|
2012-01-09 20:33:11 +01:00
|
|
|
const Settings *_settings,
|
|
|
|
const Token *tok,
|
|
|
|
std::list<Token *> &templateInstantiations,
|
|
|
|
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;
|
2014-11-01 22:07:24 +01:00
|
|
|
tok = TemplateParametersInDeclaration(tok->tokAt(2), typeParametersInDeclaration);
|
2012-01-09 20:33:11 +01:00
|
|
|
|
|
|
|
// bail out if the end of the file was reached
|
|
|
|
if (!tok)
|
2012-07-29 16:01:05 +02:00
|
|
|
return false;
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2015-04-10 14:18:52 +02:00
|
|
|
const bool printDebug = _settings->debugwarnings;
|
|
|
|
|
2012-01-09 20:33:11 +01:00
|
|
|
// get the position of the template name
|
2012-09-12 20:46:56 +02:00
|
|
|
int namepos = TemplateSimplifier::getTemplateNamePosition(tok);
|
2012-01-09 20:33:11 +01:00
|
|
|
if (namepos == -1) {
|
|
|
|
// debug message that we bail out..
|
2015-04-10 14:18:52 +02:00
|
|
|
if (printDebug && errorlogger) {
|
2012-05-06 10:38:55 +02:00
|
|
|
std::list<const Token *> callstack(1, tok);
|
2014-05-20 21:55:08 +02:00
|
|
|
errorlogger->reportErr(ErrorLogger::ErrorMessage(callstack, &tokenlist, Severity::debug, "debug", "simplifyTemplates: bailing out", false));
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
2012-07-29 16:01:05 +02:00
|
|
|
return false;
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// name of template function/class..
|
|
|
|
const std::string name(tok->strAt(namepos));
|
|
|
|
|
|
|
|
const bool isfunc(tok->strAt(namepos + 1) == "(");
|
|
|
|
|
|
|
|
// locate template usage..
|
|
|
|
std::string::size_type amountOftemplateInstantiations = templateInstantiations.size();
|
|
|
|
unsigned int recursiveCount = 0;
|
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
bool instantiated = false;
|
|
|
|
|
2012-01-09 20:33:11 +01:00
|
|
|
for (std::list<Token *>::const_iterator iter2 = templateInstantiations.begin(); iter2 != templateInstantiations.end(); ++iter2) {
|
|
|
|
if (amountOftemplateInstantiations != templateInstantiations.size()) {
|
|
|
|
amountOftemplateInstantiations = templateInstantiations.size();
|
2012-05-06 10:38:55 +02:00
|
|
|
simplifyCalculations(tokenlist.front());
|
2012-01-09 20:33:11 +01:00
|
|
|
++recursiveCount;
|
|
|
|
if (recursiveCount > 100) {
|
|
|
|
// bail out..
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Token * const tok2 = *iter2;
|
|
|
|
if (tok2->str() != name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::Match(tok2->previous(), "[;{}=]") &&
|
2015-01-31 10:50:39 +01:00
|
|
|
!TemplateSimplifier::instantiateMatch(*iter2, name, typeParametersInDeclaration.size(), isfunc ? "(" : "*| %name%"))
|
2012-01-09 20:33:11 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// New type..
|
2013-02-10 07:43:09 +01:00
|
|
|
std::vector<const Token *> typesUsedInTemplateInstantiation;
|
2015-04-01 12:43:03 +02:00
|
|
|
std::string typeForNewName;
|
2012-01-09 20:33:11 +01:00
|
|
|
std::string templateMatchPattern(name + " < ");
|
2012-08-08 06:43:41 +02:00
|
|
|
unsigned int indentlevel = 0;
|
2012-08-09 17:19:15 +02:00
|
|
|
for (const Token *tok3 = tok2->tokAt(2); tok3 && (indentlevel > 0 || tok3->str() != ">"); tok3 = tok3->next()) {
|
2013-01-16 15:37:07 +01:00
|
|
|
// #2648 - unhandled parentheses => bail out
|
2012-01-09 20:33:11 +01:00
|
|
|
// #2721 - unhandled [ => bail out
|
2014-09-14 11:26:16 +02:00
|
|
|
if (Token::Match(tok3, "(|[")) {
|
2015-04-01 12:43:03 +02:00
|
|
|
typeForNewName.clear();
|
2012-01-09 20:33:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!tok3->next()) {
|
2015-04-01 12:43:03 +02:00
|
|
|
typeForNewName.clear();
|
2012-01-09 20:33:11 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-01-31 10:50:39 +01:00
|
|
|
if (Token::Match(tok3->tokAt(-2), "[<,] %name% <") && templateParameters(tok3) > 0)
|
2012-08-08 06:43:41 +02:00
|
|
|
++indentlevel;
|
2012-10-14 19:48:53 +02:00
|
|
|
else if (indentlevel > 0 && Token::Match(tok3, "> [,>]"))
|
2012-08-08 06:43:41 +02:00
|
|
|
--indentlevel;
|
2015-03-22 11:20:47 +01:00
|
|
|
bool constconst = tok3->str() == "const" && tok3->strAt(1) == "const";
|
|
|
|
if (!constconst) {
|
|
|
|
templateMatchPattern += tok3->str();
|
|
|
|
templateMatchPattern += ' ';
|
|
|
|
}
|
2012-08-08 06:43:41 +02:00
|
|
|
if (indentlevel == 0 && Token::Match(tok3->previous(), "[<,]"))
|
2013-02-10 07:43:09 +01:00
|
|
|
typesUsedInTemplateInstantiation.push_back(tok3);
|
2012-01-09 20:33:11 +01:00
|
|
|
// add additional type information
|
2015-03-22 11:20:47 +01:00
|
|
|
if (!constconst && tok3->str() != "class") {
|
2013-04-11 18:58:39 +02:00
|
|
|
if (tok3->isUnsigned())
|
2015-04-01 12:43:03 +02:00
|
|
|
typeForNewName += "unsigned";
|
2013-04-11 18:58:39 +02:00
|
|
|
else if (tok3->isSigned())
|
2015-04-01 12:43:03 +02:00
|
|
|
typeForNewName += "signed";
|
2013-04-11 18:58:39 +02:00
|
|
|
if (tok3->isLong())
|
2015-04-01 12:43:03 +02:00
|
|
|
typeForNewName += "long";
|
|
|
|
typeForNewName += tok3->str();
|
2013-04-11 18:58:39 +02:00
|
|
|
}
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
templateMatchPattern += ">";
|
|
|
|
|
2013-02-10 07:43:09 +01:00
|
|
|
if (typeForNewName.empty() || typeParametersInDeclaration.size() != typesUsedInTemplateInstantiation.size()) {
|
2015-04-10 14:18:52 +02:00
|
|
|
if (printDebug && errorlogger) {
|
2015-04-05 14:54:24 +02:00
|
|
|
std::list<const Token *> callstack(1, tok2);
|
2014-05-20 21:55:08 +02:00
|
|
|
errorlogger->reportErr(ErrorLogger::ErrorMessage(callstack, &tokenlist, Severity::debug, "debug",
|
2015-04-05 14:54:24 +02:00
|
|
|
"Failed to instantiate template \"" + name + "\". The checking continues anyway.", false));
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
if (typeForNewName.empty())
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// New classname/funcname..
|
|
|
|
const std::string newName(name + "<" + typeForNewName + ">");
|
|
|
|
|
|
|
|
if (expandedtemplates.find(newName) == expandedtemplates.end()) {
|
|
|
|
expandedtemplates.insert(newName);
|
2013-02-10 07:43:09 +01:00
|
|
|
TemplateSimplifier::expandTemplate(tokenlist, tok,name,typeParametersInDeclaration,newName,typesUsedInTemplateInstantiation,templateInstantiations);
|
2012-07-29 16:01:05 +02:00
|
|
|
instantiated = true;
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace all these template usages..
|
|
|
|
std::list< std::pair<Token *, Token *> > removeTokens;
|
|
|
|
for (Token *tok4 = tok2; tok4; tok4 = tok4->next()) {
|
|
|
|
if (Token::simpleMatch(tok4, templateMatchPattern.c_str())) {
|
|
|
|
Token * tok5 = tok4->tokAt(2);
|
2013-02-10 07:43:09 +01:00
|
|
|
unsigned int typeCountInInstantiation = 1U; // There is always at least one type
|
|
|
|
const Token *typetok = (!typesUsedInTemplateInstantiation.empty()) ? typesUsedInTemplateInstantiation[0] : 0;
|
2012-10-14 19:48:53 +02:00
|
|
|
unsigned int indentlevel5 = 0; // indentlevel for tok5
|
|
|
|
while (tok5 && (indentlevel5 > 0 || tok5->str() != ">")) {
|
|
|
|
if (tok5->str() == "<" && templateParameters(tok5) > 0)
|
|
|
|
++indentlevel5;
|
|
|
|
else if (indentlevel5 > 0 && Token::Match(tok5, "> [,>]"))
|
|
|
|
--indentlevel5;
|
|
|
|
else if (indentlevel5 == 0) {
|
|
|
|
if (tok5->str() != ",") {
|
|
|
|
if (!typetok ||
|
|
|
|
tok5->isUnsigned() != typetok->isUnsigned() ||
|
|
|
|
tok5->isSigned() != typetok->isSigned() ||
|
|
|
|
tok5->isLong() != typetok->isLong()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
typetok = typetok ? typetok->next() : 0;
|
|
|
|
} else {
|
2013-02-10 07:43:09 +01:00
|
|
|
typetok = (typeCountInInstantiation < typesUsedInTemplateInstantiation.size()) ? typesUsedInTemplateInstantiation[typeCountInInstantiation] : 0;
|
|
|
|
++typeCountInInstantiation;
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tok5 = tok5->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// matching template usage => replace tokens..
|
|
|
|
// Foo < int > => Foo<int>
|
2013-02-10 07:43:09 +01:00
|
|
|
if (tok5 && tok5->str() == ">" && typeCountInInstantiation == typesUsedInTemplateInstantiation.size()) {
|
2012-01-09 20:33:11 +01:00
|
|
|
tok4->str(newName);
|
|
|
|
for (Token *tok6 = tok4->next(); tok6 != tok5; tok6 = tok6->next()) {
|
|
|
|
if (tok6->isName())
|
|
|
|
templateInstantiations.remove(tok6);
|
|
|
|
}
|
|
|
|
removeTokens.push_back(std::pair<Token*,Token*>(tok4, tok5->next()));
|
|
|
|
}
|
|
|
|
|
|
|
|
tok4 = tok5;
|
|
|
|
if (!tok4)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!removeTokens.empty()) {
|
|
|
|
Token::eraseTokens(removeTokens.back().first, removeTokens.back().second);
|
|
|
|
removeTokens.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2012-07-29 16:01:05 +02:00
|
|
|
|
|
|
|
// Template has been instantiated .. then remove the template declaration
|
|
|
|
return instantiated;
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TemplateSimplifier::simplifyTemplates(
|
2012-05-06 10:38:55 +02:00
|
|
|
TokenList& tokenlist,
|
2014-05-20 21:55:08 +02:00
|
|
|
ErrorLogger* errorlogger,
|
2012-01-09 20:33:11 +01:00
|
|
|
const Settings *_settings,
|
|
|
|
bool &_codeWithTemplates
|
|
|
|
)
|
|
|
|
{
|
|
|
|
|
2012-09-12 20:46:56 +02:00
|
|
|
std::set<std::string> expandedtemplates(TemplateSimplifier::expandSpecialized(tokenlist.front()));
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2014-03-12 19:36:22 +01:00
|
|
|
if (TemplateSimplifier::getTemplateDeclarations(tokenlist.front(), _codeWithTemplates).empty())
|
2012-01-09 20:33:11 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
// There are templates..
|
|
|
|
// Remove "typename" unless used in template arguments..
|
2012-05-06 10:38:55 +02:00
|
|
|
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
|
2012-01-09 20:33:11 +01:00
|
|
|
if (tok->str() == "typename")
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, "template <")) {
|
|
|
|
while (tok && tok->str() != ">")
|
|
|
|
tok = tok->next();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-14 05:36:44 +01:00
|
|
|
|
2014-03-12 19:36:22 +01:00
|
|
|
std::list<Token *> templates(TemplateSimplifier::getTemplateDeclarations(tokenlist.front(), _codeWithTemplates));
|
2012-01-09 20:33:11 +01:00
|
|
|
|
|
|
|
// Locate possible instantiations of templates..
|
2012-09-12 20:46:56 +02:00
|
|
|
std::list<Token *> templateInstantiations(TemplateSimplifier::getTemplateInstantiations(tokenlist.front()));
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2012-07-29 16:01:05 +02:00
|
|
|
// No template instantiations? Then return.
|
|
|
|
if (templateInstantiations.empty())
|
2012-01-09 20:33:11 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Template arguments with default values
|
2013-11-21 20:27:24 +01:00
|
|
|
TemplateSimplifier::useDefaultArgumentValues(templates, &templateInstantiations);
|
2012-01-09 20:33:11 +01:00
|
|
|
|
|
|
|
// expand templates
|
|
|
|
//bool done = false;
|
|
|
|
//while (!done)
|
|
|
|
{
|
|
|
|
//done = true;
|
2012-07-29 16:01:05 +02:00
|
|
|
std::list<Token *> templates2;
|
2012-01-09 20:33:11 +01:00
|
|
|
for (std::list<Token *>::reverse_iterator iter1 = templates.rbegin(); iter1 != templates.rend(); ++iter1) {
|
2013-02-10 07:43:09 +01:00
|
|
|
bool instantiated = TemplateSimplifier::simplifyTemplateInstantiations(tokenlist,
|
2012-07-29 16:01:05 +02:00
|
|
|
errorlogger,
|
|
|
|
_settings,
|
|
|
|
*iter1,
|
|
|
|
templateInstantiations,
|
|
|
|
expandedtemplates);
|
|
|
|
if (instantiated)
|
|
|
|
templates2.push_back(*iter1);
|
2012-01-09 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
2015-06-23 20:53:57 +02:00
|
|
|
for (std::list<Token *>::const_iterator it = templates2.begin(); it != templates2.end(); ++it) {
|
2012-07-29 16:01:05 +02:00
|
|
|
std::list<Token *>::iterator it1 = std::find(templates.begin(), templates.end(), *it);
|
|
|
|
if (it1 != templates.end()) {
|
|
|
|
templates.erase(it1);
|
|
|
|
removeTemplate(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|