2011-02-19 04:10:50 +01:00
|
|
|
/*
|
2009-01-24 18:15:38 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2012-01-01 00:05:37 +01:00
|
|
|
* Copyright (C) 2007-2012 Daniel Marjamäki and Cppcheck team.
|
2009-01-24 18:15:38 +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/>.
|
2009-01-24 18:15:38 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2010-04-13 19:25:08 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable: 4503)
|
|
|
|
#endif
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
#include "tokenize.h"
|
2009-07-13 19:11:31 +02:00
|
|
|
#include "token.h"
|
2009-04-06 19:23:30 +02:00
|
|
|
#include "mathlib.h"
|
2009-07-13 19:11:31 +02:00
|
|
|
#include "settings.h"
|
|
|
|
#include "errorlogger.h"
|
2009-10-17 23:11:48 +02:00
|
|
|
#include "check.h"
|
2010-10-29 21:21:27 +02:00
|
|
|
#include "path.h"
|
2010-11-23 18:41:07 +01:00
|
|
|
#include "symboldatabase.h"
|
2012-01-01 21:55:05 +01:00
|
|
|
#include "templatesimplifier.h"
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <sstream>
|
|
|
|
#include <list>
|
2009-09-13 15:00:48 +02:00
|
|
|
#include <cassert>
|
2009-01-24 18:15:38 +01:00
|
|
|
#include <cctype>
|
2009-08-27 18:33:42 +02:00
|
|
|
#include <stack>
|
2011-10-16 14:16:35 +02:00
|
|
|
#include <cstdlib>
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-10-24 02:52:55 +02:00
|
|
|
Tokenizer::Tokenizer() :
|
|
|
|
_tokens(0), //no tokens to start with
|
|
|
|
_tokensBack(0),
|
|
|
|
_settings(0),
|
|
|
|
_errorLogger(0),
|
|
|
|
_symbolDatabase(0),
|
|
|
|
_varId(0),
|
|
|
|
_codeWithTemplates(false) //is there any templates?
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-24 02:52:55 +02:00
|
|
|
Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger) :
|
|
|
|
_tokens(0), //no tokens to start with
|
|
|
|
_tokensBack(0),
|
|
|
|
_settings(settings),
|
|
|
|
_errorLogger(errorLogger),
|
|
|
|
_symbolDatabase(0),
|
|
|
|
_varId(0),
|
|
|
|
_codeWithTemplates(false) //is there any templates?
|
2009-03-16 22:31:52 +01:00
|
|
|
{
|
2010-12-01 18:00:55 +01:00
|
|
|
// make sure settings are specified
|
|
|
|
assert(_settings);
|
2009-03-16 22:31:52 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
Tokenizer::~Tokenizer()
|
|
|
|
{
|
2009-07-05 22:16:43 +02:00
|
|
|
deallocateTokens();
|
2010-11-23 18:41:07 +01:00
|
|
|
delete _symbolDatabase;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Helper functions..
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const Token *Tokenizer::tokens() const
|
|
|
|
{
|
|
|
|
return _tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::vector<std::string> *Tokenizer::getFiles() const
|
|
|
|
{
|
|
|
|
return &_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// addtoken
|
|
|
|
// add a token. Used by 'Tokenizer'
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2010-04-03 09:54:10 +02:00
|
|
|
void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsigned int fileno, bool split)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str[0] == 0)
|
2009-01-24 18:15:38 +01:00
|
|
|
return;
|
|
|
|
|
2010-04-03 09:54:10 +02:00
|
|
|
// If token contains # characters, split it up
|
2011-10-13 20:53:06 +02:00
|
|
|
if (split && strstr(str, "##")) {
|
2010-04-03 09:54:10 +02:00
|
|
|
std::string temp;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (unsigned int i = 0; str[i]; ++i) {
|
|
|
|
if (strncmp(&str[i], "##", 2) == 0) {
|
2010-04-03 09:54:10 +02:00
|
|
|
addtoken(temp.c_str(), lineno, fileno, false);
|
|
|
|
temp.clear();
|
|
|
|
addtoken("##", lineno, fileno, false);
|
|
|
|
++i;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2010-04-03 09:54:10 +02:00
|
|
|
temp += str[i];
|
|
|
|
}
|
|
|
|
addtoken(temp.c_str(), lineno, fileno, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Replace hexadecimal value with decimal
|
|
|
|
std::ostringstream str2;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0) {
|
2009-01-24 18:15:38 +01:00
|
|
|
str2 << std::strtoul(str + 2, NULL, 16);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (strncmp(str, "_Bool", 5) == 0) {
|
2011-09-03 23:10:16 +02:00
|
|
|
str2 << "bool";
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2009-01-24 18:15:38 +01:00
|
|
|
str2 << str;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (_tokensBack) {
|
2011-12-08 21:28:34 +01:00
|
|
|
_tokensBack->insertToken(str2.str());
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-01-06 20:19:27 +01:00
|
|
|
_tokens = new Token(&_tokensBack);
|
2009-01-24 18:15:38 +01:00
|
|
|
_tokensBack = _tokens;
|
2009-06-14 08:55:23 +02:00
|
|
|
_tokensBack->str(str2.str());
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_tokensBack->linenr(lineno);
|
|
|
|
_tokensBack->fileIndex(fileno);
|
|
|
|
}
|
2010-03-31 17:14:49 +02:00
|
|
|
|
|
|
|
void Tokenizer::addtoken(const Token * tok, const unsigned int lineno, const unsigned int fileno)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok == 0)
|
2010-03-31 17:14:49 +02:00
|
|
|
return;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (_tokensBack) {
|
2011-12-08 21:28:34 +01:00
|
|
|
_tokensBack->insertToken(tok->str());
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-03-31 17:14:49 +02:00
|
|
|
_tokens = new Token(&_tokensBack);
|
|
|
|
_tokensBack = _tokens;
|
2011-12-08 21:28:34 +01:00
|
|
|
_tokensBack->str(tok->str());
|
2010-03-31 17:14:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_tokensBack->linenr(lineno);
|
|
|
|
_tokensBack->fileIndex(fileno);
|
|
|
|
_tokensBack->isUnsigned(tok->isUnsigned());
|
|
|
|
_tokensBack->isSigned(tok->isSigned());
|
|
|
|
_tokensBack->isLong(tok->isLong());
|
2010-06-14 15:46:57 +02:00
|
|
|
_tokensBack->isUnused(tok->isUnused());
|
2010-03-31 17:14:49 +02:00
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// SizeOfType - gives the size of a type
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-10 21:54:58 +02:00
|
|
|
unsigned int Tokenizer::sizeOfType(const Token *type) const
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!type || type->str().empty())
|
2009-01-24 18:15:38 +01:00
|
|
|
return 0;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type->str()[0] == '"')
|
2009-10-11 16:40:50 +02:00
|
|
|
return static_cast<unsigned int>(Token::getStrLength(type) + 1);
|
2009-10-07 09:54:34 +02:00
|
|
|
|
2011-02-09 13:45:19 +01:00
|
|
|
std::map<std::string, unsigned int>::const_iterator it = _typeSize.find(type->str());
|
2010-04-02 07:30:58 +02:00
|
|
|
if (it == _typeSize.end())
|
2009-01-24 18:15:38 +01:00
|
|
|
return 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (type->isLong()) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type->str() == "double")
|
2011-09-18 01:40:52 +02:00
|
|
|
return _settings->sizeof_long_double;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (type->str() == "long")
|
2011-09-18 01:40:52 +02:00
|
|
|
return _settings->sizeof_long_long;
|
2010-03-28 15:56:13 +02:00
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// InsertTokens - Copy and insert tokens
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-09-09 23:25:58 +02:00
|
|
|
void Tokenizer::insertTokens(Token *dest, const Token *src, unsigned int n)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2011-05-07 14:23:14 +02:00
|
|
|
std::stack<Token *> link;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (n > 0) {
|
2011-05-07 14:23:14 +02:00
|
|
|
dest->insertToken(src->str());
|
2009-01-24 18:15:38 +01:00
|
|
|
dest = dest->next();
|
2011-05-07 14:23:14 +02:00
|
|
|
|
|
|
|
// Set links
|
|
|
|
if (Token::Match(dest, "(|[|{"))
|
|
|
|
link.push(dest);
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (!link.empty() && Token::Match(dest, ")|]|}")) {
|
2011-05-07 14:23:14 +02:00
|
|
|
Token::createMutualLinks(dest, link.top());
|
|
|
|
link.pop();
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
dest->fileIndex(src->fileIndex());
|
|
|
|
dest->linenr(src->linenr());
|
|
|
|
dest->varId(src->varId());
|
2011-03-08 02:04:25 +01:00
|
|
|
dest->isName(src->isName());
|
|
|
|
dest->isNumber(src->isNumber());
|
|
|
|
dest->isBoolean(src->isBoolean());
|
2010-03-28 15:56:13 +02:00
|
|
|
dest->isUnsigned(src->isUnsigned());
|
|
|
|
dest->isSigned(src->isSigned());
|
2011-07-07 15:14:33 +02:00
|
|
|
dest->isPointerCompare(src->isPointerCompare());
|
2010-03-28 15:56:13 +02:00
|
|
|
dest->isLong(src->isLong());
|
2011-03-08 02:04:25 +01:00
|
|
|
dest->isUnused(src->isUnused());
|
2009-01-24 18:15:38 +01:00
|
|
|
src = src->next();
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
}
|
2011-03-08 02:04:25 +01:00
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-02-10 04:02:17 +01:00
|
|
|
Token *Tokenizer::copyTokens(Token *dest, const Token *first, const Token *last)
|
|
|
|
{
|
|
|
|
std::stack<Token *> links;
|
|
|
|
Token *tok2 = dest;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok = first; tok != last->next(); tok = tok->next()) {
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2->insertToken(tok->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok2->fileIndex(dest->fileIndex());
|
|
|
|
tok2->linenr(dest->linenr());
|
2011-03-08 02:04:25 +01:00
|
|
|
tok2->isName(tok->isName());
|
|
|
|
tok2->isNumber(tok->isNumber());
|
|
|
|
tok2->isBoolean(tok->isBoolean());
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2->isUnsigned(tok->isUnsigned());
|
|
|
|
tok2->isSigned(tok->isSigned());
|
2011-07-07 15:14:33 +02:00
|
|
|
tok2->isPointerCompare(tok->isPointerCompare());
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2->isLong(tok->isLong());
|
2011-03-08 02:04:25 +01:00
|
|
|
tok2->isUnused(tok->isUnused());
|
2011-12-18 13:33:23 +01:00
|
|
|
tok2->setExpandedMacro(tok->isExpandedMacro());
|
2011-06-26 22:53:16 +02:00
|
|
|
tok2->varId(tok->varId());
|
2011-02-10 04:02:17 +01:00
|
|
|
|
|
|
|
// Check for links and fix them up
|
|
|
|
if (tok2->str() == "(" || tok2->str() == "[" || tok2->str() == "{")
|
|
|
|
links.push(tok2);
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok2->str() == ")" || tok2->str() == "]" || tok2->str() == "}") {
|
2011-02-10 04:02:17 +01:00
|
|
|
Token * link = links.top();
|
|
|
|
|
|
|
|
tok2->link(link);
|
|
|
|
link->link(tok2);
|
|
|
|
|
|
|
|
links.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tok2;
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Tokenize - tokenizes a given file.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-05-09 21:32:29 +02:00
|
|
|
void Tokenizer::createTokens(std::istream &code)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// line number in parsed code
|
|
|
|
unsigned int lineno = 1;
|
|
|
|
|
|
|
|
// The current token being parsed
|
|
|
|
std::string CurrentToken;
|
|
|
|
|
|
|
|
// lineNumbers holds line numbers for files in fileIndexes
|
2010-12-15 18:45:53 +01:00
|
|
|
// every time an include file is completely parsed, last item in the vector
|
2009-01-24 18:15:38 +01:00
|
|
|
// is removed and lineno is set to point to that value.
|
|
|
|
std::vector<unsigned int> lineNumbers;
|
|
|
|
|
|
|
|
// fileIndexes holds index for _files vector about currently parsed files
|
2010-12-15 18:45:53 +01:00
|
|
|
// every time an include file is completely parsed, last item in the vector
|
2009-01-24 18:15:38 +01:00
|
|
|
// is removed and FileIndex is set to point to that value.
|
|
|
|
std::vector<unsigned int> fileIndexes;
|
|
|
|
|
|
|
|
// FileIndex. What file in the _files vector is read now?
|
|
|
|
unsigned int FileIndex = 0;
|
|
|
|
|
2011-12-18 13:33:23 +01:00
|
|
|
bool expandedMacro = false;
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Read one byte at a time from code and create tokens
|
2011-10-13 20:53:06 +02:00
|
|
|
for (char ch = (char)code.get(); code.good(); ch = (char)code.get()) {
|
2011-12-18 13:33:23 +01:00
|
|
|
if (ch == '$') {
|
|
|
|
while (code.peek() == '$')
|
|
|
|
code.get();
|
|
|
|
ch = ' ';
|
|
|
|
expandedMacro = true;
|
|
|
|
} else if (ch == '\n') {
|
|
|
|
expandedMacro = false;
|
|
|
|
}
|
|
|
|
|
2009-03-04 18:02:45 +01:00
|
|
|
// char/string..
|
2010-04-02 20:48:32 +02:00
|
|
|
// multiline strings are not handled. The preprocessor should handle that for us.
|
2012-01-01 20:17:16 +01:00
|
|
|
else if (ch == '\'' || ch == '\"') {
|
2009-03-15 22:09:27 +01:00
|
|
|
std::string line;
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-03-04 18:02:45 +01:00
|
|
|
// read char
|
2009-01-24 18:15:38 +01:00
|
|
|
bool special = false;
|
|
|
|
char c = ch;
|
2011-10-13 20:53:06 +02:00
|
|
|
do {
|
2009-01-24 18:15:38 +01:00
|
|
|
// Append token..
|
2009-03-15 22:09:27 +01:00
|
|
|
line += c;
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Special sequence '\.'
|
2010-04-02 07:30:58 +02:00
|
|
|
if (special)
|
2009-01-24 18:15:38 +01:00
|
|
|
special = false;
|
|
|
|
else
|
|
|
|
special = (c == '\\');
|
|
|
|
|
|
|
|
// Get next character
|
|
|
|
c = (char)code.get();
|
2011-10-13 20:53:06 +02:00
|
|
|
} while (code.good() && (special || c != ch));
|
2009-03-15 22:09:27 +01:00
|
|
|
line += ch;
|
2009-03-04 18:02:45 +01:00
|
|
|
|
2009-03-15 22:09:27 +01:00
|
|
|
// Handle #file "file.h"
|
2011-10-13 20:53:06 +02:00
|
|
|
if (CurrentToken == "#file") {
|
2009-01-24 18:15:38 +01:00
|
|
|
// Extract the filename
|
2009-03-15 22:09:27 +01:00
|
|
|
line = line.substr(1, line.length() - 2);
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Has this file been tokenized already?
|
|
|
|
++lineno;
|
|
|
|
bool foundOurfile = false;
|
|
|
|
fileIndexes.push_back(FileIndex);
|
2011-10-29 20:36:44 +02:00
|
|
|
for (unsigned int i = 0; i < _files.size(); ++i) {
|
2011-12-08 21:28:34 +01:00
|
|
|
if (Path::sameFileName(_files[i], line)) {
|
2009-01-24 18:15:38 +01:00
|
|
|
// Use this index
|
|
|
|
foundOurfile = true;
|
|
|
|
FileIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!foundOurfile) {
|
2009-01-24 18:15:38 +01:00
|
|
|
// The "_files" vector remembers what files have been tokenized..
|
2010-10-29 21:21:27 +02:00
|
|
|
_files.push_back(Path::simplifyPath(line.c_str()));
|
2009-02-07 21:55:25 +01:00
|
|
|
FileIndex = static_cast<unsigned int>(_files.size() - 1);
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lineNumbers.push_back(lineno);
|
2009-03-15 22:09:27 +01:00
|
|
|
lineno = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2009-03-15 22:09:27 +01:00
|
|
|
// Add previous token
|
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
2011-12-18 13:33:23 +01:00
|
|
|
if (!CurrentToken.empty())
|
|
|
|
_tokensBack->setExpandedMacro(expandedMacro);
|
2009-03-15 22:09:27 +01:00
|
|
|
|
|
|
|
// Add content of the string
|
2009-01-24 18:15:38 +01:00
|
|
|
addtoken(line.c_str(), lineno, FileIndex);
|
2011-12-18 13:33:23 +01:00
|
|
|
if (!line.empty())
|
|
|
|
_tokensBack->setExpandedMacro(expandedMacro);
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
2009-03-15 22:09:27 +01:00
|
|
|
|
|
|
|
CurrentToken.clear();
|
|
|
|
|
|
|
|
continue;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2012-01-01 20:17:16 +01:00
|
|
|
if (ch == '.' &&
|
|
|
|
CurrentToken.length() > 0 &&
|
|
|
|
std::isdigit(CurrentToken[0])) {
|
|
|
|
// Don't separate doubles "5.4"
|
|
|
|
} else if (strchr("+-", ch) &&
|
|
|
|
CurrentToken.length() > 0 &&
|
|
|
|
std::isdigit(CurrentToken[0]) &&
|
|
|
|
CurrentToken.compare(0,2,"0x") != 0 &&
|
|
|
|
(CurrentToken[CurrentToken.length()-1] == 'e' ||
|
|
|
|
CurrentToken[CurrentToken.length()-1] == 'E')) {
|
|
|
|
// Don't separate doubles "4.2e+10"
|
|
|
|
} else if (CurrentToken.empty() && ch == '.' && std::isdigit(code.peek())) {
|
|
|
|
// tokenize .125 into 0.125
|
|
|
|
CurrentToken = "0";
|
|
|
|
} else if (ch=='&' && code.peek() == '&') {
|
|
|
|
if (!CurrentToken.empty()) {
|
2010-04-03 09:54:10 +02:00
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex, true);
|
2011-12-18 13:33:23 +01:00
|
|
|
if (!CurrentToken.empty())
|
|
|
|
_tokensBack->setExpandedMacro(expandedMacro);
|
2009-02-08 10:51:45 +01:00
|
|
|
CurrentToken.clear();
|
2012-01-01 20:17:16 +01:00
|
|
|
}
|
2009-03-15 22:09:27 +01:00
|
|
|
|
2012-01-01 20:17:16 +01:00
|
|
|
// &&
|
|
|
|
ch = (char)code.get();
|
|
|
|
addtoken("&&", lineno, FileIndex, true);
|
|
|
|
_tokensBack->setExpandedMacro(expandedMacro);
|
|
|
|
continue;
|
|
|
|
} else if (ch==':' && CurrentToken.empty() && code.peek() == ' ') {
|
|
|
|
// :
|
|
|
|
addtoken(":", lineno, FileIndex, true);
|
|
|
|
_tokensBack->setExpandedMacro(expandedMacro);
|
|
|
|
CurrentToken.clear();
|
|
|
|
continue;
|
|
|
|
} else if (ch==':' && CurrentToken.empty() && code.peek() == ':') {
|
|
|
|
// ::
|
|
|
|
ch = (char)code.get();
|
|
|
|
addtoken("::", lineno, FileIndex, true);
|
|
|
|
_tokensBack->setExpandedMacro(expandedMacro);
|
|
|
|
CurrentToken.clear();
|
|
|
|
continue;
|
|
|
|
} else if (strchr("+-*/%&|^?!=<>[](){};:,.~\n ", ch)) {
|
|
|
|
if (CurrentToken == "#file") {
|
|
|
|
// Handle this where strings are handled
|
|
|
|
continue;
|
|
|
|
} else if (CurrentToken == "#endfile") {
|
|
|
|
if (lineNumbers.empty() || fileIndexes.empty()) {
|
|
|
|
cppcheckError(0);
|
|
|
|
deallocateTokens();
|
|
|
|
return;
|
2009-03-15 22:09:27 +01:00
|
|
|
}
|
|
|
|
|
2012-01-01 20:17:16 +01:00
|
|
|
lineno = lineNumbers.back();
|
|
|
|
lineNumbers.pop_back();
|
|
|
|
FileIndex = fileIndexes.back();
|
|
|
|
fileIndexes.pop_back();
|
2009-02-08 10:51:45 +01:00
|
|
|
CurrentToken.clear();
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-01 20:17:16 +01:00
|
|
|
|
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex, true);
|
|
|
|
if (!CurrentToken.empty())
|
|
|
|
_tokensBack->setExpandedMacro(expandedMacro);
|
|
|
|
|
|
|
|
CurrentToken.clear();
|
|
|
|
|
|
|
|
if (ch == '\n') {
|
|
|
|
++lineno;
|
|
|
|
continue;
|
|
|
|
} else if (ch == ' ') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentToken += ch;
|
|
|
|
// Add "++", "--" or ">>" token
|
|
|
|
if ((ch == '+' || ch == '-' || ch == '>') && (code.peek() == ch))
|
|
|
|
CurrentToken += (char)code.get();
|
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
_tokensBack->setExpandedMacro(expandedMacro);
|
|
|
|
CurrentToken.clear();
|
|
|
|
continue;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CurrentToken += ch;
|
|
|
|
}
|
2010-04-03 09:54:10 +02:00
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex, true);
|
2011-12-18 13:33:23 +01:00
|
|
|
if (!CurrentToken.empty())
|
|
|
|
_tokensBack->setExpandedMacro(expandedMacro);
|
2010-08-03 16:36:21 +02:00
|
|
|
_tokens->assignProgressValues();
|
2009-05-09 21:32:29 +02:00
|
|
|
}
|
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
void Tokenizer::duplicateTypedefError(const Token *tok1, const Token *tok2, const std::string &type)
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
2011-10-22 13:26:33 +02:00
|
|
|
if (tok1 && !(_settings->isEnabled("style") && _settings->inconclusive))
|
2010-02-20 09:07:29 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
2011-06-29 03:46:54 +02:00
|
|
|
std::string tok2_str;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok1 && tok2) {
|
2011-06-29 03:46:54 +02:00
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok1->linenr();
|
|
|
|
loc.setfile(file(tok1));
|
|
|
|
locationList.push_back(loc);
|
|
|
|
loc.line = tok2->linenr();
|
|
|
|
loc.setfile(file(tok2));
|
|
|
|
locationList.push_back(loc);
|
|
|
|
tok2_str = tok2->str();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2011-06-29 03:46:54 +02:00
|
|
|
tok2_str = "name";
|
2010-02-20 09:07:29 +01:00
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
2010-07-14 22:11:32 +02:00
|
|
|
Severity::style,
|
2011-06-29 03:46:54 +02:00
|
|
|
std::string(type + " '" + tok2_str +
|
2010-04-15 20:08:51 +02:00
|
|
|
"' hides typedef with same name"),
|
2011-04-14 18:02:01 +02:00
|
|
|
"variableHidingTypedef",
|
2011-10-22 13:26:33 +02:00
|
|
|
true);
|
2010-02-20 09:07:29 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_errorLogger)
|
2010-02-20 09:07:29 +01:00
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
|
|
|
}
|
|
|
|
|
2010-04-12 19:05:31 +02:00
|
|
|
void Tokenizer::duplicateDeclarationError(const Token *tok1, const Token *tok2, const std::string &type)
|
|
|
|
{
|
2011-08-07 09:28:08 +02:00
|
|
|
if (tok1 && !(_settings->isEnabled("style")))
|
2010-04-12 19:05:31 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
2011-06-29 03:46:54 +02:00
|
|
|
std::string tok2_str;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok1 && tok2) {
|
2011-06-29 03:46:54 +02:00
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok1->linenr();
|
|
|
|
loc.setfile(file(tok1));
|
|
|
|
locationList.push_back(loc);
|
|
|
|
loc.line = tok2->linenr();
|
|
|
|
loc.setfile(file(tok2));
|
|
|
|
locationList.push_back(loc);
|
|
|
|
tok2_str = tok2->str();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2011-06-29 03:46:54 +02:00
|
|
|
tok2_str = "name";
|
2010-04-12 19:05:31 +02:00
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
2010-07-14 22:11:32 +02:00
|
|
|
Severity::style,
|
2011-06-29 03:46:54 +02:00
|
|
|
std::string(type + " '" + tok2_str +
|
2010-04-15 20:08:51 +02:00
|
|
|
"' forward declaration unnecessary, already declared"),
|
2011-04-14 18:02:01 +02:00
|
|
|
"unnecessaryForwardDeclaration",
|
|
|
|
false);
|
2010-04-12 19:05:31 +02:00
|
|
|
|
|
|
|
if (_errorLogger)
|
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
|
|
|
}
|
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
// check if this statement is a duplicate definition
|
2011-08-15 00:06:05 +02:00
|
|
|
bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef, bool undefinedStruct)
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
|
|
|
// check for an end of definition
|
|
|
|
const Token * tok = *tokPtr;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok && Token::Match(tok->next(), ";|,|[|=|)|>|(|{")) {
|
2010-02-20 09:07:29 +01:00
|
|
|
const Token * end = tok->next();
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (end->str() == "[") {
|
2010-02-20 09:07:29 +01:00
|
|
|
end = end->link()->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (end->str() == ",") {
|
2010-02-26 21:43:00 +01:00
|
|
|
// check for derived class
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous(), "public|private|protected"))
|
2010-02-26 21:43:00 +01:00
|
|
|
return false;
|
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
// find end of definition
|
|
|
|
int level = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (end && end->next() && (!Token::Match(end->next(), ";|)|>") ||
|
2011-10-13 20:53:06 +02:00
|
|
|
(end->next()->str() == ")" && level == 0))) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end->next()->str() == "(")
|
2011-10-29 20:36:44 +02:00
|
|
|
++level;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (end->next()->str() == ")")
|
2011-10-29 20:36:44 +02:00
|
|
|
--level;
|
2010-02-03 07:58:36 +01:00
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
end = end->next();
|
|
|
|
}
|
2011-07-30 16:12:35 +02:00
|
|
|
if (end)
|
|
|
|
end = end->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (end->str() == "(") {
|
|
|
|
if (tok->previous()->str().find("operator") == 0) {
|
2010-02-27 07:27:51 +01:00
|
|
|
// conversion operator
|
|
|
|
return false;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->previous()->str() == "typedef") {
|
2010-02-27 07:27:51 +01:00
|
|
|
// typedef of function returning this type
|
|
|
|
return false;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::Match(tok->previous(), "public:|private:|protected:")) {
|
2010-02-27 07:27:51 +01:00
|
|
|
return false;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->previous()->str() == ">") {
|
2010-04-18 09:13:55 +02:00
|
|
|
if (!Token::Match(tok->tokAt(-2), "%type%"))
|
|
|
|
return false;
|
|
|
|
|
2010-12-16 07:48:46 +01:00
|
|
|
if (!Token::Match(tok->tokAt(-3), ",|<"))
|
|
|
|
return false;
|
|
|
|
|
2010-02-27 07:27:51 +01:00
|
|
|
duplicateTypedefError(*tokPtr, name, "Template instantiation");
|
|
|
|
*tokPtr = end->link();
|
|
|
|
return true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::Match(tok->previous(), "%type%")) {
|
|
|
|
if (end->link()->next()->str() == "{") {
|
2010-02-27 07:27:51 +01:00
|
|
|
duplicateTypedefError(*tokPtr, name, "Function");
|
|
|
|
*tokPtr = end->link()->next()->link();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-02-20 09:07:29 +01:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (end) {
|
|
|
|
if (Token::simpleMatch(end, ") {")) { // function parameter ?
|
2010-02-20 09:07:29 +01:00
|
|
|
// look backwards
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous(), "%type%") &&
|
2011-10-13 20:53:06 +02:00
|
|
|
!Token::Match(tok->previous(), "return|new|const")) {
|
2010-02-20 09:07:29 +01:00
|
|
|
duplicateTypedefError(*tokPtr, name, "Function parameter");
|
|
|
|
// duplicate definition so skip entire function
|
|
|
|
*tokPtr = end->next()->link();
|
|
|
|
return true;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (end->str() == ">") { // template parameter ?
|
2010-02-20 09:07:29 +01:00
|
|
|
// look backwards
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous(), "%type%") &&
|
2011-10-13 20:53:06 +02:00
|
|
|
!Token::Match(tok->previous(), "return|new|const|volatile")) {
|
2010-02-20 09:07:29 +01:00
|
|
|
// duplicate definition so skip entire template
|
2010-04-02 07:30:58 +02:00
|
|
|
while (end && end->str() != "{")
|
2010-02-20 09:07:29 +01:00
|
|
|
end = end->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (end) {
|
2010-02-20 09:07:29 +01:00
|
|
|
duplicateTypedefError(*tokPtr, name, "Template parameter");
|
|
|
|
*tokPtr = end->link();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-02-20 09:07:29 +01:00
|
|
|
// look backwards
|
2010-07-13 08:08:12 +02:00
|
|
|
if (Token::Match(tok->previous(), "typedef|}|>") ||
|
|
|
|
(tok->previous()->str() == "*" && tok->next()->str() != "(") ||
|
2010-04-02 07:30:58 +02:00
|
|
|
(Token::Match(tok->previous(), "%type%") &&
|
2010-12-22 18:37:23 +01:00
|
|
|
(!Token::Match(tok->previous(), "return|new|const|friend|public|private|protected|throw|extern") &&
|
2011-10-13 20:53:06 +02:00
|
|
|
!Token::simpleMatch(tok->tokAt(-2), "friend class")))) {
|
2010-02-20 09:07:29 +01:00
|
|
|
// scan backwards for the end of the previous statement
|
|
|
|
int level = (tok->previous()->str() == "}") ? 1 : 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok && tok->previous() && (!Token::Match(tok->previous(), ";|{") || (level != 0))) {
|
|
|
|
if (tok->previous()->str() == "}") {
|
2011-02-12 01:09:24 +01:00
|
|
|
tok = tok->previous()->link();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->previous()->str() == "typedef") {
|
2010-02-20 09:07:29 +01:00
|
|
|
duplicateTypedefError(*tokPtr, name, "Typedef");
|
|
|
|
return true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->previous()->str() == "enum") {
|
2010-02-20 09:07:29 +01:00
|
|
|
duplicateTypedefError(*tokPtr, name, "Enum");
|
|
|
|
return true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->previous()->str() == "struct") {
|
2011-02-12 01:09:24 +01:00
|
|
|
if (tok->strAt(-2) == "typedef" &&
|
|
|
|
tok->next()->str() == "{" &&
|
2011-10-13 20:53:06 +02:00
|
|
|
typeDef->strAt(3) != "{") {
|
2011-02-12 01:09:24 +01:00
|
|
|
// declaration after forward declaration
|
|
|
|
return true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->next()->str() == "{") {
|
2011-08-15 00:06:05 +02:00
|
|
|
if (!undefinedStruct)
|
|
|
|
duplicateTypedefError(*tokPtr, name, "Struct");
|
|
|
|
return true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::Match(tok->next(), ")|*")) {
|
2011-08-15 00:50:33 +02:00
|
|
|
return true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->next()->str() == name->str()) {
|
2011-08-15 12:56:15 +02:00
|
|
|
return true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->next()->str() != ";") {
|
2010-04-12 19:05:31 +02:00
|
|
|
duplicateTypedefError(*tokPtr, name, "Struct");
|
|
|
|
return true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-04-12 19:05:31 +02:00
|
|
|
// forward declaration after declaration
|
|
|
|
duplicateDeclarationError(*tokPtr, name, "Struct");
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->previous()->str() == "union") {
|
|
|
|
if (tok->next()->str() != ";") {
|
2010-04-14 19:06:51 +02:00
|
|
|
duplicateTypedefError(*tokPtr, name, "Union");
|
|
|
|
return true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-04-14 19:06:51 +02:00
|
|
|
// forward declaration after declaration
|
|
|
|
duplicateDeclarationError(*tokPtr, name, "Union");
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->previous()->str() == "class") {
|
|
|
|
if (tok->next()->str() != ";") {
|
2010-04-14 19:06:51 +02:00
|
|
|
duplicateTypedefError(*tokPtr, name, "Class");
|
|
|
|
return true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-04-14 19:06:51 +02:00
|
|
|
// forward declaration after declaration
|
|
|
|
duplicateDeclarationError(*tokPtr, name, "Class");
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->previous()->str() == "{")
|
2011-10-29 20:36:44 +02:00
|
|
|
--level;
|
2010-02-20 09:07:29 +01:00
|
|
|
|
|
|
|
tok = tok->previous();
|
|
|
|
}
|
|
|
|
|
|
|
|
duplicateTypedefError(*tokPtr, name, "Variable");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-02-03 07:58:36 +01:00
|
|
|
}
|
|
|
|
}
|
2010-02-20 09:07:29 +01:00
|
|
|
|
2010-02-03 07:58:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-26 20:44:13 +02:00
|
|
|
void Tokenizer::unsupportedTypedef(const Token *tok) const
|
|
|
|
{
|
2010-08-27 20:28:00 +02:00
|
|
|
if (!_settings->debugwarnings)
|
|
|
|
return;
|
|
|
|
|
2010-08-26 20:44:13 +02:00
|
|
|
std::ostringstream str;
|
|
|
|
const Token *tok1 = tok;
|
2011-10-29 20:36:44 +02:00
|
|
|
unsigned int level = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok) {
|
2011-01-07 08:02:47 +01:00
|
|
|
if (level == 0 && tok->str() == ";")
|
|
|
|
break;
|
|
|
|
else if (tok->str() == "{")
|
2011-10-29 20:36:44 +02:00
|
|
|
++level;
|
|
|
|
else if (tok->str() == "}") {
|
|
|
|
if (!level)
|
|
|
|
break;
|
|
|
|
--level;
|
|
|
|
}
|
2011-01-07 08:02:47 +01:00
|
|
|
|
2010-08-26 20:44:13 +02:00
|
|
|
if (tok != tok1)
|
|
|
|
str << " ";
|
|
|
|
str << tok->str();
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
if (tok)
|
|
|
|
str << " ;";
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok1->linenr();
|
|
|
|
loc.setfile(file(tok1));
|
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
2010-08-27 20:28:00 +02:00
|
|
|
Severity::debug,
|
2010-08-26 20:44:13 +02:00
|
|
|
"Failed to parse \'" + str.str() + "\'. The checking continues anyway.",
|
2011-04-14 18:02:01 +02:00
|
|
|
"debug",
|
|
|
|
false);
|
2010-08-26 20:44:13 +02:00
|
|
|
|
|
|
|
if (_errorLogger)
|
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
|
|
|
}
|
|
|
|
|
2011-01-04 07:43:40 +01:00
|
|
|
Token * Tokenizer::deleteInvalidTypedef(Token *typeDef)
|
|
|
|
{
|
|
|
|
Token *tok = NULL;
|
2011-10-29 20:36:44 +02:00
|
|
|
unsigned int level = 0;
|
2011-01-04 07:43:40 +01:00
|
|
|
|
|
|
|
// remove typedef but leave ;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (typeDef->next()) {
|
2011-12-04 17:30:25 +01:00
|
|
|
if (level == 0 && typeDef->next()->str() == ";") {
|
|
|
|
typeDef->deleteNext();
|
2011-01-07 08:02:47 +01:00
|
|
|
break;
|
2011-12-04 17:30:25 +01:00
|
|
|
} else if (typeDef->next()->str() == "{")
|
2011-10-29 20:36:44 +02:00
|
|
|
++level;
|
|
|
|
else if (typeDef->next()->str() == "}") {
|
|
|
|
if (!level)
|
|
|
|
break;
|
|
|
|
--level;
|
|
|
|
}
|
2011-01-04 07:43:40 +01:00
|
|
|
typeDef->deleteNext();
|
2011-01-07 08:02:47 +01:00
|
|
|
}
|
2011-01-04 07:43:40 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (typeDef != _tokens) {
|
2011-01-04 07:43:40 +01:00
|
|
|
tok = typeDef->previous();
|
|
|
|
tok->deleteNext();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-01-04 07:43:40 +01:00
|
|
|
_tokens->deleteThis();
|
|
|
|
tok = _tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
struct Space {
|
2010-02-03 07:58:36 +01:00
|
|
|
std::string className;
|
|
|
|
const Token * classEnd;
|
2011-10-24 02:52:55 +02:00
|
|
|
bool isNamespace;
|
2010-02-03 07:58:36 +01:00
|
|
|
};
|
|
|
|
|
2011-01-05 17:42:55 +01:00
|
|
|
static Token *splitDefinitionFromTypedef(Token *tok)
|
|
|
|
{
|
|
|
|
Token *tok1;
|
|
|
|
std::string name;
|
|
|
|
bool isConst = false;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok->next()->str() == "const") {
|
2011-12-05 00:08:50 +01:00
|
|
|
tok->deleteNext();
|
2011-01-05 17:42:55 +01:00
|
|
|
isConst = true;
|
|
|
|
}
|
|
|
|
|
2011-10-29 01:57:53 +02:00
|
|
|
if (tok->strAt(2) == "{") { // unnamed
|
2011-11-20 14:22:39 +01:00
|
|
|
tok1 = tok->linkAt(2);
|
2011-01-05 17:42:55 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok1 && tok1->next()) {
|
2011-01-05 17:42:55 +01:00
|
|
|
// use typedef name if available
|
|
|
|
if (Token::Match(tok1->next(), "%type%"))
|
|
|
|
name = tok1->next()->str();
|
2011-10-13 20:53:06 +02:00
|
|
|
else { // create a unique name
|
2011-10-29 20:36:44 +02:00
|
|
|
static unsigned int count = 0;
|
|
|
|
name = "Unnamed" + MathLib::toString<unsigned int>(count++);
|
2011-01-05 17:42:55 +01:00
|
|
|
}
|
2011-12-08 21:28:34 +01:00
|
|
|
tok->next()->insertToken(name);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2011-01-05 17:42:55 +01:00
|
|
|
return NULL;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->strAt(3) == ":") {
|
2011-01-05 17:42:55 +01:00
|
|
|
tok1 = tok->tokAt(4);
|
|
|
|
while (tok1 && tok1->str() != "{")
|
|
|
|
tok1 = tok1->next();
|
|
|
|
if (!tok1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tok1 = tok1->link();
|
|
|
|
|
2011-10-29 01:57:53 +02:00
|
|
|
name = tok->strAt(2);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else { // has a name
|
2011-11-20 14:22:39 +01:00
|
|
|
tok1 = tok->linkAt(3);
|
2011-01-05 17:42:55 +01:00
|
|
|
|
|
|
|
if (!tok1)
|
|
|
|
return NULL;
|
|
|
|
|
2011-10-29 01:57:53 +02:00
|
|
|
name = tok->strAt(2);
|
2011-01-05 17:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tok1->insertToken(";");
|
|
|
|
tok1 = tok1->next();
|
2011-02-19 20:18:37 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok1->next() && tok1->next()->str() == ";" && tok1 && tok1->previous()->str() == "}") {
|
2011-02-19 20:18:37 +01:00
|
|
|
tok->deleteThis();
|
|
|
|
tok1->deleteThis();
|
|
|
|
return NULL;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-02-19 20:18:37 +01:00
|
|
|
tok1->insertToken("typedef");
|
2011-01-05 17:42:55 +01:00
|
|
|
tok1 = tok1->next();
|
2011-02-19 20:18:37 +01:00
|
|
|
Token * tok3 = tok1;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (isConst) {
|
2011-02-19 20:18:37 +01:00
|
|
|
tok1->insertToken("const");
|
|
|
|
tok1 = tok1->next();
|
|
|
|
}
|
|
|
|
tok1->insertToken(tok->next()->str()); // struct, union or enum
|
|
|
|
tok1 = tok1->next();
|
2011-12-08 21:28:34 +01:00
|
|
|
tok1->insertToken(name);
|
2011-02-19 20:18:37 +01:00
|
|
|
tok->deleteThis();
|
|
|
|
tok = tok3;
|
2011-01-05 17:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2011-02-13 19:34:55 +01:00
|
|
|
/* This function is called when processing function related typedefs.
|
|
|
|
* If simplifyTypedef generates an "Internal Error" message and the
|
|
|
|
* code that generated it deals in some way with functions, then this
|
2011-10-13 20:55:55 +02:00
|
|
|
* function will probably need to be extended to handle a new function
|
2011-02-13 19:34:55 +01:00
|
|
|
* related pattern */
|
|
|
|
static Token *processFunc(Token *tok2, bool inOperator)
|
|
|
|
{
|
|
|
|
if (tok2->next() && tok2->next()->str() != ")" &&
|
2011-10-13 20:53:06 +02:00
|
|
|
tok2->next()->str() != ",") {
|
2011-02-13 19:34:55 +01:00
|
|
|
// skip over tokens for some types of canonicalization
|
|
|
|
if (Token::Match(tok2->next(), "( * %type% ) ("))
|
2011-11-20 14:22:39 +01:00
|
|
|
tok2 = tok2->linkAt(5);
|
2011-02-13 19:34:55 +01:00
|
|
|
else if (Token::Match(tok2->next(), "* ( * %type% ) ("))
|
2011-11-20 14:22:39 +01:00
|
|
|
tok2 = tok2->linkAt(6);
|
2011-02-14 01:02:57 +01:00
|
|
|
else if (Token::Match(tok2->next(), "* ( * %type% ) ;"))
|
|
|
|
tok2 = tok2->tokAt(5);
|
2011-02-13 19:34:55 +01:00
|
|
|
else if (Token::Match(tok2->next(), "* ( %type% [") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::Match(tok2->linkAt(4), "] ) ;|="))
|
|
|
|
tok2 = tok2->linkAt(4)->next();
|
2011-02-13 19:34:55 +01:00
|
|
|
else if (Token::Match(tok2->next(), "* ( * %type% ("))
|
2011-11-20 14:22:39 +01:00
|
|
|
tok2 = tok2->linkAt(5)->next();
|
2011-11-26 21:02:04 +01:00
|
|
|
else if (Token::simpleMatch(tok2->next(), "* [") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::simpleMatch(tok2->linkAt(2), "] ;"))
|
2011-02-23 04:11:17 +01:00
|
|
|
tok2 = tok2->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2011-02-13 19:34:55 +01:00
|
|
|
if (tok2->next()->str() == "(")
|
|
|
|
tok2 = tok2->next()->link();
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (!inOperator && !Token::Match(tok2->next(), "[|>|;")) {
|
2011-02-13 19:34:55 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
while (Token::Match(tok2, "*|&") &&
|
|
|
|
!Token::Match(tok2->next(), ")|>"))
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// skip over namespace
|
|
|
|
while (Token::Match(tok2, "%var% ::"))
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
|
|
|
|
if (tok2->str() == "(" &&
|
2011-10-13 20:53:06 +02:00
|
|
|
tok2->link()->next()->str() == "(") {
|
2011-02-13 19:34:55 +01:00
|
|
|
tok2 = tok2->link();
|
|
|
|
|
|
|
|
if (tok2->next()->str() == "(")
|
|
|
|
tok2 = tok2->next()->link();
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip over typedef parameter
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->next()->str() == "(") {
|
2011-02-13 19:34:55 +01:00
|
|
|
tok2 = tok2->next()->link();
|
|
|
|
|
|
|
|
if (tok2->next()->str() == "(")
|
|
|
|
tok2 = tok2->next()->link();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tok2;
|
|
|
|
}
|
|
|
|
|
2009-09-30 13:35:00 +02:00
|
|
|
void Tokenizer::simplifyTypedef()
|
|
|
|
{
|
2011-01-17 07:21:59 +01:00
|
|
|
std::vector<Space> spaceInfo;
|
2010-02-03 07:58:36 +01:00
|
|
|
bool isNamespace = false;
|
2009-09-30 13:35:00 +02:00
|
|
|
std::string className;
|
2010-02-03 07:58:36 +01:00
|
|
|
bool hasClass = false;
|
2011-12-04 17:15:53 +01:00
|
|
|
bool goback = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
2010-08-03 16:36:21 +02:00
|
|
|
if (_errorLogger && !_files.empty())
|
|
|
|
_errorLogger->reportProgress(_files[0], "Tokenize (typedef)", tok->progressValue());
|
2010-07-25 18:19:37 +02:00
|
|
|
|
2011-12-04 17:15:53 +01:00
|
|
|
if (goback) {
|
|
|
|
//jump back once, see the comment at the end of the function
|
|
|
|
goback = false;
|
|
|
|
tok = tok->previous();
|
|
|
|
}
|
|
|
|
|
2011-02-03 07:58:49 +01:00
|
|
|
if (Token::Match(tok, "class|struct|namespace %any%") &&
|
2011-10-13 20:53:06 +02:00
|
|
|
(!tok->previous() || (tok->previous() && tok->previous()->str() != "enum"))) {
|
2010-02-03 07:58:36 +01:00
|
|
|
isNamespace = (tok->str() == "namespace");
|
|
|
|
hasClass = true;
|
2009-09-30 13:35:00 +02:00
|
|
|
className = tok->next()->str();
|
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (hasClass && tok->str() == ";") {
|
2010-02-03 07:58:36 +01:00
|
|
|
hasClass = false;
|
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (hasClass && tok->str() == "{") {
|
2011-01-17 07:21:59 +01:00
|
|
|
Space info;
|
2010-02-03 07:58:36 +01:00
|
|
|
info.isNamespace = isNamespace;
|
|
|
|
info.className = className;
|
|
|
|
info.classEnd = tok->link();
|
|
|
|
spaceInfo.push_back(info);
|
2009-09-30 13:35:00 +02:00
|
|
|
|
2010-02-03 07:58:36 +01:00
|
|
|
hasClass = false;
|
2009-09-30 13:35:00 +02:00
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (!spaceInfo.empty() && tok->str() == "}" && spaceInfo.back().classEnd == tok) {
|
2010-02-03 07:58:36 +01:00
|
|
|
spaceInfo.pop_back();
|
2009-09-30 13:35:00 +02:00
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->str() != "typedef")
|
2009-09-30 13:35:00 +02:00
|
|
|
continue;
|
|
|
|
|
2011-01-14 07:41:22 +01:00
|
|
|
// check for syntax errors
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok->previous() && tok->previous()->str() == "(") {
|
2011-01-14 07:41:22 +01:00
|
|
|
syntaxError(tok);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-14 19:06:51 +02:00
|
|
|
// pull struct, union, enum or class definition out of typedef
|
|
|
|
// use typedef name for unnamed struct, union, enum or class
|
2010-07-02 15:22:29 +02:00
|
|
|
if (Token::Match(tok->next(), "const| struct|enum|union|class %type% {") ||
|
2011-10-13 20:53:06 +02:00
|
|
|
Token::Match(tok->next(), "const| struct|enum|union|class {")) {
|
2011-01-05 17:42:55 +01:00
|
|
|
Token *tok1 = splitDefinitionFromTypedef(tok);
|
|
|
|
if (!tok1)
|
|
|
|
continue;
|
|
|
|
tok = tok1;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::Match(tok->next(), "const| struct|class %type% :")) {
|
2011-01-05 17:42:55 +01:00
|
|
|
Token *tok1 = tok;
|
|
|
|
while (tok1 && tok1->str() != ";" && tok1->str() != "{")
|
|
|
|
tok1 = tok1->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok1 && tok1->str() == "{") {
|
2011-01-05 17:42:55 +01:00
|
|
|
tok1 = splitDefinitionFromTypedef(tok);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok1)
|
2010-01-10 08:49:02 +01:00
|
|
|
continue;
|
2011-01-05 17:42:55 +01:00
|
|
|
tok = tok1;
|
2010-01-10 08:49:02 +01:00
|
|
|
}
|
2009-12-28 17:57:52 +01:00
|
|
|
}
|
2010-09-08 06:45:57 +02:00
|
|
|
|
2011-08-15 00:06:05 +02:00
|
|
|
/** @todo add support for union */
|
|
|
|
bool undefinedStruct = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(tok, "typedef enum|struct %type% %type% ;") && tok->strAt(2) == tok->strAt(3)) {
|
2011-10-29 01:57:53 +02:00
|
|
|
if (tok->next()->str() == "enum") {
|
2011-12-08 01:41:53 +01:00
|
|
|
tok->deleteNext(3);
|
2011-12-05 00:26:08 +01:00
|
|
|
tok->deleteThis();
|
2011-12-04 18:56:12 +01:00
|
|
|
if (tok->next())
|
|
|
|
tok->deleteThis();
|
|
|
|
//now the next token to process is 'tok', not 'tok->next()';
|
|
|
|
goback = true;
|
2011-08-15 00:06:05 +02:00
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-08-15 00:06:05 +02:00
|
|
|
const std::string pattern("struct " + tok->strAt(2) + " {|:");
|
|
|
|
const Token *tok2 = Token::findmatch(_tokens, pattern.c_str(), tok);
|
|
|
|
if (!tok2)
|
|
|
|
undefinedStruct = true;
|
|
|
|
}
|
2010-09-08 06:45:57 +02:00
|
|
|
}
|
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
Token *typeName;
|
2010-01-18 19:06:50 +01:00
|
|
|
std::list<std::string> pointers;
|
2010-01-30 08:00:11 +01:00
|
|
|
Token *typeStart = 0;
|
|
|
|
Token *typeEnd = 0;
|
|
|
|
Token *argStart = 0;
|
|
|
|
Token *argEnd = 0;
|
2010-01-30 19:41:22 +01:00
|
|
|
Token *arrayStart = 0;
|
|
|
|
Token *arrayEnd = 0;
|
2010-05-23 10:46:39 +02:00
|
|
|
Token *specStart = 0;
|
|
|
|
Token *specEnd = 0;
|
2010-01-16 09:18:21 +01:00
|
|
|
Token *typeDef = tok;
|
2010-05-25 20:43:44 +02:00
|
|
|
Token *argFuncRetStart = 0;
|
|
|
|
Token *argFuncRetEnd = 0;
|
2010-11-11 17:02:04 +01:00
|
|
|
Token *funcStart = 0;
|
|
|
|
Token *funcEnd = 0;
|
2011-10-30 18:34:49 +01:00
|
|
|
unsigned short offset = 1;
|
2010-05-10 07:12:06 +02:00
|
|
|
bool function = false;
|
2010-01-20 21:16:40 +01:00
|
|
|
bool functionPtr = false;
|
2010-01-27 19:03:24 +01:00
|
|
|
bool functionRef = false;
|
2010-05-25 20:43:44 +02:00
|
|
|
bool functionRetFuncPtr = false;
|
|
|
|
bool functionPtrRetFuncPtr = false;
|
2010-05-28 06:55:54 +02:00
|
|
|
bool ptrToArray = false;
|
|
|
|
bool refToArray = false;
|
|
|
|
bool ptrMember = false;
|
2010-11-11 06:40:49 +01:00
|
|
|
bool typeOf = false;
|
2010-05-29 07:52:06 +02:00
|
|
|
Token *namespaceStart = 0;
|
|
|
|
Token *namespaceEnd = 0;
|
2009-12-29 07:28:00 +01:00
|
|
|
|
2011-02-22 13:48:34 +01:00
|
|
|
// check for invalid input
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!tok->next()) {
|
2011-02-22 13:48:34 +01:00
|
|
|
syntaxError(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-02 17:12:46 +01:00
|
|
|
if (Token::simpleMatch(tok->next(), "::") ||
|
2011-10-13 20:53:06 +02:00
|
|
|
Token::Match(tok->next(), "%type%")) {
|
2010-01-30 08:00:11 +01:00
|
|
|
typeStart = tok->next();
|
2010-01-31 21:46:18 +01:00
|
|
|
offset = 1;
|
2010-01-17 08:16:17 +01:00
|
|
|
|
2011-09-02 01:10:58 +02:00
|
|
|
while (Token::Match(tok->tokAt(offset), "const|signed|unsigned|struct|enum %type%") ||
|
2011-03-25 03:26:42 +01:00
|
|
|
(tok->tokAt(offset + 1) && tok->tokAt(offset + 1)->isStandardType()))
|
2011-10-29 20:36:44 +02:00
|
|
|
++offset;
|
2010-05-23 10:46:39 +02:00
|
|
|
|
2010-01-31 21:46:18 +01:00
|
|
|
typeEnd = tok->tokAt(offset++);
|
|
|
|
|
|
|
|
bool atEnd = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (!atEnd) {
|
2011-02-02 17:12:46 +01:00
|
|
|
if (Token::simpleMatch(tok->tokAt(offset), "::"))
|
2010-01-31 21:46:18 +01:00
|
|
|
typeEnd = tok->tokAt(offset++);
|
|
|
|
|
2010-06-20 20:51:36 +02:00
|
|
|
if (Token::Match(tok->tokAt(offset), "%type%") &&
|
2010-05-10 07:12:06 +02:00
|
|
|
tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), "[|;|,|("))
|
2010-01-31 21:46:18 +01:00
|
|
|
typeEnd = tok->tokAt(offset++);
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::simpleMatch(tok->tokAt(offset), "const (")) {
|
2010-05-23 10:46:39 +02:00
|
|
|
typeEnd = tok->tokAt(offset++);
|
|
|
|
atEnd = true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2010-01-31 21:46:18 +01:00
|
|
|
atEnd = true;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2010-01-31 21:46:18 +01:00
|
|
|
continue; // invalid input
|
2009-12-29 07:28:00 +01:00
|
|
|
|
2011-02-22 13:48:34 +01:00
|
|
|
// check for invalid input
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!tok->tokAt(offset)) {
|
2011-02-22 13:48:34 +01:00
|
|
|
syntaxError(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-31 21:46:18 +01:00
|
|
|
// check for template
|
2011-11-13 13:10:59 +01:00
|
|
|
if (tok->strAt(offset) == "<") {
|
2011-10-29 20:36:44 +02:00
|
|
|
unsigned int level = 0;
|
|
|
|
unsigned int paren = 0;
|
2010-01-31 21:46:18 +01:00
|
|
|
typeEnd = tok->tokAt(offset + 1);
|
2011-10-13 20:53:06 +02:00
|
|
|
for (; typeEnd ; typeEnd = typeEnd->next()) {
|
|
|
|
if (typeEnd->str() == ">") {
|
2011-10-29 20:36:44 +02:00
|
|
|
if (!paren) {
|
|
|
|
if (!level)
|
2009-12-29 07:28:00 +01:00
|
|
|
break;
|
2011-10-29 20:36:44 +02:00
|
|
|
--level;
|
2009-12-29 07:28:00 +01:00
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (typeEnd->str() == "<") {
|
2011-10-29 20:36:44 +02:00
|
|
|
if (!paren)
|
|
|
|
++level;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (typeEnd->str() == "(")
|
2011-10-29 20:36:44 +02:00
|
|
|
++paren;
|
|
|
|
else if (typeEnd->str() == ")") {
|
|
|
|
if (!paren)
|
|
|
|
break;
|
|
|
|
--paren;
|
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
2009-12-29 07:28:00 +01:00
|
|
|
|
2010-06-20 20:51:36 +02:00
|
|
|
while (typeEnd && Token::Match(typeEnd->next(), ":: %type%"))
|
2010-01-30 08:00:11 +01:00
|
|
|
typeEnd = typeEnd->tokAt(2);
|
2010-01-13 07:59:47 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!typeEnd) {
|
2010-01-29 16:00:48 +01:00
|
|
|
// internal error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-11 21:41:33 +02:00
|
|
|
while (Token::Match(typeEnd->next(), "const|volatile"))
|
|
|
|
typeEnd = typeEnd->next();
|
|
|
|
|
2010-01-30 08:00:11 +01:00
|
|
|
tok = typeEnd;
|
2010-01-31 21:46:18 +01:00
|
|
|
offset = 1;
|
2010-01-18 19:06:50 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
|
2010-01-31 21:46:18 +01:00
|
|
|
// check for pointers and references
|
2010-07-05 23:03:54 +02:00
|
|
|
while (Token::Match(tok->tokAt(offset), "*|&|const"))
|
2011-11-13 13:10:59 +01:00
|
|
|
pointers.push_back(tok->strAt(offset++));
|
2009-10-03 17:02:23 +02:00
|
|
|
|
2011-03-09 02:24:57 +01:00
|
|
|
// check for invalid input
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!tok->tokAt(offset)) {
|
2011-03-09 02:24:57 +01:00
|
|
|
syntaxError(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(tok->tokAt(offset), "%type%")) {
|
2010-01-31 21:46:18 +01:00
|
|
|
// found the type name
|
2010-02-20 09:07:29 +01:00
|
|
|
typeName = tok->tokAt(offset++);
|
2010-01-18 19:06:50 +01:00
|
|
|
|
2010-01-31 21:46:18 +01:00
|
|
|
// check for array
|
2011-11-13 13:10:59 +01:00
|
|
|
if (tok->tokAt(offset) && tok->strAt(offset) == "[") {
|
2010-01-30 19:41:22 +01:00
|
|
|
arrayStart = tok->tokAt(offset);
|
|
|
|
|
|
|
|
bool atEnd = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (!atEnd) {
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), ";|,"))
|
2011-10-29 20:36:44 +02:00
|
|
|
++offset;
|
2010-01-30 19:41:22 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->tokAt(offset + 1))
|
2010-01-30 19:41:22 +01:00
|
|
|
return; // invalid input
|
2011-11-13 13:10:59 +01:00
|
|
|
else if (tok->strAt(offset + 1) == ";")
|
2010-01-30 19:41:22 +01:00
|
|
|
atEnd = true;
|
2011-11-13 13:10:59 +01:00
|
|
|
else if (tok->strAt(offset) == "]")
|
2010-01-30 19:41:22 +01:00
|
|
|
atEnd = true;
|
|
|
|
else
|
2011-10-29 20:36:44 +02:00
|
|
|
++offset;
|
2010-01-30 19:41:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
arrayEnd = tok->tokAt(offset++);
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
|
|
|
|
2010-01-31 21:46:18 +01:00
|
|
|
// check for end or another
|
2010-06-20 20:51:36 +02:00
|
|
|
if (Token::Match(tok->tokAt(offset), ";|,"))
|
2010-01-18 19:06:50 +01:00
|
|
|
tok = tok->tokAt(offset);
|
2010-05-10 07:12:06 +02:00
|
|
|
|
|
|
|
// or a function typedef
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::simpleMatch(tok->tokAt(offset), "(")) {
|
2011-01-04 07:43:40 +01:00
|
|
|
// unhandled typedef, skip it and continue
|
2011-10-13 20:53:06 +02:00
|
|
|
if (typeName->str() == "void") {
|
2010-12-28 14:04:44 +01:00
|
|
|
unsupportedTypedef(typeDef);
|
2011-01-04 07:43:40 +01:00
|
|
|
tok = deleteInvalidTypedef(typeDef);
|
2011-12-04 18:56:12 +01:00
|
|
|
if (tok == _tokens)
|
|
|
|
//now the next token to process is 'tok', not 'tok->next()';
|
|
|
|
goback = true;
|
2011-11-11 10:53:49 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unhandled function pointer, skip it and continue
|
2011-11-11 11:07:03 +01:00
|
|
|
// TODO: handle such typedefs. See ticket #3314
|
2011-11-11 10:53:49 +01:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( %type% ::") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::Match(tok->linkAt(offset)->tokAt(-3), ":: * %var% ) (")) {
|
2011-11-11 10:53:49 +01:00
|
|
|
unsupportedTypedef(typeDef);
|
|
|
|
tok = deleteInvalidTypedef(typeDef);
|
2011-12-04 18:56:12 +01:00
|
|
|
if (tok == _tokens)
|
|
|
|
//now the next token to process is 'tok', not 'tok->next()';
|
|
|
|
goback = true;
|
2010-12-28 14:04:44 +01:00
|
|
|
continue;
|
|
|
|
}
|
2011-06-19 23:57:01 +02:00
|
|
|
|
|
|
|
// function pointer
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( * %var% ) (")) {
|
2011-06-20 02:19:16 +02:00
|
|
|
// name token wasn't a name, it was part of the type
|
|
|
|
typeEnd = typeEnd->next();
|
2011-06-19 23:57:01 +02:00
|
|
|
functionPtr = true;
|
|
|
|
funcStart = tok->tokAt(offset + 1);
|
2011-06-20 02:19:16 +02:00
|
|
|
funcEnd = tok->tokAt(offset + 1);
|
|
|
|
typeName = tok->tokAt(offset + 2);
|
|
|
|
argStart = tok->tokAt(offset + 4);
|
2011-11-20 14:22:39 +01:00
|
|
|
argEnd = tok->linkAt(offset + 4);
|
2011-06-19 23:57:01 +02:00
|
|
|
tok = argEnd->next();
|
2010-05-10 07:12:06 +02:00
|
|
|
}
|
2011-03-07 00:59:56 +01:00
|
|
|
|
2011-06-19 23:57:01 +02:00
|
|
|
// function
|
2011-11-20 14:22:39 +01:00
|
|
|
else if (Token::Match(tok->linkAt(offset), ") const| ;|,")) {
|
2011-06-19 23:57:01 +02:00
|
|
|
function = true;
|
2011-11-20 14:22:39 +01:00
|
|
|
if (tok->linkAt(offset)->next()->str() == "const") {
|
|
|
|
specStart = tok->linkAt(offset)->next();
|
2011-06-19 23:57:01 +02:00
|
|
|
specEnd = specStart;
|
|
|
|
}
|
|
|
|
argStart = tok->tokAt(offset);
|
2011-11-20 14:22:39 +01:00
|
|
|
argEnd = tok->linkAt(offset);
|
2011-06-19 23:57:01 +02:00
|
|
|
tok = argEnd->next();
|
|
|
|
if (specStart)
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// syntax error
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2011-06-19 23:57:01 +02:00
|
|
|
syntaxError(tok);
|
|
|
|
return;
|
2011-04-14 05:24:41 +02:00
|
|
|
}
|
2010-05-10 07:12:06 +02:00
|
|
|
}
|
2011-01-04 07:43:40 +01:00
|
|
|
|
|
|
|
// unhandled typedef, skip it and continue
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2010-08-26 20:44:13 +02:00
|
|
|
unsupportedTypedef(typeDef);
|
2011-01-04 07:43:40 +01:00
|
|
|
tok = deleteInvalidTypedef(typeDef);
|
2011-12-04 18:56:12 +01:00
|
|
|
if (tok == _tokens)
|
|
|
|
//now the next token to process is 'tok', not 'tok->next()';
|
|
|
|
goback = true;
|
2010-01-18 19:06:50 +01:00
|
|
|
continue;
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
|
|
|
}
|
2010-11-11 06:40:49 +01:00
|
|
|
|
2010-11-11 17:02:04 +01:00
|
|
|
// typeof: typedef __typeof__ ( ... ) type;
|
2010-11-11 06:40:49 +01:00
|
|
|
else if (Token::simpleMatch(tok->tokAt(offset - 1), "__typeof__ (") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::Match(tok->linkAt(offset), ") %type% ;")) {
|
2010-11-11 06:40:49 +01:00
|
|
|
argStart = tok->tokAt(offset);
|
2011-11-20 14:22:39 +01:00
|
|
|
argEnd = tok->linkAt(offset);
|
|
|
|
typeName = tok->linkAt(offset)->next();
|
2010-11-11 06:40:49 +01:00
|
|
|
tok = typeName->next();
|
|
|
|
typeOf = true;
|
|
|
|
}
|
|
|
|
|
2010-11-11 17:02:04 +01:00
|
|
|
// function: typedef ... ( .... type )( ... );
|
2011-03-16 00:24:30 +01:00
|
|
|
// typedef ... (( .... type )( ... ));
|
2011-03-18 01:00:49 +01:00
|
|
|
// typedef ... ( * ( .... type )( ... ));
|
2011-11-13 13:10:59 +01:00
|
|
|
else if ((tok->strAt(offset) == "(" &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::Match(tok->linkAt(offset)->previous(), "%type% ) (") &&
|
|
|
|
Token::Match(tok->linkAt(offset)->next()->link(), ") const|volatile|;")) ||
|
2011-03-16 00:24:30 +01:00
|
|
|
(Token::simpleMatch(tok->tokAt(offset), "( (") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::Match(tok->linkAt(offset + 1)->previous(), "%type% ) (") &&
|
|
|
|
Token::Match(tok->linkAt(offset + 1)->next()->link(), ") const|volatile| ) ;|,")) ||
|
2011-03-18 01:00:49 +01:00
|
|
|
(Token::simpleMatch(tok->tokAt(offset), "( * (") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::Match(tok->linkAt(offset + 2)->previous(), "%type% ) (") &&
|
|
|
|
Token::Match(tok->linkAt(offset + 2)->next()->link(), ") const|volatile| ) ;|,"))) {
|
2011-03-16 00:24:30 +01:00
|
|
|
if (tok->strAt(offset + 1) == "(")
|
2011-10-29 20:36:44 +02:00
|
|
|
++offset;
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::simpleMatch(tok->tokAt(offset), "( * (")) {
|
2011-10-31 02:24:59 +01:00
|
|
|
++offset;
|
2011-03-18 01:00:49 +01:00
|
|
|
pointers.push_back("*");
|
2011-10-31 02:24:59 +01:00
|
|
|
++offset;
|
2011-03-18 01:00:49 +01:00
|
|
|
}
|
2011-03-16 00:24:30 +01:00
|
|
|
|
2011-11-20 14:22:39 +01:00
|
|
|
if (tok->linkAt(offset)->strAt(-2) == "*")
|
2011-03-16 04:28:45 +01:00
|
|
|
functionPtr = true;
|
|
|
|
else
|
|
|
|
function = true;
|
2010-11-11 17:02:04 +01:00
|
|
|
funcStart = tok->tokAt(offset + 1);
|
2011-11-20 14:22:39 +01:00
|
|
|
funcEnd = tok->linkAt(offset)->tokAt(-2);
|
|
|
|
typeName = tok->linkAt(offset)->previous();
|
|
|
|
argStart = tok->linkAt(offset)->next();
|
|
|
|
argEnd = tok->linkAt(offset)->next()->link();
|
2010-11-11 17:02:04 +01:00
|
|
|
tok = argEnd->next();
|
2010-05-23 10:46:39 +02:00
|
|
|
Token *spec = tok;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(spec, "const|volatile")) {
|
2010-05-23 10:46:39 +02:00
|
|
|
specStart = spec;
|
|
|
|
specEnd = spec;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (Token::Match(spec->next(), "const|volatile")) {
|
2010-05-23 10:46:39 +02:00
|
|
|
specEnd = spec->next();
|
|
|
|
spec = specEnd;
|
|
|
|
}
|
|
|
|
tok = specEnd->next();
|
|
|
|
}
|
2011-03-16 00:24:30 +01:00
|
|
|
if (tok->str() == ")")
|
|
|
|
tok = tok->next();
|
2010-05-11 21:41:33 +02:00
|
|
|
}
|
2010-11-11 17:02:04 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( %type% (")) {
|
2010-05-10 17:50:40 +02:00
|
|
|
function = true;
|
2011-11-20 14:22:39 +01:00
|
|
|
if (tok->linkAt(offset)->next()) {
|
2010-05-10 17:50:40 +02:00
|
|
|
typeName = tok->tokAt(offset + 1);
|
|
|
|
argStart = tok->tokAt(offset + 2);
|
2011-11-20 14:22:39 +01:00
|
|
|
argEnd = tok->linkAt(offset + 2);
|
|
|
|
tok = tok->linkAt(offset)->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-05-10 17:50:40 +02:00
|
|
|
// internal error
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-05-25 20:43:44 +02:00
|
|
|
|
|
|
|
// pointer to function returning pointer to function
|
2011-03-18 01:00:49 +01:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( * ( * %type% ) (") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::simpleMatch(tok->linkAt(offset + 6), ") ) (") &&
|
|
|
|
Token::Match(tok->linkAt(offset + 6)->linkAt(2), ") ;|,")) {
|
2010-05-25 20:43:44 +02:00
|
|
|
functionPtrRetFuncPtr = true;
|
|
|
|
|
|
|
|
typeName = tok->tokAt(offset + 4);
|
|
|
|
argStart = tok->tokAt(offset + 6);
|
2011-11-20 14:22:39 +01:00
|
|
|
argEnd = tok->linkAt(offset + 6);
|
2010-05-25 20:43:44 +02:00
|
|
|
|
|
|
|
argFuncRetStart = argEnd->tokAt(2);
|
2011-11-20 14:22:39 +01:00
|
|
|
argFuncRetEnd = argEnd->linkAt(2);
|
2010-05-25 20:43:44 +02:00
|
|
|
|
|
|
|
tok = argFuncRetEnd->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// function returning pointer to function
|
2010-06-20 20:51:36 +02:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( * %type% (") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::simpleMatch(tok->linkAt(offset + 3), ") ) (") &&
|
|
|
|
Token::Match(tok->linkAt(offset + 3)->linkAt(2), ") ;|,")) {
|
2010-05-25 20:43:44 +02:00
|
|
|
functionRetFuncPtr = true;
|
|
|
|
|
|
|
|
typeName = tok->tokAt(offset + 2);
|
|
|
|
argStart = tok->tokAt(offset + 3);
|
2011-11-20 14:22:39 +01:00
|
|
|
argEnd = tok->linkAt(offset + 3);
|
2010-05-25 20:43:44 +02:00
|
|
|
|
|
|
|
argFuncRetStart = argEnd->tokAt(2);
|
2011-11-20 14:22:39 +01:00
|
|
|
argFuncRetEnd = argEnd->linkAt(2);
|
2010-05-25 20:43:44 +02:00
|
|
|
|
|
|
|
tok = argFuncRetEnd->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::Match(tok->tokAt(offset), "( * ( %type% ) (")) {
|
2010-05-25 20:43:44 +02:00
|
|
|
functionRetFuncPtr = true;
|
|
|
|
|
|
|
|
typeName = tok->tokAt(offset + 3);
|
|
|
|
argStart = tok->tokAt(offset + 5);
|
2011-11-20 14:22:39 +01:00
|
|
|
argEnd = tok->linkAt(offset + 5);
|
2010-05-25 20:43:44 +02:00
|
|
|
|
|
|
|
argFuncRetStart = argEnd->tokAt(2);
|
2011-11-20 14:22:39 +01:00
|
|
|
argFuncRetEnd = argEnd->linkAt(2);
|
2010-05-25 20:43:44 +02:00
|
|
|
|
|
|
|
tok = argFuncRetEnd->next();
|
|
|
|
}
|
|
|
|
|
2010-05-28 06:55:54 +02:00
|
|
|
// pointer/reference to array
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( *|& %type% ) [")) {
|
2011-11-13 13:10:59 +01:00
|
|
|
ptrToArray = (tok->strAt(offset + 1) == "*");
|
|
|
|
refToArray = (tok->strAt(offset + 1) == "&");
|
2010-05-28 06:55:54 +02:00
|
|
|
typeName = tok->tokAt(offset + 2);
|
|
|
|
arrayStart = tok->tokAt(offset + 4);
|
|
|
|
arrayEnd = arrayStart->link();
|
|
|
|
tok = arrayEnd->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// pointer to class member
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( %type% :: * %type% ) ;")) {
|
2010-05-29 07:52:06 +02:00
|
|
|
namespaceStart = tok->tokAt(offset + 1);
|
|
|
|
namespaceEnd = tok->tokAt(offset + 2);
|
2010-05-28 06:55:54 +02:00
|
|
|
ptrMember = true;
|
|
|
|
typeName = tok->tokAt(offset + 4);
|
|
|
|
tok = tok->tokAt(offset + 6);
|
|
|
|
}
|
|
|
|
|
2010-05-25 20:43:44 +02:00
|
|
|
// unhandled typedef, skip it and continue
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2010-08-26 20:44:13 +02:00
|
|
|
unsupportedTypedef(typeDef);
|
2011-01-04 07:43:40 +01:00
|
|
|
tok = deleteInvalidTypedef(typeDef);
|
2011-12-04 18:56:12 +01:00
|
|
|
if (tok == _tokens)
|
|
|
|
//now the next token to process is 'tok', not 'tok->next()';
|
|
|
|
goback = true;
|
2010-01-17 08:16:17 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
bool ok = true;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (!done) {
|
2010-08-26 07:32:48 +02:00
|
|
|
std::string pattern = typeName->str();
|
2010-02-03 07:58:36 +01:00
|
|
|
int scope = 0;
|
2010-01-17 08:16:17 +01:00
|
|
|
bool inScope = true;
|
|
|
|
bool exitThisScope = false;
|
|
|
|
int exitScope = 0;
|
|
|
|
bool simplifyType = false;
|
2011-01-02 17:28:47 +01:00
|
|
|
bool inMemberFunc = false;
|
|
|
|
int memberScope = 0;
|
2011-05-03 04:42:52 +02:00
|
|
|
bool globalScope = false;
|
2010-12-31 09:38:03 +01:00
|
|
|
std::size_t classLevel = spaceInfo.size();
|
2010-02-03 07:58:36 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
|
2011-01-02 17:28:47 +01:00
|
|
|
// check for end of scope
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->str() == "}") {
|
2011-01-02 17:28:47 +01:00
|
|
|
// check for end of member function
|
2011-10-13 20:53:06 +02:00
|
|
|
if (inMemberFunc) {
|
2011-10-29 20:36:44 +02:00
|
|
|
--memberScope;
|
2011-01-02 17:28:47 +01:00
|
|
|
if (memberScope == 0)
|
|
|
|
inMemberFunc = false;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (classLevel > 0 && tok2 == spaceInfo[classLevel - 1].classEnd) {
|
2010-02-03 07:58:36 +01:00
|
|
|
--classLevel;
|
|
|
|
pattern.clear();
|
|
|
|
|
2011-10-29 20:36:44 +02:00
|
|
|
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i)
|
2010-02-03 07:58:36 +01:00
|
|
|
pattern += (spaceInfo[i].className + " :: ");
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
pattern += typeName->str();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-10-29 20:36:44 +02:00
|
|
|
--scope;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (scope < 0)
|
2010-02-03 07:58:36 +01:00
|
|
|
inScope = false;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (exitThisScope) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (scope < exitScope)
|
2010-02-20 09:07:29 +01:00
|
|
|
exitThisScope = false;
|
2010-02-03 07:58:36 +01:00
|
|
|
}
|
2009-11-27 17:32:53 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
2011-01-02 17:28:47 +01:00
|
|
|
|
2011-01-04 07:39:41 +01:00
|
|
|
// check for operator typedef
|
|
|
|
/** @todo add support for multi-token operators */
|
|
|
|
else if (tok2->str() == "operator" &&
|
|
|
|
tok2->next()->str() == typeName->str() &&
|
|
|
|
tok2->strAt(2) == "(" &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::Match(tok2->linkAt(2), ") const| {")) {
|
2011-01-04 07:39:41 +01:00
|
|
|
// check for qualifier
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->previous()->str() == "::") {
|
2011-01-04 07:39:41 +01:00
|
|
|
// check for available and matching class name
|
|
|
|
if (!spaceInfo.empty() && classLevel < spaceInfo.size() &&
|
2011-10-13 20:53:06 +02:00
|
|
|
tok2->strAt(-2) == spaceInfo[classLevel].className) {
|
2011-01-04 07:39:41 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
simplifyType = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-02 17:28:47 +01:00
|
|
|
// check for member functions
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok2, ") const| {")) {
|
2011-01-02 17:28:47 +01:00
|
|
|
const Token *func = tok2->link()->previous();
|
|
|
|
|
|
|
|
/** @todo add support for multi-token operators */
|
|
|
|
if (func->previous()->str() == "operator")
|
|
|
|
func = func->previous();
|
|
|
|
|
|
|
|
// check for qualifier
|
2011-10-13 20:53:06 +02:00
|
|
|
if (func->previous()->str() == "::") {
|
2011-01-02 17:28:47 +01:00
|
|
|
// check for available and matching class name
|
2011-01-04 07:34:32 +01:00
|
|
|
if (!spaceInfo.empty() && classLevel < spaceInfo.size() &&
|
2011-10-13 20:53:06 +02:00
|
|
|
func->strAt(-2) == spaceInfo[classLevel].className) {
|
2011-01-02 17:28:47 +01:00
|
|
|
memberScope = 0;
|
|
|
|
inMemberFunc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-16 16:35:23 +02:00
|
|
|
// check for entering a new namespace
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok2, "namespace %any% {")) {
|
2011-04-20 13:49:04 +02:00
|
|
|
if (classLevel < spaceInfo.size() &&
|
|
|
|
spaceInfo[classLevel].isNamespace &&
|
2011-10-13 20:53:06 +02:00
|
|
|
spaceInfo[classLevel].className == tok2->next()->str()) {
|
2011-10-29 20:36:44 +02:00
|
|
|
++classLevel;
|
2011-04-16 16:35:23 +02:00
|
|
|
pattern.clear();
|
2011-10-29 20:36:44 +02:00
|
|
|
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i)
|
2011-04-16 16:35:23 +02:00
|
|
|
pattern += (spaceInfo[i].className + " :: ");
|
|
|
|
|
|
|
|
pattern += typeName->str();
|
|
|
|
}
|
2011-10-29 20:36:44 +02:00
|
|
|
++scope;
|
2011-04-16 16:35:23 +02:00
|
|
|
}
|
|
|
|
|
2011-01-02 17:28:47 +01:00
|
|
|
// check for entering a new scope
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok2->str() == "{") {
|
2011-01-02 17:28:47 +01:00
|
|
|
// keep track of scopes within member function
|
|
|
|
if (inMemberFunc)
|
2011-10-29 20:36:44 +02:00
|
|
|
++memberScope;
|
2011-01-02 17:28:47 +01:00
|
|
|
|
2011-10-29 20:36:44 +02:00
|
|
|
++scope;
|
2010-02-20 09:07:29 +01:00
|
|
|
}
|
2011-01-02 17:28:47 +01:00
|
|
|
|
|
|
|
// check for typedef that can be substituted
|
|
|
|
else if (Token::Match(tok2, pattern.c_str()) ||
|
2011-10-13 20:53:06 +02:00
|
|
|
(inMemberFunc && tok2->str() == typeName->str())) {
|
2011-01-02 17:28:47 +01:00
|
|
|
std::string pattern1;
|
|
|
|
|
|
|
|
// member function class variables don't need qualification
|
|
|
|
if (inMemberFunc && tok2->str() == typeName->str())
|
|
|
|
pattern1 = tok2->str();
|
|
|
|
else
|
|
|
|
pattern1 = pattern;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (pattern1.find("::") != std::string::npos) { // has a "something ::"
|
|
|
|
if (Token::simpleMatch(tok2->previous(), "::")) {
|
2011-11-12 22:33:03 +01:00
|
|
|
tok2->tokAt(-2)->deleteNext();
|
2011-05-03 04:42:52 +02:00
|
|
|
globalScope = true;
|
|
|
|
}
|
|
|
|
|
2011-10-29 20:36:44 +02:00
|
|
|
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i) {
|
2011-12-08 01:41:53 +01:00
|
|
|
tok2->deleteNext(2);
|
2010-02-03 07:58:36 +01:00
|
|
|
}
|
|
|
|
simplifyType = true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if ((inScope && !exitThisScope) || inMemberFunc) {
|
|
|
|
if (Token::simpleMatch(tok2->previous(), "::")) {
|
2010-02-03 07:58:36 +01:00
|
|
|
// Don't replace this typename if it's preceded by "::" unless it's a namespace
|
2011-10-29 01:57:53 +02:00
|
|
|
if (!spaceInfo.empty() && (tok2->strAt(-2) == spaceInfo[0].className) && spaceInfo[0].isNamespace) {
|
2010-02-03 07:58:36 +01:00
|
|
|
tok2 = tok2->tokAt(-3);
|
2011-12-08 01:41:53 +01:00
|
|
|
tok2->deleteNext(2);
|
2010-02-03 07:58:36 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
simplifyType = true;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::Match(tok2->previous(), "case %type% :")) {
|
2011-01-01 20:20:03 +01:00
|
|
|
tok2 = tok2->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (duplicateTypedef(&tok2, typeName, typeDef, undefinedStruct)) {
|
2010-02-20 09:07:29 +01:00
|
|
|
exitScope = scope;
|
|
|
|
|
|
|
|
// skip to end of scope if not already there
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->str() != "}") {
|
2010-02-24 17:50:02 +01:00
|
|
|
int level = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok2->next() && (tok2->next()->str() != "}" || level)) {
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->next()->str() == "{")
|
2011-10-29 20:36:44 +02:00
|
|
|
++level;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->next()->str() == "}")
|
2011-10-29 20:36:44 +02:00
|
|
|
--level;
|
2010-02-24 17:50:02 +01:00
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok2->previous()->str() != ".") {
|
2010-02-20 09:07:29 +01:00
|
|
|
simplifyType = true;
|
2010-02-03 07:58:36 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
|
|
|
}
|
2009-09-30 13:35:00 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (simplifyType) {
|
2011-03-16 04:28:45 +01:00
|
|
|
// can't simplify 'operator functionPtr ()' and 'functionPtr operator ... ()'
|
|
|
|
if (functionPtr && (tok2->previous()->str() == "operator" ||
|
2011-10-13 20:53:06 +02:00
|
|
|
tok2->next()->str() == "operator")) {
|
2011-03-16 04:28:45 +01:00
|
|
|
simplifyType = false;
|
|
|
|
tok2 = tok2->next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-12-15 18:45:53 +01:00
|
|
|
// There are 2 categories of typedef substitutions:
|
2010-07-02 15:22:29 +02:00
|
|
|
// 1. variable declarations that preserve the variable name like
|
|
|
|
// global, local, and function parameters
|
|
|
|
// 2. not variable declarations that have no name like derived
|
2010-07-07 07:40:11 +02:00
|
|
|
// classes, casts, operators, and template parameters
|
2010-04-14 19:06:51 +02:00
|
|
|
|
2010-12-15 18:45:53 +01:00
|
|
|
// try to determine which category this substitution is
|
2010-07-02 15:22:29 +02:00
|
|
|
bool isDerived = false;
|
2010-01-22 17:27:40 +01:00
|
|
|
bool inCast = false;
|
2010-07-02 15:22:29 +02:00
|
|
|
bool inTemplate = false;
|
2010-07-07 07:40:11 +02:00
|
|
|
bool inOperator = false;
|
2011-02-09 01:47:14 +01:00
|
|
|
bool inSizeof = false;
|
2010-01-22 17:27:40 +01:00
|
|
|
|
2010-07-02 15:22:29 +02:00
|
|
|
// check for derived class: class A : some_typedef {
|
|
|
|
isDerived = Token::Match(tok2->previous(), "public|protected|private %type% {|,");
|
|
|
|
|
|
|
|
// check for cast: (some_typedef) A or static_cast<some_typedef>(A)
|
|
|
|
// todo: check for more complicated casts like: (const some_typedef *)A
|
2011-02-09 01:47:14 +01:00
|
|
|
if ((tok2->previous()->str() == "(" && tok2->next()->str() == ")" && tok2->strAt(-2) != "sizeof") ||
|
2010-07-02 15:22:29 +02:00
|
|
|
(tok2->previous()->str() == "<" && Token::simpleMatch(tok2->next(), "> (")))
|
2010-01-22 17:27:40 +01:00
|
|
|
inCast = true;
|
|
|
|
|
2010-07-02 15:22:29 +02:00
|
|
|
// check for template parameters: t<some_typedef> t1
|
|
|
|
else if (Token::Match(tok2->previous(), "<|,") &&
|
|
|
|
Token::Match(tok2->next(), "&|*| &|*| >|,"))
|
|
|
|
inTemplate = true;
|
|
|
|
|
2011-02-09 01:47:14 +01:00
|
|
|
else if (Token::Match(tok2->tokAt(-2), "sizeof ( %type% )"))
|
|
|
|
inSizeof = true;
|
|
|
|
|
2010-07-07 07:40:11 +02:00
|
|
|
// check for operator
|
2011-02-02 17:12:46 +01:00
|
|
|
if (Token::simpleMatch(tok2->previous(), "operator") ||
|
|
|
|
Token::simpleMatch(tok2->tokAt(-2), "operator const"))
|
2010-07-07 07:40:11 +02:00
|
|
|
inOperator = true;
|
|
|
|
|
2010-04-14 19:06:51 +02:00
|
|
|
// skip over class or struct in derived class declaration
|
2011-05-03 04:42:52 +02:00
|
|
|
bool structRemoved = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (isDerived && Token::Match(typeStart, "class|struct")) {
|
2011-05-03 04:42:52 +02:00
|
|
|
if (typeStart->str() == "struct")
|
|
|
|
structRemoved = true;
|
2010-04-14 19:06:51 +02:00
|
|
|
typeStart = typeStart->next();
|
2011-05-03 04:42:52 +02:00
|
|
|
}
|
2010-04-14 19:06:51 +02:00
|
|
|
|
2010-07-02 15:22:29 +02:00
|
|
|
// start substituting at the typedef name by replacing it with the type
|
2010-01-30 08:00:11 +01:00
|
|
|
tok2->str(typeStart->str());
|
2011-05-03 04:42:52 +02:00
|
|
|
|
|
|
|
// restore qualification if it was removed
|
2011-10-13 20:53:06 +02:00
|
|
|
if (typeStart->str() == "struct" || structRemoved) {
|
2011-05-03 04:42:52 +02:00
|
|
|
if (structRemoved)
|
|
|
|
tok2 = tok2->previous();
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (globalScope) {
|
2011-05-03 04:42:52 +02:00
|
|
|
tok2->insertToken("::");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
2011-10-29 20:36:44 +02:00
|
|
|
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i) {
|
2011-05-03 04:42:52 +02:00
|
|
|
tok2->insertToken(spaceInfo[i].className);
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok2->insertToken("::");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add remainder of type
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2 = copyTokens(tok2, typeStart->next(), typeEnd);
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!pointers.empty()) {
|
2010-05-18 18:20:11 +02:00
|
|
|
std::list<std::string>::const_iterator iter;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (iter = pointers.begin(); iter != pointers.end(); ++iter) {
|
2010-05-18 18:20:11 +02:00
|
|
|
tok2->insertToken(*iter);
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
2010-01-20 21:16:40 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (funcStart && funcEnd) {
|
2010-11-11 17:02:04 +01:00
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token *tok3 = tok2;
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2 = copyTokens(tok2, funcStart, funcEnd);
|
2010-11-11 17:02:04 +01:00
|
|
|
|
|
|
|
if (!inCast)
|
2011-02-13 19:34:55 +01:00
|
|
|
tok2 = processFunc(tok2, inOperator);
|
2010-11-11 17:02:04 +01:00
|
|
|
|
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok3);
|
|
|
|
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2 = copyTokens(tok2, argStart, argEnd);
|
2010-11-11 17:02:04 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (specStart) {
|
2010-11-11 17:02:04 +01:00
|
|
|
Token *spec = specStart;
|
|
|
|
tok2->insertToken(spec->str());
|
|
|
|
tok2 = tok2->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
while (spec != specEnd) {
|
2010-11-11 17:02:04 +01:00
|
|
|
spec = spec->next();
|
|
|
|
tok2->insertToken(spec->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (functionPtr || functionRef || function) {
|
2010-05-18 07:11:23 +02:00
|
|
|
// don't add parenthesis around function names because it
|
|
|
|
// confuses other simplifications
|
|
|
|
bool needParen = true;
|
2011-08-10 00:31:02 +02:00
|
|
|
if (!inTemplate && function && tok2->next() && tok2->next()->str() != "*")
|
2010-05-18 07:11:23 +02:00
|
|
|
needParen = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (needParen) {
|
2010-05-18 07:11:23 +02:00
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2010-01-20 21:16:40 +01:00
|
|
|
Token *tok3 = tok2;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (namespaceStart) {
|
2010-05-29 07:52:06 +02:00
|
|
|
const Token *tok4 = namespaceStart;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok4 != namespaceEnd) {
|
2010-05-29 07:52:06 +02:00
|
|
|
tok2->insertToken(tok4->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok4 = tok4->next();
|
|
|
|
}
|
|
|
|
tok2->insertToken(namespaceEnd->str());
|
2010-05-11 21:41:33 +02:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
if (functionPtr) {
|
2010-01-27 19:03:24 +01:00
|
|
|
tok2->insertToken("*");
|
2010-07-02 15:22:29 +02:00
|
|
|
tok2 = tok2->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (functionRef) {
|
2010-01-27 19:03:24 +01:00
|
|
|
tok2->insertToken("&");
|
2010-07-02 15:22:29 +02:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2010-01-20 21:16:40 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!inCast)
|
2011-02-13 19:34:55 +01:00
|
|
|
tok2 = processFunc(tok2, inOperator);
|
2010-01-20 21:16:40 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (needParen) {
|
2010-05-18 07:11:23 +02:00
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok3);
|
|
|
|
}
|
2010-07-02 15:22:29 +02:00
|
|
|
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2 = copyTokens(tok2, argStart, argEnd);
|
2010-07-02 15:22:29 +02:00
|
|
|
|
|
|
|
if (inTemplate)
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (specStart) {
|
2010-05-23 10:46:39 +02:00
|
|
|
Token *spec = specStart;
|
|
|
|
tok2->insertToken(spec->str());
|
|
|
|
tok2 = tok2->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
while (spec != specEnd) {
|
2010-05-23 10:46:39 +02:00
|
|
|
spec = spec->next();
|
|
|
|
tok2->insertToken(spec->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (functionRetFuncPtr || functionPtrRetFuncPtr) {
|
2010-05-25 20:43:44 +02:00
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token *tok3 = tok2;
|
|
|
|
tok2->insertToken("*");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
Token * tok4 = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (functionPtrRetFuncPtr) {
|
2010-05-25 20:43:44 +02:00
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok4 = tok2;
|
|
|
|
tok2->insertToken("*");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
2011-01-05 17:39:53 +01:00
|
|
|
// skip over variable name if there
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!inCast) {
|
2011-01-05 17:39:53 +01:00
|
|
|
if (tok2->next()->str() != ")")
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2010-05-25 20:43:44 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok4 && functionPtrRetFuncPtr) {
|
2010-05-25 20:43:44 +02:00
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok4);
|
|
|
|
}
|
|
|
|
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2 = copyTokens(tok2, argStart, argEnd);
|
2010-05-25 20:43:44 +02:00
|
|
|
|
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok3);
|
|
|
|
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2 = copyTokens(tok2, argFuncRetStart, argFuncRetEnd);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (ptrToArray || refToArray) {
|
2010-05-28 06:55:54 +02:00
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token *tok3 = tok2;
|
|
|
|
|
|
|
|
if (ptrToArray)
|
|
|
|
tok2->insertToken("*");
|
|
|
|
else
|
|
|
|
tok2->insertToken("&");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// skip over name
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->next()->str() != ")") {
|
2010-07-06 08:26:30 +02:00
|
|
|
if (tok2->next()->str() != "(")
|
|
|
|
tok2 = tok2->next();
|
2010-06-10 07:18:55 +02:00
|
|
|
|
|
|
|
// check for function and skip over args
|
|
|
|
if (tok2->next()->str() == "(")
|
|
|
|
tok2 = tok2->next()->link();
|
2010-07-06 08:26:30 +02:00
|
|
|
|
|
|
|
// check for array
|
|
|
|
if (tok2->next()->str() == "[")
|
|
|
|
tok2 = tok2->next()->link();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-06-10 07:18:55 +02:00
|
|
|
// syntax error
|
|
|
|
}
|
2010-05-28 06:55:54 +02:00
|
|
|
|
|
|
|
tok2->insertToken(")");
|
|
|
|
Token::createMutualLinks(tok2->next(), tok3);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (ptrMember) {
|
|
|
|
if (Token::simpleMatch(tok2, "* (")) {
|
2010-12-29 20:22:06 +01:00
|
|
|
tok2->insertToken("*");
|
2010-05-29 07:52:06 +02:00
|
|
|
tok2 = tok2->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-12-29 20:22:06 +01:00
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token *tok3 = tok2;
|
2010-05-28 06:55:54 +02:00
|
|
|
|
2010-12-29 20:22:06 +01:00
|
|
|
const Token *tok4 = namespaceStart;
|
2010-05-28 06:55:54 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok4 != namespaceEnd) {
|
2010-12-29 20:22:06 +01:00
|
|
|
tok2->insertToken(tok4->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok4 = tok4->next();
|
|
|
|
}
|
|
|
|
tok2->insertToken(namespaceEnd->str());
|
|
|
|
tok2 = tok2->next();
|
2010-05-28 06:55:54 +02:00
|
|
|
|
2010-12-29 20:22:06 +01:00
|
|
|
tok2->insertToken("*");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// skip over name
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok3);
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (typeOf) {
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2 = copyTokens(tok2, argStart, argEnd);
|
2011-10-29 01:57:53 +02:00
|
|
|
} else if (tok2->tokAt(2) && tok2->strAt(2) == "[") {
|
|
|
|
while (tok2->tokAt(2) && tok2->strAt(2) == "[")
|
2011-11-20 14:22:39 +01:00
|
|
|
tok2 = tok2->linkAt(2)->previous();
|
2010-08-20 07:11:02 +02:00
|
|
|
}
|
2010-01-20 21:16:40 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (arrayStart && arrayEnd) {
|
|
|
|
do {
|
|
|
|
if (!tok2->next()) {
|
2011-08-15 02:38:16 +02:00
|
|
|
syntaxError(tok2);
|
|
|
|
return; // can't recover so quit
|
|
|
|
}
|
|
|
|
|
2011-02-09 01:47:14 +01:00
|
|
|
if (!inCast && !inSizeof)
|
|
|
|
tok2 = tok2->next();
|
2011-03-03 13:27:53 +01:00
|
|
|
|
2011-03-04 02:32:10 +01:00
|
|
|
// reference to array?
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->str() == "&") {
|
2011-03-04 02:32:10 +01:00
|
|
|
tok2 = tok2->previous();
|
|
|
|
tok2->insertToken("(");
|
2011-04-14 05:58:58 +02:00
|
|
|
Token *tok3 = tok2->next();
|
|
|
|
|
|
|
|
// handle missing variable name
|
|
|
|
if (tok2->strAt(3) == ")" || tok2->strAt(3) == ",")
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
else
|
|
|
|
tok2 = tok2->tokAt(3);
|
|
|
|
|
2011-03-04 02:32:10 +01:00
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
2011-04-14 05:58:58 +02:00
|
|
|
Token::createMutualLinks(tok2, tok3);
|
2011-03-04 02:32:10 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2 = copyTokens(tok2, arrayStart, arrayEnd);
|
2010-10-13 20:02:37 +02:00
|
|
|
tok2 = tok2->next();
|
2010-10-20 06:09:29 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->str() == "=") {
|
2010-10-20 06:09:29 +02:00
|
|
|
if (tok2->next()->str() == "{")
|
|
|
|
tok2 = tok2->next()->link()->next();
|
|
|
|
else if (tok2->next()->str().at(0) == '\"')
|
2011-11-12 22:33:03 +01:00
|
|
|
tok2 = tok2->tokAt(2);
|
2010-10-20 06:09:29 +02:00
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} while (Token::Match(tok2, ", %var% ;|'|=|,"));
|
2010-01-16 09:18:21 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
|
|
|
|
simplifyType = false;
|
2010-01-16 09:18:21 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ";")
|
2010-01-17 08:16:17 +01:00
|
|
|
done = true;
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok->str() == ",") {
|
2010-01-30 19:41:22 +01:00
|
|
|
arrayStart = 0;
|
|
|
|
arrayEnd = 0;
|
2010-01-18 19:06:50 +01:00
|
|
|
offset = 1;
|
2010-05-18 18:20:11 +02:00
|
|
|
pointers.clear();
|
2010-01-17 08:16:17 +01:00
|
|
|
|
2010-06-20 20:51:36 +02:00
|
|
|
while (Token::Match(tok->tokAt(offset), "*|&"))
|
2011-11-13 13:10:59 +01:00
|
|
|
pointers.push_back(tok->strAt(offset++));
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(tok->tokAt(offset), "%type%")) {
|
2010-02-20 09:07:29 +01:00
|
|
|
typeName = tok->tokAt(offset++);
|
2010-01-11 17:09:04 +01:00
|
|
|
|
2011-11-13 13:10:59 +01:00
|
|
|
if (tok->tokAt(offset) && tok->strAt(offset) == "[") {
|
2010-01-31 07:16:19 +01:00
|
|
|
arrayStart = tok->tokAt(offset);
|
|
|
|
|
2010-01-30 19:41:22 +01:00
|
|
|
bool atEnd = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (!atEnd) {
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), ";|,"))
|
2011-10-29 20:36:44 +02:00
|
|
|
++offset;
|
2010-01-30 19:41:22 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->tokAt(offset + 1))
|
2010-01-30 19:41:22 +01:00
|
|
|
return; // invalid input
|
2011-11-13 13:10:59 +01:00
|
|
|
else if (tok->strAt(offset + 1) == ";")
|
2010-01-30 19:41:22 +01:00
|
|
|
atEnd = true;
|
2011-11-13 13:10:59 +01:00
|
|
|
else if (tok->strAt(offset) == "]")
|
2010-01-30 19:41:22 +01:00
|
|
|
atEnd = true;
|
|
|
|
else
|
2011-10-29 20:36:44 +02:00
|
|
|
++offset;
|
2010-01-30 19:41:22 +01:00
|
|
|
}
|
2010-01-31 07:16:19 +01:00
|
|
|
|
|
|
|
arrayEnd = tok->tokAt(offset++);
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
2010-01-18 19:06:50 +01:00
|
|
|
|
2010-06-20 20:51:36 +02:00
|
|
|
if (Token::Match(tok->tokAt(offset), ";|,"))
|
2010-01-18 19:06:50 +01:00
|
|
|
tok = tok->tokAt(offset);
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2010-01-18 19:06:50 +01:00
|
|
|
// we encountered a typedef we don't support yet so just continue
|
|
|
|
done = true;
|
|
|
|
ok = false;
|
2009-10-03 17:02:23 +02:00
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-01-17 08:16:17 +01:00
|
|
|
// we encountered a typedef we don't support yet so just continue
|
|
|
|
done = true;
|
|
|
|
ok = false;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-01-17 08:16:17 +01:00
|
|
|
// something is really wrong (internal error)
|
|
|
|
done = true;
|
|
|
|
ok = false;
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (ok) {
|
2011-12-04 17:15:53 +01:00
|
|
|
// remove typedef
|
2011-12-04 15:40:05 +01:00
|
|
|
Token::eraseTokens(typeDef, tok);
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (typeDef != _tokens) {
|
2010-01-16 09:18:21 +01:00
|
|
|
tok = typeDef->previous();
|
|
|
|
tok->deleteNext();
|
2011-12-04 17:15:53 +01:00
|
|
|
//no need to remove last token in the list
|
|
|
|
if (tok->tokAt(2))
|
|
|
|
tok->deleteNext();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-01-16 09:18:21 +01:00
|
|
|
_tokens->deleteThis();
|
2011-12-04 17:15:53 +01:00
|
|
|
//no need to remove last token in the list
|
|
|
|
if (_tokens->next())
|
|
|
|
_tokens->deleteThis();
|
2010-01-16 09:18:21 +01:00
|
|
|
tok = _tokens;
|
2011-12-04 17:15:53 +01:00
|
|
|
//now the next token to process is 'tok', not 'tok->next()';
|
|
|
|
goback = true;
|
2010-01-16 09:18:21 +01:00
|
|
|
}
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 20:47:51 +01:00
|
|
|
void Tokenizer::simplifyMulAndParens()
|
2011-05-18 07:25:30 +02:00
|
|
|
{
|
2011-12-09 20:47:51 +01:00
|
|
|
for (Token *tok = _tokens->tokAt(3); tok; tok = tok->next()) {
|
|
|
|
if (tok->isName()) {
|
|
|
|
//fix ticket #2784 - improved by ticket #3184
|
|
|
|
unsigned int closedpars = 0;
|
|
|
|
Token *tokend = tok->next();
|
|
|
|
Token *tokbegin = tok->previous();
|
|
|
|
while (tokend && tokend->str() == ")") {
|
|
|
|
++closedpars;
|
|
|
|
tokend = tokend->next();
|
2011-08-10 18:16:31 +02:00
|
|
|
}
|
2011-12-09 20:47:51 +01:00
|
|
|
if (!tokend || !(tokend->isAssignmentOp()))
|
2011-08-10 18:16:31 +02:00
|
|
|
continue;
|
2011-12-09 20:47:51 +01:00
|
|
|
while (tokbegin && (tokbegin->str() == "&" || tokbegin->str() == "(")) {
|
|
|
|
if (tokbegin->str() == "&") {
|
|
|
|
if (Token::Match(tokbegin->tokAt(-2), "[;{}&(] *")) {
|
|
|
|
//remove '* &'
|
|
|
|
tokbegin = tokbegin->tokAt(-2);
|
|
|
|
tokbegin->deleteNext(2);
|
|
|
|
} else if (Token::Match(tokbegin->tokAt(-3), "[;{}&(] * (")) {
|
|
|
|
if (!closedpars)
|
|
|
|
break;
|
|
|
|
--closedpars;
|
|
|
|
//remove ')'
|
|
|
|
tok->deleteNext();
|
|
|
|
//remove '* ( &'
|
|
|
|
tokbegin = tokbegin->tokAt(-3);
|
|
|
|
tokbegin->deleteNext(3);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
} else if (tokbegin->str() == "(") {
|
|
|
|
if (!closedpars)
|
|
|
|
break;
|
|
|
|
|
|
|
|
//find consecutive opening parentheses
|
|
|
|
unsigned int openpars = 0;
|
|
|
|
while (tokbegin && tokbegin->str() == "(" && openpars <= closedpars) {
|
|
|
|
++openpars;
|
|
|
|
tokbegin = tokbegin->previous();
|
|
|
|
}
|
|
|
|
if (!tokbegin || openpars > closedpars)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((openpars == closedpars && Token::Match(tokbegin, "[;{}]")) ||
|
|
|
|
Token::Match(tokbegin->tokAt(-2), "[;{}&(] * &") ||
|
|
|
|
Token::Match(tokbegin->tokAt(-3), "[;{}&(] * ( &")) {
|
|
|
|
//remove the excessive parentheses around the variable
|
|
|
|
while (openpars--) {
|
|
|
|
tok->deleteNext();
|
|
|
|
tokbegin->deleteNext();
|
|
|
|
--closedpars;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2011-08-10 18:16:31 +02:00
|
|
|
}
|
2011-05-18 07:25:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-02 23:01:12 +02:00
|
|
|
bool Tokenizer::tokenize(std::istream &code,
|
|
|
|
const char FileName[],
|
|
|
|
const std::string &configuration,
|
|
|
|
const bool preprocessorCondition)
|
2009-05-09 21:32:29 +02:00
|
|
|
{
|
2010-12-01 18:00:55 +01:00
|
|
|
// make sure settings specified
|
|
|
|
assert(_settings);
|
|
|
|
|
2011-09-18 01:40:52 +02:00
|
|
|
// Fill the map _typeSize..
|
|
|
|
_typeSize.clear();
|
|
|
|
_typeSize["char"] = 1;
|
|
|
|
_typeSize["bool"] = _settings->sizeof_bool;
|
|
|
|
_typeSize["short"] = _settings->sizeof_short;
|
|
|
|
_typeSize["int"] = _settings->sizeof_int;
|
|
|
|
_typeSize["long"] = _settings->sizeof_long;
|
|
|
|
_typeSize["float"] = _settings->sizeof_float;
|
|
|
|
_typeSize["double"] = _settings->sizeof_double;
|
|
|
|
_typeSize["size_t"] = _settings->sizeof_size_t;
|
|
|
|
_typeSize["*"] = _settings->sizeof_pointer;
|
|
|
|
|
2010-01-23 22:18:11 +01:00
|
|
|
_configuration = configuration;
|
|
|
|
|
2009-05-09 21:32:29 +02:00
|
|
|
// The "_files" vector remembers what files have been tokenized..
|
2010-10-29 21:21:27 +02:00
|
|
|
_files.push_back(Path::simplifyPath(FileName));
|
2009-05-09 21:32:29 +02:00
|
|
|
|
|
|
|
createTokens(code);
|
|
|
|
|
2011-01-27 21:16:25 +01:00
|
|
|
// if MACRO
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "if|for|while %var% (")) {
|
2011-01-27 21:16:25 +01:00
|
|
|
syntaxError(tok);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-30 08:34:58 +01:00
|
|
|
// replace inline SQL with "asm()" (Oracle PRO*C). Ticket: #1959
|
2011-10-13 20:53:06 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (Token::simpleMatch(tok, "EXEC SQL")) {
|
2010-08-31 21:40:51 +02:00
|
|
|
// delete all tokens until ";"
|
2011-12-13 21:42:38 +01:00
|
|
|
const Token *end = tok->tokAt(2);
|
2010-09-02 20:51:01 +02:00
|
|
|
while (end && end->str() != ";")
|
|
|
|
end = end->next();
|
2011-12-11 01:11:15 +01:00
|
|
|
|
2011-12-13 21:42:38 +01:00
|
|
|
std::string instruction = tok->stringify(end);
|
2010-09-02 20:51:01 +02:00
|
|
|
Token::eraseTokens(tok, end);
|
2010-08-31 21:40:51 +02:00
|
|
|
|
2011-12-13 21:42:38 +01:00
|
|
|
// insert "asm ( "instruction" ) ;"
|
2011-12-11 01:11:15 +01:00
|
|
|
tok->str("asm");
|
|
|
|
// it can happen that 'end' is NULL when wrong code is inserted
|
|
|
|
if (!tok->next())
|
|
|
|
tok->insertToken(";");
|
|
|
|
tok->insertToken(")");
|
2011-12-13 21:42:38 +01:00
|
|
|
tok->insertToken("\"" + instruction + "\"");
|
2011-12-11 01:11:15 +01:00
|
|
|
tok->insertToken("(");
|
|
|
|
// jump to ';' and continue
|
|
|
|
tok = tok->tokAt(3);
|
2010-08-31 21:40:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-28 22:44:53 +01:00
|
|
|
// Simplify JAVA/C# code
|
|
|
|
if (isJavaOrCSharp())
|
|
|
|
simplifyJavaAndCSharp();
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!createLinks()) {
|
2009-05-07 22:17:29 +02:00
|
|
|
// Source has syntax errors, can't proceed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-28 16:49:05 +01:00
|
|
|
//easy simplifications...
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
|
|
|
|
// replace __LINE__ macro with line number
|
|
|
|
if (tok->str() == "__LINE__")
|
|
|
|
tok->str(MathLib::toString(tok->linenr()));
|
|
|
|
|
|
|
|
// 'double sharp' token concatenation
|
|
|
|
// TODO: pattern should be "%var%|%num% ## %var%|%num%"
|
|
|
|
while (Token::Match(tok, "%any% ## %any%") &&
|
2011-12-28 21:05:10 +01:00
|
|
|
(tok->isName() || tok->isNumber()) &&
|
|
|
|
(tok->tokAt(2)->isName() || tok->tokAt(2)->isNumber())) {
|
2011-12-28 16:49:05 +01:00
|
|
|
tok->str(tok->str() + tok->strAt(2));
|
|
|
|
tok->deleteNext(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Replace NULL with 0..
|
|
|
|
if (tok->str() == "NULL" || tok->str() == "__null" ||
|
|
|
|
tok->str() == "'\\0'" || tok->str() == "'\\x0'") {
|
|
|
|
tok->str("0");
|
|
|
|
} else if (tok->isNumber() &&
|
|
|
|
MathLib::isInt(tok->str()) &&
|
|
|
|
MathLib::toLongNumber(tok->str()) == 0) {
|
|
|
|
tok->str("0");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine "- %num%" ..
|
|
|
|
if (Token::Match(tok, "?|:|,|(|[|=|return|case|sizeof|%op% - %num%")) {
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->next()->str("-" + tok->next()->str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// simplify round "(" parenthesis between "[;{}] and "{"
|
2011-12-28 21:05:10 +01:00
|
|
|
if (Token::Match(tok, "[;{}] ( {") &&
|
2011-12-28 16:49:05 +01:00
|
|
|
Token::simpleMatch(tok->linkAt(2), "} ) ;")) {
|
|
|
|
tok->linkAt(2)->previous()->deleteNext(2);
|
|
|
|
tok->deleteNext(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-28 22:07:31 +01:00
|
|
|
|
2011-11-05 12:23:05 +01:00
|
|
|
// Convert K&R function declarations to modern C
|
|
|
|
simplifyVarDecl(true);
|
|
|
|
simplifyFunctionParameters();
|
|
|
|
|
2010-07-22 19:57:48 +02:00
|
|
|
// check for simple syntax errors..
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next()) {
|
2010-07-22 19:57:48 +02:00
|
|
|
if (Token::simpleMatch(tok, "> struct {") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::simpleMatch(tok->linkAt(2), "} ;")) {
|
2010-07-22 19:57:48 +02:00
|
|
|
syntaxError(tok);
|
|
|
|
deallocateTokens();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-21 09:47:41 +01:00
|
|
|
// specify array size..
|
2010-02-20 18:13:09 +01:00
|
|
|
arraySize();
|
|
|
|
|
2009-09-30 20:42:14 +02:00
|
|
|
simplifyDoWhileAddBraces();
|
2011-06-11 21:51:12 +02:00
|
|
|
|
|
|
|
if (!simplifyIfAddBraces())
|
|
|
|
return false;
|
2009-09-30 20:42:14 +02:00
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Combine tokens..
|
2011-10-13 20:53:06 +02:00
|
|
|
for (Token *tok = _tokens; tok && tok->next(); tok = tok->next()) {
|
2010-08-11 17:29:33 +02:00
|
|
|
const char c1 = tok->str()[0];
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok->str().length() == 1 && tok->next()->str().length() == 1) {
|
2010-08-11 17:29:33 +02:00
|
|
|
const char c2 = tok->next()->str()[0];
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-08-11 17:29:33 +02:00
|
|
|
// combine equal tokens..
|
2011-10-13 20:53:06 +02:00
|
|
|
if (c1 == c2 && (c1 == '<' || c1 == '|' || c1 == ':')) {
|
2010-08-11 17:29:33 +02:00
|
|
|
tok->str(tok->str() + c2);
|
|
|
|
tok->deleteNext();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (c1 == '<' && Token::simpleMatch(tok->next(), "=")) {
|
2010-10-31 08:47:13 +01:00
|
|
|
tok->str("<<=");
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
2010-08-11 17:29:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-08-11 17:29:33 +02:00
|
|
|
// combine +-*/ and =
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (c2 == '=' && (strchr("+-*/%&|^=!<>", c1))) {
|
2010-08-11 17:29:33 +02:00
|
|
|
tok->str(tok->str() + c2);
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-08-11 17:29:33 +02:00
|
|
|
// replace "->" with "."
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (c1 == '-' && c2 == '>') {
|
2010-08-11 17:29:33 +02:00
|
|
|
tok->str(".");
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok->str() == ">>" && tok->next()->str() == "=") {
|
2010-10-31 08:47:13 +01:00
|
|
|
tok->str(">>=");
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
else if ((c1 == 'p' || c1 == '_') && tok->next()->str() == ":" && tok->strAt(2) != ":") {
|
|
|
|
if (tok->str() == "private" || tok->str() == "protected" || tok->str() == "public" || tok->str() == "__published") {
|
2010-08-11 17:29:33 +02:00
|
|
|
tok->str(tok->str() + ":");
|
2009-01-24 18:15:38 +01:00
|
|
|
tok->deleteNext();
|
2010-08-11 17:29:33 +02:00
|
|
|
continue;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-10 14:13:48 +01:00
|
|
|
// simplify labels and 'case|default'-like syntaxes
|
|
|
|
simplifyLabelsCaseDefault();
|
2011-10-19 14:20:09 +02:00
|
|
|
|
2011-12-09 20:47:51 +01:00
|
|
|
// simplify '[;{}] * & ( %any% ) =' to '%any% ='
|
|
|
|
simplifyMulAndParens();
|
|
|
|
|
2010-10-31 08:47:13 +01:00
|
|
|
// ";a+=b;" => ";a=a+b;"
|
|
|
|
simplifyCompoundAssignment();
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!preprocessorCondition) {
|
2011-12-24 22:23:08 +01:00
|
|
|
if (hasComplicatedSyntaxErrorsInTemplates()) {
|
|
|
|
deallocateTokens();
|
|
|
|
return false;
|
2010-08-25 21:57:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-24 22:23:08 +01:00
|
|
|
simplifyDefaultAndDeleteInsideClass();
|
2010-08-17 19:50:21 +02:00
|
|
|
|
2010-04-24 09:40:05 +02:00
|
|
|
// Remove __declspec()
|
|
|
|
simplifyDeclspec();
|
2010-02-03 20:01:56 +01:00
|
|
|
|
2011-03-30 19:49:55 +02:00
|
|
|
// remove some unhandled macros in global scope
|
|
|
|
removeMacrosInGlobalScope();
|
|
|
|
|
2010-04-21 21:08:47 +02:00
|
|
|
// remove calling conventions __cdecl, __stdcall..
|
|
|
|
simplifyCallingConvention();
|
|
|
|
|
2010-05-27 18:15:42 +02:00
|
|
|
// remove __attribute__((?))
|
|
|
|
simplifyAttribute();
|
|
|
|
|
2011-02-02 07:40:08 +01:00
|
|
|
// remove unnecessary member qualification..
|
|
|
|
removeUnnecessaryQualification();
|
|
|
|
|
2010-08-18 22:42:04 +02:00
|
|
|
// remove Microsoft MFC..
|
|
|
|
simplifyMicrosoftMFC();
|
|
|
|
|
2011-09-23 01:59:56 +02:00
|
|
|
// convert Microsoft memory functions
|
|
|
|
simplifyMicrosoftMemoryFunctions();
|
|
|
|
|
2011-09-24 20:51:03 +02:00
|
|
|
// convert Microsoft string functions
|
|
|
|
simplifyMicrosoftStringFunctions();
|
|
|
|
|
2010-12-02 17:41:49 +01:00
|
|
|
// Remove Qt signals and slots
|
|
|
|
simplifyQtSignalsSlots();
|
|
|
|
|
2010-09-01 18:10:12 +02:00
|
|
|
// remove Borland stuff..
|
|
|
|
simplifyBorland();
|
|
|
|
|
2011-01-16 11:37:03 +01:00
|
|
|
// Remove "volatile", "inline", "register", and "restrict"
|
|
|
|
simplifyKeyword();
|
|
|
|
|
|
|
|
// Remove __builtin_expect, likely and unlikely
|
|
|
|
simplifyBuiltinExpect();
|
|
|
|
|
2011-12-24 22:23:08 +01:00
|
|
|
if (hasEnumsWithTypedef()) {
|
|
|
|
// #2449: syntax error: enum with typedef in it
|
|
|
|
deallocateTokens();
|
|
|
|
return false;
|
2011-01-16 11:54:28 +01:00
|
|
|
}
|
|
|
|
|
2011-12-24 22:23:08 +01:00
|
|
|
simplifyDebugNew();
|
2011-08-07 01:10:15 +02:00
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// typedef..
|
2009-09-30 13:35:00 +02:00
|
|
|
simplifyTypedef();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2011-02-13 19:34:55 +01:00
|
|
|
// catch bad typedef canonicalization
|
2011-05-07 14:40:47 +02:00
|
|
|
//
|
|
|
|
// to reproduce bad typedef, download upx-ucl from:
|
|
|
|
// http://packages.debian.org/sid/upx-ucl
|
|
|
|
// analyse the file src/stub/src/i386-linux.elf.interp-main.c
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!validate()) {
|
2011-01-23 09:38:38 +01:00
|
|
|
// Source has syntax errors, can't proceed
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-23 09:04:34 +01:00
|
|
|
|
2010-01-29 15:57:26 +01:00
|
|
|
// enum..
|
2010-01-04 17:22:06 +01:00
|
|
|
simplifyEnum();
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Remove __asm..
|
2010-06-30 08:10:39 +02:00
|
|
|
simplifyAsm();
|
2009-12-30 08:24:27 +01:00
|
|
|
|
2010-12-26 15:07:14 +01:00
|
|
|
// When the assembly code has been cleaned up, no @ is allowed
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next()) {
|
2010-12-26 15:07:14 +01:00
|
|
|
if (tok->str() == "(")
|
|
|
|
tok = tok->link();
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok->str()[0] == '@') {
|
2010-12-26 15:07:14 +01:00
|
|
|
deallocateTokens();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-18 16:31:31 +02:00
|
|
|
// convert platform dependent types to standard types
|
|
|
|
// 32 bits: size_t -> unsigned long
|
|
|
|
// 64 bits: size_t -> unsigned long long
|
|
|
|
simplifyPlatformTypes();
|
|
|
|
|
2010-12-15 18:45:53 +01:00
|
|
|
// collapse compound standard types into a single token
|
2010-03-28 15:56:13 +02:00
|
|
|
// unsigned long long int => long _isUnsigned=true,_isLong=true
|
|
|
|
simplifyStdType();
|
|
|
|
|
2010-08-22 13:25:47 +02:00
|
|
|
// simplify bit fields..
|
|
|
|
simplifyBitfields();
|
|
|
|
|
2009-10-04 07:51:12 +02:00
|
|
|
// Use "<" comparison instead of ">"
|
2009-11-04 23:58:15 +01:00
|
|
|
simplifyComparisonOrder();
|
2009-10-04 07:51:12 +02:00
|
|
|
|
2011-04-02 21:21:05 +02:00
|
|
|
// Simplify '(p == 0)' to '(!p)'
|
|
|
|
simplifyIfNot();
|
|
|
|
simplifyIfNotNull();
|
|
|
|
|
2011-10-27 02:57:38 +02:00
|
|
|
//simplify for: move out start-statement "for (a;b;c);" => "{ a; for(;b;c); }"
|
|
|
|
//not enabled because it fails many tests with testrunner.
|
|
|
|
//@todo fix these fails before enabling this simplification
|
|
|
|
/*for (Token* tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == "(" && ( !tok->previous() || tok->previous()->str() != "for")) {
|
|
|
|
tok = tok->link();
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-28 00:37:39 +02:00
|
|
|
if (!Token::Match(tok->previous(),"[{};] for ("))
|
2011-10-27 02:57:38 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
//find the two needed semicolons inside the 'for'
|
2011-10-28 12:49:03 +02:00
|
|
|
const Token *firstsemicolon = Token::findsimplematch(tok->next(), ";", tok->next()->link());
|
2011-10-28 00:37:39 +02:00
|
|
|
if (!firstsemicolon)
|
2011-10-27 02:57:38 +02:00
|
|
|
continue;
|
2011-10-28 12:49:03 +02:00
|
|
|
const Token *secondsemicolon = Token::findsimplematch(firstsemicolon->next(), ";", tok->next()->link());
|
2011-10-28 00:37:39 +02:00
|
|
|
if (!secondsemicolon)
|
|
|
|
continue;
|
2011-10-28 12:49:03 +02:00
|
|
|
if (Token::findsimplematch(secondsemicolon->next(), ";", tok->next()->link()))
|
2011-10-28 00:37:39 +02:00
|
|
|
continue; //no more than two semicolons!
|
|
|
|
if (!tok->next()->link()->next())
|
|
|
|
continue; //there should be always something after 'for (...)'
|
|
|
|
|
|
|
|
Token *fortok = tok;
|
|
|
|
Token *begin = tok->tokAt(2);
|
|
|
|
Token *end = tok->next()->link();
|
|
|
|
if ( begin->str() != ";" ) {
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->insertToken(";");
|
|
|
|
tok->insertToken("{");
|
|
|
|
tok = tok->next();
|
|
|
|
if (end->next()->str() =="{") {
|
|
|
|
end = end->next()->link();
|
|
|
|
end->insertToken("}");
|
|
|
|
Token::createMutualLinks(tok, end->next());
|
|
|
|
end = end->link()->previous();
|
|
|
|
} else {
|
|
|
|
if (end->next()->str() != ";")
|
|
|
|
end->insertToken(";");
|
|
|
|
end = end->next();
|
|
|
|
end->insertToken("}");
|
|
|
|
Token::createMutualLinks(tok, end->next());
|
|
|
|
}
|
|
|
|
end = firstsemicolon->previous();
|
|
|
|
Token::move(begin, end, tok);
|
|
|
|
tok = fortok;
|
|
|
|
end = fortok->next()->link();
|
|
|
|
}
|
|
|
|
//every 'for' is changed to 'for(;b;c), now it's possible to convert the 'for' to a 'while'.
|
|
|
|
//precisely, 'for(;b;c){code}'-> 'while(b){code + c;}'
|
|
|
|
fortok->str("while");
|
|
|
|
begin = firstsemicolon->previous();
|
|
|
|
begin->deleteNext();
|
|
|
|
begin = secondsemicolon->previous();
|
|
|
|
begin->deleteNext();
|
|
|
|
begin = begin->next();
|
|
|
|
if (begin->str() == ")") { //'for(;b;)' -> 'while(b)'
|
|
|
|
if (begin->previous()->str() == "(") //'for(;;)' -> 'while(true)'
|
|
|
|
begin->previous()->insertToken("true");
|
|
|
|
tok = fortok;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (end->next()->str() =="{") {
|
|
|
|
tok = end->next()->link()->previous();
|
|
|
|
tok->insertToken(";");
|
2011-10-27 02:57:38 +02:00
|
|
|
} else {
|
2011-10-28 00:37:39 +02:00
|
|
|
tok = end;
|
|
|
|
if (end->next()->str() != ";")
|
|
|
|
tok->insertToken(";");
|
|
|
|
tok->insertToken("{");
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
tok->insertToken("}");
|
|
|
|
Token::createMutualLinks(tok->previous(), tok->next());
|
|
|
|
tok = tok->previous();
|
2011-10-27 02:57:38 +02:00
|
|
|
}
|
2011-10-28 00:37:39 +02:00
|
|
|
end = end->previous();
|
2011-10-27 02:57:38 +02:00
|
|
|
Token::move(begin, end, tok);
|
2011-10-28 00:37:39 +02:00
|
|
|
tok = fortok;
|
|
|
|
}*/
|
2009-07-14 15:30:23 +02:00
|
|
|
|
2009-11-12 22:49:39 +01:00
|
|
|
simplifyConst();
|
|
|
|
|
2010-03-18 18:14:52 +01:00
|
|
|
// struct simplification "struct S {} s; => struct S { } ; S s ;
|
|
|
|
simplifyStructDecl();
|
|
|
|
|
2010-03-13 11:52:48 +01:00
|
|
|
// struct initialization (must be used before simplifyVarDecl)
|
|
|
|
simplifyStructInit();
|
|
|
|
|
2011-01-16 11:24:58 +01:00
|
|
|
// Change initialisation of variable to assignment
|
|
|
|
simplifyInitVar();
|
|
|
|
|
2009-06-10 19:36:00 +02:00
|
|
|
// Split up variable declarations.
|
2011-11-05 12:23:05 +01:00
|
|
|
simplifyVarDecl(false);
|
2009-03-18 20:32:05 +01:00
|
|
|
|
2010-09-09 19:40:36 +02:00
|
|
|
// f(x=g()) => x=g(); f(x)
|
|
|
|
simplifyAssignmentInFunctionCall();
|
|
|
|
|
2010-05-01 10:26:15 +02:00
|
|
|
simplifyVariableMultipleAssign();
|
|
|
|
|
2010-12-15 18:45:53 +01:00
|
|
|
// Remove redundant parentheses
|
2011-03-30 16:45:31 +02:00
|
|
|
simplifyRedundantParenthesis();
|
2010-01-30 09:33:16 +01:00
|
|
|
|
2009-03-11 18:50:24 +01:00
|
|
|
// Handle templates..
|
2009-05-03 21:23:47 +02:00
|
|
|
simplifyTemplates();
|
2009-05-08 16:19:22 +02:00
|
|
|
|
2010-03-16 19:53:09 +01:00
|
|
|
// Simplify templates.. sometimes the "simplifyTemplates" fail and
|
|
|
|
// then unsimplified function calls etc remain. These have the
|
|
|
|
// "wrong" syntax. So this function will just fix so that the
|
|
|
|
// syntax is corrected.
|
2012-01-01 21:55:05 +01:00
|
|
|
TemplateSimplifier::cleanupAfterSimplify(_tokens);
|
2010-03-16 19:53:09 +01:00
|
|
|
|
2009-07-14 12:06:38 +02:00
|
|
|
// Simplify the operator "?:"
|
|
|
|
simplifyConditionOperator();
|
|
|
|
|
2009-10-01 19:45:48 +02:00
|
|
|
// remove exception specifications..
|
|
|
|
removeExceptionSpecifications(_tokens);
|
|
|
|
|
2011-01-27 18:44:20 +01:00
|
|
|
// Collapse operator name tokens into single token
|
|
|
|
// operator = => operator=
|
|
|
|
simplifyOperatorName();
|
|
|
|
|
2011-10-16 08:09:57 +02:00
|
|
|
// Simplify pointer to standard types (C only)
|
|
|
|
simplifyPointerToStandardType();
|
|
|
|
|
2010-01-20 21:19:06 +01:00
|
|
|
// simplify function pointers
|
2010-01-23 07:43:12 +01:00
|
|
|
simplifyFunctionPointers();
|
2010-05-13 13:59:41 +02:00
|
|
|
|
|
|
|
// "if (not p)" => "if (!p)"
|
|
|
|
// "if (p and q)" => "if (p && q)"
|
|
|
|
// "if (p or q)" => "if (p || q)"
|
2010-05-21 12:13:05 +02:00
|
|
|
while (simplifyLogicalOperators()) { }
|
2010-05-13 13:59:41 +02:00
|
|
|
|
2010-08-28 13:32:43 +02:00
|
|
|
// Change initialisation of variable to assignment
|
|
|
|
simplifyInitVar();
|
|
|
|
|
2011-01-16 11:24:58 +01:00
|
|
|
// Split up variable declarations.
|
2011-11-05 12:23:05 +01:00
|
|
|
simplifyVarDecl(false);
|
2011-01-06 20:16:14 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!preprocessorCondition) {
|
2010-09-02 23:01:12 +02:00
|
|
|
setVarId();
|
2010-03-13 11:52:48 +01:00
|
|
|
|
2010-09-02 23:01:12 +02:00
|
|
|
// Change initialisation of variable to assignment
|
|
|
|
simplifyInitVar();
|
|
|
|
}
|
2010-08-27 22:58:21 +02:00
|
|
|
|
2011-05-01 08:36:27 +02:00
|
|
|
// Convert e.g. atol("0") into 0
|
|
|
|
simplifyMathFunctions();
|
|
|
|
|
2011-12-24 21:51:55 +01:00
|
|
|
simplifyDoublePlusAndDoubleMinus();
|
2011-05-01 08:36:27 +02:00
|
|
|
|
2011-12-24 22:23:08 +01:00
|
|
|
simplifyArrayAccessSyntax();
|
|
|
|
|
|
|
|
_tokens->assignProgressValues();
|
|
|
|
|
|
|
|
removeRedundantSemicolons();
|
|
|
|
|
|
|
|
simplifyReservedWordNullptr();
|
|
|
|
|
|
|
|
simplifyParameterVoid();
|
|
|
|
|
|
|
|
simplifyRedundantConsecutiveBraces();
|
|
|
|
|
|
|
|
return validate();
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool Tokenizer::hasComplicatedSyntaxErrorsInTemplates()
|
|
|
|
{
|
|
|
|
// check for more complicated syntax errors when using templates..
|
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
// skip executing scopes..
|
|
|
|
if (Token::Match(tok, ") const| {") || Token::Match(tok, "[,=] {")) {
|
|
|
|
while (tok->str() != "{")
|
|
|
|
tok = tok->next();
|
|
|
|
tok = tok->link();
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip executing scopes (ticket #1984)..
|
|
|
|
if (Token::simpleMatch(tok, "; {"))
|
|
|
|
tok = tok->next()->link();
|
|
|
|
|
|
|
|
// skip executing scopes (ticket #3183)..
|
|
|
|
if (Token::simpleMatch(tok, "( {"))
|
|
|
|
tok = tok->next()->link();
|
|
|
|
|
|
|
|
// skip executing scopes (ticket #1985)..
|
|
|
|
if (Token::simpleMatch(tok, "try {")) {
|
|
|
|
tok = tok->next()->link();
|
|
|
|
while (Token::simpleMatch(tok, "} catch (")) {
|
|
|
|
tok = tok->linkAt(2);
|
|
|
|
if (Token::simpleMatch(tok, ") {"))
|
|
|
|
tok = tok->next()->link();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not start of statement?
|
|
|
|
if (tok->previous() && !Token::Match(tok, "[;{}]"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// skip starting tokens.. ;;; typedef typename foo::bar::..
|
|
|
|
while (Token::Match(tok, "[;{}]"))
|
|
|
|
tok = tok->next();
|
|
|
|
while (Token::Match(tok, "typedef|typename"))
|
|
|
|
tok = tok->next();
|
|
|
|
while (Token::Match(tok, "%type% ::"))
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// template variable or type..
|
|
|
|
if (Token::Match(tok, "%type% <")) {
|
|
|
|
// these are used types..
|
|
|
|
std::set<std::string> usedtypes;
|
|
|
|
|
|
|
|
// parse this statement and see if the '<' and '>' are matching
|
|
|
|
unsigned int level = 0;
|
|
|
|
for (const Token *tok2 = tok; tok2 && !Token::Match(tok2, "[;{}]"); tok2 = tok2->next()) {
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
tok2 = tok2->link();
|
|
|
|
else if (tok2->str() == "<") {
|
|
|
|
bool inclevel = false;
|
|
|
|
if (Token::simpleMatch(tok2->previous(), "operator <"))
|
|
|
|
;
|
|
|
|
else if (level == 0)
|
|
|
|
inclevel = true;
|
|
|
|
else if (tok2->next() && tok2->next()->isStandardType())
|
|
|
|
inclevel = true;
|
|
|
|
else if (Token::simpleMatch(tok2, "< typename"))
|
|
|
|
inclevel = true;
|
|
|
|
else if (Token::Match(tok2->tokAt(-2), "<|, %type% <") && usedtypes.find(tok2->previous()->str()) != usedtypes.end())
|
|
|
|
inclevel = true;
|
|
|
|
else if (Token::Match(tok2, "< %type%") && usedtypes.find(tok2->next()->str()) != usedtypes.end())
|
|
|
|
inclevel = true;
|
|
|
|
else if (Token::Match(tok2, "< %type%")) {
|
|
|
|
// is the next token a type and not a variable/constant?
|
|
|
|
// assume it's a type if there comes another "<"
|
|
|
|
const Token *tok3 = tok2->next();
|
|
|
|
while (Token::Match(tok3, "%type% ::"))
|
|
|
|
tok3 = tok3->tokAt(2);
|
|
|
|
if (Token::Match(tok3, "%type% <"))
|
|
|
|
inclevel = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inclevel) {
|
|
|
|
++level;
|
|
|
|
if (Token::Match(tok2->tokAt(-2), "<|, %type% <"))
|
|
|
|
usedtypes.insert(tok2->previous()->str());
|
|
|
|
}
|
|
|
|
} else if (tok2->str() == ">") {
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
} else if (tok2->str() == ">>") {
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (level > 0) {
|
|
|
|
syntaxError(tok);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tokenizer::simplifyDefaultAndDeleteInsideClass()
|
|
|
|
{
|
|
|
|
// Remove "= default|delete" inside class|struct definitions
|
|
|
|
// Todo: Remove it if it is used "externally" too.
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "struct|class %var% :|{")) {
|
|
|
|
unsigned int indentlevel = 0;
|
2011-12-28 16:49:05 +01:00
|
|
|
for (Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) {
|
2011-12-24 22:23:08 +01:00
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}") {
|
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
} else if (indentlevel == 1 && Token::Match(tok2, ") = delete|default ;")) {
|
|
|
|
Token * const end = tok2->tokAt(4);
|
|
|
|
tok2 = tok2->link()->previous();
|
|
|
|
|
|
|
|
// operator ==|>|<|..
|
|
|
|
if (Token::Match(tok2->previous(), "operator %any%"))
|
|
|
|
tok2 = tok2->previous();
|
|
|
|
else if (Token::simpleMatch(tok2->tokAt(-2), "operator [ ]"))
|
|
|
|
tok2 = tok2->tokAt(-2);
|
|
|
|
else if (Token::simpleMatch(tok2->tokAt(-2), "operator ( )"))
|
|
|
|
tok2 = tok2->tokAt(-2);
|
|
|
|
else if (Token::simpleMatch(tok2->tokAt(-3), "operator delete [ ]"))
|
|
|
|
tok2 = tok2->tokAt(-3);
|
|
|
|
|
|
|
|
while ((tok2->isName() && tok2->str().find(":") == std::string::npos) ||
|
|
|
|
Token::Match(tok2, "[&*~]"))
|
|
|
|
tok2 = tok2->previous();
|
|
|
|
if (Token::Match(tok2, "[;{}]") || tok2->isName())
|
|
|
|
Token::eraseTokens(tok2, end);
|
2011-12-31 21:43:06 +01:00
|
|
|
else
|
|
|
|
tok2 = end->previous();
|
2011-12-24 22:23:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Tokenizer::hasEnumsWithTypedef()
|
|
|
|
{
|
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "enum %var% {")) {
|
2011-12-30 11:36:09 +01:00
|
|
|
tok = tok->tokAt(2);
|
|
|
|
const Token *tok2 = Token::findmatch(tok, "typedef", tok->link());
|
|
|
|
if (tok2) {
|
|
|
|
syntaxError(tok2);
|
|
|
|
return true;
|
2011-12-24 22:23:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tokenizer::simplifyDebugNew()
|
|
|
|
{
|
|
|
|
// convert Microsoft DEBUG_NEW macro to new
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == "DEBUG_NEW")
|
|
|
|
tok->str("new");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tokenizer::simplifyArrayAccessSyntax()
|
|
|
|
{
|
2011-05-01 08:36:27 +02:00
|
|
|
// 0[a] -> a[0]
|
2011-10-13 20:53:06 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "%num% [ %var% ]")) {
|
2011-05-01 08:36:27 +02:00
|
|
|
const std::string temp = tok->str();
|
2011-10-29 01:57:53 +02:00
|
|
|
tok->str(tok->strAt(2));
|
2011-05-01 08:36:27 +02:00
|
|
|
tok->tokAt(2)->str(temp);
|
|
|
|
}
|
|
|
|
}
|
2011-12-24 22:23:08 +01:00
|
|
|
}
|
2011-05-01 08:36:27 +02:00
|
|
|
|
2011-12-24 22:23:08 +01:00
|
|
|
void Tokenizer::simplifyParameterVoid()
|
|
|
|
{
|
|
|
|
for (Token* tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "%var% ( void )"))
|
|
|
|
tok->next()->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
2010-10-26 21:05:20 +02:00
|
|
|
|
2011-12-24 22:23:08 +01:00
|
|
|
void Tokenizer::simplifyReservedWordNullptr()
|
|
|
|
{
|
2011-10-22 09:45:48 +02:00
|
|
|
if (_settings->standards.cpp11) {
|
2011-10-16 12:54:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == "nullptr")
|
|
|
|
tok->str("0");
|
|
|
|
}
|
|
|
|
}
|
2011-12-24 22:23:08 +01:00
|
|
|
}
|
2011-10-16 12:54:58 +02:00
|
|
|
|
2011-12-24 22:23:08 +01:00
|
|
|
void Tokenizer::simplifyRedundantConsecutiveBraces()
|
|
|
|
{
|
2011-11-12 16:59:20 +01:00
|
|
|
// Remove redundant consecutive braces, i.e. '.. { { .. } } ..' -> '.. { .. } ..'.
|
2011-11-14 09:21:42 +01:00
|
|
|
for (Token *tok = _tokens; tok;) {
|
2011-11-12 16:59:20 +01:00
|
|
|
if (Token::simpleMatch(tok, "{ {") && Token::simpleMatch(tok->next()->link(), "} }")) {
|
|
|
|
//remove internal parentheses
|
|
|
|
tok->next()->link()->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
} else
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
2009-05-03 21:23:47 +02:00
|
|
|
}
|
|
|
|
|
2011-12-24 21:51:55 +01:00
|
|
|
void Tokenizer::simplifyDoublePlusAndDoubleMinus()
|
|
|
|
{
|
|
|
|
// Convert + + into + and + - into -
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
while (tok->next()) {
|
|
|
|
if (tok->str() == "+") {
|
|
|
|
if (tok->next()->str() == "+") {
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
} else if (tok->next()->str() == "-") {
|
|
|
|
tok->str("-");
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (tok->str() == "-") {
|
|
|
|
if (tok->next()->str() == "-") {
|
|
|
|
tok->str("+");
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
} else if (tok->next()->str() == "+") {
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-28 22:51:13 +01:00
|
|
|
void Tokenizer::simplifyJavaAndCSharp()
|
|
|
|
{
|
|
|
|
// better don't call isJava in the loop
|
2012-01-02 00:45:32 +01:00
|
|
|
const bool isJava_ = isJava();
|
2011-12-28 22:51:13 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == "private")
|
|
|
|
tok->str("private:");
|
|
|
|
else if (tok->str() == "protected")
|
|
|
|
tok->str("protected:");
|
|
|
|
else if (tok->str() == "public")
|
|
|
|
tok->str("public:");
|
|
|
|
|
|
|
|
else if (isJava_) {
|
|
|
|
if (Token::Match(tok, ") throws %var% {"))
|
|
|
|
tok->deleteNext(2);
|
|
|
|
} else {
|
2012-01-02 00:45:32 +01:00
|
|
|
//remove 'using var;' from code
|
|
|
|
if (Token::Match(tok, "using %var% ;") &&
|
|
|
|
(!tok->previous() || Token::Match(tok->previous(), "[,;{}]"))) {
|
|
|
|
tok->deleteNext(2);
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
|
2012-01-02 00:21:58 +01:00
|
|
|
//simplify C# arrays of arrays and multidimension arrays
|
|
|
|
while (Token::Match(tok, "%type% [ ,|]") &&
|
2011-12-30 12:12:54 +01:00
|
|
|
(!tok->previous() || Token::Match(tok->previous(), "[,;{}]"))) {
|
2012-01-02 00:21:58 +01:00
|
|
|
Token *tok2 = tok->tokAt(2);
|
2011-12-30 12:12:54 +01:00
|
|
|
unsigned int count = 1;
|
2012-01-02 00:21:58 +01:00
|
|
|
while (tok2 && tok2->str() == ",") {
|
2011-12-30 12:12:54 +01:00
|
|
|
++count;
|
2012-01-02 00:21:58 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
if (!tok2 || tok2->str() != "]")
|
|
|
|
break;
|
|
|
|
tok2 = tok2->next();
|
|
|
|
while (Token::Match(tok2, "[ ,|]")) {
|
|
|
|
tok2 = tok2->next();
|
|
|
|
while (tok2 && tok2->str() == ",") {
|
|
|
|
++count;
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
if (!tok2 || tok2->str() != "]")
|
|
|
|
break;
|
|
|
|
++count;
|
|
|
|
tok2 = tok2->next();
|
2011-12-30 12:12:54 +01:00
|
|
|
}
|
|
|
|
if (!tok2)
|
|
|
|
break;
|
|
|
|
else if (Token::Match(tok2, "%var% [;,=]")) {
|
|
|
|
Token::eraseTokens(tok, tok2);
|
|
|
|
do {
|
|
|
|
tok->insertToken("*");
|
|
|
|
} while (--count);
|
|
|
|
tok = tok2->tokAt(2);
|
|
|
|
}
|
2011-12-28 22:51:13 +01:00
|
|
|
}
|
2011-12-30 12:12:54 +01:00
|
|
|
if (!tok)
|
|
|
|
break;
|
2011-12-28 22:51:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-17 22:16:18 +01:00
|
|
|
/** Specify array size if it hasn't been given */
|
2010-02-20 18:13:09 +01:00
|
|
|
|
|
|
|
void Tokenizer::arraySize()
|
|
|
|
{
|
2011-12-11 23:13:37 +01:00
|
|
|
bool addlength = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
2011-12-11 23:13:37 +01:00
|
|
|
if (Token::Match(tok, "%var% [ ] = { %str% } ;")) {
|
2011-12-07 02:23:21 +01:00
|
|
|
Token *t = tok->tokAt(3);
|
|
|
|
t->deleteNext();
|
|
|
|
t->next()->deleteNext();
|
2011-12-11 23:13:37 +01:00
|
|
|
addlength = true;
|
2010-10-12 21:52:02 +02:00
|
|
|
}
|
|
|
|
|
2011-12-11 23:13:37 +01:00
|
|
|
if (addlength || Token::Match(tok, "%var% [ ] = %str% ;")) {
|
|
|
|
tok = tok->next();
|
|
|
|
std::size_t sz = tok->strAt(3).length() - 1;
|
|
|
|
tok->insertToken(MathLib::toString<unsigned int>((unsigned int)sz));
|
|
|
|
addlength = false;
|
|
|
|
tok = tok->tokAt(5);
|
|
|
|
}
|
2011-12-07 21:15:00 +01:00
|
|
|
|
2011-12-11 23:13:37 +01:00
|
|
|
else if (Token::Match(tok, "%var% [ ] = {")) {
|
|
|
|
unsigned int sz = 1;
|
|
|
|
tok = tok->next();
|
|
|
|
Token *end = tok->linkAt(3);
|
|
|
|
for (Token *tok2 = tok->tokAt(4); tok2 && tok2 != end; tok2 = tok2->next()) {
|
2011-12-07 21:15:00 +01:00
|
|
|
if (tok2->str() == "{" || tok2->str() == "(" || tok2->str() == "[")
|
|
|
|
tok2 = tok2->link();
|
|
|
|
else if (tok2->str() == "<") { // Bailout. TODO: When link() supports <>, this bailout becomes unnecessary
|
|
|
|
sz = 0;
|
2011-02-01 21:46:07 +01:00
|
|
|
break;
|
2011-12-07 21:15:00 +01:00
|
|
|
} else if (tok2->str() == ",") {
|
|
|
|
if (!Token::Match(tok2->next(), "[},]"))
|
|
|
|
++sz;
|
2011-12-12 02:23:49 +01:00
|
|
|
else {
|
|
|
|
tok2 = tok2->previous();
|
|
|
|
tok2->deleteNext();
|
|
|
|
}
|
2011-12-07 21:15:00 +01:00
|
|
|
}
|
2010-02-20 18:13:09 +01:00
|
|
|
}
|
|
|
|
|
2011-12-07 21:15:00 +01:00
|
|
|
if (sz != 0)
|
2011-12-11 23:13:37 +01:00
|
|
|
tok->insertToken(MathLib::toString<unsigned int>(sz));
|
2010-04-24 21:48:58 +02:00
|
|
|
|
2011-12-11 23:13:37 +01:00
|
|
|
tok = end->next() ? end->next() : end;
|
2010-04-24 21:48:58 +02:00
|
|
|
}
|
2010-02-20 18:13:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-10 14:13:48 +01:00
|
|
|
/** simplify labels and case|default in the code: add a ";" if not already in.*/
|
2010-02-21 09:47:41 +01:00
|
|
|
|
2011-12-10 14:13:48 +01:00
|
|
|
void Tokenizer::simplifyLabelsCaseDefault()
|
2010-02-21 09:47:41 +01:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, ") const| {")) {
|
2010-02-21 09:47:41 +01:00
|
|
|
// Simplify labels in the executable scope..
|
|
|
|
unsigned int indentlevel = 0;
|
2011-12-01 10:48:14 +01:00
|
|
|
while (NULL != (tok = tok->next())) {
|
2011-12-10 23:49:56 +01:00
|
|
|
if (tok->str() == "{") {
|
|
|
|
if (tok->previous() && tok->previous()->str() == "=")
|
|
|
|
tok = tok->link();
|
|
|
|
else
|
|
|
|
++indentlevel;
|
|
|
|
} else if (tok->str() == "}") {
|
2010-02-21 09:47:41 +01:00
|
|
|
--indentlevel;
|
2011-10-19 14:20:09 +02:00
|
|
|
if (!indentlevel)
|
|
|
|
break;
|
2011-12-10 14:13:48 +01:00
|
|
|
} else if (tok->str() == "(" || tok->str() == "[")
|
2011-12-08 17:42:26 +01:00
|
|
|
tok = tok->link();
|
2010-02-21 09:47:41 +01:00
|
|
|
|
2011-12-10 14:13:48 +01:00
|
|
|
if (Token::Match(tok, "[;{}] case")) {
|
2011-12-01 10:48:14 +01:00
|
|
|
while (NULL != (tok = tok->next())) {
|
2011-12-07 21:15:00 +01:00
|
|
|
if (tok->str() == ":")
|
2011-10-19 14:20:09 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-12-10 14:13:48 +01:00
|
|
|
if (Token::Match(tok, ": !!;")) {
|
2011-10-19 14:20:09 +02:00
|
|
|
tok->insertToken(";");
|
2011-10-08 21:13:53 +02:00
|
|
|
}
|
2011-12-10 14:13:48 +01:00
|
|
|
} else if (Token::Match(tok, "[;{}] %var% : !!;")) {
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
tok->insertToken(";");
|
2010-08-18 22:42:04 +02:00
|
|
|
}
|
2010-02-21 09:47:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-20 18:13:09 +01:00
|
|
|
|
2009-11-01 15:56:00 +01:00
|
|
|
/**
|
2010-03-17 22:16:18 +01:00
|
|
|
* is the token pointing at a template parameters block
|
2009-11-01 15:56:00 +01:00
|
|
|
* < int , 3 > => yes
|
|
|
|
* \param tok start token that must point at "<"
|
2011-02-20 12:17:05 +01:00
|
|
|
* \return number of parameters (invalid parameters => 0)
|
2009-11-01 15:56:00 +01:00
|
|
|
*/
|
2011-02-20 12:17:05 +01:00
|
|
|
static unsigned int templateParameters(const Token *tok)
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2011-02-20 12:17:05 +01:00
|
|
|
unsigned int numberOfParameters = 0;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2011-02-20 12:17:05 +01:00
|
|
|
return 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != "<")
|
2011-02-20 12:17:05 +01:00
|
|
|
return 0;
|
2009-11-01 15:56:00 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
2011-12-04 11:38:41 +01:00
|
|
|
unsigned int level = 0;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok) {
|
2011-12-04 11:38:41 +01:00
|
|
|
if (level == 0)
|
|
|
|
++numberOfParameters;
|
2011-02-20 12:22:01 +01:00
|
|
|
|
2011-02-20 12:17:05 +01:00
|
|
|
// skip std::
|
|
|
|
while (Token::Match(tok, "%var% ::"))
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
if (!tok)
|
|
|
|
return 0;
|
|
|
|
|
2009-11-01 15:56:00 +01:00
|
|
|
// num/type ..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->isNumber() && !tok->isName())
|
2011-02-20 12:17:05 +01:00
|
|
|
return 0;
|
2009-11-01 15:56:00 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// optional "*"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "*")
|
2009-11-01 15:56:00 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
2011-12-04 11:38:41 +01:00
|
|
|
// inner template
|
2012-01-02 12:15:02 +01:00
|
|
|
if (tok->str() ==
|