cppcheck/lib/token.cpp

1179 lines
33 KiB
C++
Raw Normal View History

2008-12-18 22:28:57 +01:00
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2013 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-12-18 22:28:57 +01:00
*/
#include "token.h"
#include "errorlogger.h"
#include "check.h"
#include <cassert>
2008-12-18 22:28:57 +01:00
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <cctype>
#include <sstream>
#include <map>
Token::Token(Token **t) :
2010-04-15 20:08:51 +02:00
tokensBack(t),
_next(0),
_previous(0),
_link(0),
_scope(0),
_function(0), // Initialize whole union
_str(""),
_varId(0),
_fileIndex(0),
_linenr(0),
_progressValue(0),
_type(eNone),
2010-04-15 20:08:51 +02:00
_isUnsigned(false),
_isSigned(false),
_isPointerCompare(false),
2010-04-15 20:08:51 +02:00
_isLong(false),
_isStandardType(false),
_isExpandedMacro(false),
_isAttributeConstructor(false),
_isAttributeUnused(false),
_astOperand1(NULL),
_astOperand2(NULL),
_astParent(NULL)
2008-12-18 22:28:57 +01:00
{
}
Token::~Token()
2008-12-18 22:28:57 +01:00
{
2008-12-18 22:28:57 +01: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()) {
if (_str == "true" || _str == "false")
_type = eBoolean;
else if (_str[0] == '_' || std::isalpha(_str[0])) { // Name
if (_varId)
_type = eVariable;
else if (_type != eVariable && _type != eFunction && _type != eType)
_type = eName;
} else if (std::isdigit(_str[0]) || (_str.length() > 1 && _str[0] == '-' && std::isdigit(_str[1])))
_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;
else if ((_str == "==" ||
_str == "!=" ||
_str == "<" ||
_str == "<=" ||
_str == ">" ||
_str == ">=") && !_link)
_type = eComparisonOp;
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;
else
_type = eOther;
} else {
_type = eNone;
}
update_property_isStandardType();
}
void Token::update_property_isStandardType()
{
_isStandardType = false;
if (_str.size() < 3)
return;
static const char * const stdtype[] = {"int", "char", "bool", "long", "short", "float", "double", "wchar_t", "size_t", "void", 0};
for (int i = 0; stdtype[i]; i++) {
if (_str == stdtype[i]) {
_isStandardType = true;
_type = eType;
break;
}
}
}
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;
}
void Token::str(const std::string &s)
{
_str = s;
2008-12-18 22:28:57 +01:00
_varId = 0;
update_property_info();
2008-12-18 22:28:57 +01:00
}
void Token::concatStr(std::string const& b)
{
_str.erase(_str.length() - 1);
_str.append(b.begin() + 1, b.end());
update_property_info();
}
std::string Token::strValue() const
{
assert(_type == eString);
return _str.substr(1, _str.length() - 2);
}
void Token::deleteNext(unsigned long index)
2008-12-18 22:28:57 +01:00
{
while (_next && index--) {
Token *n = _next;
_next = n->next();
delete n;
}
if (_next)
2008-12-18 22:28:57 +01:00
_next->previous(this);
else if (tokensBack)
*tokensBack = this;
2008-12-18 22:28:57 +01:00
}
void Token::deleteThis()
{
if (_next) { // Copy next to this and delete next
_str = _next->_str;
_type = _next->_type;
2011-03-08 02:04:25 +01:00
_isUnsigned = _next->_isUnsigned;
_isSigned = _next->_isSigned;
_isPointerCompare = _next->_isPointerCompare;
2011-03-08 02:04:25 +01:00
_isLong = _next->_isLong;
_isStandardType = _next->_isStandardType;
_isExpandedMacro = _next->_isExpandedMacro;
_isAttributeConstructor = _next->_isAttributeConstructor;
_isAttributeUnused = _next->_isAttributeUnused;
_varId = _next->_varId;
_fileIndex = _next->_fileIndex;
_linenr = _next->_linenr;
_link = _next->_link;
_scope = _next->_scope;
_function = _next->_function;
_variable = _next->_variable;
_originalName = _next->_originalName;
if (_link)
_link->link(this);
deleteNext();
} else if (_previous && _previous->_previous) { // Copy previous to this and delete previous
_str = _previous->_str;
_type = _previous->_type;
_isUnsigned = _previous->_isUnsigned;
_isSigned = _previous->_isSigned;
_isPointerCompare = _previous->_isPointerCompare;
_isLong = _previous->_isLong;
_isStandardType = _previous->_isStandardType;
_isExpandedMacro = _previous->_isExpandedMacro;
_isAttributeConstructor = _previous->_isAttributeConstructor;
_isAttributeUnused = _previous->_isAttributeUnused;
_varId = _previous->_varId;
_fileIndex = _previous->_fileIndex;
_linenr = _previous->_linenr;
_link = _previous->_link;
_scope = _previous->_scope;
_function = _previous->_function;
_variable = _previous->_variable;
_originalName = _previous->_originalName;
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 {
// We are the last token in the list, we can't delete
// ourselves, so just make us empty
str("");
}
}
void Token::replace(Token *replaceThis, Token *start, Token *end)
{
// Fix the whole in the old location of start and end
if (start->previous())
start->previous()->next(end->next());
if (end->next())
end->next()->previous(start->previous());
// Move start and end to their new location
if (replaceThis->previous())
replaceThis->previous()->next(start);
if (replaceThis->next())
replaceThis->next()->previous(end);
start->previous(replaceThis->previous());
end->next(replaceThis->next());
2011-10-13 20:53:06 +02:00
if (end->tokensBack && *(end->tokensBack) == end) {
while (end->next())
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())
tok->_progressValue = replaceThis->_progressValue;
// Delete old token, which is replaced
delete replaceThis;
}
const Token *Token::tokAt(int index) const
2008-12-18 22:28:57 +01:00
{
const Token *tok = this;
int num = std::abs(index);
2011-10-13 20:53:06 +02:00
while (num > 0 && tok) {
if (index > 0)
tok = tok->next();
else
tok = tok->previous();
--num;
2008-12-18 22:28:57 +01:00
}
return tok;
}
const Token *Token::linkAt(int index) const
{
const Token *tok = this->tokAt(index);
if (!tok) {
throw InternalError(this, "Internal error. Token::linkAt called with index outside the tokens range.");
}
return tok->link();
}
const std::string &Token::strAt(int index) const
2008-12-18 22:28:57 +01:00
{
static const std::string empty_str;
const Token *tok = this->tokAt(index);
return tok ? tok->_str : empty_str;
2008-12-18 22:28:57 +01:00
}
static int multiComparePercent(const Token *tok, const char ** haystack_p,
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] != ' ') {
if (haystack[1] == 'o' && // "%op%"
haystack[2] == 'p' &&
haystack[3] == '%') {
if (tok->isOp())
return 1;
*haystack_p = haystack = haystack + 4;
} else if (haystack[1] == 'c' && // "%cop%"
haystack[2] == 'o' &&
haystack[3] == 'p' &&
haystack[4] == '%') {
if (tok->isConstOp())
return 1;
*haystack_p = haystack = haystack + 5;
} else if (haystack[1] == 'o' && // "%or%"
haystack[2] == 'r' &&
haystack[3] == '%') {
if (*needle == '|' && needle[1] != '|' && needle[1] != '=')
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;
} else if (haystack[1] == 'v' && // "%var%"
haystack[2] == 'a' &&
haystack[3] == 'r' &&
haystack[4] == '%') {
if (tok->isName())
return 1;
*haystack_p = haystack = haystack + 5;
}
if (*haystack == '|')
*haystack_p = haystack = haystack + 1;
else if (*haystack == ' ' || *haystack == '\0')
return emptyStringFound ? 0 : -1;
else
return -1;
}
return 0xFFFF;
}
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') {
if (haystack[2] == 'p' && // "%op%|"
haystack[3] == '%' &&
2011-10-13 20:53:06 +02:00
haystack[4] == '|') {
haystack = haystack + 5;
if (tok->isOp())
return 1;
2011-10-13 20:53:06 +02:00
} else if (haystack[2] == 'r' && // "%or%|"
haystack[3] == '%' &&
haystack[4] == '|') {
haystack = haystack + 5;
if (*needle == '|' && needle[1] != '|' && needle[1] != '=')
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] == '|') {
haystack = haystack + 7;
if (needle[0] == '|' && needle[1] == '|')
return 1;
}
} 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;
}
2008-12-18 22:28:57 +01:00
bool emptyStringFound = false;
const char *needlePointer = needle;
2012-03-25 11:51:59 +02:00
for (;;) {
2011-10-13 20:53:06 +02:00
if (*needlePointer == *haystack) {
if (*needlePointer == '\0')
return 1;
++needlePointer;
++haystack;
2011-10-13 20:53:06 +02:00
} else if (*haystack == '|') {
if (*needlePointer == 0) {
// 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) {
// 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;
}
2008-12-18 22:28:57 +01:00
needlePointer = needle;
++haystack;
int ret = multiComparePercent(tok, &haystack, needle, emptyStringFound);
if (ret < 2)
return ret;
2011-10-13 20:53:06 +02:00
} else if (*haystack == ' ' || *haystack == '\0') {
if (needlePointer == needle)
return 0;
break;
2008-12-18 22:28:57 +01:00
}
// If haystack and needle don't share the same character,
// find next '|' character.
2011-10-13 20:53:06 +02:00
else {
needlePointer = needle;
2008-12-18 22:28:57 +01:00
2011-10-13 20:53:06 +02:00
do {
++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') {
return emptyStringFound ? 0 : -1;
}
2008-12-18 22:28:57 +01:00
++haystack;
int ret = multiComparePercent(tok, &haystack, needle, emptyStringFound);
if (ret < 2)
return ret;
}
}
if (*needlePointer == '\0')
return 1;
// If empty string was found earlier from the haystack
if (emptyStringFound)
2008-12-18 22:28:57 +01:00
return 0;
return -1;
}
bool Token::simpleMatch(const Token *tok, const char pattern[])
{
const char *current, *next;
current = pattern;
2012-12-27 11:51:12 +01:00
next = std::strchr(pattern, ' ');
if (!next)
2012-12-27 11:51:12 +01:00
next = pattern + std::strlen(pattern);
2011-10-13 20:53:06 +02:00
while (*current) {
std::size_t length = static_cast<std::size_t>(next - current);
2012-12-27 11:51:12 +01:00
if (!tok || length != tok->_str.length() || std::strncmp(current, tok->_str.c_str(), length))
return false;
current = next;
2011-10-13 20:53:06 +02:00
if (*next) {
2012-12-27 11:51:12 +01:00
next = std::strchr(++current, ' ');
if (!next)
2012-12-27 11:51:12 +01:00
next = current + std::strlen(current);
}
tok = tok->next();
}
return true;
}
bool Token::firstWordEquals(const char *str, const char *word)
{
2011-10-13 20:53:06 +02:00
for (;;) {
if (*str != *word) {
return (*str == ' ' && *word == 0);
2011-10-13 20:53:06 +02:00
} else if (*str == 0)
break;
++str;
++word;
}
return true;
}
const char *Token::chrInFirstWord(const char *str, char c)
{
2011-10-13 20:53:06 +02:00
for (;;) {
if (*str == ' ' || *str == 0)
return 0;
if (*str == c)
return str;
++str;
}
}
int Token::firstWordLen(const char *str)
{
int len = 0;
2011-10-13 20:53:06 +02:00
for (;;) {
if (*str == ' ' || *str == 0)
break;
++len;
++str;
}
return len;
}
#define multicompare(p,cond,ismulticomp) \
{ \
if (!(cond)) { \
if (*(p) != '|') \
return false; \
++(p); \
ismulticomp = (*(p) && *(p) != ' '); \
continue; \
} \
if (*(p) == '|') { \
while (*(p) && *(p) != ' ') \
++(p); \
} \
ismulticomp = false; \
}
bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
2008-12-18 22:28:57 +01:00
{
const char *p = pattern;
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..
while (*p == ' ')
2009-01-01 23:22:28 +01:00
++p;
2008-12-18 22:28:57 +01:00
// No token => Success!
if (*p == '\0')
break;
2008-12-18 22:28:57 +01:00
2011-10-13 20:53:06 +02:00
if (!tok) {
// 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;
continue;
2011-10-13 20:53:06 +02:00
} else
return false;
}
// 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 ' ':
case '|':
2012-11-25 15:13:41 +01:00
//simple '%' character
{
multicompare(p, tok->str() == "%", ismulticomp);
}
break;
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;
multicompare(p,tok->isName(),ismulticomp);
2011-10-13 20:53:06 +02:00
} else { // %varid%
if (varid == 0) {
throw InternalError(tok, "Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers");
}
if (tok->varId() != varid)
return false;
2012-11-25 15:13:41 +01:00
p += 6;
}
break;
case 't':
// Type (%type%)
{
2012-11-25 15:13:41 +01:00
p += 5;
multicompare(p,tok->isName() && tok->varId() == 0 && tok->str() != "delete",ismulticomp);
}
break;
case 'a':
// Accept any token (%any%)
{
2012-11-25 15:13:41 +01:00
p += 4;
if (p[0] == '|') {
while (*p && *p != ' ')
++p;
2012-11-25 15:13:41 +01:00
}
ismulticomp = false;
}
break;
case 'n':
// Number (%num%)
{
2012-11-25 15:13:41 +01:00
p += 4;
multicompare(p,tok->isNumber(),ismulticomp);
2008-12-18 22:28:57 +01:00
}
break;
case 'c': {
p += 1;
// Character (%char%)
if (p[0] == 'h') {
p += 4;
multicompare(p,tok->type() == eChar,ismulticomp);
}
// Const operator (%cop%)
else if (p[1] == 'p') {
p += 3;
multicompare(p,tok->isConstOp(),ismulticomp);
}
// Comparison operator (%comp%)
else {
p += 4;
multicompare(p,tok->isComparisonOp(),ismulticomp);
}
}
break;
case 's':
// String (%str%)
{
2012-11-25 15:13:41 +01:00
p += 4;
multicompare(p,tok->type() == eString,ismulticomp);
2010-09-20 20:15:07 +02:00
}
break;
case 'b':
// Bool (%bool%)
{
2012-11-25 15:13:41 +01:00
p += 5;
multicompare(p,tok->isBoolean(),ismulticomp);
}
break;
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') {
p += 2;
multicompare(p,tok->isOp(),ismulticomp);
2012-11-25 15:13:41 +01:00
}
// Or (%or%)
else {
p += 2;
multicompare(p,tok->str() == "|",ismulticomp)
}
}
// Oror (%oror%)
2012-11-25 15:13:41 +01:00
else {
p += 4;
multicompare(p,tok->str() == "||",ismulticomp);
}
}
break;
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
}
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, ']')) {
if (tok->str().length() != 1)
return false;
2011-07-15 19:02:16 +02:00
const char *temp = p+1;
bool chrFound = false;
unsigned int count = 0;
2011-10-13 20:53:06 +02:00
while (*temp && *temp != ' ') {
if (*temp == ']') {
++count;
}
else if (*temp == tok->str()[0]) {
chrFound = true;
break;
}
++temp;
}
if (count > 1 && tok->str()[0] == ']')
chrFound = true;
if (!chrFound)
2008-12-18 22:28:57 +01:00
return false;
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)) {
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
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;
while (*p && *p != ' ')
++p;
2008-12-18 22:28:57 +01: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;
}
std::size_t Token::getStrLength(const Token *tok)
{
assert(tok != NULL);
std::size_t len = 0;
const std::string strValue(tok->strValue());
const char *str = strValue.c_str();
2011-10-13 20:53:06 +02:00
while (*str) {
if (*str == '\\') {
++str;
// string ends at '\0'
if (*str == '0')
break;
}
++str;
++len;
}
return len;
}
2013-01-13 20:52:38 +01:00
std::string Token::getCharAt(const Token *tok, std::size_t index)
{
assert(tok != NULL);
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";
}
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);
// Update _progressValue
for (Token *tok = srcStart; tok != srcEnd->next(); tok = tok->next())
tok->_progressValue = newLocation->_progressValue;
}
Token* Token::nextArgument() const
2011-10-23 11:23:48 +02:00
{
for (const Token* tok = this; tok; tok = tok->next()) {
if (tok->str() == ",")
return tok->next();
2011-10-23 11:23:48 +02:00
else if (tok->str() == "(" || tok->str() == "{" || tok->str() == "[")
tok = tok->link();
else if (tok->str() == "<" && tok->link())
tok = tok->link();
else if (tok->str() == ")" || tok->str() == ";")
return 0;
2011-10-23 11:23:48 +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
{
2013-07-31 10:30:20 +02:00
const Token *closing = 0;
if (_str == "<") {
unsigned int depth = 0;
for (closing = this; closing != NULL; closing = closing->next()) {
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;
else if (closing->str() == "<")
++depth;
else if (closing->str() == ">") {
if (--depth == 0)
2013-07-31 10:30:20 +02:00
break;
} else if (closing->str() == ">>") {
if (--depth == 0)
2013-07-31 10:30:20 +02:00
break;
if (--depth == 0)
2013-07-31 10:30:20 +02:00
break;
}
}
}
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());
}
2008-12-18 22:28:57 +01: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;
}
const Token *Token::findmatch(const Token *tok, const char pattern[], unsigned int varId)
{
2011-10-13 20:53:06 +02:00
for (; tok; tok = tok->next()) {
if (Token::Match(tok, pattern, varId))
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;
}
void Token::insertToken(const std::string &tokenStr, bool prepend)
2008-12-18 22:28:57 +01:00
{
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);
2010-04-09 21:40:37 +02:00
newToken->str(tokenStr);
newToken->_linenr = _linenr;
2008-12-18 22:28:57 +01:00
newToken->_fileIndex = _fileIndex;
newToken->_progressValue = _progressValue;
2008-12-18 22:28:57 +01: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;
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);
}
}
2008-12-18 22:28:57 +01:00
}
void Token::eraseTokens(Token *begin, const Token *end)
2008-12-18 22:28:57 +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();
}
}
void Token::createMutualLinks(Token *begin, Token *end)
{
assert(begin != NULL);
assert(end != NULL);
assert(begin != end);
begin->link(end);
end->link(begin);
}
void Token::printOut(const char *title) const
2008-12-18 22:28:57 +01:00
{
if (title && title[0])
std::cout << "\n### " << title << " ###\n";
std::cout << stringifyList(true, true, true, true, true, 0, 0) << std::endl;
}
void Token::printOut(const char *title, const std::vector<std::string> &fileNames) const
{
if (title && title[0])
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
}
void Token::stringify(std::ostream& os, bool varid, bool attributes) const
{
if (attributes) {
if (isUnsigned())
os << "unsigned ";
else if (isSigned())
os << "signed ";
if (isLong()) {
if (_type == eString || _type == eChar)
os << "L";
else
os << "long ";
}
}
if (isExpandedMacro())
os << "$";
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];
}
}
if (varid && _varId != 0)
os << '@' << _varId;
}
std::string Token::stringifyList(bool varid, bool attributes, bool linenumbers, bool linebreaks, bool files, const std::vector<std::string>* fileNames, const Token* end) const
{
if (this == end)
return "";
std::ostringstream ret;
unsigned int lineNumber = _linenr;
int fileInd = files ? -1 : static_cast<int>(_fileIndex);
std::map<int, unsigned int> lineNumbers;
for (const Token *tok = this; tok != end; tok = tok->next()) {
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;
}
2010-04-09 21:40:37 +02:00
fileInd = static_cast<int>(tok->_fileIndex);
if (files) {
ret << "\n\n##file ";
if (fileNames && fileNames->size() > tok->_fileIndex)
ret << fileNames->at(tok->_fileIndex);
else
ret << fileInd;
}
2010-04-09 21:40:37 +02:00
lineNumber = lineNumbers[fileInd];
fileChange = true;
}
if (linebreaks && (lineNumber != tok->linenr() || fileChange)) {
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 << ' ';
}
}
}
2010-04-09 21:40:37 +02:00
lineNumber = tok->linenr();
}
tok->stringify(ret, varid, attributes); // print token
if (tok->next() != end && (!linebreaks || (tok->next()->linenr() <= tok->linenr() && tok->next()->fileIndex() == tok->fileIndex())))
ret << ' ';
}
if (linebreaks && files)
ret << '\n';
return ret.str();
}
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);
}
void Token::astOperand1(Token *tok)
{
// goto parent operator
while (tok->_astParent)
tok = tok->_astParent;
tok->_astParent = this;
_astOperand1 = tok;
}
void Token::astOperand2(Token *tok)
{
// goto parent operator
while (tok->_astParent)
tok = tok->_astParent;
tok->_astParent = this;
_astOperand2 = tok;
}
2012-12-16 10:06:55 +01:00
void Token::astFunctionCall()
{
_astOperand1 = _next;
_next->_astParent = this;
}
void Token::astHandleParentheses()
{
2013-02-23 07:43:12 +01:00
// Assumptions:
// * code is valid
// * _str is one of: ( ) ]
2012-12-16 10:06:55 +01:00
Token *innerTop;
2013-02-23 07:43:12 +01:00
if (Token::Match(this, ")|]"))
2012-12-16 10:06:55 +01:00
innerTop = _previous;
else if (_next && _next->_str == ")")
return;
2013-02-23 07:43:12 +01:00
else // _str = "("
2012-12-16 10:06:55 +01:00
innerTop = _next;
while (innerTop->_astParent)
innerTop = innerTop->_astParent;
if (_astParent) {
if (_str == "(" && _astParent->_astOperand2 != NULL)
_astParent->_astOperand2 = innerTop;
else
_astParent->_astOperand1 = innerTop;
innerTop->_astParent = _astParent;
} else {
_astParent = innerTop;
}
}