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
|
2014-02-15 07:45:39 +01:00
|
|
|
* Copyright (C) 2007-2014 Daniel Marjamäki and 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"
|
2010-07-24 10:51:17 +02:00
|
|
|
#include "errorlogger.h"
|
|
|
|
#include "check.h"
|
2014-04-02 06:49:28 +02:00
|
|
|
#include "settings.h"
|
2009-08-22 16:22:50 +02:00
|
|
|
#include <cassert>
|
2008-12-18 22:28:57 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2009-01-23 21:25:13 +01:00
|
|
|
#include <cctype>
|
2009-02-13 07:25:29 +01:00
|
|
|
#include <sstream>
|
2009-03-03 21:17:23 +01:00
|
|
|
#include <map>
|
2013-12-28 11:02:39 +01:00
|
|
|
#include <stack>
|
2009-01-07 16:43:20 +01:00
|
|
|
|
2014-03-08 20:56:39 +01:00
|
|
|
bool Token::_isCPP = true;
|
|
|
|
|
2010-01-06 20:19:27 +01:00
|
|
|
Token::Token(Token **t) :
|
2010-04-15 20:08:51 +02:00
|
|
|
tokensBack(t),
|
2011-10-24 02:52:55 +02:00
|
|
|
_next(0),
|
|
|
|
_previous(0),
|
|
|
|
_link(0),
|
2012-08-11 20:47:11 +02:00
|
|
|
_scope(0),
|
2013-03-05 17:50:01 +01:00
|
|
|
_function(0), // Initialize whole union
|
2012-05-14 20:46:23 +02:00
|
|
|
_str(""),
|
|
|
|
_varId(0),
|
|
|
|
_fileIndex(0),
|
|
|
|
_linenr(0),
|
|
|
|
_progressValue(0),
|
2012-04-23 21:05:26 +02:00
|
|
|
_type(eNone),
|
2010-04-15 20:08:51 +02:00
|
|
|
_isUnsigned(false),
|
|
|
|
_isSigned(false),
|
2011-07-07 15:14:33 +02:00
|
|
|
_isPointerCompare(false),
|
2010-04-15 20:08:51 +02:00
|
|
|
_isLong(false),
|
2011-11-09 21:45:59 +01:00
|
|
|
_isStandardType(false),
|
2012-12-15 20:21:09 +01:00
|
|
|
_isExpandedMacro(false),
|
2013-08-30 06:27:46 +02:00
|
|
|
_isAttributeConstructor(false),
|
2014-03-14 05:40:17 +01:00
|
|
|
_isAttributeDestructor(false),
|
2013-08-30 06:27:46 +02:00
|
|
|
_isAttributeUnused(false),
|
2014-03-14 05:40:17 +01:00
|
|
|
_isAttributePure(false),
|
|
|
|
_isAttributeConst(false),
|
2014-04-20 20:40:55 +02:00
|
|
|
_isAttributeNothrow(false),
|
2014-02-15 08:05:54 +01:00
|
|
|
_astOperand1(nullptr),
|
|
|
|
_astOperand2(nullptr),
|
|
|
|
_astParent(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
|
|
|
{
|
2009-02-07 22:05:45 +01:00
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2011-10-23 20:38:03 +02:00
|
|
|
void Token::update_property_info()
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!_str.empty()) {
|
2012-04-23 21:05:26 +02:00
|
|
|
if (_str == "true" || _str == "false")
|
|
|
|
_type = eBoolean;
|
2014-03-18 21:41:47 +01:00
|
|
|
else if (_str[0] == '_' || std::isalpha((unsigned char)_str[0])) { // Name
|
2012-04-23 21:05:26 +02:00
|
|
|
if (_varId)
|
|
|
|
_type = eVariable;
|
2013-03-05 17:50:01 +01:00
|
|
|
else if (_type != eVariable && _type != eFunction && _type != eType)
|
|
|
|
_type = 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])))
|
2012-04-23 21:05:26 +02:00
|
|
|
_type = eNumber;
|
|
|
|
else if (_str.length() > 1 && _str[0] == '"' && _str[_str.length()-1] == '"')
|
|
|
|
_type = eString;
|
|
|
|
else if (_str.length() > 1 && _str[0] == '\'' && _str[_str.length()-1] == '\'')
|
|
|
|
_type = eChar;
|
|
|
|
else if (_str == "=" ||
|
|
|
|
_str == "+=" ||
|
|
|
|
_str == "-=" ||
|
|
|
|
_str == "*=" ||
|
|
|
|
_str == "/=" ||
|
|
|
|
_str == "%=" ||
|
|
|
|
_str == "&=" ||
|
|
|
|
_str == "^=" ||
|
|
|
|
_str == "|=" ||
|
|
|
|
_str == "<<=" ||
|
|
|
|
_str == ">>=")
|
|
|
|
_type = eAssignmentOp;
|
|
|
|
else if (_str.size() == 1 && _str.find_first_of(",[]()?:") != std::string::npos)
|
|
|
|
_type = eExtendedOp;
|
|
|
|
else if (_str=="<<" || _str==">>" || (_str.size()==1 && _str.find_first_of("+-*/%") != std::string::npos))
|
|
|
|
_type = eArithmeticalOp;
|
|
|
|
else if (_str.size() == 1 && _str.find_first_of("&|^~") != std::string::npos)
|
|
|
|
_type = eBitOp;
|
|
|
|
else if (_str == "&&" ||
|
|
|
|
_str == "||" ||
|
|
|
|
_str == "!")
|
|
|
|
_type = eLogicalOp;
|
2012-08-02 20:36:54 +02:00
|
|
|
else if ((_str == "==" ||
|
|
|
|
_str == "!=" ||
|
|
|
|
_str == "<" ||
|
|
|
|
_str == "<=" ||
|
|
|
|
_str == ">" ||
|
|
|
|
_str == ">=") && !_link)
|
2012-04-26 23:04:55 +02:00
|
|
|
_type = eComparisonOp;
|
2012-04-23 21:05:26 +02:00
|
|
|
else if (_str == "++" ||
|
|
|
|
_str == "--")
|
|
|
|
_type = eIncDecOp;
|
|
|
|
else if (_str.size() == 1 && (_str.find_first_of("{}") != std::string::npos || (_link && _str.find_first_of("<>") != std::string::npos)))
|
|
|
|
_type = eBracket;
|
2011-04-18 06:56:39 +02:00
|
|
|
else
|
2012-04-23 21:05:26 +02:00
|
|
|
_type = eOther;
|
2011-10-23 20:38:03 +02:00
|
|
|
} else {
|
2012-04-23 21:05:26 +02:00
|
|
|
_type = eNone;
|
2011-04-18 06:56:39 +02:00
|
|
|
}
|
2011-11-09 21:45:59 +01:00
|
|
|
|
|
|
|
update_property_isStandardType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Token::update_property_isStandardType()
|
|
|
|
{
|
|
|
|
_isStandardType = false;
|
|
|
|
|
|
|
|
if (_str.size() < 3)
|
|
|
|
return;
|
|
|
|
|
2014-03-28 18:24:39 +01:00
|
|
|
static const char * const stdtype[] = { "bool", "char", "char16_t", "char32_t", "double", "float", "int", "long", "short", "size_t", "void", "wchar_t"};
|
|
|
|
if (std::binary_search(stdtype, stdtype + sizeof(stdtype) / sizeof(stdtype[0]), _str)) {
|
|
|
|
_isStandardType = true;
|
|
|
|
_type = 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;
|
|
|
|
for (unsigned int i = 0; i < _str.length(); ++i) {
|
|
|
|
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
|
|
|
{
|
2012-08-02 20:36:54 +02:00
|
|
|
assert(_type == eString);
|
2009-09-12 22:54:47 +02:00
|
|
|
return _str.substr(1, _str.length() - 2);
|
|
|
|
}
|
|
|
|
|
2011-12-07 23:36:11 +01:00
|
|
|
void Token::deleteNext(unsigned long index)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2011-12-08 01:41:53 +01:00
|
|
|
while (_next && index--) {
|
2011-12-07 23:36:11 +01:00
|
|
|
Token *n = _next;
|
|
|
|
_next = n->next();
|
|
|
|
delete n;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
2009-03-13 00:07:05 +01:00
|
|
|
_str = _next->_str;
|
2012-04-23 21:05:26 +02:00
|
|
|
_type = _next->_type;
|
2011-03-08 02:04:25 +01:00
|
|
|
_isUnsigned = _next->_isUnsigned;
|
|
|
|
_isSigned = _next->_isSigned;
|
2011-07-07 15:14:33 +02:00
|
|
|
_isPointerCompare = _next->_isPointerCompare;
|
2011-03-08 02:04:25 +01:00
|
|
|
_isLong = _next->_isLong;
|
2011-11-09 21:45:59 +01:00
|
|
|
_isStandardType = _next->_isStandardType;
|
2011-12-18 13:33:23 +01:00
|
|
|
_isExpandedMacro = _next->_isExpandedMacro;
|
2013-08-30 06:27:46 +02:00
|
|
|
_isAttributeConstructor = _next->_isAttributeConstructor;
|
2014-03-14 05:40:17 +01:00
|
|
|
_isAttributeDestructor = _next->_isAttributeDestructor;
|
2013-08-30 06:27:46 +02:00
|
|
|
_isAttributeUnused = _next->_isAttributeUnused;
|
2014-03-14 05:40:17 +01:00
|
|
|
_isAttributePure = _next->_isAttributePure;
|
|
|
|
_isAttributeConst = _next->_isAttributeConst;
|
2014-04-20 20:40:55 +02:00
|
|
|
_isAttributeNothrow = _next->_isAttributeNothrow;
|
2009-03-13 00:07:05 +01:00
|
|
|
_varId = _next->_varId;
|
|
|
|
_fileIndex = _next->_fileIndex;
|
|
|
|
_linenr = _next->_linenr;
|
2009-03-13 22:25:56 +01:00
|
|
|
_link = _next->_link;
|
2012-08-11 20:47:11 +02:00
|
|
|
_scope = _next->_scope;
|
2013-01-31 06:41:18 +01:00
|
|
|
_function = _next->_function;
|
|
|
|
_variable = _next->_variable;
|
2013-09-24 06:43:03 +02:00
|
|
|
_originalName = _next->_originalName;
|
2014-01-06 16:37:52 +01:00
|
|
|
values = _next->values;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_link)
|
2009-08-28 22:31:11 +02:00
|
|
|
_link->link(this);
|
|
|
|
|
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
|
|
|
|
_str = _previous->_str;
|
2012-04-23 21:05:26 +02:00
|
|
|
_type = _previous->_type;
|
2012-02-13 17:44:08 +01:00
|
|
|
_isUnsigned = _previous->_isUnsigned;
|
|
|
|
_isSigned = _previous->_isSigned;
|
|
|
|
_isPointerCompare = _previous->_isPointerCompare;
|
|
|
|
_isLong = _previous->_isLong;
|
|
|
|
_isStandardType = _previous->_isStandardType;
|
|
|
|
_isExpandedMacro = _previous->_isExpandedMacro;
|
2013-08-30 06:27:46 +02:00
|
|
|
_isAttributeConstructor = _previous->_isAttributeConstructor;
|
2014-03-14 05:40:17 +01:00
|
|
|
_isAttributeDestructor = _previous->_isAttributeDestructor;
|
2013-08-30 06:27:46 +02:00
|
|
|
_isAttributeUnused = _previous->_isAttributeUnused;
|
2014-03-14 05:40:17 +01:00
|
|
|
_isAttributePure = _previous->_isAttributePure;
|
|
|
|
_isAttributeConst = _previous->_isAttributeConst;
|
2014-04-20 20:40:55 +02:00
|
|
|
_isAttributeNothrow = _previous->_isAttributeNothrow;
|
2012-02-13 17:44:08 +01:00
|
|
|
_varId = _previous->_varId;
|
|
|
|
_fileIndex = _previous->_fileIndex;
|
|
|
|
_linenr = _previous->_linenr;
|
|
|
|
_link = _previous->_link;
|
2012-08-11 20:47:11 +02:00
|
|
|
_scope = _previous->_scope;
|
2013-01-31 06:41:18 +01:00
|
|
|
_function = _previous->_function;
|
|
|
|
_variable = _previous->_variable;
|
2013-08-31 06:26:39 +02:00
|
|
|
_originalName = _previous->_originalName;
|
2014-01-06 16:37:52 +01:00
|
|
|
values = _previous->values;
|
2012-02-13 17:44:08 +01:00
|
|
|
if (_link)
|
|
|
|
_link->link(this);
|
|
|
|
|
|
|
|
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;
|
2009-08-13 22:13:52 +02:00
|
|
|
int num = std::abs(index);
|
2011-10-13 20:53:06 +02:00
|
|
|
while (num > 0 && tok) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (index > 0)
|
2009-03-20 18:50:11 +01:00
|
|
|
tok = tok->next();
|
|
|
|
else
|
|
|
|
tok = tok->previous();
|
|
|
|
--num;
|
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
|
|
|
{
|
2011-11-14 09:16:47 +01:00
|
|
|
static const std::string empty_str;
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
const Token *tok = this->tokAt(index);
|
2011-11-14 09:16:47 +01:00
|
|
|
return tok ? tok->_str : empty_str;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2013-08-07 16:27:37 +02:00
|
|
|
static int multiComparePercent(const Token *tok, const char ** haystack_p,
|
2011-11-05 19:24:21 +01:00
|
|
|
const char * needle,
|
|
|
|
bool emptyStringFound)
|
|
|
|
{
|
|
|
|
const char *haystack = *haystack_p;
|
|
|
|
|
2012-08-30 13:33:19 +02:00
|
|
|
if (haystack[0] == '%' && haystack[1] != '|' && haystack[1] != '\0' && haystack[1] != ' ') {
|
2011-11-05 19:24:21 +01:00
|
|
|
if (haystack[1] == 'o' && // "%op%"
|
|
|
|
haystack[2] == 'p' &&
|
|
|
|
haystack[3] == '%') {
|
2013-03-01 11:43:59 +01:00
|
|
|
if (tok->isOp())
|
2011-11-05 19:24:21 +01:00
|
|
|
return 1;
|
|
|
|
*haystack_p = haystack = haystack + 4;
|
2013-03-01 11:43:59 +01:00
|
|
|
} else if (haystack[1] == 'c' && // "%cop%"
|
|
|
|
haystack[2] == 'o' &&
|
|
|
|
haystack[3] == 'p' &&
|
|
|
|
haystack[4] == '%') {
|
|
|
|
if (tok->isConstOp())
|
|
|
|
return 1;
|
|
|
|
*haystack_p = haystack = haystack + 5;
|
2011-11-05 19:24:21 +01:00
|
|
|
} else if (haystack[1] == 'o' && // "%or%"
|
|
|
|
haystack[2] == 'r' &&
|
|
|
|
haystack[3] == '%') {
|
2011-11-06 22:12:28 +01:00
|
|
|
if (*needle == '|' && needle[1] != '|' && needle[1] != '=')
|
2011-11-05 19:24:21 +01:00
|
|
|
return 1;
|
|
|
|
*haystack_p = haystack = haystack + 4;
|
|
|
|
} else if (haystack[1] == 'o' && // "%oror%"
|
|
|
|
haystack[2] == 'r' &&
|
|
|
|
haystack[3] == 'o' &&
|
|
|
|
haystack[4] == 'r' &&
|
|
|
|
haystack[5] == '%') {
|
|
|
|
if (needle[0] == '|' && needle[1] == '|')
|
|
|
|
return 1;
|
|
|
|
*haystack_p = haystack = haystack + 6;
|
2013-05-15 13:22:25 +02:00
|
|
|
} else if (haystack[1] == 'v' && // "%var%"
|
|
|
|
haystack[2] == 'a' &&
|
|
|
|
haystack[3] == 'r' &&
|
|
|
|
haystack[4] == '%') {
|
|
|
|
if (tok->isName())
|
|
|
|
return 1;
|
|
|
|
*haystack_p = haystack = haystack + 5;
|
2011-11-05 19:24:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*haystack == '|')
|
|
|
|
*haystack_p = haystack = haystack + 1;
|
|
|
|
else if (*haystack == ' ' || *haystack == '\0')
|
|
|
|
return emptyStringFound ? 0 : -1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2013-01-09 16:53:19 +01:00
|
|
|
int Token::multiCompare(const Token *tok, const char *haystack, const char *needle)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (haystack[0] == '%' && haystack[1] == 'o') {
|
2011-09-20 19:03:19 +02:00
|
|
|
if (haystack[2] == 'p' && // "%op%|"
|
|
|
|
haystack[3] == '%' &&
|
2011-10-13 20:53:06 +02:00
|
|
|
haystack[4] == '|') {
|
2011-04-10 08:39:55 +02:00
|
|
|
haystack = haystack + 5;
|
2013-03-01 11:43:59 +01:00
|
|
|
if (tok->isOp())
|
2011-04-10 08:39:55 +02:00
|
|
|
return 1;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (haystack[2] == 'r' && // "%or%|"
|
|
|
|
haystack[3] == '%' &&
|
|
|
|
haystack[4] == '|') {
|
2011-04-10 08:39:55 +02:00
|
|
|
haystack = haystack + 5;
|
2011-11-06 22:12:28 +01:00
|
|
|
if (*needle == '|' && needle[1] != '|' && needle[1] != '=')
|
2011-04-10 08:39:55 +02:00
|
|
|
return 1;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (haystack[2] == 'r' && // "%oror%|"
|
|
|
|
haystack[3] == 'o' &&
|
|
|
|
haystack[4] == 'r' &&
|
|
|
|
haystack[5] == '%' &&
|
|
|
|
haystack[6] == '|') {
|
2011-04-10 08:39:55 +02:00
|
|
|
haystack = haystack + 7;
|
|
|
|
if (needle[0] == '|' && needle[1] == '|')
|
|
|
|
return 1;
|
|
|
|
}
|
2013-03-01 11:43:59 +01:00
|
|
|
} else if (haystack[0] == '%' && haystack[1] == 'c' && haystack[2] == 'o' && // "%cop%|"
|
|
|
|
haystack[3] == 'p' && haystack[4] == '%' &&
|
|
|
|
haystack[5] == '|') {
|
|
|
|
haystack = haystack + 6;
|
|
|
|
if (tok->isConstOp())
|
|
|
|
return 1;
|
2011-04-09 18:09:13 +02:00
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
bool emptyStringFound = false;
|
2009-01-27 20:30:01 +01:00
|
|
|
const char *needlePointer = needle;
|
2012-03-25 11:51:59 +02:00
|
|
|
for (;;) {
|
2011-10-13 20:53:06 +02:00
|
|
|
if (*needlePointer == *haystack) {
|
2010-09-19 15:14:13 +02:00
|
|
|
if (*needlePointer == '\0')
|
|
|
|
return 1;
|
|
|
|
++needlePointer;
|
|
|
|
++haystack;
|
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;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (needlePointer == needle) {
|
2009-01-27 20:30:01 +01:00
|
|
|
// If needlePointer was not increased at all, we had a empty
|
|
|
|
// string in the haystack
|
2008-12-18 22:28:57 +01:00
|
|
|
emptyStringFound = true;
|
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;
|
2011-11-05 19:24:21 +01:00
|
|
|
|
2013-01-09 16:53:19 +01:00
|
|
|
int ret = multiComparePercent(tok, &haystack, needle, emptyStringFound);
|
2011-11-05 19:24:21 +01:00
|
|
|
if (ret < 2)
|
|
|
|
return ret;
|
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') {
|
2010-09-19 15:14:13 +02:00
|
|
|
return emptyStringFound ? 0 : -1;
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2010-09-19 15:14:13 +02:00
|
|
|
++haystack;
|
2011-04-09 18:09:13 +02:00
|
|
|
|
2013-01-09 16:53:19 +01:00
|
|
|
int ret = multiComparePercent(tok, &haystack, needle, emptyStringFound);
|
2011-11-05 19:24:21 +01:00
|
|
|
if (ret < 2)
|
|
|
|
return ret;
|
2009-01-27 20:30:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 15:14:13 +02:00
|
|
|
if (*needlePointer == '\0')
|
|
|
|
return 1;
|
|
|
|
|
2009-01-27 20:30:01 +01:00
|
|
|
// If empty string was found earlier from the haystack
|
2010-04-02 07:30:58 +02:00
|
|
|
if (emptyStringFound)
|
2008-12-18 22:28:57 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2008-12-23 22:45:47 +01:00
|
|
|
const char *current, *next;
|
|
|
|
|
|
|
|
current = pattern;
|
2012-12-27 11:51:12 +01:00
|
|
|
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) {
|
2012-07-08 23:39:46 +02:00
|
|
|
std::size_t length = static_cast<std::size_t>(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)
|
2009-08-16 22:28:17 +02:00
|
|
|
return 0;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (*str == c)
|
2009-08-16 22:28:17 +02:00
|
|
|
return str;
|
|
|
|
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Token::firstWordLen(const char *str)
|
|
|
|
{
|
|
|
|
int len = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (;;) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (*str == ' ' || *str == 0)
|
2009-08-16 22:28:17 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
++len;
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-12-01 23:58:01 +01:00
|
|
|
#define multicompare(p,cond,ismulticomp) \
|
|
|
|
{ \
|
|
|
|
if (!(cond)) { \
|
|
|
|
if (*(p) != '|') \
|
|
|
|
return false; \
|
|
|
|
++(p); \
|
2014-04-02 07:25:13 +02:00
|
|
|
(ismulticomp) = (*(p) && *(p) != ' '); \
|
2012-12-01 23:58:01 +01:00
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
if (*(p) == '|') { \
|
|
|
|
while (*(p) && *(p) != ' ') \
|
|
|
|
++(p); \
|
|
|
|
} \
|
2014-04-02 07:25:13 +02:00
|
|
|
(ismulticomp) = false; \
|
2012-11-17 20:25:12 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2012-11-17 20:25:12 +01:00
|
|
|
bool ismulticomp = false;
|
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;
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
// Compare the first character of the string for optimization reasons
|
|
|
|
// before doing more detailed checks.
|
2012-11-25 15:13:41 +01:00
|
|
|
if (p[0] == '%') {
|
|
|
|
++p;
|
|
|
|
switch (p[0]) {
|
|
|
|
case '\0':
|
|
|
|
case ' ':
|
2012-12-01 00:47:07 +01:00
|
|
|
case '|':
|
2012-11-25 15:13:41 +01:00
|
|
|
//simple '%' character
|
|
|
|
{
|
|
|
|
multicompare(p, tok->str() == "%", ismulticomp);
|
|
|
|
}
|
|
|
|
break;
|
2011-03-22 08:15:12 +01:00
|
|
|
case 'v':
|
|
|
|
// TODO: %var% should match only for
|
|
|
|
// variables that have varId != 0, but that needs a lot of
|
|
|
|
// work, before that change can be made.
|
|
|
|
// Any symbolname..
|
2012-11-25 15:13:41 +01:00
|
|
|
if (p[3] == '%') { // %var%
|
|
|
|
p += 4;
|
2012-11-17 20:25:12 +01:00
|
|
|
multicompare(p,tok->isName(),ismulticomp);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else { // %varid%
|
|
|
|
if (varid == 0) {
|
2012-01-08 21:19:44 +01:00
|
|
|
throw InternalError(tok, "Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers");
|
2011-03-22 08:15:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->varId() != varid)
|
|
|
|
return false;
|
|
|
|
|
2012-11-25 15:13:41 +01:00
|
|
|
p += 6;
|
2011-03-22 08:15:12 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
// Type (%type%)
|
2009-06-10 23:12:26 +02:00
|
|
|
{
|
2012-11-25 15:13:41 +01:00
|
|
|
p += 5;
|
2014-03-08 20:56:39 +01:00
|
|
|
multicompare(p, tok->isName() && tok->varId() == 0 && (tok->str() != "delete" || !Token::isCPP()), ismulticomp);
|
2009-06-10 23:12:26 +02:00
|
|
|
}
|
2011-03-22 08:15:12 +01:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
// Accept any token (%any%)
|
2008-12-23 21:17:05 +01:00
|
|
|
{
|
2012-11-25 15:13:41 +01:00
|
|
|
p += 4;
|
|
|
|
if (p[0] == '|') {
|
2012-11-17 20:25:12 +01:00
|
|
|
while (*p && *p != ' ')
|
|
|
|
++p;
|
2012-11-25 15:13:41 +01:00
|
|
|
}
|
2012-11-17 20:25:12 +01:00
|
|
|
ismulticomp = false;
|
2008-12-23 21:17:05 +01:00
|
|
|
}
|
2011-03-22 08:15:12 +01:00
|
|
|
break;
|
|
|
|
case 'n':
|
2012-05-29 06:19:22 +02:00
|
|
|
// Number (%num%)
|
2008-12-23 21:17:05 +01:00
|
|
|
{
|
2012-11-25 15:13:41 +01:00
|
|
|
p += 4;
|
2012-11-17 20:25:12 +01:00
|
|
|
multicompare(p,tok->isNumber(),ismulticomp);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2011-03-22 08:15:12 +01:00
|
|
|
break;
|
2012-12-01 03:17:55 +01:00
|
|
|
case 'c': {
|
2012-12-01 00:47:07 +01:00
|
|
|
p += 1;
|
|
|
|
// Character (%char%)
|
|
|
|
if (p[0] == 'h') {
|
|
|
|
p += 4;
|
|
|
|
multicompare(p,tok->type() == eChar,ismulticomp);
|
|
|
|
}
|
2013-03-01 11:43:59 +01:00
|
|
|
// Const operator (%cop%)
|
|
|
|
else if (p[1] == 'p') {
|
|
|
|
p += 3;
|
|
|
|
multicompare(p,tok->isConstOp(),ismulticomp);
|
|
|
|
}
|
2012-12-01 00:47:07 +01:00
|
|
|
// Comparison operator (%comp%)
|
|
|
|
else {
|
|
|
|
p += 4;
|
|
|
|
multicompare(p,tok->isComparisonOp(),ismulticomp);
|
|
|
|
}
|
2012-11-04 16:34:32 +01:00
|
|
|
}
|
|
|
|
break;
|
2011-03-22 08:15:12 +01:00
|
|
|
case 's':
|
|
|
|
// String (%str%)
|
2008-12-23 21:17:05 +01:00
|
|
|
{
|
2012-11-25 15:13:41 +01:00
|
|
|
p += 4;
|
2012-11-20 00:16:53 +01:00
|
|
|
multicompare(p,tok->type() == eString,ismulticomp);
|
2010-09-20 20:15:07 +02:00
|
|
|
}
|
2011-03-22 08:15:12 +01:00
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
// Bool (%bool%)
|
2010-10-25 03:14:21 +02:00
|
|
|
{
|
2012-11-25 15:13:41 +01:00
|
|
|
p += 5;
|
2012-11-17 20:25:12 +01:00
|
|
|
multicompare(p,tok->isBoolean(),ismulticomp);
|
2010-10-25 03:14:21 +02:00
|
|
|
}
|
2011-03-22 08:15:12 +01:00
|
|
|
break;
|
2012-12-01 03:17:55 +01:00
|
|
|
case 'o': {
|
2012-11-25 15:13:41 +01:00
|
|
|
++p;
|
|
|
|
if (p[1] == '%') {
|
2012-11-19 16:30:07 +01:00
|
|
|
// Op (%op%)
|
2012-11-25 15:13:41 +01:00
|
|
|
if (p[0] == 'p') {
|
2012-11-17 20:25:12 +01:00
|
|
|
p += 2;
|
2013-03-01 11:43:59 +01:00
|
|
|
multicompare(p,tok->isOp(),ismulticomp);
|
2012-11-25 15:13:41 +01:00
|
|
|
}
|
|
|
|
// Or (%or%)
|
|
|
|
else {
|
|
|
|
p += 2;
|
|
|
|
multicompare(p,tok->str() == "|",ismulticomp)
|
2011-04-10 08:39:55 +02:00
|
|
|
}
|
2011-03-22 08:15:12 +01:00
|
|
|
}
|
2011-04-10 08:39:55 +02:00
|
|
|
|
2011-03-22 08:15:12 +01:00
|
|
|
// Oror (%oror%)
|
2012-11-25 15:13:41 +01:00
|
|
|
else {
|
|
|
|
p += 4;
|
2012-11-17 20:25:12 +01:00
|
|
|
multicompare(p,tok->str() == "||",ismulticomp);
|
2011-04-10 08:39:55 +02:00
|
|
|
}
|
2012-12-01 00:47:07 +01:00
|
|
|
}
|
|
|
|
break;
|
2011-03-22 08:15:12 +01:00
|
|
|
default:
|
2012-11-25 15:13:41 +01:00
|
|
|
//unknown %cmd%, abort
|
2012-12-27 11:51:12 +01:00
|
|
|
std::abort();
|
2010-09-20 20:15:07 +02:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2012-11-17 20:25:12 +01:00
|
|
|
else if (ismulticomp) {
|
|
|
|
ismulticomp = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
// [.. => search for a one-character token..
|
2011-10-13 20:53:06 +02:00
|
|
|
else 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
|
|
|
}
|
|
|
|
|
|
|
|
// Parse multi options, such as void|int|char (accept token which is one of these 3)
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (chrInFirstWord(p, '|') && (p[0] != '|' || firstWordLen(p) > 2)) {
|
2013-01-09 16:53:19 +01:00
|
|
|
int res = multiCompare(tok, p, tok->_str.c_str());
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse "not" options. Token can be anything except the given one
|
2012-11-20 00:16:53 +01:00
|
|
|
else if (p[0] == '!' && p[1] == '!' && p[2] != '\0') {
|
|
|
|
p += 2;
|
|
|
|
if (firstWordEquals(p, tok->str().c_str()))
|
2008-12-18 22:28:57 +01:00
|
|
|
return false;
|
2012-11-20 00:16:53 +01:00
|
|
|
while (*p && *p != ' ')
|
|
|
|
++p;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2012-04-26 16:39:16 +02:00
|
|
|
else if (!firstWordEquals(p, tok->_str.c_str())) {
|
2008-12-18 22:28:57 +01:00
|
|
|
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);
|
2009-08-30 13:07:10 +02:00
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
std::size_t len = 0;
|
2009-09-13 12:50:21 +02:00
|
|
|
const std::string strValue(tok->strValue());
|
|
|
|
const char *str = strValue.c_str();
|
2009-08-30 13:07:10 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (*str) {
|
|
|
|
if (*str == '\\') {
|
2009-08-30 13:07:10 +02:00
|
|
|
++str;
|
2009-09-26 17:58:14 +02:00
|
|
|
|
|
|
|
// string ends at '\0'
|
2010-04-02 07:30:58 +02:00
|
|
|
if (*str == '0')
|
2009-09-26 17:58:14 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-30 13:07:10 +02:00
|
|
|
++str;
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
const std::string strValue(tok->strValue());
|
|
|
|
const char *str = strValue.c_str();
|
|
|
|
|
|
|
|
while (*str) {
|
|
|
|
if (index == 0) {
|
|
|
|
std::string ret;
|
|
|
|
if (*str == '\\') {
|
|
|
|
ret = *str;
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
ret += *str;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*str == '\\')
|
|
|
|
++str;
|
|
|
|
++str;
|
|
|
|
--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();
|
2011-10-23 11:23:48 +02:00
|
|
|
else if (tok->str() == "(" || tok->str() == "{" || tok->str() == "[")
|
|
|
|
tok = tok->link();
|
2012-04-16 15:28:38 +02:00
|
|
|
else if (tok->str() == "<" && tok->link())
|
|
|
|
tok = tok->link();
|
2011-10-28 18:57:10 +02:00
|
|
|
else if (tok->str() == ")" || tok->str() == ";")
|
|
|
|
return 0;
|
2011-10-23 11:23:48 +02:00
|
|
|
}
|
2011-10-28 18:57:10 +02:00
|
|
|
return 0;
|
2011-10-23 11:23:48 +02: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()) {
|
2012-04-18 16:02:03 +02:00
|
|
|
if (closing->str() == "{" || closing->str() == "[" || closing->str() == "(")
|
|
|
|
closing = closing->link();
|
|
|
|
else if (closing->str() == "}" || closing->str() == "]" || closing->str() == ")" || closing->str() == ";" || closing->str() == "=")
|
2013-07-31 10:30:20 +02:00
|
|
|
break;
|
2012-04-18 16:02:03 +02:00
|
|
|
else if (closing->str() == "<")
|
|
|
|
++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
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-10-27 10:54:50 +02:00
|
|
|
const Token *Token::findsimplematch(const Token *tok, const char pattern[])
|
|
|
|
{
|
|
|
|
for (; tok; tok = tok->next()) {
|
|
|
|
if (Token::simpleMatch(tok, pattern))
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token *Token::findsimplematch(const Token *tok, const char pattern[], const Token *end)
|
|
|
|
{
|
|
|
|
for (; tok && tok != end; tok = tok->next()) {
|
|
|
|
if (Token::simpleMatch(tok, pattern))
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
const Token *Token::findmatch(const Token *tok, const char pattern[], unsigned int varId)
|
2009-01-04 20:55:12 +01:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
for (; 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;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-26 16:46:37 +02:00
|
|
|
const Token *Token::findmatch(const Token *tok, const char pattern[], const Token *end, unsigned int varId)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
for (; tok && tok != end; tok = tok->next()) {
|
2010-07-26 16:46:37 +02:00
|
|
|
if (Token::Match(tok, pattern, varId))
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-20 02:58:19 +01:00
|
|
|
void Token::insertToken(const std::string &tokenStr, bool prepend)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2012-02-13 17:44:08 +01:00
|
|
|
Token *newToken;
|
2012-11-20 02:58:19 +01:00
|
|
|
|
|
|
|
//TODO: Find a solution for the first token on the list
|
|
|
|
if (prepend && !this->previous())
|
|
|
|
return;
|
|
|
|
|
2012-04-16 15:28:38 +02:00
|
|
|
if (_str.empty())
|
2012-02-13 17:44:08 +01:00
|
|
|
newToken = this;
|
|
|
|
else
|
|
|
|
newToken = new Token(tokensBack);
|
2010-04-09 21:40:37 +02:00
|
|
|
newToken->str(tokenStr);
|
2008-12-16 18:05:43 +01:00
|
|
|
newToken->_linenr = _linenr;
|
2008-12-18 22:28:57 +01:00
|
|
|
newToken->_fileIndex = _fileIndex;
|
2010-08-03 16:36:21 +02:00
|
|
|
newToken->_progressValue = _progressValue;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2013-09-24 06:43:03 +02:00
|
|
|
if (newToken != this) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Token::insertToken(const std::string &tokenStr, const std::string &originalNameStr, bool prepend)
|
|
|
|
{
|
|
|
|
Token *newToken;
|
|
|
|
|
|
|
|
//TODO: Find a solution for the first token on the list
|
|
|
|
if (prepend && !this->previous())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_str.empty())
|
|
|
|
newToken = this;
|
|
|
|
else
|
|
|
|
newToken = new Token(tokensBack);
|
|
|
|
newToken->str(tokenStr);
|
|
|
|
newToken->_originalName = originalNameStr;
|
|
|
|
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";
|
|
|
|
std::cout << stringifyList(true, true, true, true, true, 0, 0) << 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";
|
|
|
|
std::cout << stringifyList(true, true, true, true, true, &fileNames, 0) << std::endl;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-02-13 07:25:29 +01:00
|
|
|
|
2012-04-16 19:51:07 +02:00
|
|
|
void Token::stringify(std::ostream& os, bool varid, bool attributes) 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 ";
|
2013-08-31 06:26:39 +02:00
|
|
|
if (isLong()) {
|
2013-09-06 05:36:33 +02:00
|
|
|
if (_type == eString || _type == eChar)
|
2013-08-31 06:26:39 +02:00
|
|
|
os << "L";
|
|
|
|
else
|
|
|
|
os << "long ";
|
|
|
|
}
|
2011-12-13 21:42:38 +01:00
|
|
|
}
|
2013-10-13 17:42:06 +02:00
|
|
|
if (isExpandedMacro())
|
|
|
|
os << "$";
|
2012-12-25 08:56:12 +01:00
|
|
|
if (_str[0] != '\"' || _str.find("\0") == std::string::npos)
|
|
|
|
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
|
|
|
|
2012-04-16 19:51:07 +02:00
|
|
|
unsigned int lineNumber = _linenr;
|
2013-02-10 07:43:09 +01:00
|
|
|
int fileInd = files ? -1 : static_cast<int>(_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;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (static_cast<int>(tok->_fileIndex) != fileInd) {
|
|
|
|
if (fileInd != -1) {
|
2010-04-09 21:40:37 +02:00
|
|
|
lineNumbers[fileInd] = tok->_fileIndex;
|
2009-03-03 21:17:23 +01:00
|
|
|
}
|
|
|
|
|
2010-04-09 21:40:37 +02:00
|
|
|
fileInd = static_cast<int>(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;
|
|
|
|
}
|
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)) {
|
2012-08-02 20:36:54 +02:00
|
|
|
if (lineNumber+4 < tok->linenr() && fileInd == static_cast<int>(tok->_fileIndex)) {
|
|
|
|
ret << '\n' << lineNumber+1 << ":\n|\n";
|
|
|
|
ret << tok->linenr()-1 << ":\n";
|
|
|
|
ret << tok->linenr() << ": ";
|
|
|
|
} 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
|
|
|
|
2012-04-16 19:51:07 +02:00
|
|
|
tok->stringify(ret, varid, attributes); // print token
|
|
|
|
if (tok->next() != end && (!linebreaks || (tok->next()->linenr() <= tok->linenr() && tok->next()->fileIndex() == tok->fileIndex())))
|
|
|
|
ret << ' ';
|
2009-02-13 07:25:29 +01:00
|
|
|
}
|
2012-04-16 19:51:07 +02:00
|
|
|
if (linebreaks && files)
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return stringifyList(false, attributes, false, false, false, 0, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Token::stringifyList(bool varid) const
|
|
|
|
{
|
|
|
|
return stringifyList(varid, false, true, true, true, 0, 0);
|
|
|
|
}
|
2012-12-15 20:21:09 +01:00
|
|
|
|
|
|
|
void Token::astOperand1(Token *tok)
|
|
|
|
{
|
2012-12-16 11:48:19 +01:00
|
|
|
// goto parent operator
|
|
|
|
while (tok->_astParent)
|
2012-12-16 08:41:04 +01:00
|
|
|
tok = tok->_astParent;
|
2012-12-15 20:21:09 +01:00
|
|
|
tok->_astParent = this;
|
|
|
|
_astOperand1 = tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Token::astOperand2(Token *tok)
|
|
|
|
{
|
2012-12-16 11:48:19 +01:00
|
|
|
// goto parent operator
|
|
|
|
while (tok->_astParent)
|
2012-12-16 08:41:04 +01:00
|
|
|
tok = tok->_astParent;
|
2012-12-15 20:21:09 +01:00
|
|
|
tok->_astParent = this;
|
2012-12-16 11:48:19 +01:00
|
|
|
_astOperand2 = tok;
|
2012-12-15 20:21:09 +01:00
|
|
|
}
|
|
|
|
|
2013-12-28 11:02:39 +01:00
|
|
|
bool Token::isCalculation() const
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-17 18:37:49 +01:00
|
|
|
std::string Token::expressionString() const
|
|
|
|
{
|
|
|
|
const Token * const top = this;
|
|
|
|
const Token *start = top;
|
|
|
|
while (start->astOperand1() && start->astOperand2())
|
|
|
|
start = start->astOperand1();
|
|
|
|
const Token *end = top;
|
|
|
|
while (end->astOperand1() && end->astOperand2())
|
|
|
|
end = end->astOperand2();
|
|
|
|
std::string ret;
|
|
|
|
for (const Token *tok = start; tok && tok != end; tok = tok->next()) {
|
|
|
|
ret += tok->str();
|
|
|
|
if (Token::Match(tok, "%var%|%num% %var%|%num%"))
|
|
|
|
ret += " ";
|
|
|
|
}
|
|
|
|
return ret + end->str();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-24 17:22:29 +01:00
|
|
|
void Token::printAst(bool verbose) const
|
2013-11-02 18:37:35 +01:00
|
|
|
{
|
|
|
|
bool title = false;
|
|
|
|
|
|
|
|
bool print = true;
|
|
|
|
for (const Token *tok = this; tok; tok = tok->next()) {
|
|
|
|
if (print && tok->_astOperand1) {
|
|
|
|
if (!title)
|
|
|
|
std::cout << "\n\n##AST" << std::endl;
|
|
|
|
title = true;
|
2014-02-24 17:22:29 +01:00
|
|
|
if (verbose)
|
|
|
|
std::cout << tok->astTop()->astStringVerbose(0,0) << std::endl;
|
|
|
|
else
|
|
|
|
std::cout << tok->astTop()->astString(" ") << std::endl;
|
2013-11-02 18:37:35 +01:00
|
|
|
print = false;
|
2014-01-18 09:58:32 +01:00
|
|
|
if (tok->str() == "(")
|
|
|
|
tok = tok->link();
|
2013-11-02 18:37:35 +01:00
|
|
|
}
|
|
|
|
if (Token::Match(tok, "[;{}]"))
|
|
|
|
print = true;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
std::string ret = _str + "\n";
|
|
|
|
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-01-18 09:58:32 +01:00
|
|
|
void Token::printValueFlow() const
|
|
|
|
{
|
2014-03-24 18:14:23 +01:00
|
|
|
unsigned int line = 0;
|
2014-01-18 09:58:32 +01:00
|
|
|
std::cout << "\n\n##Value flow" << std::endl;
|
|
|
|
for (const Token *tok = this; tok; tok = tok->next()) {
|
|
|
|
if (tok->values.empty())
|
|
|
|
continue;
|
|
|
|
if (line != tok->linenr())
|
|
|
|
std::cout << "Line " << tok->linenr() << std::endl;
|
|
|
|
line = tok->linenr();
|
|
|
|
std::cout << " " << tok->str() << ":{";
|
|
|
|
for (std::list<ValueFlow::Value>::const_iterator it=tok->values.begin(); it!=tok->values.end(); ++it) {
|
|
|
|
if (it != tok->values.begin())
|
|
|
|
std::cout << ",";
|
|
|
|
std::cout << it->intvalue;
|
|
|
|
}
|
|
|
|
std::cout << "}" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 06:49:28 +02:00
|
|
|
|
|
|
|
const ValueFlow::Value * Token::getValueLE(const MathLib::bigint val, const Settings *settings) const
|
|
|
|
{
|
|
|
|
const ValueFlow::Value *ret = nullptr;
|
|
|
|
std::list<ValueFlow::Value>::const_iterator it;
|
|
|
|
for (it = values.begin(); it != values.end(); ++it) {
|
|
|
|
if (it->intvalue <= val) {
|
|
|
|
if (!ret || ret->inconclusive || (ret->condition && !it->inconclusive))
|
|
|
|
ret = &(*it);
|
|
|
|
if (!ret->inconclusive && !ret->condition)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (settings && ret) {
|
|
|
|
if (ret->inconclusive && !settings->inconclusive)
|
|
|
|
return nullptr;
|
|
|
|
if (ret->condition && !settings->isEnabled("warning"))
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueFlow::Value * Token::getValueGE(const MathLib::bigint val, const Settings *settings) const
|
|
|
|
{
|
|
|
|
const ValueFlow::Value *ret = nullptr;
|
|
|
|
std::list<ValueFlow::Value>::const_iterator it;
|
|
|
|
for (it = values.begin(); it != values.end(); ++it) {
|
|
|
|
if (it->intvalue >= val) {
|
|
|
|
if (!ret || ret->inconclusive || (ret->condition && !it->inconclusive))
|
|
|
|
ret = &(*it);
|
|
|
|
if (!ret->inconclusive && !ret->condition)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (settings && ret) {
|
|
|
|
if (ret->inconclusive && !settings->inconclusive)
|
|
|
|
return nullptr;
|
|
|
|
if (ret->condition && !settings->isEnabled("warning"))
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|