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
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 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"
|
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>
|
2009-01-07 16:43:20 +01:00
|
|
|
|
2010-01-06 20:19:27 +01:00
|
|
|
Token::Token(Token **t) :
|
|
|
|
tokensBack(t),
|
2009-01-05 16:49:57 +01:00
|
|
|
_str(""),
|
|
|
|
_isName(false),
|
|
|
|
_isNumber(false),
|
|
|
|
_isBoolean(false),
|
|
|
|
_varId(0),
|
|
|
|
_next(0),
|
|
|
|
_previous(0),
|
2009-03-13 22:25:56 +01:00
|
|
|
_link(0),
|
2009-01-05 16:49:57 +01:00
|
|
|
_fileIndex(0),
|
|
|
|
_linenr(0)
|
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
|
|
|
}
|
|
|
|
|
2009-06-14 08:55:23 +02:00
|
|
|
void Token::str(const std::string &s)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
_str = s;
|
2009-11-28 09:48:43 +01:00
|
|
|
|
2009-01-23 21:25:13 +01:00
|
|
|
_isName = bool(_str[0] == '_' || std::isalpha(_str[0]));
|
2009-11-28 09:48:43 +01:00
|
|
|
|
|
|
|
if (std::isdigit(_str[0]))
|
|
|
|
_isNumber = true;
|
2009-11-28 11:47:44 +01:00
|
|
|
else if (_str.length() > 1 && _str[0] == '-' && std::isdigit(_str[1]))
|
2009-11-28 09:48:43 +01:00
|
|
|
_isNumber = true;
|
|
|
|
else
|
|
|
|
_isNumber = false;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (_str == "true" || _str == "false")
|
2008-12-21 14:58:56 +01:00
|
|
|
_isBoolean = true;
|
|
|
|
else
|
|
|
|
_isBoolean = false;
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
_varId = 0;
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2009-09-13 10:02:23 +02:00
|
|
|
std::string Token::strValue() const
|
2009-09-12 22:54:47 +02:00
|
|
|
{
|
|
|
|
assert(_str.length() >= 2);
|
|
|
|
assert(_str[0] == '"');
|
|
|
|
assert(_str[_str.length()-1] == '"');
|
|
|
|
return _str.substr(1, _str.length() - 2);
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
void Token::deleteNext()
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
Token *n = _next;
|
2008-12-18 22:28:57 +01:00
|
|
|
_next = n->next();
|
2008-12-17 20:20:11 +01:00
|
|
|
delete n;
|
|
|
|
if (_next)
|
2008-12-18 22:28:57 +01:00
|
|
|
_next->previous(this);
|
2010-01-06 20:19:27 +01:00
|
|
|
else if (tokensBack)
|
|
|
|
*tokensBack = this;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-03-13 00:07:05 +01:00
|
|
|
void Token::deleteThis()
|
|
|
|
{
|
|
|
|
if (_next)
|
|
|
|
{
|
|
|
|
_str = _next->_str;
|
|
|
|
_isName = _next->_isName;
|
|
|
|
_isNumber = _next->_isNumber;
|
|
|
|
_isBoolean = _next->_isBoolean;
|
|
|
|
_varId = _next->_varId;
|
|
|
|
_fileIndex = _next->_fileIndex;
|
|
|
|
_linenr = _next->_linenr;
|
2009-03-13 22:25:56 +01:00
|
|
|
_link = _next->_link;
|
2009-08-28 22:31:11 +02:00
|
|
|
if (_link)
|
|
|
|
_link->link(this);
|
|
|
|
|
2009-03-13 00:07:05 +01:00
|
|
|
deleteNext();
|
|
|
|
}
|
|
|
|
else if (_previous)
|
|
|
|
{
|
|
|
|
// This should never be used for tokens
|
|
|
|
// at the end of the list
|
|
|
|
str(";");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We are the last token in the list, we can't delete
|
|
|
|
// ourselves, so just make us ;
|
|
|
|
str(";");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2009-05-26 22:22:00 +02:00
|
|
|
if (start->previous())
|
|
|
|
start->previous()->next(end->next());
|
|
|
|
|
|
|
|
if (end->next())
|
|
|
|
end->next()->previous(start->previous());
|
2009-01-26 23:26:50 +01:00
|
|
|
|
|
|
|
// Move start and end to their new location
|
2009-05-26 22:22:00 +02:00
|
|
|
if (replaceThis->previous())
|
|
|
|
replaceThis->previous()->next(start);
|
|
|
|
|
|
|
|
if (replaceThis->next())
|
|
|
|
replaceThis->next()->previous(end);
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
start->previous(replaceThis->previous());
|
|
|
|
end->next(replaceThis->next());
|
|
|
|
|
2010-01-06 21:18:42 +01:00
|
|
|
if (end->tokensBack && *(end->tokensBack) == end)
|
2010-01-06 20:19:27 +01:00
|
|
|
{
|
|
|
|
while (end->next())
|
|
|
|
end = end->next();
|
|
|
|
*(end->tokensBack) = end;
|
|
|
|
}
|
|
|
|
|
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);
|
2009-03-20 18:50:11 +01:00
|
|
|
while (num > 0 && tok)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-03-20 18:50:11 +01:00
|
|
|
if (index > 0)
|
|
|
|
tok = tok->next();
|
|
|
|
else
|
|
|
|
tok = tok->previous();
|
|
|
|
--num;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2009-01-20 07:20:55 +01:00
|
|
|
Token *Token::tokAt(int index)
|
|
|
|
{
|
|
|
|
Token *tok = this;
|
2009-08-13 22:13:52 +02:00
|
|
|
int num = std::abs(index);
|
2009-03-20 18:50:11 +01:00
|
|
|
while (num > 0 && tok)
|
2009-01-20 07:20:55 +01:00
|
|
|
{
|
2009-03-20 18:50:11 +01:00
|
|
|
if (index > 0)
|
|
|
|
tok = tok->next();
|
|
|
|
else
|
|
|
|
tok = tok->previous();
|
|
|
|
--num;
|
2009-01-20 07:20:55 +01:00
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
const char *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);
|
2009-02-07 22:05:45 +01:00
|
|
|
return tok ? tok->_str.c_str() : "";
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 20:30:01 +01:00
|
|
|
int Token::multiCompare(const char *haystack, const char *needle)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
bool emptyStringFound = false;
|
2009-01-27 20:30:01 +01:00
|
|
|
bool noMatch = false;
|
|
|
|
const char *needlePointer = needle;
|
2009-08-16 22:28:17 +02:00
|
|
|
for (; *haystack && *haystack != ' '; ++haystack)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-27 20:30:01 +01:00
|
|
|
if (*haystack == '|')
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-27 20:30:01 +01:00
|
|
|
if (noMatch)
|
|
|
|
{
|
|
|
|
// We didn't have a match at this round
|
|
|
|
noMatch = false;
|
|
|
|
}
|
|
|
|
else if (*needlePointer == 0)
|
|
|
|
{
|
|
|
|
// If needle and haystack are both 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
|
|
|
}
|
|
|
|
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;
|
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;
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-27 20:30:01 +01:00
|
|
|
if (noMatch)
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
2009-01-27 20:30:01 +01:00
|
|
|
// If haystack and needle don't share the same character,
|
|
|
|
// find next '|' character.
|
|
|
|
if (*needlePointer != *haystack)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-27 20:30:01 +01:00
|
|
|
noMatch = true;
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All characters in haystack and needle have matched this far
|
2009-01-27 20:30:01 +01:00
|
|
|
++needlePointer;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 20:30:01 +01:00
|
|
|
if (!noMatch)
|
|
|
|
{
|
|
|
|
if (*needlePointer == 0)
|
|
|
|
{
|
|
|
|
// If both needle and haystack are at the end, then we have a match.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (needlePointer == needle)
|
|
|
|
{
|
|
|
|
// Last string in haystack was empty string e.g. "one|two|"
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If empty string was found earlier from the haystack
|
|
|
|
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;
|
2008-12-25 19:24:57 +01:00
|
|
|
next = strchr(pattern, ' ');
|
2009-01-05 16:49:57 +01:00
|
|
|
if (!next)
|
2008-12-23 22:45:47 +01:00
|
|
|
next = pattern + strlen(pattern);
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
while (*current)
|
2008-12-22 00:28:09 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
size_t length = static_cast<size_t>(next - current);
|
2008-12-23 22:45:47 +01:00
|
|
|
|
2009-02-07 22:05:45 +01:00
|
|
|
if (!tok || length != tok->_str.length() || 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;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (*next)
|
2008-12-25 19:24:57 +01:00
|
|
|
{
|
|
|
|
next = strchr(++current, ' ');
|
2009-01-05 16:49:57 +01:00
|
|
|
if (!next)
|
2008-12-25 19:24:57 +01:00
|
|
|
next = current + 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;
|
|
|
|
}
|
|
|
|
|
2009-08-16 22:28:17 +02:00
|
|
|
int Token::firstWordEquals(const char *str, const char *word)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (*str == ' ' && *word == 0)
|
|
|
|
return 0;
|
|
|
|
else if (*str != *word)
|
|
|
|
return 1;
|
|
|
|
else if (*str == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
++str;
|
|
|
|
++word;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Token::chrInFirstWord(const char *str, char c)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (*str == ' ' || *str == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (*str == c)
|
|
|
|
return str;
|
|
|
|
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Token::firstWordLen(const char *str)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (*str == ' ' || *str == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
++len;
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
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;
|
2009-01-10 22:13:10 +01:00
|
|
|
bool firstpattern = true;
|
2009-08-16 22:28:17 +02:00
|
|
|
bool first = true;
|
2009-01-05 16:49:57 +01:00
|
|
|
while (*p)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-08-16 22:28:17 +02:00
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
while (*p && *p != ' ')
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
// Skip spaces in pattern..
|
2009-01-05 16:49:57 +01:00
|
|
|
while (*p == ' ')
|
2009-01-01 23:22:28 +01:00
|
|
|
++p;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
// No token => Success!
|
2009-08-16 22:28:17 +02:00
|
|
|
if (*p == 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
return true;
|
|
|
|
|
2009-01-10 01:33:48 +01:00
|
|
|
if (!tok)
|
|
|
|
{
|
|
|
|
// If we have no tokens, pattern "!!else" should return true
|
2009-08-16 22:28:17 +02:00
|
|
|
if (p[1] == '!' && p[0] == '!' && p[2] != '\0')
|
2009-01-10 01:33:48 +01:00
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-10 22:13:10 +01:00
|
|
|
// If we are in the first token, we skip all initial !! patterns
|
2009-08-16 22:28:17 +02:00
|
|
|
if (firstpattern && !tok->previous() && tok->next() && p[1] == '!' && p[0] == '!' && p[2] != '\0')
|
2009-01-10 22:13:10 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
firstpattern = false;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
// Compare the first character of the string for optimization reasons
|
|
|
|
// before doing more detailed checks.
|
2008-12-23 21:17:05 +01:00
|
|
|
bool patternIdentified = false;
|
2009-08-16 22:28:17 +02:00
|
|
|
if (p[0] == '%')
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2008-12-23 21:17:05 +01:00
|
|
|
// Any symbolname..
|
2009-08-16 22:28:17 +02:00
|
|
|
if (firstWordEquals(p, "%var%") == 0)
|
2008-12-23 21:17:05 +01:00
|
|
|
{
|
|
|
|
if (!tok->isName())
|
|
|
|
return false;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2008-12-23 21:17:05 +01:00
|
|
|
patternIdentified = true;
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-10 23:12:26 +02:00
|
|
|
// Type..
|
2009-08-16 22:28:17 +02:00
|
|
|
if (firstWordEquals(p, "%type%") == 0)
|
2009-06-10 23:12:26 +02:00
|
|
|
{
|
|
|
|
if (!tok->isName())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (tok->str() == "delete")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
patternIdentified = true;
|
|
|
|
}
|
|
|
|
|
2008-12-23 21:17:05 +01:00
|
|
|
// Accept any token
|
2009-08-16 22:28:17 +02:00
|
|
|
else if (firstWordEquals(p, "%any%") == 0)
|
2008-12-23 21:17:05 +01:00
|
|
|
{
|
|
|
|
patternIdentified = true;
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-08-16 22:28:17 +02:00
|
|
|
else if (firstWordEquals(p, "%varid%") == 0)
|
2008-12-23 21:17:05 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (varid == 0)
|
2009-01-04 21:33:12 +01:00
|
|
|
{
|
2009-10-03 16:35:25 +02:00
|
|
|
std::cerr << "\n###### If you see this, there is a bug ######" << std::endl
|
|
|
|
<< "Token::Match(\"" << pattern << "\", 0)" << std::endl;
|
2009-01-04 21:33:12 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->varId() != varid)
|
2008-12-18 22:28:57 +01:00
|
|
|
return false;
|
|
|
|
|
2008-12-23 21:17:05 +01:00
|
|
|
patternIdentified = true;
|
|
|
|
}
|
|
|
|
|
2009-08-16 22:28:17 +02:00
|
|
|
else if (firstWordEquals(p, "%num%") == 0)
|
2008-12-23 21:17:05 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (!tok->isNumber())
|
2008-12-18 22:28:57 +01:00
|
|
|
return false;
|
|
|
|
|
2008-12-23 21:17:05 +01:00
|
|
|
patternIdentified = true;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-08-16 22:28:17 +02:00
|
|
|
else if (firstWordEquals(p, "%bool%") == 0)
|
2008-12-23 21:17:05 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (!tok->isBoolean())
|
2008-12-23 21:17:05 +01:00
|
|
|
return false;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2008-12-23 21:17:05 +01:00
|
|
|
patternIdentified = true;
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-08-16 22:28:17 +02:00
|
|
|
else if (firstWordEquals(p, "%str%") == 0)
|
2008-12-23 21:17:05 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (tok->_str[0] != '\"')
|
2008-12-23 21:17:05 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
patternIdentified = true;
|
|
|
|
}
|
2008-12-21 14:58:56 +01:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (patternIdentified)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2008-12-23 21:17:05 +01:00
|
|
|
// Pattern was identified already above.
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// [.. => search for a one-character token..
|
2009-08-16 22:50:00 +02:00
|
|
|
else if (p[0] == '[' && chrInFirstWord(p, ']') && tok->_str.length() == 1)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-08-16 22:28:17 +02:00
|
|
|
const char *temp = p + 1;
|
|
|
|
bool chrFound = false;
|
|
|
|
int count = 0;
|
|
|
|
while (*temp && *temp != ' ')
|
|
|
|
{
|
|
|
|
if (*temp == ']')
|
|
|
|
{
|
|
|
|
++count;
|
|
|
|
++temp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*temp == tok->_str[0])
|
|
|
|
{
|
|
|
|
chrFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 1)
|
|
|
|
{
|
|
|
|
if (tok->_str[0] == ']')
|
|
|
|
chrFound = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!chrFound)
|
2008-12-18 22:28:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse multi options, such as void|int|char (accept token which is one of these 3)
|
2009-08-16 22:28:17 +02:00
|
|
|
else if (chrInFirstWord(p, '|') && (p[0] != '|' || firstWordLen(p) > 2))
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-08-16 22:28:17 +02:00
|
|
|
int res = multiCompare(p, tok->_str.c_str());
|
2009-01-05 16:49:57 +01:00
|
|
|
if (res == 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Empty alternative matches, use the same token on next round
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-05 16:49:57 +01: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
|
2009-08-16 22:28:17 +02:00
|
|
|
else if (p[1] == '!' && p[0] == '!' && p[2] != '\0')
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-08-16 22:28:17 +02:00
|
|
|
if (firstWordEquals(&(p[2]), tok->str().c_str()) == 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-16 22:28:17 +02:00
|
|
|
else if (firstWordEquals(p, tok->_str.c_str()) != 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The end of the pattern has been reached and nothing wrong has been found
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-30 13:07:10 +02:00
|
|
|
size_t Token::getStrLength(const Token *tok)
|
|
|
|
{
|
|
|
|
assert(tok != NULL);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
while (*str)
|
|
|
|
{
|
|
|
|
if (*str == '\\')
|
2009-09-26 17:58:14 +02:00
|
|
|
{
|
2009-08-30 13:07:10 +02:00
|
|
|
++str;
|
2009-09-26 17:58:14 +02:00
|
|
|
|
|
|
|
// string ends at '\0'
|
|
|
|
if (*str == '0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-30 13:07:10 +02:00
|
|
|
++str;
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:29:20 +01:00
|
|
|
bool Token::isStandardType() const
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
bool ret = false;
|
2009-11-12 22:31:13 +01:00
|
|
|
const char *type[] = {"bool", "char", "short", "int", "long", "float", "double", "size_t", 0};
|
2008-12-18 22:28:57 +01:00
|
|
|
for (int i = 0; type[i]; i++)
|
|
|
|
ret |= (_str == type[i]);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
for (; tok; tok = tok->next())
|
2009-01-04 20:55:12 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (Token::Match(tok, pattern, varId))
|
2009-01-04 20:55:12 +01:00
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
void Token::insertToken(const char str[])
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2010-01-06 20:19:27 +01:00
|
|
|
Token *newToken = new Token(tokensBack);
|
2009-01-05 16:49:57 +01:00
|
|
|
newToken->str(str);
|
2008-12-16 18:05:43 +01:00
|
|
|
newToken->_linenr = _linenr;
|
2008-12-18 22:28:57 +01:00
|
|
|
newToken->_fileIndex = _fileIndex;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (this->next())
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
newToken->next(this->next());
|
|
|
|
newToken->next()->previous(newToken);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2010-01-06 20:19:27 +01:00
|
|
|
else if (tokensBack)
|
|
|
|
{
|
|
|
|
*tokensBack = newToken;
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
this->next(newToken);
|
|
|
|
newToken->previous(this);
|
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
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (! begin)
|
2008-12-18 22:28:57 +01:00
|
|
|
return;
|
|
|
|
|
2009-01-05 16:49:57 +01: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)
|
|
|
|
{
|
|
|
|
assert(begin != NULL);
|
|
|
|
assert(end != NULL);
|
|
|
|
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
|
|
|
{
|
2009-11-27 23:04:04 +01:00
|
|
|
const std::vector<std::string> fileNames;
|
|
|
|
std::cout << stringifyList(true, title, fileNames) << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Token::printOut(const char *title, const std::vector<std::string> &fileNames) const
|
|
|
|
{
|
|
|
|
std::cout << stringifyList(true, title, fileNames) << std::endl;
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-02-13 07:25:29 +01:00
|
|
|
|
2009-09-19 09:16:01 +02:00
|
|
|
std::string Token::stringifyList(bool varid, const char *title) const
|
2009-11-27 23:04:04 +01:00
|
|
|
{
|
|
|
|
const std::vector<std::string> fileNames;
|
|
|
|
return stringifyList(varid, title, fileNames);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Token::stringifyList(bool varid, const char *title, const std::vector<std::string> &fileNames) const
|
2009-02-13 07:25:29 +01:00
|
|
|
{
|
|
|
|
std::ostringstream ret;
|
2009-03-03 20:45:58 +01:00
|
|
|
if (title)
|
|
|
|
ret << "\n### " << title << " ###\n";
|
|
|
|
|
2009-02-13 07:25:29 +01:00
|
|
|
unsigned int linenr = 0;
|
2009-03-03 21:17:23 +01:00
|
|
|
int fileIndex = -1;
|
|
|
|
std::map<unsigned int, unsigned int> lineNumbers;
|
2009-02-13 07:25:29 +01:00
|
|
|
for (const Token *tok = this; tok; tok = tok->next())
|
|
|
|
{
|
2009-03-03 21:17:23 +01:00
|
|
|
bool fileChange = false;
|
|
|
|
if (static_cast<int>(tok->_fileIndex) != fileIndex)
|
2009-02-13 07:25:29 +01:00
|
|
|
{
|
2009-03-03 21:17:23 +01:00
|
|
|
if (fileIndex != -1)
|
|
|
|
{
|
|
|
|
lineNumbers[fileIndex] = tok->_fileIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileIndex = static_cast<int>(tok->_fileIndex);
|
2009-11-27 23:04:04 +01:00
|
|
|
ret << "\n\n##file ";
|
|
|
|
if (fileNames.size() > static_cast<unsigned int>(fileIndex))
|
|
|
|
ret << fileNames.at(fileIndex);
|
|
|
|
else
|
|
|
|
ret << fileIndex;
|
2009-03-03 21:17:23 +01:00
|
|
|
|
|
|
|
linenr = lineNumbers[fileIndex];
|
|
|
|
fileChange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (linenr != tok->linenr() || fileChange)
|
|
|
|
{
|
|
|
|
while (linenr < tok->linenr())
|
|
|
|
{
|
|
|
|
++linenr;
|
|
|
|
ret << "\n" << linenr << ":";
|
|
|
|
}
|
|
|
|
linenr = tok->linenr();
|
2009-02-13 07:25:29 +01:00
|
|
|
}
|
2009-03-03 21:17:23 +01:00
|
|
|
|
2009-02-13 07:25:29 +01:00
|
|
|
ret << " " << tok->str();
|
|
|
|
if (varid && tok->varId() > 0)
|
|
|
|
ret << "@" << tok->varId();
|
|
|
|
}
|
|
|
|
ret << "\n";
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|