2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2016-01-01 14:34:45 +01:00
|
|
|
* Copyright (C) 2007-2016 Cppcheck team.
|
2008-12-18 22:28:57 +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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-18 22:28:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "token.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2010-07-24 10:51:17 +02:00
|
|
|
#include "errorlogger.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "library.h"
|
2014-04-02 06:49:28 +02:00
|
|
|
#include "settings.h"
|
2014-08-05 06:24:23 +02:00
|
|
|
#include "symboldatabase.h"
|
2015-11-29 10:49:10 +01:00
|
|
|
#include "utils.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2009-08-22 16:22:50 +02:00
|
|
|
#include <cassert>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <cctype>
|
2008-12-18 22:28:57 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
2009-03-03 21:17:23 +01:00
|
|
|
#include <map>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <set>
|
2013-12-28 11:02:39 +01:00
|
|
|
#include <stack>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <utility>
|
2014-03-08 20:56:39 +01:00
|
|
|
|
2017-08-13 14:15:24 +02:00
|
|
|
const std::list<ValueFlow::Value> Token::emptyValueList;
|
|
|
|
|
lib: fix a bunch of warnings about differing function arguments in definition and declaration.
[lib/token.h:72] -> [lib/token.cpp:36]: (style, inconclusive) Function 'Token' argument 1 names different: declaration 'tokensBack' definition 't'.
[lib/token.h:445] -> [lib/token.cpp:497]: (style, inconclusive) Function 'multiCompare' argument 1 names different: declaration 'needle' definition 'tok'.
[lib/checkio.h:73] -> [lib/checkio.cpp:1385]: (style, inconclusive) Function 'ArgumentInfo' argument 3 names different: declaration 'isCPP' definition '_isCPP'.
[lib/checkother.h:216] -> [lib/checkother.cpp:2136]: (style, inconclusive) Function 'checkComparisonFunctionIsAlwaysTrueOrFalseError' argument 2 names different: declaration 'strFunctionName' definition 'functionName'.
[lib/errorlogger.h:214] -> [lib/errorlogger.cpp:51]: (style, inconclusive) Function 'ErrorMessage' argument 2 names different: declaration 'file0' definition 'file0_'.
[lib/errorlogger.h:215] -> [lib/errorlogger.cpp:65]: (style, inconclusive) Function 'ErrorMessage' argument 2 names different: declaration 'file0' definition 'file0_'.
[lib/library.h:327] -> [lib/library.cpp:1043]: (style, inconclusive) Function 'ignorefunction' argument 1 names different: declaration 'function' definition 'functionName'.
[lib/mathlib.h:112] -> [lib/mathlib.cpp:1275]: (style, inconclusive) Function 'isNullValue' argument 1 names different: declaration 'tok' definition 'str'.
[lib/preprocessor.h:91] -> [lib/preprocessor.cpp:122]: (style, inconclusive) Function 'setDirectives' argument 1 names different: declaration 'tokens' definition 'tokens1'.
[lib/symboldatabase.h:860] -> [lib/symboldatabase.cpp:1801]: (style, inconclusive) Function 'argsMatch' argument 1 names different: declaration 'info' definition 'scope'.
[lib/symboldatabase.h:1171] -> [lib/symboldatabase.cpp:2048]: (style, inconclusive) Function 'addClassFunction' argument 1 names different: declaration 'info' definition 'scope'.
[lib/symboldatabase.h:1174] -> [lib/symboldatabase.cpp:2208]: (style, inconclusive) Function 'addNewFunction' argument 1 names different: declaration 'info' definition 'scope'.
[lib/symboldatabase.h:1090] -> [lib/symboldatabase.cpp:3648]: (style, inconclusive) Function 'findVariableType' argument 2 names different: declaration 'type' definition 'typeTok'.
[lib/symboldatabase.h:1101] -> [lib/symboldatabase.cpp:4308]: (style, inconclusive) Function 'findType' argument 1 names different: declaration 'tok' definition 'startTok'.
[lib/symboldatabase.h:1176] -> [lib/symboldatabase.cpp:4349]: (style, inconclusive) Function 'findTypeInNested' argument 1 names different: declaration 'tok' definition 'startTok'.
[lib/symboldatabase.h:1193] -> [lib/symboldatabase.cpp:4501]: (style, inconclusive) Function 'setValueType' argument 2 names different: declaration 'enumerators' definition 'enumerator'.
[lib/path.h:159] -> [lib/path.cpp:247]: (style, inconclusive) Function 'isCPP' argument 1 names different: declaration 'extensionInLowerCase' definition 'path'.
[lib/path.h:145] -> [lib/path.cpp:266]: (style, inconclusive) Function 'acceptFile' argument 1 names different: declaration 'filename' definition 'path'.
2017-04-03 00:06:46 +02:00
|
|
|
Token::Token(Token **tokens) :
|
|
|
|
tokensBack(tokens),
|
2017-08-09 20:00:26 +02:00
|
|
|
_next(nullptr),
|
|
|
|
_previous(nullptr),
|
|
|
|
_link(nullptr),
|
|
|
|
_scope(nullptr),
|
|
|
|
_function(nullptr), // Initialize whole union
|
2012-05-14 20:46:23 +02:00
|
|
|
_varId(0),
|
|
|
|
_fileIndex(0),
|
|
|
|
_linenr(0),
|
2017-05-17 14:57:54 +02:00
|
|
|
_col(0),
|
2012-05-14 20:46:23 +02:00
|
|
|
_progressValue(0),
|
2015-08-14 20:46:13 +02:00
|
|
|
_tokType(eNone),
|
2014-05-06 06:35:48 +02:00
|
|
|
_flags(0),
|
2014-02-15 08:05:54 +01:00
|
|
|
_astOperand1(nullptr),
|
|
|
|
_astOperand2(nullptr),
|
2014-06-26 10:57:39 +02:00
|
|
|
_astParent(nullptr),
|
2015-10-04 19:42:58 +02:00
|
|
|
_originalName(nullptr),
|
2017-03-27 18:48:34 +02:00
|
|
|
valuetype(nullptr),
|
|
|
|
_values(nullptr)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
Token::~Token()
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2014-06-26 10:57:39 +02:00
|
|
|
delete _originalName;
|
2015-10-04 19:42:58 +02:00
|
|
|
delete valuetype;
|
2017-03-27 18:48:34 +02:00
|
|
|
delete _values;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2017-11-17 22:10:39 +01:00
|
|
|
static const std::set<std::string> controlFlowKeywords = make_container< std::set<std::string> > () <<
|
|
|
|
"goto" <<
|
|
|
|
"do" <<
|
|
|
|
"if" <<
|
|
|
|
"else" <<
|
|
|
|
"for" <<
|
|
|
|
"while" <<
|
|
|
|
"switch" <<
|
|
|
|
"case" <<
|
|
|
|
"break" <<
|
|
|
|
"continue" <<
|
|
|
|
"return";
|
|
|
|
|
2011-10-23 20:38:03 +02:00
|
|
|
void Token::update_property_info()
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2017-11-17 22:10:39 +01:00
|
|
|
setFlag(fIsControlFlowKeyword, controlFlowKeywords.find(_str) != controlFlowKeywords.end());
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!_str.empty()) {
|
2012-04-23 21:05:26 +02:00
|
|
|
if (_str == "true" || _str == "false")
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eBoolean);
|
2016-07-29 13:42:11 +02:00
|
|
|
else if (std::isalpha((unsigned char)_str[0]) || _str[0] == '_' || _str[0] == '$') { // Name
|
2012-04-23 21:05:26 +02:00
|
|
|
if (_varId)
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eVariable);
|
2015-08-14 20:46:13 +02:00
|
|
|
else if (_tokType != eVariable && _tokType != eFunction && _tokType != eType && _tokType != eKeyword)
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eName);
|
2014-03-27 07:23:47 +01:00
|
|
|
} else if (std::isdigit((unsigned char)_str[0]) || (_str.length() > 1 && _str[0] == '-' && std::isdigit((unsigned char)_str[1])))
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eNumber);
|
2017-04-01 18:14:18 +02:00
|
|
|
else if (_str.length() > 1 && _str[0] == '"' && endsWith(_str,'"'))
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eString);
|
2017-04-01 18:14:18 +02:00
|
|
|
else if (_str.length() > 1 && _str[0] == '\'' && endsWith(_str,'\''))
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eChar);
|
2014-06-26 17:32:24 +02:00
|
|
|
else if (_str == "=" || _str == "<<=" || _str == ">>=" ||
|
|
|
|
(_str.size() == 2U && _str[1] == '=' && std::strchr("+-*/%&^|", _str[0])))
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eAssignmentOp);
|
2012-04-23 21:05:26 +02:00
|
|
|
else if (_str.size() == 1 && _str.find_first_of(",[]()?:") != std::string::npos)
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eExtendedOp);
|
2012-04-23 21:05:26 +02:00
|
|
|
else if (_str=="<<" || _str==">>" || (_str.size()==1 && _str.find_first_of("+-*/%") != std::string::npos))
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eArithmeticalOp);
|
2012-04-23 21:05:26 +02:00
|
|
|
else if (_str.size() == 1 && _str.find_first_of("&|^~") != std::string::npos)
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eBitOp);
|
2014-06-26 17:32:24 +02:00
|
|
|
else if (_str.size() <= 2 &&
|
|
|
|
(_str == "&&" ||
|
|
|
|
_str == "||" ||
|
|
|
|
_str == "!"))
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eLogicalOp);
|
2014-06-26 17:32:24 +02:00
|
|
|
else if (_str.size() <= 2 && !_link &&
|
|
|
|
(_str == "==" ||
|
2012-08-02 20:36:54 +02:00
|
|
|
_str == "!=" ||
|
|
|
|
_str == "<" ||
|
|
|
|
_str == "<=" ||
|
|
|
|
_str == ">" ||
|
2014-06-26 17:32:24 +02:00
|
|
|
_str == ">="))
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eComparisonOp);
|
2014-06-26 17:32:24 +02:00
|
|
|
else if (_str.size() == 2 &&
|
|
|
|
(_str == "++" ||
|
|
|
|
_str == "--"))
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eIncDecOp);
|
2012-04-23 21:05:26 +02:00
|
|
|
else if (_str.size() == 1 && (_str.find_first_of("{}") != std::string::npos || (_link && _str.find_first_of("<>") != std::string::npos)))
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eBracket);
|
2011-04-18 06:56:39 +02:00
|
|
|
else
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eOther);
|
2011-10-23 20:38:03 +02:00
|
|
|
} else {
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eNone);
|
2011-04-18 06:56:39 +02:00
|
|
|
}
|
2011-11-09 21:45:59 +01:00
|
|
|
|
|
|
|
update_property_isStandardType();
|
|
|
|
}
|
|
|
|
|
2017-03-04 11:13:28 +01:00
|
|
|
static const std::set<std::string> stdTypes =
|
|
|
|
make_container<std::set<std::string> >() << "bool"
|
|
|
|
<< "_Bool"
|
|
|
|
<< "char"
|
|
|
|
<< "double"
|
|
|
|
<< "float"
|
|
|
|
<< "int"
|
|
|
|
<< "long"
|
|
|
|
<< "short"
|
|
|
|
<< "size_t"
|
|
|
|
<< "void"
|
|
|
|
<< "wchar_t";
|
2015-06-10 21:14:17 +02:00
|
|
|
|
2011-11-09 21:45:59 +01:00
|
|
|
void Token::update_property_isStandardType()
|
|
|
|
{
|
2014-05-06 06:35:48 +02:00
|
|
|
isStandardType(false);
|
2011-11-09 21:45:59 +01:00
|
|
|
|
|
|
|
if (_str.size() < 3)
|
|
|
|
return;
|
|
|
|
|
2015-05-24 09:43:17 +02:00
|
|
|
if (stdTypes.find(_str)!=stdTypes.end()) {
|
2014-05-06 06:35:48 +02:00
|
|
|
isStandardType(true);
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eType);
|
2011-11-09 21:45:59 +01:00
|
|
|
}
|
2011-10-23 20:38:03 +02:00
|
|
|
}
|
2008-12-21 14:58:56 +01:00
|
|
|
|
2011-11-09 21:45:59 +01:00
|
|
|
|
2012-06-21 19:00:53 +02:00
|
|
|
bool Token::isUpperCaseName() const
|
|
|
|
{
|
|
|
|
if (!isName())
|
|
|
|
return false;
|
2014-07-05 12:10:23 +02:00
|
|
|
for (size_t i = 0; i < _str.length(); ++i) {
|
2012-06-21 19:00:53 +02:00
|
|
|
if (std::islower(_str[i]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:33:55 +01:00
|
|
|
void Token::concatStr(std::string const& b)
|
|
|
|
{
|
|
|
|
_str.erase(_str.length() - 1);
|
|
|
|
_str.append(b.begin() + 1, b.end());
|
2011-10-23 20:38:03 +02:00
|
|
|
|
|
|
|
update_property_info();
|
2009-03-28 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
2009-09-13 10:02:23 +02:00
|
|
|
std::string Token::strValue() const
|
2009-09-12 22:54:47 +02:00
|
|
|
{
|
2015-08-14 20:46:13 +02:00
|
|
|
assert(_tokType == eString);
|
2015-06-07 11:25:33 +02:00
|
|
|
std::string ret(_str.substr(1, _str.length() - 2));
|
|
|
|
std::string::size_type pos = 0U;
|
2015-08-14 20:46:13 +02:00
|
|
|
while ((pos = ret.find('\\', pos)) != std::string::npos) {
|
2015-06-07 11:25:33 +02:00
|
|
|
ret.erase(pos,1U);
|
|
|
|
if (ret[pos] >= 'a') {
|
|
|
|
if (ret[pos] == 'n')
|
|
|
|
ret[pos] = '\n';
|
|
|
|
else if (ret[pos] == 'r')
|
|
|
|
ret[pos] = '\r';
|
|
|
|
else if (ret[pos] == 't')
|
|
|
|
ret[pos] = '\t';
|
|
|
|
}
|
|
|
|
if (ret[pos] == '0')
|
|
|
|
return ret.substr(0,pos);
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
return ret;
|
2009-09-12 22:54:47 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 23:36:11 +01:00
|
|
|
void Token::deleteNext(unsigned long index)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2014-12-09 23:04:14 +01:00
|
|
|
while (_next && index) {
|
2011-12-07 23:36:11 +01:00
|
|
|
Token *n = _next;
|
2017-08-25 17:17:19 +02:00
|
|
|
|
|
|
|
// #8154 we are about to be unknown -> destroy the link to us
|
|
|
|
if (n->_link && n->_link->_link == n)
|
|
|
|
n->_link->link(nullptr);
|
|
|
|
|
2011-12-07 23:36:11 +01:00
|
|
|
_next = n->next();
|
|
|
|
delete n;
|
2014-12-09 23:04:14 +01:00
|
|
|
--index;
|
2011-12-07 23:36:11 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_next)
|
2008-12-18 22:28:57 +01:00
|
|
|
_next->previous(this);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tokensBack)
|
2010-01-06 20:19:27 +01:00
|
|
|
*tokensBack = this;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 10:53:26 +01:00
|
|
|
void Token::swapWithNext()
|
|
|
|
{
|
|
|
|
if (_next) {
|
2015-10-18 20:28:17 +02:00
|
|
|
std::swap(_str, _next->_str);
|
|
|
|
std::swap(_tokType, _next->_tokType);
|
|
|
|
std::swap(_flags, _next->_flags);
|
|
|
|
std::swap(_varId, _next->_varId);
|
|
|
|
std::swap(_fileIndex, _next->_fileIndex);
|
2016-02-02 17:30:14 +01:00
|
|
|
std::swap(_linenr, _next->_linenr);
|
2016-02-02 17:47:22 +01:00
|
|
|
if (_next->_link)
|
|
|
|
_next->_link->_link = this;
|
|
|
|
if (this->_link)
|
|
|
|
this->_link->_link = _next;
|
2015-10-18 20:28:17 +02:00
|
|
|
std::swap(_link, _next->_link);
|
|
|
|
std::swap(_scope, _next->_scope);
|
|
|
|
std::swap(_function, _next->_function);
|
|
|
|
std::swap(_originalName, _next->_originalName);
|
2017-03-27 18:48:34 +02:00
|
|
|
std::swap(_values, _next->_values);
|
2016-02-02 17:30:14 +01:00
|
|
|
std::swap(valuetype, _next->valuetype);
|
2015-10-18 20:28:17 +02:00
|
|
|
std::swap(_progressValue, _next->_progressValue);
|
2014-12-27 10:53:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 23:30:04 +02:00
|
|
|
void Token::takeData(Token *fromToken)
|
|
|
|
{
|
|
|
|
_str = fromToken->_str;
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(fromToken->_tokType);
|
2017-08-25 23:30:04 +02:00
|
|
|
_flags = fromToken->_flags;
|
|
|
|
_varId = fromToken->_varId;
|
|
|
|
_fileIndex = fromToken->_fileIndex;
|
|
|
|
_linenr = fromToken->_linenr;
|
|
|
|
_link = fromToken->_link;
|
|
|
|
_scope = fromToken->_scope;
|
|
|
|
_function = fromToken->_function;
|
|
|
|
if (fromToken->_originalName) {
|
|
|
|
delete _originalName;
|
|
|
|
_originalName = fromToken->_originalName;
|
|
|
|
fromToken->_originalName = nullptr;
|
|
|
|
}
|
2017-08-25 23:32:26 +02:00
|
|
|
delete _values;
|
|
|
|
_values = fromToken->_values;
|
|
|
|
fromToken->_values = nullptr;
|
|
|
|
delete valuetype;
|
|
|
|
valuetype = fromToken->valuetype;
|
|
|
|
fromToken->valuetype = nullptr;
|
2017-08-25 23:30:04 +02:00
|
|
|
if (_link)
|
|
|
|
_link->link(this);
|
|
|
|
}
|
|
|
|
|
2009-03-13 00:07:05 +01:00
|
|
|
void Token::deleteThis()
|
|
|
|
{
|
2012-02-13 17:44:08 +01:00
|
|
|
if (_next) { // Copy next to this and delete next
|
2017-08-25 23:30:04 +02:00
|
|
|
takeData(_next);
|
2017-08-25 17:17:19 +02:00
|
|
|
_next->link(nullptr); // mark as unlinked
|
2009-03-13 00:07:05 +01:00
|
|
|
deleteNext();
|
2012-02-13 17:44:08 +01:00
|
|
|
} else if (_previous && _previous->_previous) { // Copy previous to this and delete previous
|
2017-08-25 23:30:04 +02:00
|
|
|
takeData(_previous);
|
2012-02-13 17:44:08 +01:00
|
|
|
|
|
|
|
Token* toDelete = _previous;
|
|
|
|
_previous = _previous->_previous;
|
|
|
|
_previous->_next = this;
|
|
|
|
|
|
|
|
delete toDelete;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2009-03-13 00:07:05 +01:00
|
|
|
// We are the last token in the list, we can't delete
|
2012-02-13 17:44:08 +01:00
|
|
|
// ourselves, so just make us empty
|
|
|
|
str("");
|
2009-03-13 00:07:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
void Token::replace(Token *replaceThis, Token *start, Token *end)
|
|
|
|
{
|
|
|
|
// Fix the whole in the old location of start and end
|
2010-04-02 07:30:58 +02:00
|
|
|
if (start->previous())
|
2009-05-26 22:22:00 +02:00
|
|
|
start->previous()->next(end->next());
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end->next())
|
2009-05-26 22:22:00 +02:00
|
|
|
end->next()->previous(start->previous());
|
2009-01-26 23:26:50 +01:00
|
|
|
|
|
|
|
// Move start and end to their new location
|
2010-04-02 07:30:58 +02:00
|
|
|
if (replaceThis->previous())
|
2009-05-26 22:22:00 +02:00
|
|
|
replaceThis->previous()->next(start);
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (replaceThis->next())
|
2009-05-26 22:22:00 +02:00
|
|
|
replaceThis->next()->previous(end);
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
start->previous(replaceThis->previous());
|
|
|
|
end->next(replaceThis->next());
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (end->tokensBack && *(end->tokensBack) == end) {
|
2010-04-02 07:30:58 +02:00
|
|
|
while (end->next())
|
2010-01-06 20:19:27 +01:00
|
|
|
end = end->next();
|
|
|
|
*(end->tokensBack) = end;
|
|
|
|
}
|
|
|
|
|
2012-01-22 00:02:55 +01:00
|
|
|
// Update _progressValue, fileIndex and linenr
|
|
|
|
for (Token *tok = start; tok != end->next(); tok = tok->next())
|
2012-01-21 21:05:41 +01:00
|
|
|
tok->_progressValue = replaceThis->_progressValue;
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
// Delete old token, which is replaced
|
|
|
|
delete replaceThis;
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
const Token *Token::tokAt(int index) const
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
const Token *tok = this;
|
2015-12-14 22:04:26 +01:00
|
|
|
while (index > 0 && tok) {
|
|
|
|
tok = tok->next();
|
|
|
|
--index;
|
|
|
|
}
|
|
|
|
while (index < 0 && tok) {
|
|
|
|
tok = tok->previous();
|
|
|
|
++index;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2011-11-11 21:55:37 +01:00
|
|
|
const Token *Token::linkAt(int index) const
|
|
|
|
{
|
|
|
|
const Token *tok = this->tokAt(index);
|
2011-11-19 13:34:36 +01:00
|
|
|
if (!tok) {
|
2012-07-25 06:43:54 +02:00
|
|
|
throw InternalError(this, "Internal error. Token::linkAt called with index outside the tokens range.");
|
2011-11-19 13:34:36 +01:00
|
|
|
}
|
2012-09-09 14:34:07 +02:00
|
|
|
return tok->link();
|
2011-11-11 21:55:37 +01:00
|
|
|
}
|
|
|
|
|
2011-11-14 09:16:47 +01:00
|
|
|
const std::string &Token::strAt(int index) const
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
const Token *tok = this->tokAt(index);
|
2014-06-26 11:44:19 +02:00
|
|
|
return tok ? tok->_str : emptyString;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2016-02-01 09:55:16 +01:00
|
|
|
static int multiComparePercent(const Token *tok, const char*& haystack, unsigned int varid)
|
2011-11-05 19:24:21 +01:00
|
|
|
{
|
2014-06-26 18:17:05 +02:00
|
|
|
++haystack;
|
|
|
|
// Compare only the first character of the string for optimization reasons
|
|
|
|
switch (haystack[0]) {
|
|
|
|
case '\0':
|
|
|
|
case ' ':
|
|
|
|
case '|':
|
|
|
|
//simple '%' character
|
|
|
|
haystack += 1;
|
|
|
|
if (tok->isArithmeticalOp() && tok->str() == "%")
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
if (haystack[3] == '%') { // %var%
|
|
|
|
haystack += 4;
|
2015-01-31 10:50:39 +01:00
|
|
|
if (tok->varId() != 0)
|
2014-06-26 18:17:05 +02:00
|
|
|
return 1;
|
|
|
|
} else { // %varid%
|
|
|
|
if (varid == 0) {
|
|
|
|
throw InternalError(tok, "Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers");
|
|
|
|
}
|
2011-11-05 19:24:21 +01:00
|
|
|
|
2014-06-26 18:17:05 +02:00
|
|
|
haystack += 6;
|
|
|
|
|
|
|
|
if (tok->varId() == varid)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
// Type (%type%)
|
|
|
|
{
|
|
|
|
haystack += 5;
|
|
|
|
if (tok->isName() && tok->varId() == 0 && !tok->isKeyword())
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'a':
|
2015-12-31 01:15:49 +01:00
|
|
|
// Accept any token (%any%) or assign (%assign%)
|
2014-06-26 18:17:05 +02:00
|
|
|
{
|
2015-12-31 01:15:49 +01:00
|
|
|
if (haystack[3] == '%') { // %any%
|
|
|
|
haystack += 4;
|
|
|
|
return 1;
|
|
|
|
} else { // %assign%
|
|
|
|
haystack += 7;
|
|
|
|
if (tok->isAssignmentOp())
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-26 18:17:05 +02:00
|
|
|
}
|
2015-12-31 01:15:49 +01:00
|
|
|
break;
|
2014-06-26 18:17:05 +02:00
|
|
|
case 'n':
|
2015-01-31 10:50:39 +01:00
|
|
|
// Number (%num%) or name (%name%)
|
2014-06-26 18:17:05 +02:00
|
|
|
{
|
2015-01-31 10:50:39 +01:00
|
|
|
if (haystack[4] == '%') { // %name%
|
|
|
|
haystack += 5;
|
|
|
|
if (tok->isName())
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
haystack += 4;
|
|
|
|
if (tok->isNumber())
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-26 18:17:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c': {
|
|
|
|
haystack += 1;
|
|
|
|
// Character (%char%)
|
|
|
|
if (haystack[0] == 'h') {
|
|
|
|
haystack += 4;
|
2015-08-14 20:46:13 +02:00
|
|
|
if (tok->tokType() == Token::eChar)
|
2011-11-05 19:24:21 +01:00
|
|
|
return 1;
|
2014-06-26 18:17:05 +02:00
|
|
|
}
|
|
|
|
// Const operator (%cop%)
|
|
|
|
else if (haystack[1] == 'p') {
|
|
|
|
haystack += 3;
|
2013-03-01 11:43:59 +01:00
|
|
|
if (tok->isConstOp())
|
|
|
|
return 1;
|
2014-06-26 18:17:05 +02:00
|
|
|
}
|
|
|
|
// Comparison operator (%comp%)
|
|
|
|
else {
|
|
|
|
haystack += 4;
|
|
|
|
if (tok->isComparisonOp())
|
2011-11-05 19:24:21 +01:00
|
|
|
return 1;
|
2014-06-26 18:17:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
// String (%str%)
|
|
|
|
{
|
|
|
|
haystack += 4;
|
2015-08-14 20:46:13 +02:00
|
|
|
if (tok->tokType() == Token::eString)
|
2014-06-26 18:17:05 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
// Bool (%bool%)
|
|
|
|
{
|
|
|
|
haystack += 5;
|
|
|
|
if (tok->isBoolean())
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'o': {
|
|
|
|
++haystack;
|
|
|
|
if (haystack[1] == '%') {
|
|
|
|
// Op (%op%)
|
|
|
|
if (haystack[0] == 'p') {
|
|
|
|
haystack += 2;
|
|
|
|
if (tok->isOp())
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// Or (%or%)
|
|
|
|
else {
|
|
|
|
haystack += 2;
|
2015-08-14 20:46:13 +02:00
|
|
|
if (tok->tokType() == Token::eBitOp && tok->str() == "|")
|
2014-06-26 18:17:05 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Oror (%oror%)
|
|
|
|
else {
|
|
|
|
haystack += 4;
|
2015-08-14 20:46:13 +02:00
|
|
|
if (tok->tokType() == Token::eLogicalOp && tok->str() == "||")
|
2011-11-05 19:24:21 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-06-26 18:17:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//unknown %cmd%, abort
|
2015-03-29 21:05:18 +02:00
|
|
|
throw InternalError(tok, "Unexpected command");
|
2011-11-05 19:24:21 +01:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:17:05 +02:00
|
|
|
if (*haystack == '|')
|
|
|
|
haystack += 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
2011-11-05 19:24:21 +01:00
|
|
|
return 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:17:05 +02:00
|
|
|
int Token::multiCompare(const Token *tok, const char *haystack, unsigned int varid)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2014-06-26 17:32:24 +02:00
|
|
|
const char *needle = tok->str().c_str();
|
2009-01-27 20:30:01 +01:00
|
|
|
const char *needlePointer = needle;
|
2012-03-25 11:51:59 +02:00
|
|
|
for (;;) {
|
2014-06-26 18:17:05 +02:00
|
|
|
if (needlePointer == needle && haystack[0] == '%' && haystack[1] != '|' && haystack[1] != '\0' && haystack[1] != ' ') {
|
2016-02-01 09:55:16 +01:00
|
|
|
int ret = multiComparePercent(tok, haystack, varid);
|
2014-06-26 18:17:05 +02:00
|
|
|
if (ret < 2)
|
|
|
|
return ret;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (*haystack == '|') {
|
|
|
|
if (*needlePointer == 0) {
|
2010-09-19 15:14:13 +02:00
|
|
|
// If needle is at the end, we have a match.
|
2008-12-18 22:28:57 +01:00
|
|
|
return 1;
|
2009-01-27 20:30:01 +01:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-27 20:30:01 +01:00
|
|
|
needlePointer = needle;
|
2010-09-19 15:14:13 +02:00
|
|
|
++haystack;
|
2014-06-26 18:17:05 +02:00
|
|
|
} else if (*needlePointer == *haystack) {
|
|
|
|
if (*needlePointer == '\0')
|
|
|
|
return 1;
|
|
|
|
++needlePointer;
|
|
|
|
++haystack;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (*haystack == ' ' || *haystack == '\0') {
|
2010-09-19 15:14:13 +02:00
|
|
|
if (needlePointer == needle)
|
|
|
|
return 0;
|
|
|
|
break;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-01-27 20:30:01 +01:00
|
|
|
// If haystack and needle don't share the same character,
|
|
|
|
// find next '|' character.
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2010-09-19 15:14:13 +02:00
|
|
|
needlePointer = needle;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
do {
|
2010-09-19 15:14:13 +02:00
|
|
|
++haystack;
|
2011-10-13 20:53:06 +02:00
|
|
|
} while (*haystack != ' ' && *haystack != '|' && *haystack);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (*haystack == ' ' || *haystack == '\0') {
|
2016-02-01 09:55:16 +01:00
|
|
|
return -1;
|
2010-09-19 15:14:13 +02:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2010-09-19 15:14:13 +02:00
|
|
|
++haystack;
|
2009-01-27 20:30:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 15:14:13 +02:00
|
|
|
if (*needlePointer == '\0')
|
|
|
|
return 1;
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
bool Token::simpleMatch(const Token *tok, const char pattern[])
|
2008-12-22 00:28:09 +01:00
|
|
|
{
|
2014-04-27 09:32:02 +02:00
|
|
|
if (!tok)
|
|
|
|
return false; // shortcut
|
2014-07-05 12:10:23 +02:00
|
|
|
const char *current = pattern;
|
|
|
|
const char *next = std::strchr(pattern, ' ');
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!next)
|
2012-12-27 11:51:12 +01:00
|
|
|
next = pattern + std::strlen(pattern);
|
2008-12-23 22:45:47 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (*current) {
|
2015-10-11 12:36:23 +02:00
|
|
|
std::size_t length = next - current;
|
2008-12-23 22:45:47 +01:00
|
|
|
|
2012-12-27 11:51:12 +01:00
|
|
|
if (!tok || length != tok->_str.length() || std::strncmp(current, tok->_str.c_str(), length))
|
2008-12-22 00:28:09 +01:00
|
|
|
return false;
|
2008-12-23 22:45:47 +01:00
|
|
|
|
|
|
|
current = next;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (*next) {
|
2012-12-27 11:51:12 +01:00
|
|
|
next = std::strchr(++current, ' ');
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!next)
|
2012-12-27 11:51:12 +01:00
|
|
|
next = current + std::strlen(current);
|
2008-12-23 22:45:47 +01:00
|
|
|
}
|
|
|
|
tok = tok->next();
|
2008-12-22 00:28:09 +01:00
|
|
|
}
|
2008-12-23 22:45:47 +01:00
|
|
|
|
2008-12-22 00:28:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-16 15:28:38 +02:00
|
|
|
bool Token::firstWordEquals(const char *str, const char *word)
|
2009-08-16 22:28:17 +02:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
for (;;) {
|
|
|
|
if (*str != *word) {
|
2012-04-26 16:39:16 +02:00
|
|
|
return (*str == ' ' && *word == 0);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (*str == 0)
|
2009-08-16 22:28:17 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
++str;
|
|
|
|
++word;
|
|
|
|
}
|
|
|
|
|
2012-04-26 16:39:16 +02:00
|
|
|
return true;
|
2009-08-16 22:28:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *Token::chrInFirstWord(const char *str, char c)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
for (;;) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (*str == ' ' || *str == 0)
|
2017-08-09 20:00:26 +02:00
|
|
|
return nullptr;
|
2009-08-16 22:28:17 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (*str == c)
|
2009-08-16 22:28:17 +02:00
|
|
|
return str;
|
|
|
|
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-06 00:06:51 +01:00
|
|
|
bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
const char *p = pattern;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (*p) {
|
2008-12-18 22:28:57 +01:00
|
|
|
// Skip spaces in pattern..
|
2010-04-02 07:30:58 +02:00
|
|
|
while (*p == ' ')
|
2009-01-01 23:22:28 +01:00
|
|
|
++p;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
// No token => Success!
|
2012-12-01 00:47:07 +01:00
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!tok) {
|
2009-01-10 01:33:48 +01:00
|
|
|
// If we have no tokens, pattern "!!else" should return true
|
2012-11-25 15:13:41 +01:00
|
|
|
if (p[0] == '!' && p[1] == '!' && p[2] != '\0') {
|
2010-09-20 20:15:07 +02:00
|
|
|
while (*p && *p != ' ')
|
|
|
|
++p;
|
2009-01-10 01:33:48 +01:00
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2009-01-10 01:33:48 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
// [.. => search for a one-character token..
|
2014-06-26 18:17:05 +02:00
|
|
|
if (p[0] == '[' && chrInFirstWord(p, ']')) {
|
2012-11-20 00:16:53 +01:00
|
|
|
if (tok->str().length() != 1)
|
2011-07-15 19:01:36 +02:00
|
|
|
return false;
|
2011-07-15 19:02:16 +02:00
|
|
|
|
2012-11-20 00:16:53 +01:00
|
|
|
const char *temp = p+1;
|
2009-08-16 22:28:17 +02:00
|
|
|
bool chrFound = false;
|
2012-11-20 00:16:53 +01:00
|
|
|
unsigned int count = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (*temp && *temp != ' ') {
|
|
|
|
if (*temp == ']') {
|
2009-08-16 22:28:17 +02:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2012-11-20 00:16:53 +01:00
|
|
|
else if (*temp == tok->str()[0]) {
|
2009-08-16 22:28:17 +02:00
|
|
|
chrFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++temp;
|
|
|
|
}
|
|
|
|
|
2012-11-20 00:16:53 +01:00
|
|
|
if (count > 1 && tok->str()[0] == ']')
|
|
|
|
chrFound = true;
|
2009-08-16 22:28:17 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!chrFound)
|
2008-12-18 22:28:57 +01:00
|
|
|
return false;
|
2012-11-20 00:16:53 +01:00
|
|
|
|
|
|
|
p = temp;
|
|
|
|
while (*p && *p != ' ')
|
|
|
|
++p;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:17:05 +02:00
|
|
|
// Parse "not" options. Token can be anything except the given one
|
|
|
|
else if (p[0] == '!' && p[1] == '!' && p[2] != '\0') {
|
|
|
|
p += 2;
|
|
|
|
if (firstWordEquals(p, tok->str().c_str()))
|
|
|
|
return false;
|
|
|
|
while (*p && *p != ' ')
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
// Parse multi options, such as void|int|char (accept token which is one of these 3)
|
2014-06-26 18:17:05 +02:00
|
|
|
else {
|
|
|
|
int res = multiCompare(tok, p, varid);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (res == 0) {
|
2008-12-18 22:28:57 +01:00
|
|
|
// Empty alternative matches, use the same token on next round
|
2010-09-20 20:15:07 +02:00
|
|
|
while (*p && *p != ' ')
|
|
|
|
++p;
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (res == -1) {
|
2008-12-18 22:28:57 +01:00
|
|
|
// No match
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-20 20:15:07 +02:00
|
|
|
while (*p && *p != ' ')
|
|
|
|
++p;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The end of the pattern has been reached and nothing wrong has been found
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-22 21:14:14 +01:00
|
|
|
std::size_t Token::getStrLength(const Token *tok)
|
2009-08-30 13:07:10 +02:00
|
|
|
{
|
2014-02-15 08:05:54 +01:00
|
|
|
assert(tok != nullptr);
|
2015-08-14 20:46:13 +02:00
|
|
|
assert(tok->_tokType == eString);
|
2009-08-30 13:07:10 +02:00
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
std::size_t len = 0;
|
2015-06-07 11:25:33 +02:00
|
|
|
std::string::const_iterator it = tok->str().begin() + 1U;
|
|
|
|
const std::string::const_iterator end = tok->str().end() - 1U;
|
2009-08-30 13:07:10 +02:00
|
|
|
|
2015-06-07 11:25:33 +02:00
|
|
|
while (it != end) {
|
|
|
|
if (*it == '\\') {
|
|
|
|
++it;
|
2009-09-26 17:58:14 +02:00
|
|
|
|
|
|
|
// string ends at '\0'
|
2015-06-07 11:25:33 +02:00
|
|
|
if (*it == '0')
|
|
|
|
return len;
|
2009-09-26 17:58:14 +02:00
|
|
|
}
|
|
|
|
|
2015-06-07 11:25:33 +02:00
|
|
|
if (*it == '\0')
|
|
|
|
return len;
|
|
|
|
|
|
|
|
++it;
|
2009-08-30 13:07:10 +02:00
|
|
|
++len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-08-01 13:12:18 +02:00
|
|
|
std::size_t Token::getStrSize(const Token *tok)
|
|
|
|
{
|
2015-08-14 20:46:13 +02:00
|
|
|
assert(tok != nullptr && tok->tokType() == eString);
|
2014-08-01 13:12:18 +02:00
|
|
|
const std::string &str = tok->str();
|
|
|
|
unsigned int sizeofstring = 1U;
|
|
|
|
for (unsigned int i = 1U; i < str.size() - 1U; i++) {
|
|
|
|
if (str[i] == '\\')
|
|
|
|
++i;
|
|
|
|
++sizeofstring;
|
|
|
|
}
|
|
|
|
return sizeofstring;
|
|
|
|
}
|
|
|
|
|
2013-01-13 20:52:38 +01:00
|
|
|
std::string Token::getCharAt(const Token *tok, std::size_t index)
|
|
|
|
{
|
2014-02-15 08:05:54 +01:00
|
|
|
assert(tok != nullptr);
|
2013-01-13 20:52:38 +01:00
|
|
|
|
2015-10-26 11:51:05 +01:00
|
|
|
std::string::const_iterator it = tok->str().begin() + 1U;
|
|
|
|
const std::string::const_iterator end = tok->str().end() - 1U;
|
2013-01-13 20:52:38 +01:00
|
|
|
|
2015-10-26 11:51:05 +01:00
|
|
|
while (it != end) {
|
2013-01-13 20:52:38 +01:00
|
|
|
if (index == 0) {
|
2015-10-26 11:51:05 +01:00
|
|
|
if (*it == '\0')
|
|
|
|
return "\\0";
|
|
|
|
|
|
|
|
std::string ret(1, *it);
|
|
|
|
if (*it == '\\') {
|
|
|
|
++it;
|
|
|
|
ret += *it;
|
2013-01-13 20:52:38 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-26 11:51:05 +01:00
|
|
|
if (*it == '\\')
|
|
|
|
++it;
|
|
|
|
++it;
|
2013-01-13 20:52:38 +01:00
|
|
|
--index;
|
|
|
|
}
|
|
|
|
assert(index == 0);
|
|
|
|
|
|
|
|
return "\\0";
|
|
|
|
}
|
|
|
|
|
2009-11-27 22:21:13 +01:00
|
|
|
void Token::move(Token *srcStart, Token *srcEnd, Token *newLocation)
|
|
|
|
{
|
|
|
|
/**[newLocation] -> b -> c -> [srcStart] -> [srcEnd] -> f */
|
|
|
|
|
|
|
|
// Fix the gap, which tokens to be moved will leave
|
|
|
|
srcStart->previous()->next(srcEnd->next());
|
|
|
|
srcEnd->next()->previous(srcStart->previous());
|
|
|
|
|
|
|
|
// Fix the tokens to be moved
|
|
|
|
srcEnd->next(newLocation->next());
|
|
|
|
srcStart->previous(newLocation);
|
|
|
|
|
|
|
|
// Fix the tokens at newLocation
|
|
|
|
newLocation->next()->previous(srcEnd);
|
|
|
|
newLocation->next(srcStart);
|
2010-08-03 16:36:21 +02:00
|
|
|
|
|
|
|
// Update _progressValue
|
2012-01-21 21:05:41 +01:00
|
|
|
for (Token *tok = srcStart; tok != srcEnd->next(); tok = tok->next())
|
2010-08-03 16:36:21 +02:00
|
|
|
tok->_progressValue = newLocation->_progressValue;
|
2009-11-27 22:21:13 +01:00
|
|
|
}
|
|
|
|
|
2011-12-07 21:15:00 +01:00
|
|
|
Token* Token::nextArgument() const
|
2011-10-23 11:23:48 +02:00
|
|
|
{
|
|
|
|
for (const Token* tok = this; tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == ",")
|
2011-10-28 18:57:10 +02:00
|
|
|
return tok->next();
|
2014-09-14 11:26:16 +02:00
|
|
|
else if (tok->link() && Token::Match(tok, "(|{|[|<"))
|
2012-04-16 15:28:38 +02:00
|
|
|
tok = tok->link();
|
2014-09-14 11:26:16 +02:00
|
|
|
else if (Token::Match(tok, ")|;"))
|
2015-12-06 12:50:05 +01:00
|
|
|
return nullptr;
|
2011-10-23 11:23:48 +02:00
|
|
|
}
|
2015-12-06 12:50:05 +01:00
|
|
|
return nullptr;
|
2011-10-23 11:23:48 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 14:57:00 +02:00
|
|
|
Token* Token::nextArgumentBeforeCreateLinks2() const
|
|
|
|
{
|
|
|
|
for (const Token* tok = this; tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == ",")
|
|
|
|
return tok->next();
|
2014-09-14 11:26:16 +02:00
|
|
|
else if (tok->link() && Token::Match(tok, "(|{|["))
|
2014-08-20 14:57:00 +02:00
|
|
|
tok = tok->link();
|
|
|
|
else if (tok->str() == "<") {
|
|
|
|
const Token* temp = tok->findClosingBracket();
|
|
|
|
if (temp)
|
|
|
|
tok = temp;
|
2014-09-14 11:26:16 +02:00
|
|
|
} else if (Token::Match(tok, ")|;"))
|
2015-12-06 12:50:05 +01:00
|
|
|
return nullptr;
|
2014-08-20 14:57:00 +02:00
|
|
|
}
|
2015-12-06 12:50:05 +01:00
|
|
|
return nullptr;
|
2014-08-20 14:57:00 +02:00
|
|
|
}
|
|
|
|
|
2015-01-03 20:35:33 +01:00
|
|
|
Token* Token::nextTemplateArgument() const
|
|
|
|
{
|
|
|
|
for (const Token* tok = this; tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == ",")
|
|
|
|
return tok->next();
|
|
|
|
else if (tok->link() && Token::Match(tok, "(|{|[|<"))
|
|
|
|
tok = tok->link();
|
|
|
|
else if (Token::Match(tok, ">|;"))
|
2015-12-06 12:50:05 +01:00
|
|
|
return nullptr;
|
2015-01-03 20:35:33 +01:00
|
|
|
}
|
2015-12-06 12:50:05 +01:00
|
|
|
return nullptr;
|
2015-01-03 20:35:33 +01:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:30:20 +02:00
|
|
|
const Token * Token::findClosingBracket() const
|
2012-04-18 16:02:03 +02:00
|
|
|
{
|
2014-02-16 11:47:52 +01:00
|
|
|
const Token *closing = nullptr;
|
2013-07-31 10:30:20 +02:00
|
|
|
|
2012-04-18 16:02:03 +02:00
|
|
|
if (_str == "<") {
|
|
|
|
unsigned int depth = 0;
|
2014-02-15 08:05:54 +01:00
|
|
|
for (closing = this; closing != nullptr; closing = closing->next()) {
|
2015-06-30 12:16:55 +02:00
|
|
|
if (Token::Match(closing, "{|[|(")) {
|
2012-04-18 16:02:03 +02:00
|
|
|
closing = closing->link();
|
2015-06-30 12:16:55 +02:00
|
|
|
if (!closing)
|
|
|
|
return nullptr; // #6803
|
2016-02-04 10:00:54 +01:00
|
|
|
} else if (Token::Match(closing, "}|]|)|;")) {
|
|
|
|
if (depth > 0)
|
|
|
|
return nullptr;
|
2013-07-31 10:30:20 +02:00
|
|
|
break;
|
2016-02-04 10:00:54 +01:00
|
|
|
} else if (closing->str() == "<")
|
2012-04-18 16:02:03 +02:00
|
|
|
++depth;
|
|
|
|
else if (closing->str() == ">") {
|
|
|
|
if (--depth == 0)
|
2013-07-31 10:30:20 +02:00
|
|
|
break;
|
2012-04-18 16:02:03 +02:00
|
|
|
} else if (closing->str() == ">>") {
|
|
|
|
if (--depth == 0)
|
2013-07-31 10:30:20 +02:00
|
|
|
break;
|
2012-04-18 16:02:03 +02:00
|
|
|
if (--depth == 0)
|
2013-07-31 10:30:20 +02:00
|
|
|
break;
|
2012-04-18 16:02:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 10:30:20 +02:00
|
|
|
return closing;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token * Token::findClosingBracket()
|
|
|
|
{
|
|
|
|
// return value of const function
|
|
|
|
return const_cast<Token*>(const_cast<const Token*>(this)->findClosingBracket());
|
2012-04-18 16:02:03 +02:00
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2016-10-29 21:37:45 +02:00
|
|
|
const Token *Token::findsimplematch(const Token * const startTok, const char pattern[])
|
2011-10-27 10:54:50 +02:00
|
|
|
{
|
2015-08-27 16:14:33 +02:00
|
|
|
for (const Token* tok = startTok; tok; tok = tok->next()) {
|
2011-10-27 10:54:50 +02:00
|
|
|
if (Token::simpleMatch(tok, pattern))
|
|
|
|
return tok;
|
|
|
|
}
|
2017-08-09 20:00:26 +02:00
|
|
|
return nullptr;
|
2011-10-27 10:54:50 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 21:37:45 +02:00
|
|
|
const Token *Token::findsimplematch(const Token * const startTok, const char pattern[], const Token * const end)
|
2011-10-27 10:54:50 +02:00
|
|
|
{
|
2015-08-27 16:14:33 +02:00
|
|
|
for (const Token* tok = startTok; tok && tok != end; tok = tok->next()) {
|
2011-10-27 10:54:50 +02:00
|
|
|
if (Token::simpleMatch(tok, pattern))
|
|
|
|
return tok;
|
|
|
|
}
|
2015-12-06 12:50:05 +01:00
|
|
|
return nullptr;
|
2011-10-27 10:54:50 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 21:37:45 +02:00
|
|
|
const Token *Token::findmatch(const Token * const startTok, const char pattern[], const unsigned int varId)
|
2009-01-04 20:55:12 +01:00
|
|
|
{
|
2015-08-27 16:14:33 +02:00
|
|
|
for (const Token* tok = startTok; tok; tok = tok->next()) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, pattern, varId))
|
2009-01-04 20:55:12 +01:00
|
|
|
return tok;
|
|
|
|
}
|
2015-12-06 12:50:05 +01:00
|
|
|
return nullptr;
|
2009-01-04 20:55:12 +01:00
|
|
|
}
|
|
|
|
|
2016-10-29 21:37:45 +02:00
|
|
|
const Token *Token::findmatch(const Token * const startTok, const char pattern[], const Token * const end, const unsigned int varId)
|
2010-07-26 16:46:37 +02:00
|
|
|
{
|
2015-08-27 16:14:33 +02:00
|
|
|
for (const Token* tok = startTok; tok && tok != end; tok = tok->next()) {
|
2010-07-26 16:46:37 +02:00
|
|
|
if (Token::Match(tok, pattern, varId))
|
|
|
|
return tok;
|
|
|
|
}
|
2015-12-06 12:50:05 +01:00
|
|
|
return nullptr;
|
2010-07-26 16:46:37 +02:00
|
|
|
}
|
|
|
|
|
2013-09-24 06:43:03 +02:00
|
|
|
void Token::insertToken(const std::string &tokenStr, const std::string &originalNameStr, bool prepend)
|
|
|
|
{
|
|
|
|
//TODO: Find a solution for the first token on the list
|
|
|
|
if (prepend && !this->previous())
|
|
|
|
return;
|
|
|
|
|
2014-07-05 12:10:23 +02:00
|
|
|
Token *newToken;
|
2013-09-24 06:43:03 +02:00
|
|
|
if (_str.empty())
|
|
|
|
newToken = this;
|
|
|
|
else
|
|
|
|
newToken = new Token(tokensBack);
|
|
|
|
newToken->str(tokenStr);
|
2014-06-26 10:57:39 +02:00
|
|
|
if (!originalNameStr.empty())
|
|
|
|
newToken->originalName(originalNameStr);
|
2013-09-24 06:43:03 +02:00
|
|
|
newToken->_linenr = _linenr;
|
|
|
|
newToken->_fileIndex = _fileIndex;
|
|
|
|
newToken->_progressValue = _progressValue;
|
|
|
|
|
2012-02-13 17:44:08 +01:00
|
|
|
if (newToken != this) {
|
2012-11-20 02:58:19 +01:00
|
|
|
if (prepend) {
|
|
|
|
/*if (this->previous())*/ {
|
|
|
|
newToken->previous(this->previous());
|
|
|
|
newToken->previous()->next(newToken);
|
|
|
|
} /*else if (tokensFront?) {
|
|
|
|
*tokensFront? = newToken;
|
|
|
|
}*/
|
|
|
|
this->previous(newToken);
|
|
|
|
newToken->next(this);
|
|
|
|
} else {
|
|
|
|
if (this->next()) {
|
|
|
|
newToken->next(this->next());
|
|
|
|
newToken->next()->previous(newToken);
|
|
|
|
} else if (tokensBack) {
|
|
|
|
*tokensBack = newToken;
|
|
|
|
}
|
|
|
|
this->next(newToken);
|
|
|
|
newToken->previous(this);
|
2012-02-13 17:44:08 +01:00
|
|
|
}
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
void Token::eraseTokens(Token *begin, const Token *end)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2012-02-13 17:44:08 +01:00
|
|
|
if (!begin || begin == end)
|
2008-12-18 22:28:57 +01:00
|
|
|
return;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (begin->next() && begin->next() != end) {
|
2008-12-18 22:28:57 +01:00
|
|
|
begin->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-22 16:22:50 +02:00
|
|
|
void Token::createMutualLinks(Token *begin, Token *end)
|
|
|
|
{
|
2014-02-15 08:05:54 +01:00
|
|
|
assert(begin != nullptr);
|
|
|
|
assert(end != nullptr);
|
2009-08-22 16:22:50 +02:00
|
|
|
assert(begin != end);
|
|
|
|
begin->link(end);
|
|
|
|
end->link(begin);
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
void Token::printOut(const char *title) const
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2013-08-18 09:01:03 +02:00
|
|
|
if (title && title[0])
|
2012-04-16 19:51:07 +02:00
|
|
|
std::cout << "\n### " << title << " ###\n";
|
2017-08-09 20:00:26 +02:00
|
|
|
std::cout << stringifyList(true, true, true, true, true, nullptr, nullptr) << std::endl;
|
2009-11-27 23:04:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Token::printOut(const char *title, const std::vector<std::string> &fileNames) const
|
|
|
|
{
|
2013-08-18 09:01:03 +02:00
|
|
|
if (title && title[0])
|
2012-04-16 19:51:07 +02:00
|
|
|
std::cout << "\n### " << title << " ###\n";
|
2017-08-09 20:00:26 +02:00
|
|
|
std::cout << stringifyList(true, true, true, true, true, &fileNames, nullptr) << std::endl;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-02-13 07:25:29 +01:00
|
|
|
|
2014-09-29 14:50:00 +02:00
|
|
|
void Token::stringify(std::ostream& os, bool varid, bool attributes, bool macro) const
|
2011-12-13 21:42:38 +01:00
|
|
|
{
|
2012-04-16 19:51:07 +02:00
|
|
|
if (attributes) {
|
|
|
|
if (isUnsigned())
|
|
|
|
os << "unsigned ";
|
|
|
|
else if (isSigned())
|
|
|
|
os << "signed ";
|
2015-08-27 14:34:00 +02:00
|
|
|
if (isComplex())
|
|
|
|
os << "_Complex ";
|
2013-08-31 06:26:39 +02:00
|
|
|
if (isLong()) {
|
2015-08-14 20:46:13 +02:00
|
|
|
if (_tokType == eString || _tokType == eChar)
|
2013-08-31 06:26:39 +02:00
|
|
|
os << "L";
|
|
|
|
else
|
|
|
|
os << "long ";
|
|
|
|
}
|
2011-12-13 21:42:38 +01:00
|
|
|
}
|
2014-09-29 14:50:00 +02:00
|
|
|
if (macro && isExpandedMacro())
|
2013-10-13 17:42:06 +02:00
|
|
|
os << "$";
|
2015-08-14 20:46:13 +02:00
|
|
|
if (_str[0] != '\"' || _str.find('\0') == std::string::npos)
|
2012-12-25 08:56:12 +01:00
|
|
|
os << _str;
|
|
|
|
else {
|
|
|
|
for (std::size_t i = 0U; i < _str.size(); ++i) {
|
|
|
|
if (_str[i] == '\0')
|
|
|
|
os << "\\0";
|
|
|
|
else
|
|
|
|
os << _str[i];
|
|
|
|
}
|
|
|
|
}
|
2012-04-16 19:51:07 +02:00
|
|
|
if (varid && _varId != 0)
|
|
|
|
os << '@' << _varId;
|
2011-12-13 21:42:38 +01:00
|
|
|
}
|
|
|
|
|
2012-04-16 19:51:07 +02:00
|
|
|
std::string Token::stringifyList(bool varid, bool attributes, bool linenumbers, bool linebreaks, bool files, const std::vector<std::string>* fileNames, const Token* end) const
|
2009-11-27 23:04:04 +01:00
|
|
|
{
|
2012-04-16 19:51:07 +02:00
|
|
|
if (this == end)
|
|
|
|
return "";
|
2009-11-27 23:04:04 +01:00
|
|
|
|
2009-02-13 07:25:29 +01:00
|
|
|
std::ostringstream ret;
|
2009-03-03 20:45:58 +01:00
|
|
|
|
2016-07-18 10:42:03 +02:00
|
|
|
unsigned int lineNumber = _linenr - (linenumbers ? 1U : 0U);
|
2015-10-11 12:36:23 +02:00
|
|
|
unsigned int fileInd = files ? ~0U : _fileIndex;
|
2010-08-06 21:02:43 +02:00
|
|
|
std::map<int, unsigned int> lineNumbers;
|
2012-04-16 19:51:07 +02:00
|
|
|
for (const Token *tok = this; tok != end; tok = tok->next()) {
|
2009-03-03 21:17:23 +01:00
|
|
|
bool fileChange = false;
|
2015-10-11 12:36:23 +02:00
|
|
|
if (tok->_fileIndex != fileInd) {
|
|
|
|
if (fileInd != ~0U) {
|
2010-04-09 21:40:37 +02:00
|
|
|
lineNumbers[fileInd] = tok->_fileIndex;
|
2009-03-03 21:17:23 +01:00
|
|
|
}
|
|
|
|
|
2015-10-11 12:36:23 +02:00
|
|
|
fileInd = tok->_fileIndex;
|
2012-04-16 19:51:07 +02:00
|
|
|
if (files) {
|
|
|
|
ret << "\n\n##file ";
|
|
|
|
if (fileNames && fileNames->size() > tok->_fileIndex)
|
|
|
|
ret << fileNames->at(tok->_fileIndex);
|
|
|
|
else
|
|
|
|
ret << fileInd;
|
2016-07-18 10:42:03 +02:00
|
|
|
ret << '\n';
|
2012-04-16 19:51:07 +02:00
|
|
|
}
|
2009-03-03 21:17:23 +01:00
|
|
|
|
2010-04-09 21:40:37 +02:00
|
|
|
lineNumber = lineNumbers[fileInd];
|
2009-03-03 21:17:23 +01:00
|
|
|
fileChange = true;
|
|
|
|
}
|
|
|
|
|
2012-04-16 19:51:07 +02:00
|
|
|
if (linebreaks && (lineNumber != tok->linenr() || fileChange)) {
|
2015-10-11 12:36:23 +02:00
|
|
|
if (lineNumber+4 < tok->linenr() && fileInd == tok->_fileIndex) {
|
2012-08-02 20:36:54 +02:00
|
|
|
ret << '\n' << lineNumber+1 << ":\n|\n";
|
|
|
|
ret << tok->linenr()-1 << ":\n";
|
|
|
|
ret << tok->linenr() << ": ";
|
2016-07-18 10:42:03 +02:00
|
|
|
} else if (this == tok && linenumbers) {
|
|
|
|
ret << tok->linenr() << ": ";
|
2012-08-02 20:36:54 +02:00
|
|
|
} else {
|
|
|
|
while (lineNumber < tok->linenr()) {
|
|
|
|
++lineNumber;
|
|
|
|
ret << '\n';
|
|
|
|
if (linenumbers) {
|
|
|
|
ret << lineNumber << ':';
|
|
|
|
if (lineNumber == tok->linenr())
|
|
|
|
ret << ' ';
|
|
|
|
}
|
2012-04-16 19:51:07 +02:00
|
|
|
}
|
2009-03-03 21:17:23 +01:00
|
|
|
}
|
2010-04-09 21:40:37 +02:00
|
|
|
lineNumber = tok->linenr();
|
2009-02-13 07:25:29 +01:00
|
|
|
}
|
2009-03-03 21:17:23 +01:00
|
|
|
|
2014-09-29 14:50:00 +02:00
|
|
|
tok->stringify(ret, varid, attributes, attributes); // print token
|
2012-04-16 19:51:07 +02:00
|
|
|
if (tok->next() != end && (!linebreaks || (tok->next()->linenr() <= tok->linenr() && tok->next()->fileIndex() == tok->fileIndex())))
|
|
|
|
ret << ' ';
|
2009-02-13 07:25:29 +01:00
|
|
|
}
|
2016-07-18 10:42:03 +02:00
|
|
|
if (linebreaks && (files || linenumbers))
|
2012-04-16 19:51:07 +02:00
|
|
|
ret << '\n';
|
2009-02-13 07:25:29 +01:00
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
2012-04-16 19:51:07 +02:00
|
|
|
std::string Token::stringifyList(const Token* end, bool attributes) const
|
|
|
|
{
|
2017-08-09 20:00:26 +02:00
|
|
|
return stringifyList(false, attributes, false, false, false, nullptr, end);
|
2012-04-16 19:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Token::stringifyList(bool varid) const
|
|
|
|
{
|
2017-08-09 20:00:26 +02:00
|
|
|
return stringifyList(varid, false, true, true, true, nullptr, nullptr);
|
2012-04-16 19:51:07 +02:00
|
|
|
}
|
2012-12-15 20:21:09 +01:00
|
|
|
|
|
|
|
void Token::astOperand1(Token *tok)
|
|
|
|
{
|
2015-07-15 15:18:22 +02:00
|
|
|
const Token* const root = tok;
|
2014-05-19 21:54:59 +02:00
|
|
|
if (_astOperand1)
|
|
|
|
_astOperand1->_astParent = nullptr;
|
2012-12-16 11:48:19 +01:00
|
|
|
// goto parent operator
|
2014-05-19 21:54:59 +02:00
|
|
|
if (tok) {
|
2015-07-14 21:07:10 +02:00
|
|
|
while (tok->_astParent) {
|
2015-07-15 15:18:22 +02:00
|
|
|
if (tok->_astParent == this || tok->_astParent == root) // #6838/#6726 avoid hang on garbage code
|
|
|
|
throw InternalError(this, "Internal error. Token::astOperand1() cyclic dependency.");
|
2014-05-19 21:54:59 +02:00
|
|
|
tok = tok->_astParent;
|
2015-07-14 21:07:10 +02:00
|
|
|
}
|
2014-05-19 21:54:59 +02:00
|
|
|
tok->_astParent = this;
|
|
|
|
}
|
2012-12-15 20:21:09 +01:00
|
|
|
_astOperand1 = tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Token::astOperand2(Token *tok)
|
|
|
|
{
|
2015-07-15 15:18:22 +02:00
|
|
|
const Token* const root = tok;
|
2014-05-19 21:54:59 +02:00
|
|
|
if (_astOperand2)
|
|
|
|
_astOperand2->_astParent = nullptr;
|
2012-12-16 11:48:19 +01:00
|
|
|
// goto parent operator
|
2014-05-19 21:54:59 +02:00
|
|
|
if (tok) {
|
2015-07-14 21:07:10 +02:00
|
|
|
while (tok->_astParent) {
|
2015-07-15 15:18:22 +02:00
|
|
|
//std::cout << tok << " -> " << tok->_astParent ;
|
|
|
|
if (tok->_astParent == this || tok->_astParent == root) // #6838/#6726 avoid hang on garbage code
|
|
|
|
throw InternalError(this, "Internal error. Token::astOperand2() cyclic dependency.");
|
2014-05-19 21:54:59 +02:00
|
|
|
tok = tok->_astParent;
|
2015-07-14 21:07:10 +02:00
|
|
|
}
|
2014-05-19 21:54:59 +02:00
|
|
|
tok->_astParent = this;
|
|
|
|
}
|
2012-12-16 11:48:19 +01:00
|
|
|
_astOperand2 = tok;
|
2012-12-15 20:21:09 +01:00
|
|
|
}
|
|
|
|
|
2015-09-09 14:46:47 +02:00
|
|
|
bool Token::isCalculation() const
|
2013-12-28 11:02:39 +01:00
|
|
|
{
|
|
|
|
if (!Token::Match(this, "%cop%|++|--"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Token::Match(this, "*|&")) {
|
|
|
|
// dereference or address-of?
|
|
|
|
if (!this->astOperand2())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (this->astOperand2()->str() == "[")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// type specification?
|
|
|
|
std::stack<const Token *> operands;
|
|
|
|
operands.push(this);
|
|
|
|
while (!operands.empty()) {
|
|
|
|
const Token *op = operands.top();
|
|
|
|
operands.pop();
|
|
|
|
if (op->isNumber() || op->varId() > 0)
|
|
|
|
return true;
|
|
|
|
if (op->astOperand1())
|
|
|
|
operands.push(op->astOperand1());
|
|
|
|
if (op->astOperand2())
|
|
|
|
operands.push(op->astOperand2());
|
|
|
|
else if (Token::Match(op, "*|&"))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// type specification => return false
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-24 17:02:00 +02:00
|
|
|
bool Token::isUnaryPreOp() const
|
2014-07-28 14:27:35 +02:00
|
|
|
{
|
2015-05-24 17:02:00 +02:00
|
|
|
if (!astOperand1() || astOperand2())
|
2014-07-28 14:27:35 +02:00
|
|
|
return false;
|
2015-05-24 17:02:00 +02:00
|
|
|
if (!Token::Match(this, "++|--"))
|
2014-07-28 14:27:35 +02:00
|
|
|
return true;
|
2015-05-24 17:02:00 +02:00
|
|
|
const Token *tokbefore = _previous;
|
|
|
|
const Token *tokafter = _next;
|
2015-10-26 13:29:47 +01:00
|
|
|
for (int distance = 1; distance < 10 && tokbefore; distance++) {
|
2015-05-24 17:02:00 +02:00
|
|
|
if (tokbefore == _astOperand1)
|
2014-07-28 14:27:35 +02:00
|
|
|
return false;
|
2015-05-24 17:02:00 +02:00
|
|
|
if (tokafter == _astOperand1)
|
2014-07-28 14:27:35 +02:00
|
|
|
return true;
|
2015-05-24 17:02:00 +02:00
|
|
|
tokbefore = tokbefore->_previous;
|
|
|
|
tokafter = tokafter->_previous;
|
2014-07-28 14:27:35 +02:00
|
|
|
}
|
|
|
|
return false; // <- guess
|
|
|
|
}
|
|
|
|
|
2016-09-19 06:13:09 +02:00
|
|
|
static const Token* goToLeftParenthesis(const Token* start, const Token* end)
|
2014-01-17 18:37:49 +01:00
|
|
|
{
|
2015-08-12 10:55:25 +02:00
|
|
|
// move start to lpar in such expression: '(*it).x'
|
|
|
|
int par = 0;
|
2015-10-07 13:52:06 +02:00
|
|
|
for (const Token *tok = start; tok && tok != end; tok = tok->next()) {
|
2015-08-12 10:55:25 +02:00
|
|
|
if (tok->str() == "(")
|
|
|
|
++par;
|
|
|
|
else if (tok->str() == ")") {
|
|
|
|
if (par == 0)
|
|
|
|
start = tok->link();
|
|
|
|
else
|
|
|
|
--par;
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 06:13:09 +02:00
|
|
|
return start;
|
|
|
|
}
|
2015-08-12 10:55:25 +02:00
|
|
|
|
2016-09-19 06:13:09 +02:00
|
|
|
static const Token* goToRightParenthesis(const Token* start, const Token* end)
|
|
|
|
{
|
2015-08-12 10:55:25 +02:00
|
|
|
// move end to rpar in such expression: '2>(x+1)'
|
2016-09-19 06:13:09 +02:00
|
|
|
int par = 0;
|
2015-10-07 13:52:06 +02:00
|
|
|
for (const Token *tok = end; tok && tok != start; tok = tok->previous()) {
|
2015-08-12 10:55:25 +02:00
|
|
|
if (tok->str() == ")")
|
|
|
|
++par;
|
|
|
|
else if (tok->str() == "(") {
|
|
|
|
if (par == 0)
|
|
|
|
end = tok->link();
|
|
|
|
else
|
|
|
|
--par;
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 06:13:09 +02:00
|
|
|
return end;
|
|
|
|
}
|
2015-08-12 10:55:25 +02:00
|
|
|
|
2016-09-19 06:13:09 +02:00
|
|
|
static std::string stringFromTokenRange(const Token* start, const Token* end)
|
|
|
|
{
|
2017-04-30 14:22:18 +02:00
|
|
|
std::ostringstream ret;
|
2017-07-21 09:16:42 +02:00
|
|
|
if (end)
|
2017-07-21 09:17:25 +02:00
|
|
|
end = end->next();
|
2014-01-17 18:37:49 +01:00
|
|
|
for (const Token *tok = start; tok && tok != end; tok = tok->next()) {
|
2017-07-21 09:17:25 +02:00
|
|
|
if (tok->isUnsigned())
|
|
|
|
ret << "unsigned ";
|
|
|
|
if (tok->isLong())
|
|
|
|
ret << (tok->isLiteral() ? "L" : "long ");
|
2017-07-22 16:42:42 +02:00
|
|
|
if (tok->originalName().empty() || tok->isUnsigned() || tok->isLong()) {
|
2017-04-30 14:22:18 +02:00
|
|
|
ret << tok->str();
|
2017-07-21 09:16:42 +02:00
|
|
|
} else
|
|
|
|
ret << tok->originalName();
|
2015-01-31 10:50:39 +01:00
|
|
|
if (Token::Match(tok, "%name%|%num% %name%|%num%"))
|
2017-04-30 14:22:18 +02:00
|
|
|
ret << ' ';
|
2014-01-17 18:37:49 +01:00
|
|
|
}
|
2017-04-30 14:22:18 +02:00
|
|
|
return ret.str();
|
2014-01-17 18:37:49 +01:00
|
|
|
}
|
|
|
|
|
2016-09-19 06:13:09 +02:00
|
|
|
std::string Token::expressionString() const
|
|
|
|
{
|
|
|
|
const Token * const top = this;
|
|
|
|
const Token *start = top;
|
|
|
|
while (start->astOperand1() &&
|
|
|
|
(start->astOperand2() || !start->isUnaryPreOp() || Token::simpleMatch(start, "( )")))
|
|
|
|
start = start->astOperand1();
|
|
|
|
const Token *end = top;
|
|
|
|
while (end->astOperand1() && (end->astOperand2() || end->isUnaryPreOp())) {
|
|
|
|
if (Token::Match(end,"(|[") &&
|
|
|
|
!(Token::Match(end, "( %type%") && !end->astOperand2())) {
|
|
|
|
end = end->link();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end = end->astOperand2() ? end->astOperand2() : end->astOperand1();
|
|
|
|
}
|
|
|
|
|
|
|
|
start = goToLeftParenthesis(start, end);
|
|
|
|
end = goToRightParenthesis(start, end);
|
|
|
|
|
|
|
|
return stringFromTokenRange(start, end);
|
|
|
|
}
|
|
|
|
|
2014-07-14 15:51:45 +02:00
|
|
|
static void astStringXml(const Token *tok, std::size_t indent, std::ostream &out)
|
2014-07-13 17:21:45 +02:00
|
|
|
{
|
|
|
|
const std::string strindent(indent, ' ');
|
|
|
|
|
2014-07-14 15:51:45 +02:00
|
|
|
out << strindent << "<token str=\"" << tok->str() << '\"';
|
|
|
|
if (tok->varId() > 0U)
|
|
|
|
out << " varId=\"" << MathLib::toString(tok->varId()) << '\"';
|
|
|
|
if (tok->variable())
|
|
|
|
out << " variable=\"" << tok->variable() << '\"';
|
|
|
|
if (tok->function())
|
|
|
|
out << " function=\"" << tok->function() << '\"';
|
2017-03-27 18:48:34 +02:00
|
|
|
if (!tok->values().empty())
|
|
|
|
out << " values=\"" << &tok->values() << '\"';
|
2014-07-14 15:51:45 +02:00
|
|
|
|
2014-07-13 17:21:45 +02:00
|
|
|
if (!tok->astOperand1() && !tok->astOperand2()) {
|
2014-07-14 15:51:45 +02:00
|
|
|
out << "/>" << std::endl;
|
2014-07-13 17:21:45 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 15:51:45 +02:00
|
|
|
else {
|
|
|
|
out << '>' << std::endl;
|
|
|
|
if (tok->astOperand1())
|
|
|
|
astStringXml(tok->astOperand1(), indent+2U, out);
|
|
|
|
if (tok->astOperand2())
|
|
|
|
astStringXml(tok->astOperand2(), indent+2U, out);
|
|
|
|
out << strindent << "</token>" << std::endl;
|
|
|
|
}
|
2014-07-13 17:21:45 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 15:51:45 +02:00
|
|
|
void Token::printAst(bool verbose, bool xml, std::ostream &out) const
|
2013-11-02 18:37:35 +01:00
|
|
|
{
|
2015-07-21 11:54:11 +02:00
|
|
|
std::set<const Token *> printed;
|
2013-11-02 18:37:35 +01:00
|
|
|
for (const Token *tok = this; tok; tok = tok->next()) {
|
2015-07-21 11:54:11 +02:00
|
|
|
if (!tok->_astParent && tok->_astOperand1) {
|
|
|
|
if (printed.empty() && !xml)
|
2014-07-14 15:51:45 +02:00
|
|
|
out << "\n\n##AST" << std::endl;
|
2015-07-21 11:54:11 +02:00
|
|
|
else if (printed.find(tok) != printed.end())
|
|
|
|
continue;
|
|
|
|
printed.insert(tok);
|
|
|
|
|
2014-07-13 17:21:45 +02:00
|
|
|
if (xml) {
|
2014-07-14 15:51:45 +02:00
|
|
|
out << "<ast scope=\"" << tok->scope() << "\" fileIndex=\"" << tok->fileIndex() << "\" linenr=\"" << tok->linenr() << "\">" << std::endl;
|
2015-07-21 11:54:11 +02:00
|
|
|
astStringXml(tok, 2U, out);
|
2014-07-14 15:51:45 +02:00
|
|
|
out << "</ast>" << std::endl;
|
2014-07-13 17:21:45 +02:00
|
|
|
} else if (verbose)
|
2015-07-21 11:54:11 +02:00
|
|
|
out << tok->astStringVerbose(0,0) << std::endl;
|
2014-02-24 17:22:29 +01:00
|
|
|
else
|
2015-07-21 11:54:11 +02:00
|
|
|
out << tok->astString(" ") << std::endl;
|
2014-01-18 09:58:32 +01:00
|
|
|
if (tok->str() == "(")
|
|
|
|
tok = tok->link();
|
2013-11-02 18:37:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-18 09:58:32 +01:00
|
|
|
|
2014-02-24 17:22:29 +01:00
|
|
|
static std::string indent(const unsigned int indent1, const unsigned int indent2)
|
|
|
|
{
|
|
|
|
std::string ret(indent1,' ');
|
|
|
|
for (unsigned int i = indent1; i < indent2; i += 2)
|
|
|
|
ret += "| ";
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Token::astStringVerbose(const unsigned int indent1, const unsigned int indent2) const
|
|
|
|
{
|
2015-01-17 23:11:22 +01:00
|
|
|
std::string ret;
|
|
|
|
|
|
|
|
if (isExpandedMacro())
|
2016-01-01 13:54:07 +01:00
|
|
|
ret += '$';
|
|
|
|
ret += _str;
|
|
|
|
if (valuetype)
|
|
|
|
ret += " \'" + valuetype->str() + '\'';
|
|
|
|
ret += '\n';
|
2015-01-17 23:11:22 +01:00
|
|
|
|
2014-02-24 17:22:29 +01:00
|
|
|
if (_astOperand1) {
|
|
|
|
unsigned int i1 = indent1, i2 = indent2 + 2;
|
|
|
|
if (indent1==indent2 && !_astOperand2)
|
|
|
|
i1 += 2;
|
|
|
|
ret += indent(indent1,indent2) + (_astOperand2 ? "|-" : "`-") + _astOperand1->astStringVerbose(i1,i2);
|
|
|
|
}
|
|
|
|
if (_astOperand2) {
|
|
|
|
unsigned int i1 = indent1, i2 = indent2 + 2;
|
|
|
|
if (indent1==indent2)
|
|
|
|
i1 += 2;
|
|
|
|
ret += indent(indent1,indent2) + "`-" + _astOperand2->astStringVerbose(i1,i2);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-14 15:51:45 +02:00
|
|
|
void Token::printValueFlow(bool xml, std::ostream &out) const
|
2014-01-18 09:58:32 +01:00
|
|
|
{
|
2014-03-24 18:14:23 +01:00
|
|
|
unsigned int line = 0;
|
2014-07-14 15:51:45 +02:00
|
|
|
if (xml)
|
|
|
|
out << " <valueflow>" << std::endl;
|
|
|
|
else
|
|
|
|
out << "\n\n##Value flow" << std::endl;
|
2014-01-18 09:58:32 +01:00
|
|
|
for (const Token *tok = this; tok; tok = tok->next()) {
|
2017-03-27 18:48:34 +02:00
|
|
|
if (!tok->_values)
|
2014-01-18 09:58:32 +01:00
|
|
|
continue;
|
2014-07-14 15:51:45 +02:00
|
|
|
if (xml)
|
2017-03-27 18:48:34 +02:00
|
|
|
out << " <values id=\"" << tok->_values << "\">" << std::endl;
|
2014-07-14 15:51:45 +02:00
|
|
|
else if (line != tok->linenr())
|
|
|
|
out << "Line " << tok->linenr() << std::endl;
|
2014-01-18 09:58:32 +01:00
|
|
|
line = tok->linenr();
|
2015-07-26 15:36:09 +02:00
|
|
|
if (!xml) {
|
2017-03-27 18:48:34 +02:00
|
|
|
out << " " << tok->str() << (tok->_values->front().isKnown() ? " always " : " possible ");
|
|
|
|
if (tok->_values->size() > 1U)
|
2015-07-26 15:36:09 +02:00
|
|
|
out << '{';
|
|
|
|
}
|
2017-03-27 18:48:34 +02:00
|
|
|
for (std::list<ValueFlow::Value>::const_iterator it=tok->_values->begin(); it!=tok->_values->end(); ++it) {
|
2014-07-14 15:51:45 +02:00
|
|
|
if (xml) {
|
2014-08-03 20:11:22 +02:00
|
|
|
out << " <value ";
|
2016-11-13 22:59:56 +01:00
|
|
|
switch (it->valueType) {
|
|
|
|
case ValueFlow::Value::INT:
|
2017-07-22 12:19:46 +02:00
|
|
|
if (tok->valueType() && tok->valueType()->sign == ValueType::UNSIGNED)
|
|
|
|
out << "intvalue=\"" << (MathLib::biguint)it->intvalue << '\"';
|
|
|
|
else
|
|
|
|
out << "intvalue=\"" << it->intvalue << '\"';
|
2016-11-13 22:59:56 +01:00
|
|
|
break;
|
|
|
|
case ValueFlow::Value::TOK:
|
|
|
|
out << "tokvalue=\"" << it->tokvalue << '\"';
|
|
|
|
break;
|
|
|
|
case ValueFlow::Value::FLOAT:
|
|
|
|
out << "floatvalue=\"" << it->floatValue << '\"';
|
|
|
|
break;
|
2016-11-20 15:14:49 +01:00
|
|
|
case ValueFlow::Value::MOVED:
|
|
|
|
out << "movedvalue=\"" << ValueFlow::Value::toString(it->moveKind) << '\"';
|
|
|
|
break;
|
2017-04-23 18:05:14 +02:00
|
|
|
case ValueFlow::Value::UNINIT:
|
|
|
|
out << "uninit=\"1\"";
|
|
|
|
break;
|
2016-11-13 22:59:56 +01:00
|
|
|
}
|
2014-08-03 20:11:22 +02:00
|
|
|
if (it->condition)
|
2014-07-14 15:51:45 +02:00
|
|
|
out << " condition-line=\"" << it->condition->linenr() << '\"';
|
2015-07-26 15:36:09 +02:00
|
|
|
if (it->isKnown())
|
|
|
|
out << " known=\"true\"";
|
|
|
|
else if (it->isPossible())
|
|
|
|
out << " possible=\"true\"";
|
2017-09-20 22:41:36 +02:00
|
|
|
else if (it->isInconclusive())
|
|
|
|
out << " inconclusive=\"true\"";
|
2014-07-14 15:51:45 +02:00
|
|
|
out << "/>" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2017-03-27 18:48:34 +02:00
|
|
|
if (it != tok->_values->begin())
|
2014-08-03 20:11:22 +02:00
|
|
|
out << ",";
|
2016-11-13 22:59:56 +01:00
|
|
|
switch (it->valueType) {
|
|
|
|
case ValueFlow::Value::INT:
|
2017-07-22 12:19:46 +02:00
|
|
|
if (tok->valueType() && tok->valueType()->sign == ValueType::UNSIGNED)
|
|
|
|
out << (MathLib::biguint)it->intvalue;
|
|
|
|
else
|
|
|
|
out << it->intvalue;
|
2016-11-13 22:59:56 +01:00
|
|
|
break;
|
|
|
|
case ValueFlow::Value::TOK:
|
|
|
|
out << it->tokvalue->str();
|
|
|
|
break;
|
|
|
|
case ValueFlow::Value::FLOAT:
|
|
|
|
out << it->floatValue;
|
|
|
|
break;
|
2016-11-20 15:14:49 +01:00
|
|
|
case ValueFlow::Value::MOVED:
|
|
|
|
out << ValueFlow::Value::toString(it->moveKind);
|
|
|
|
break;
|
2017-04-23 18:05:14 +02:00
|
|
|
case ValueFlow::Value::UNINIT:
|
|
|
|
out << "Uninit";
|
|
|
|
break;
|
2016-11-13 22:59:56 +01:00
|
|
|
}
|
2014-07-14 15:51:45 +02:00
|
|
|
}
|
2014-01-18 09:58:32 +01:00
|
|
|
}
|
2014-07-14 15:51:45 +02:00
|
|
|
if (xml)
|
|
|
|
out << " </values>" << std::endl;
|
2017-03-27 18:48:34 +02:00
|
|
|
else if (tok->_values->size() > 1U)
|
2015-07-26 15:36:09 +02:00
|
|
|
out << '}' << std::endl;
|
2014-07-14 15:51:45 +02:00
|
|
|
else
|
2015-07-26 15:36:09 +02:00
|
|
|
out << std::endl;
|
2014-01-18 09:58:32 +01:00
|
|
|
}
|
2014-07-14 15:51:45 +02:00
|
|
|
if (xml)
|
|
|
|
out << " </valueflow>" << std::endl;
|
2014-01-18 09:58:32 +01:00
|
|
|
}
|
2014-04-02 06:49:28 +02:00
|
|
|
|
|
|
|
const ValueFlow::Value * Token::getValueLE(const MathLib::bigint val, const Settings *settings) const
|
|
|
|
{
|
2017-03-27 18:48:34 +02:00
|
|
|
if (!_values)
|
|
|
|
return nullptr;
|
2014-04-02 06:49:28 +02:00
|
|
|
const ValueFlow::Value *ret = nullptr;
|
|
|
|
std::list<ValueFlow::Value>::const_iterator it;
|
2017-03-27 18:48:34 +02:00
|
|
|
for (it = _values->begin(); it != _values->end(); ++it) {
|
2016-11-13 22:33:39 +01:00
|
|
|
if (it->isIntValue() && it->intvalue <= val) {
|
2017-09-20 22:41:36 +02:00
|
|
|
if (!ret || ret->isInconclusive() || (ret->condition && !it->isInconclusive()))
|
2014-04-02 06:49:28 +02:00
|
|
|
ret = &(*it);
|
2017-09-20 22:41:36 +02:00
|
|
|
if (!ret->isInconclusive() && !ret->condition)
|
2014-04-02 06:49:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (settings && ret) {
|
2017-09-20 22:41:36 +02:00
|
|
|
if (ret->isInconclusive() && !settings->inconclusive)
|
2014-04-02 06:49:28 +02:00
|
|
|
return nullptr;
|
2017-04-11 11:49:09 +02:00
|
|
|
if (ret->condition && !settings->isEnabled(Settings::WARNING))
|
2014-04-02 06:49:28 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueFlow::Value * Token::getValueGE(const MathLib::bigint val, const Settings *settings) const
|
|
|
|
{
|
2017-03-27 18:48:34 +02:00
|
|
|
if (!_values)
|
|
|
|
return nullptr;
|
2014-04-02 06:49:28 +02:00
|
|
|
const ValueFlow::Value *ret = nullptr;
|
|
|
|
std::list<ValueFlow::Value>::const_iterator it;
|
2017-03-27 18:48:34 +02:00
|
|
|
for (it = _values->begin(); it != _values->end(); ++it) {
|
2016-11-13 22:33:39 +01:00
|
|
|
if (it->isIntValue() && it->intvalue >= val) {
|
2017-09-20 22:41:36 +02:00
|
|
|
if (!ret || ret->isInconclusive() || (ret->condition && !it->isInconclusive()))
|
2014-04-02 06:49:28 +02:00
|
|
|
ret = &(*it);
|
2017-09-20 22:41:36 +02:00
|
|
|
if (!ret->isInconclusive() && !ret->condition)
|
2014-04-02 06:49:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (settings && ret) {
|
2017-09-20 22:41:36 +02:00
|
|
|
if (ret->isInconclusive() && !settings->inconclusive)
|
2017-04-20 22:14:54 +02:00
|
|
|
return nullptr;
|
|
|
|
if (ret->condition && !settings->isEnabled(Settings::WARNING))
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueFlow::Value * Token::getInvalidValue(const Token *ftok, unsigned int argnr, const Settings *settings) const
|
|
|
|
{
|
|
|
|
if (!_values)
|
|
|
|
return nullptr;
|
|
|
|
const ValueFlow::Value *ret = nullptr;
|
|
|
|
std::list<ValueFlow::Value>::const_iterator it;
|
|
|
|
for (it = _values->begin(); it != _values->end(); ++it) {
|
|
|
|
if (it->isIntValue() && !settings->library.isargvalid(ftok, argnr, it->intvalue)) {
|
2017-09-20 22:41:36 +02:00
|
|
|
if (!ret || ret->isInconclusive() || (ret->condition && !it->isInconclusive()))
|
2017-04-20 22:14:54 +02:00
|
|
|
ret = &(*it);
|
2017-09-20 22:41:36 +02:00
|
|
|
if (!ret->isInconclusive() && !ret->condition)
|
2017-04-20 22:14:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (settings && ret) {
|
2017-09-20 22:41:36 +02:00
|
|
|
if (ret->isInconclusive() && !settings->inconclusive)
|
2014-04-02 06:49:28 +02:00
|
|
|
return nullptr;
|
2017-04-11 11:49:09 +02:00
|
|
|
if (ret->condition && !settings->isEnabled(Settings::WARNING))
|
2014-04-02 06:49:28 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-04 08:25:10 +02:00
|
|
|
const Token *Token::getValueTokenMinStrSize() const
|
|
|
|
{
|
2017-03-27 18:48:34 +02:00
|
|
|
if (!_values)
|
|
|
|
return nullptr;
|
2014-08-04 08:25:10 +02:00
|
|
|
const Token *ret = nullptr;
|
|
|
|
std::size_t minsize = ~0U;
|
|
|
|
std::list<ValueFlow::Value>::const_iterator it;
|
2017-03-27 18:48:34 +02:00
|
|
|
for (it = _values->begin(); it != _values->end(); ++it) {
|
2016-11-13 22:33:39 +01:00
|
|
|
if (it->isTokValue() && it->tokvalue && it->tokvalue->tokType() == Token::eString) {
|
2014-08-04 08:25:10 +02:00
|
|
|
std::size_t size = getStrSize(it->tokvalue);
|
|
|
|
if (!ret || size < minsize) {
|
|
|
|
minsize = size;
|
|
|
|
ret = it->tokvalue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token *Token::getValueTokenMaxStrLength() const
|
|
|
|
{
|
2017-03-27 18:48:34 +02:00
|
|
|
if (!_values)
|
|
|
|
return nullptr;
|
2014-08-04 08:25:10 +02:00
|
|
|
const Token *ret = nullptr;
|
|
|
|
std::size_t maxlength = 0U;
|
|
|
|
std::list<ValueFlow::Value>::const_iterator it;
|
2017-03-27 18:48:34 +02:00
|
|
|
for (it = _values->begin(); it != _values->end(); ++it) {
|
2016-11-13 22:33:39 +01:00
|
|
|
if (it->isTokValue() && it->tokvalue && it->tokvalue->tokType() == Token::eString) {
|
2014-08-04 08:25:10 +02:00
|
|
|
std::size_t length = getStrLength(it->tokvalue);
|
|
|
|
if (!ret || length > maxlength) {
|
|
|
|
maxlength = length;
|
|
|
|
ret = it->tokvalue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-14 06:47:19 +02:00
|
|
|
static const Scope *getfunctionscope(const Scope *s)
|
|
|
|
{
|
|
|
|
while (s && s->type != Scope::eFunction)
|
|
|
|
s = s->nestedIn;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-08-05 06:24:23 +02:00
|
|
|
const Token *Token::getValueTokenDeadPointer() const
|
|
|
|
{
|
2014-08-14 06:47:19 +02:00
|
|
|
const Scope * const functionscope = getfunctionscope(this->scope());
|
|
|
|
|
2014-08-05 06:24:23 +02:00
|
|
|
std::list<ValueFlow::Value>::const_iterator it;
|
2017-03-27 18:48:34 +02:00
|
|
|
for (it = values().begin(); it != values().end(); ++it) {
|
2014-08-05 06:24:23 +02:00
|
|
|
// Is this a pointer alias?
|
2016-11-13 22:33:39 +01:00
|
|
|
if (!it->isTokValue() || (it->tokvalue && it->tokvalue->str() != "&"))
|
2014-08-05 06:24:23 +02:00
|
|
|
continue;
|
|
|
|
// Get variable
|
|
|
|
const Token *vartok = it->tokvalue->astOperand1();
|
|
|
|
if (!vartok || !vartok->isName() || !vartok->variable())
|
|
|
|
continue;
|
|
|
|
const Variable * const var = vartok->variable();
|
2015-01-30 19:29:37 +01:00
|
|
|
if (var->isStatic() || var->isReference())
|
2014-08-05 06:24:23 +02:00
|
|
|
continue;
|
2015-07-01 07:50:13 +02:00
|
|
|
if (!var->scope())
|
|
|
|
return nullptr; // #6804
|
2015-03-11 20:25:27 +01:00
|
|
|
if (var->scope()->type == Scope::eUnion && var->scope()->nestedIn == this->scope())
|
|
|
|
continue;
|
2014-08-14 06:47:19 +02:00
|
|
|
// variable must be in same function (not in subfunction)
|
|
|
|
if (functionscope != getfunctionscope(var->scope()))
|
|
|
|
continue;
|
|
|
|
// Is variable defined in this scope or upper scope?
|
2014-08-05 06:24:23 +02:00
|
|
|
const Scope *s = this->scope();
|
|
|
|
while ((s != nullptr) && (s != var->scope()))
|
|
|
|
s = s->nestedIn;
|
|
|
|
if (!s)
|
|
|
|
return it->tokvalue;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:48:34 +02:00
|
|
|
bool Token::addValue(const ValueFlow::Value &value)
|
|
|
|
{
|
|
|
|
if (value.isKnown() && _values) {
|
|
|
|
// Clear all other values since value is known
|
|
|
|
_values->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_values) {
|
|
|
|
// Don't handle more than 10 values for performance reasons
|
|
|
|
// TODO: add setting?
|
|
|
|
if (_values->size() >= 10U)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// if value already exists, don't add it again
|
|
|
|
std::list<ValueFlow::Value>::iterator it;
|
|
|
|
for (it = _values->begin(); it != _values->end(); ++it) {
|
|
|
|
// different intvalue => continue
|
|
|
|
if (it->intvalue != value.intvalue)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// different types => continue
|
|
|
|
if (it->valueType != value.valueType)
|
|
|
|
continue;
|
|
|
|
if (value.isTokValue() && (it->tokvalue != value.tokvalue) && (it->tokvalue->str() != value.tokvalue->str()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// same value, but old value is inconclusive so replace it
|
2017-09-20 22:41:36 +02:00
|
|
|
if (it->isInconclusive() && !value.isInconclusive()) {
|
2017-03-27 18:48:34 +02:00
|
|
|
*it = value;
|
|
|
|
if (it->varId == 0)
|
|
|
|
it->varId = _varId;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same value already exists, don't add new value
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add value
|
2017-08-01 11:33:24 +02:00
|
|
|
if (it == _values->end()) {
|
2017-03-27 18:48:34 +02:00
|
|
|
ValueFlow::Value v(value);
|
|
|
|
if (v.varId == 0)
|
|
|
|
v.varId = _varId;
|
|
|
|
_values->push_back(v);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ValueFlow::Value v(value);
|
|
|
|
if (v.varId == 0)
|
|
|
|
v.varId = _varId;
|
|
|
|
_values = new std::list<ValueFlow::Value>(1, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-20 21:55:08 +02:00
|
|
|
void Token::assignProgressValues(Token *tok)
|
|
|
|
{
|
|
|
|
unsigned int total_count = 0;
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
++total_count;
|
|
|
|
unsigned int count = 0;
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
tok2->_progressValue = count++ * 100 / total_count;
|
|
|
|
}
|
2015-10-04 19:42:58 +02:00
|
|
|
|
|
|
|
void Token::setValueType(ValueType *vt)
|
|
|
|
{
|
|
|
|
if (vt != valuetype) {
|
|
|
|
delete valuetype;
|
|
|
|
valuetype = vt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 06:02:54 +02:00
|
|
|
void Token::type(const ::Type *t)
|
|
|
|
{
|
|
|
|
_type = t;
|
|
|
|
if (t) {
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eType);
|
2016-04-22 06:02:54 +02:00
|
|
|
isEnumType(_type->isEnumType());
|
|
|
|
} else if (_tokType == eType)
|
2017-10-15 01:27:47 +02:00
|
|
|
tokType(eName);
|
2016-04-22 06:02:54 +02:00
|
|
|
}
|
|
|
|
|