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
|
2011-01-09 20:33:36 +01:00
|
|
|
* Copyright (C) 2007-2011 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"
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
#include <locale>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <list>
|
2009-09-13 15:00:48 +02:00
|
|
|
#include <cassert>
|
2009-01-24 18:15:38 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
2009-08-27 18:33:42 +02:00
|
|
|
#include <stack>
|
2009-09-01 19:30:54 +02:00
|
|
|
#include <stdexcept> // for std::runtime_error
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Tokenizer::Tokenizer()
|
2010-04-15 20:08:51 +02:00
|
|
|
: _settings(0), _errorLogger(0)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2011-01-01 11:40:32 +01:00
|
|
|
// No tokens to start with
|
2009-01-24 18:15:38 +01:00
|
|
|
_tokens = 0;
|
|
|
|
_tokensBack = 0;
|
2011-01-01 11:40:32 +01:00
|
|
|
|
|
|
|
// is there any templates?
|
2010-09-30 21:22:49 +02:00
|
|
|
_codeWithTemplates = false;
|
2011-01-01 11:40:32 +01:00
|
|
|
|
|
|
|
// symbol database
|
2010-11-23 18:41:07 +01:00
|
|
|
_symbolDatabase = NULL;
|
2011-02-26 14:42:19 +01:00
|
|
|
|
|
|
|
// variable count
|
|
|
|
_varId = 0;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-07-13 19:11:31 +02:00
|
|
|
Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger)
|
2010-04-15 20:08:51 +02:00
|
|
|
: _settings(settings), _errorLogger(errorLogger)
|
2009-03-16 22:31:52 +01:00
|
|
|
{
|
2010-12-01 18:00:55 +01:00
|
|
|
// make sure settings are specified
|
|
|
|
assert(_settings);
|
|
|
|
|
2011-01-01 11:40:32 +01:00
|
|
|
// No tokens to start with
|
2009-03-16 22:31:52 +01:00
|
|
|
_tokens = 0;
|
|
|
|
_tokensBack = 0;
|
2011-01-01 11:40:32 +01:00
|
|
|
|
|
|
|
// is there any templates?
|
2010-09-30 21:22:49 +02:00
|
|
|
_codeWithTemplates = false;
|
2011-01-01 11:40:32 +01:00
|
|
|
|
|
|
|
// symbol database
|
2010-11-23 18:41:07 +01:00
|
|
|
_symbolDatabase = NULL;
|
2011-02-26 14:42:19 +01:00
|
|
|
|
|
|
|
// variable count
|
|
|
|
_varId = 0;
|
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
|
|
|
|
if (split && strstr(str, "##"))
|
|
|
|
{
|
|
|
|
std::string temp;
|
|
|
|
for (unsigned int i = 0; str[i]; ++i)
|
|
|
|
{
|
|
|
|
if (strncmp(&str[i], "##", 2) == 0)
|
|
|
|
{
|
|
|
|
addtoken(temp.c_str(), lineno, fileno, false);
|
|
|
|
temp.clear();
|
|
|
|
addtoken("##", lineno, fileno, false);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
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;
|
2010-12-27 08:09:05 +01: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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str2 << str;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_tokensBack)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
_tokensBack->insertToken(str2.str().c_str());
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Replace hexadecimal value with decimal
|
|
|
|
std::ostringstream str2;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (strncmp(tok->str().c_str(), "0x", 2) == 0)
|
2010-03-31 17:14:49 +02:00
|
|
|
{
|
|
|
|
str2 << std::strtoul(tok->str().c_str() + 2, NULL, 16);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str2 << tok->str();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_tokensBack)
|
2010-03-31 17:14:49 +02:00
|
|
|
{
|
|
|
|
_tokensBack->insertToken(str2.str().c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_tokens = new Token(&_tokensBack);
|
|
|
|
_tokensBack = _tokens;
|
|
|
|
_tokensBack->str(str2.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
_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;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (type->isLong())
|
2010-03-28 15:56:13 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type->str() == "double")
|
2010-03-28 15:56:13 +02:00
|
|
|
return sizeof(long double);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (type->str() == "long")
|
2010-03-28 15:56:13 +02:00
|
|
|
return sizeof(long long);
|
|
|
|
}
|
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
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (n > 0)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-05-03 20:10:59 +02:00
|
|
|
dest->insertToken(src->str().c_str());
|
2009-01-24 18:15:38 +01:00
|
|
|
dest = dest->next();
|
|
|
|
dest->fileIndex(src->fileIndex());
|
|
|
|
dest->linenr(src->linenr());
|
|
|
|
dest->varId(src->varId());
|
2010-03-28 15:56:13 +02:00
|
|
|
dest->isUnsigned(src->isUnsigned());
|
|
|
|
dest->isSigned(src->isSigned());
|
|
|
|
dest->isLong(src->isLong());
|
2009-01-24 18:15:38 +01:00
|
|
|
src = src->next();
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
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;
|
|
|
|
for (const Token *tok = first; tok != last->next(); tok = tok->next())
|
|
|
|
{
|
|
|
|
tok2->insertToken(tok->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok2->fileIndex(dest->fileIndex());
|
|
|
|
tok2->linenr(dest->linenr());
|
|
|
|
tok2->isUnsigned(tok->isUnsigned());
|
|
|
|
tok2->isSigned(tok->isSigned());
|
|
|
|
tok2->isLong(tok->isLong());
|
|
|
|
|
|
|
|
// Check for links and fix them up
|
|
|
|
if (tok2->str() == "(" || tok2->str() == "[" || tok2->str() == "{")
|
|
|
|
links.push(tok2);
|
|
|
|
else if (tok2->str() == ")" || tok2->str() == "]" || tok2->str() == "}")
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Read one byte at a time from code and create tokens
|
2010-04-02 07:30:58 +02:00
|
|
|
for (char ch = (char)code.get(); code.good(); ch = (char)code.get())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
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.
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '\'' || ch == '\"')
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
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;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// 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();
|
|
|
|
}
|
2010-04-02 07:30:58 +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"
|
2010-04-02 07:30:58 +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);
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 0; i < _files.size(); i++)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2011-01-18 17:34:28 +01:00
|
|
|
if (Path::sameFileName(_files[i].c_str(), line.c_str()))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Use this index
|
|
|
|
foundOurfile = true;
|
|
|
|
FileIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +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;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-15 22:09:27 +01:00
|
|
|
// Add previous token
|
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
|
|
|
|
// Add content of the string
|
2009-01-24 18:15:38 +01:00
|
|
|
addtoken(line.c_str(), lineno, FileIndex);
|
|
|
|
}
|
2009-03-15 22:09:27 +01:00
|
|
|
|
|
|
|
CurrentToken.clear();
|
|
|
|
|
|
|
|
continue;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (strchr("+-*/%&|^?!=<>[](){};:,.~\n ", ch))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '.' &&
|
|
|
|
CurrentToken.length() > 0 &&
|
|
|
|
std::isdigit(CurrentToken[0]))
|
2009-02-08 10:51:45 +01:00
|
|
|
{
|
2009-02-08 11:56:20 +01:00
|
|
|
// Don't separate doubles "5.4"
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (strchr("+-", ch) &&
|
|
|
|
CurrentToken.length() > 0 &&
|
|
|
|
std::isdigit(CurrentToken[0]) &&
|
2010-04-04 08:01:05 +02:00
|
|
|
(CurrentToken[CurrentToken.length()-1] == 'e' ||
|
|
|
|
CurrentToken[CurrentToken.length()-1] == 'E'))
|
2009-02-08 11:56:20 +01:00
|
|
|
{
|
|
|
|
// Don't separate doubles "4.2e+10"
|
2009-02-08 10:51:45 +01:00
|
|
|
}
|
2011-01-09 10:09:54 +01:00
|
|
|
else if (CurrentToken.empty() && ch == '.' && std::isdigit(code.peek()))
|
|
|
|
{
|
|
|
|
// tokenize .125 into 0.125
|
|
|
|
CurrentToken = "0";
|
|
|
|
}
|
2010-07-05 22:41:02 +02:00
|
|
|
else if (ch=='&' && CurrentToken.empty() && code.peek() == '&')
|
|
|
|
{
|
|
|
|
// &&
|
2010-07-27 17:48:22 +02:00
|
|
|
ch = (char)code.get();
|
2010-07-05 22:41:02 +02:00
|
|
|
addtoken("&&", lineno, FileIndex, true);
|
|
|
|
continue;
|
|
|
|
}
|
2009-02-08 10:51:45 +01:00
|
|
|
else
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (CurrentToken == "#file")
|
2009-03-15 22:09:27 +01:00
|
|
|
{
|
|
|
|
// Handle this where strings are handled
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (CurrentToken == "#endfile")
|
2009-03-15 22:09:27 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (lineNumbers.empty() || fileIndexes.empty())
|
2009-03-15 22:09:27 +01:00
|
|
|
{
|
2010-07-18 12:57:29 +02:00
|
|
|
cppcheckError(0);
|
2010-07-18 12:44:55 +02:00
|
|
|
deallocateTokens();
|
|
|
|
return;
|
2009-03-15 22:09:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lineno = lineNumbers.back();
|
|
|
|
lineNumbers.pop_back();
|
|
|
|
FileIndex = fileIndexes.back();
|
|
|
|
fileIndexes.pop_back();
|
|
|
|
CurrentToken.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-03 09:54:10 +02:00
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex, true);
|
2009-03-15 22:09:27 +01:00
|
|
|
|
2009-02-08 10:51:45 +01:00
|
|
|
CurrentToken.clear();
|
2009-03-15 22:09:27 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '\n')
|
2009-03-15 22:09:27 +01:00
|
|
|
{
|
|
|
|
++lineno;
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (ch == ' ')
|
2009-03-15 22:09:27 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-02-08 10:51:45 +01:00
|
|
|
CurrentToken += ch;
|
2009-02-14 11:13:50 +01:00
|
|
|
// Add "++", "--" or ">>" token
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((ch == '+' || ch == '-' || ch == '>') && (code.peek() == ch))
|
2009-02-14 11:13:50 +01:00
|
|
|
CurrentToken += (char)code.get();
|
2009-02-08 10:51:45 +01:00
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
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);
|
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
|
|
|
{
|
2010-12-07 07:07:07 +01:00
|
|
|
if (!(_settings->_checkCodingStyle))
|
2010-02-20 09:07:29 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok1->linenr();
|
2010-07-17 00:27:40 +02:00
|
|
|
loc.setfile(file(tok1));
|
2010-02-20 09:07:29 +01:00
|
|
|
locationList.push_back(loc);
|
|
|
|
loc.line = tok2->linenr();
|
2010-07-17 00:27:40 +02:00
|
|
|
loc.setfile(file(tok2));
|
2010-02-20 09:07:29 +01:00
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
2010-07-14 22:11:32 +02:00
|
|
|
Severity::style,
|
2010-02-20 09:07:29 +01:00
|
|
|
std::string(type + " '" + tok2->str() +
|
2010-04-15 20:08:51 +02:00
|
|
|
"' hides typedef with same name"),
|
2010-02-20 09:07:29 +01:00
|
|
|
"variableHidingTypedef");
|
|
|
|
|
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)
|
|
|
|
{
|
2010-12-07 07:07:07 +01:00
|
|
|
if (!(_settings->_checkCodingStyle))
|
2010-04-12 19:05:31 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok1->linenr();
|
2010-07-17 00:27:40 +02:00
|
|
|
loc.setfile(file(tok1));
|
2010-04-12 19:05:31 +02:00
|
|
|
locationList.push_back(loc);
|
|
|
|
loc.line = tok2->linenr();
|
2010-07-17 00:27:40 +02:00
|
|
|
loc.setfile(file(tok2));
|
2010-04-12 19:05:31 +02:00
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
2010-07-14 22:11:32 +02:00
|
|
|
Severity::style,
|
2010-04-12 19:05:31 +02:00
|
|
|
std::string(type + " '" + tok2->str() +
|
2010-04-15 20:08:51 +02:00
|
|
|
"' forward declaration unnecessary, already declared"),
|
2010-04-12 19:05:31 +02:00
|
|
|
"unnecessaryForwardDeclaration");
|
|
|
|
|
|
|
|
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-02-12 01:09:24 +01:00
|
|
|
bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef)
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
|
|
|
// check for an end of definition
|
|
|
|
const Token * tok = *tokPtr;
|
2010-06-20 20:51:36 +02:00
|
|
|
if (tok && Token::Match(tok->next(), ";|,|[|=|)|>|(|{"))
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
2010-02-20 09:07:29 +01:00
|
|
|
const Token * end = tok->next();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end->str() == "[")
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
|
|
|
end = end->link()->next();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (end->str() == ",")
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
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(), ";|)|>") ||
|
|
|
|
(end->next()->str() == ")" && level == 0)))
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end->next()->str() == "(")
|
2010-02-20 09:07:29 +01:00
|
|
|
level++;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (end->next()->str() == ")")
|
2010-02-20 09:07:29 +01:00
|
|
|
level--;
|
2010-02-03 07:58:36 +01:00
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
end = end->next();
|
|
|
|
}
|
2010-02-27 07:27:51 +01:00
|
|
|
end = end->next();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (end->str() == "(")
|
2010-02-27 07:27:51 +01:00
|
|
|
{
|
2011-01-27 18:44:20 +01:00
|
|
|
if (tok->previous()->str().find("operator") == 0)
|
2010-02-27 07:27:51 +01:00
|
|
|
{
|
|
|
|
// conversion operator
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->previous()->str() == "typedef")
|
2010-02-27 07:27:51 +01:00
|
|
|
{
|
|
|
|
// typedef of function returning this type
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok->previous(), "public:|private:|protected:"))
|
2010-02-27 07:27:51 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->previous()->str() == ">")
|
2010-02-27 07:27:51 +01:00
|
|
|
{
|
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;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok->previous(), "%type%"))
|
2010-02-27 07:27:51 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end)
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
2011-02-02 16:48:00 +01:00
|
|
|
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%") &&
|
|
|
|
!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;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +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%") &&
|
|
|
|
!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();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end)
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
|
|
|
duplicateTypedefError(*tokPtr, name, "Template parameter");
|
|
|
|
*tokPtr = end->link();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// 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-02-02 17:12:46 +01: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;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok && tok->previous() && (!Token::Match(tok->previous(), ";|{") || (level != 0)))
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
2011-02-12 01:09:24 +01:00
|
|
|
if (tok->previous()->str() == "}")
|
|
|
|
{
|
|
|
|
tok = tok->previous()->link();
|
|
|
|
}
|
|
|
|
else if (tok->previous()->str() == "typedef")
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
|
|
|
duplicateTypedefError(*tokPtr, name, "Typedef");
|
|
|
|
return true;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->previous()->str() == "enum")
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
|
|
|
duplicateTypedefError(*tokPtr, name, "Enum");
|
|
|
|
return true;
|
|
|
|
}
|
2010-04-12 19:05:31 +02:00
|
|
|
else if (tok->previous()->str() == "struct")
|
|
|
|
{
|
2011-02-12 01:09:24 +01:00
|
|
|
if (tok->strAt(-2) == "typedef" &&
|
|
|
|
tok->next()->str() == "{" &&
|
|
|
|
typeDef->strAt(3) != "{")
|
|
|
|
{
|
|
|
|
// declaration after forward declaration
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (tok->next()->str() != ";")
|
2010-04-12 19:05:31 +02:00
|
|
|
{
|
|
|
|
duplicateTypedefError(*tokPtr, name, "Struct");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// forward declaration after declaration
|
|
|
|
duplicateDeclarationError(*tokPtr, name, "Struct");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-04-14 19:06:51 +02:00
|
|
|
else if (tok->previous()->str() == "union")
|
|
|
|
{
|
|
|
|
if (tok->next()->str() != ";")
|
|
|
|
{
|
|
|
|
duplicateTypedefError(*tokPtr, name, "Union");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// forward declaration after declaration
|
|
|
|
duplicateDeclarationError(*tokPtr, name, "Union");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tok->previous()->str() == "class")
|
|
|
|
{
|
|
|
|
if (tok->next()->str() != ";")
|
|
|
|
{
|
|
|
|
duplicateTypedefError(*tokPtr, name, "Class");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// forward declaration after declaration
|
|
|
|
duplicateDeclarationError(*tokPtr, name, "Class");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->previous()->str() == "{")
|
2010-02-20 09:07:29 +01:00
|
|
|
level--;
|
|
|
|
|
|
|
|
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-01-07 08:02:47 +01:00
|
|
|
int level = 0;
|
|
|
|
while (tok)
|
2010-08-26 20:44:13 +02:00
|
|
|
{
|
2011-01-07 08:02:47 +01:00
|
|
|
if (level == 0 && tok->str() == ";")
|
|
|
|
break;
|
|
|
|
else if (tok->str() == "{")
|
|
|
|
level++;
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
level--;
|
|
|
|
|
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.",
|
2010-08-27 20:28:00 +02:00
|
|
|
"debug");
|
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-01-07 08:02:47 +01:00
|
|
|
int level = 0;
|
2011-01-04 07:43:40 +01:00
|
|
|
|
|
|
|
// remove typedef but leave ;
|
2011-01-07 08:02:47 +01:00
|
|
|
while (typeDef->next())
|
|
|
|
{
|
|
|
|
if (level == 0 && typeDef->next()->str() == ";")
|
|
|
|
break;
|
|
|
|
else if (typeDef->next()->str() == "{")
|
|
|
|
level++;
|
|
|
|
else if (typeDef->next()->str() == "}")
|
|
|
|
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
|
|
|
|
|
|
|
if (typeDef != _tokens)
|
|
|
|
{
|
|
|
|
tok = typeDef->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_tokens->deleteThis();
|
|
|
|
tok = _tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2011-01-17 07:21:59 +01:00
|
|
|
struct Space
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
|
|
|
bool isNamespace;
|
|
|
|
std::string className;
|
|
|
|
const Token * classEnd;
|
|
|
|
};
|
|
|
|
|
2011-01-05 17:42:55 +01:00
|
|
|
static Token *splitDefinitionFromTypedef(Token *tok)
|
|
|
|
{
|
|
|
|
Token *tok1;
|
|
|
|
std::string name;
|
|
|
|
bool isConst = false;
|
|
|
|
|
|
|
|
if (tok->next()->str() == "const")
|
|
|
|
{
|
|
|
|
tok->next()->deleteThis();
|
|
|
|
isConst = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->tokAt(2)->str() == "{") // unnamed
|
|
|
|
{
|
|
|
|
tok1 = tok->tokAt(2)->link();
|
|
|
|
|
|
|
|
if (tok1 && tok1->next())
|
|
|
|
{
|
|
|
|
// use typedef name if available
|
|
|
|
if (Token::Match(tok1->next(), "%type%"))
|
|
|
|
name = tok1->next()->str();
|
|
|
|
else // create a unique name
|
|
|
|
{
|
|
|
|
static long count = 0;
|
|
|
|
name = "Unnamed" + MathLib::toString<long>(count++);
|
|
|
|
}
|
|
|
|
tok->tokAt(1)->insertToken(name.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (tok->strAt(3) == ":")
|
|
|
|
{
|
|
|
|
tok1 = tok->tokAt(4);
|
|
|
|
while (tok1 && tok1->str() != "{")
|
|
|
|
tok1 = tok1->next();
|
|
|
|
if (!tok1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tok1 = tok1->link();
|
|
|
|
|
|
|
|
name = tok->tokAt(2)->str();
|
|
|
|
}
|
|
|
|
else // has a name
|
|
|
|
{
|
|
|
|
tok1 = tok->tokAt(3)->link();
|
|
|
|
|
|
|
|
if (!tok1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
name = tok->tokAt(2)->str();
|
|
|
|
}
|
|
|
|
|
|
|
|
tok1->insertToken(";");
|
|
|
|
tok1 = tok1->next();
|
2011-02-19 20:18:37 +01:00
|
|
|
|
|
|
|
if (tok1->next()->str() == ";" && tok1 && tok1->previous()->str() == "}")
|
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
tok1->deleteThis();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
2011-01-05 17:42:55 +01:00
|
|
|
{
|
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;
|
|
|
|
if (isConst)
|
|
|
|
{
|
|
|
|
tok1->insertToken("const");
|
|
|
|
tok1 = tok1->next();
|
|
|
|
}
|
|
|
|
tok1->insertToken(tok->next()->str()); // struct, union or enum
|
|
|
|
tok1 = tok1->next();
|
|
|
|
tok1->insertToken(name.c_str());
|
|
|
|
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
|
|
|
|
* fucntion will probably need to be extended to handle a new function
|
|
|
|
* related pattern */
|
|
|
|
static Token *processFunc(Token *tok2, bool inOperator)
|
|
|
|
{
|
|
|
|
if (tok2->next() && tok2->next()->str() != ")" &&
|
|
|
|
tok2->next()->str() != ",")
|
|
|
|
{
|
|
|
|
// skip over tokens for some types of canonicalization
|
|
|
|
if (Token::Match(tok2->next(), "( * %type% ) ("))
|
|
|
|
tok2 = tok2->tokAt(5)->link();
|
|
|
|
else if (Token::Match(tok2->next(), "* ( * %type% ) ("))
|
|
|
|
tok2 = tok2->tokAt(6)->link();
|
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% [") &&
|
|
|
|
Token::Match(tok2->tokAt(4)->link(), "] ) ;|="))
|
|
|
|
tok2 = tok2->tokAt(4)->link()->next();
|
|
|
|
else if (Token::Match(tok2->next(), "* ( * %type% ("))
|
|
|
|
tok2 = tok2->tokAt(5)->link()->next();
|
2011-02-23 04:11:17 +01:00
|
|
|
else if (Token::Match(tok2->next(), "* [") &&
|
|
|
|
Token::simpleMatch(tok2->tokAt(2)->link(), "] ;"))
|
|
|
|
tok2 = tok2->next();
|
2011-02-13 19:34:55 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tok2->next()->str() == "(")
|
|
|
|
tok2 = tok2->next()->link();
|
|
|
|
else if (!inOperator && !Token::Match(tok2->next(), "[|>|;"))
|
|
|
|
{
|
|
|
|
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() == "(" &&
|
|
|
|
tok2->link()->next()->str() == "(")
|
|
|
|
{
|
|
|
|
tok2 = tok2->link();
|
|
|
|
|
|
|
|
if (tok2->next()->str() == "(")
|
|
|
|
tok2 = tok2->next()->link();
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip over typedef parameter
|
|
|
|
if (tok2->next()->str() == "(")
|
|
|
|
{
|
|
|
|
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;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
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-02-03 07:58:49 +01:00
|
|
|
if (Token::Match(tok, "class|struct|namespace %any%") &&
|
|
|
|
(!tok->previous() || (tok->previous() && tok->previous()->str() != "enum")))
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
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;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (hasClass && tok->str() == ";")
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-02-03 07:58:36 +01:00
|
|
|
hasClass = false;
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (hasClass && tok->str() == "{")
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
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;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (!spaceInfo.empty() && tok->str() == "}" && spaceInfo.back().classEnd == tok)
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-02-03 07:58:36 +01:00
|
|
|
spaceInfo.pop_back();
|
2009-09-30 13:35:00 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +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
|
|
|
|
if (tok->previous() && tok->previous()->str() == "(")
|
|
|
|
{
|
|
|
|
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% {") ||
|
|
|
|
Token::Match(tok->next(), "const| struct|enum|union|class {"))
|
2009-12-28 17:57:52 +01:00
|
|
|
{
|
2011-01-05 17:42:55 +01:00
|
|
|
Token *tok1 = splitDefinitionFromTypedef(tok);
|
|
|
|
if (!tok1)
|
|
|
|
continue;
|
|
|
|
tok = tok1;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok->next(), "const| struct|class %type% :"))
|
|
|
|
{
|
|
|
|
Token *tok1 = tok;
|
|
|
|
while (tok1 && tok1->str() != ";" && tok1->str() != "{")
|
|
|
|
tok1 = tok1->next();
|
|
|
|
if (tok1 && tok1->str() == "{")
|
2010-01-10 08:49:02 +01:00
|
|
|
{
|
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
|
|
|
|
|
|
|
/** @todo add support for struct and union */
|
|
|
|
if (Token::Match(tok, "typedef enum %type% %type% ;") && tok->strAt(2) == tok->strAt(3))
|
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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-05-28 06:55:54 +02:00
|
|
|
Token *const1 = 0;
|
|
|
|
Token *const2 = 0;
|
2010-11-11 17:02:04 +01:00
|
|
|
Token *funcStart = 0;
|
|
|
|
Token *funcEnd = 0;
|
2010-01-18 19:06:50 +01:00
|
|
|
int 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
|
|
|
|
if (!tok->next())
|
|
|
|
{
|
|
|
|
syntaxError(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-02 17:12:46 +01:00
|
|
|
if (Token::simpleMatch(tok->next(), "::") ||
|
2010-04-02 07:30:58 +02:00
|
|
|
Token::Match(tok->next(), "%type%"))
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
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
|
|
|
|
2010-10-06 05:43:07 +02:00
|
|
|
while (Token::Match(tok->tokAt(offset), "const|signed|unsigned"))
|
2010-05-23 10:46:39 +02:00
|
|
|
offset++;
|
|
|
|
|
2010-01-31 21:46:18 +01:00
|
|
|
typeEnd = tok->tokAt(offset++);
|
|
|
|
|
|
|
|
bool atEnd = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (!atEnd)
|
2010-01-31 21:46:18 +01:00
|
|
|
{
|
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-02-02 17:12:46 +01:00
|
|
|
else if (Token::simpleMatch(tok->tokAt(offset), "const ("))
|
2010-05-23 10:46:39 +02:00
|
|
|
{
|
|
|
|
typeEnd = tok->tokAt(offset++);
|
|
|
|
atEnd = true;
|
|
|
|
}
|
2010-01-31 21:46:18 +01:00
|
|
|
else
|
|
|
|
atEnd = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue; // invalid input
|
2009-12-29 07:28:00 +01:00
|
|
|
|
2011-02-22 13:48:34 +01:00
|
|
|
// check for invalid input
|
|
|
|
if (!tok->tokAt(offset))
|
|
|
|
{
|
|
|
|
syntaxError(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-31 21:46:18 +01:00
|
|
|
// check for template
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(offset)->str() == "<")
|
2010-01-31 21:46:18 +01:00
|
|
|
{
|
|
|
|
int level = 1;
|
2010-01-17 08:16:17 +01:00
|
|
|
int paren = 0;
|
2010-01-31 21:46:18 +01:00
|
|
|
typeEnd = tok->tokAt(offset + 1);
|
2010-04-02 07:30:58 +02:00
|
|
|
for (; typeEnd ; typeEnd = typeEnd->next())
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (typeEnd->str() == ">")
|
2009-12-29 07:28:00 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (paren == 0)
|
2009-12-29 07:28:00 +01:00
|
|
|
{
|
|
|
|
level--;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (level == 0)
|
2009-12-29 07:28:00 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (typeEnd->str() == "<")
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (paren == 0)
|
2009-12-29 07:28:00 +01:00
|
|
|
level++;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (typeEnd->str() == "(")
|
2010-01-17 08:16:17 +01:00
|
|
|
paren++;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (typeEnd->str() == ")")
|
2010-01-17 08:16:17 +01:00
|
|
|
paren--;
|
|
|
|
}
|
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
|
|
|
|
2010-04-02 07:30:58 +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"))
|
2010-01-18 19:06:50 +01:00
|
|
|
pointers.push_back(tok->tokAt(offset++)->str());
|
2009-10-03 17:02:23 +02:00
|
|
|
|
2010-06-20 20:51:36 +02:00
|
|
|
if (Token::Match(tok->tokAt(offset), "%type%"))
|
2010-01-18 19:06:50 +01:00
|
|
|
{
|
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
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(offset) && tok->tokAt(offset)->str() == "[")
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-01-30 19:41:22 +01:00
|
|
|
arrayStart = tok->tokAt(offset);
|
|
|
|
|
|
|
|
bool atEnd = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (!atEnd)
|
2010-01-30 19:41:22 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), ";|,"))
|
2010-01-30 19:41:22 +01:00
|
|
|
offset++;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->tokAt(offset + 1))
|
2010-01-30 19:41:22 +01:00
|
|
|
return; // invalid input
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->tokAt(offset + 1)->str() == ";")
|
2010-01-30 19:41:22 +01:00
|
|
|
atEnd = true;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->tokAt(offset)->str() == "]")
|
2010-01-30 19:41:22 +01:00
|
|
|
atEnd = true;
|
|
|
|
else
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-02 17:12:46 +01:00
|
|
|
else if (Token::simpleMatch(tok->tokAt(offset), "("))
|
2010-05-10 07:12:06 +02:00
|
|
|
{
|
2011-01-04 07:43:40 +01:00
|
|
|
// unhandled typedef, skip it and continue
|
2010-12-28 14:04:44 +01:00
|
|
|
if (typeName->str() == "void")
|
|
|
|
{
|
|
|
|
unsupportedTypedef(typeDef);
|
2011-01-04 07:43:40 +01:00
|
|
|
tok = deleteInvalidTypedef(typeDef);
|
2010-12-28 14:04:44 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-10 07:12:06 +02:00
|
|
|
function = true;
|
|
|
|
if (tok->tokAt(offset)->link()->next())
|
|
|
|
{
|
|
|
|
argStart = tok->tokAt(offset);
|
|
|
|
argEnd = tok->tokAt(offset)->link();
|
|
|
|
tok = argEnd->next();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// internal error
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2011-01-04 07:43:40 +01:00
|
|
|
|
|
|
|
// unhandled typedef, skip it and continue
|
2010-01-18 19:06:50 +01:00
|
|
|
else
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-08-26 20:44:13 +02:00
|
|
|
unsupportedTypedef(typeDef);
|
2011-01-04 07:43:40 +01:00
|
|
|
tok = deleteInvalidTypedef(typeDef);
|
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__ (") &&
|
|
|
|
Token::Match(tok->tokAt(offset)->link(), ") %type% ;"))
|
|
|
|
{
|
|
|
|
argStart = tok->tokAt(offset);
|
|
|
|
argEnd = tok->tokAt(offset)->link();
|
|
|
|
typeName = tok->tokAt(offset)->link()->next();
|
|
|
|
tok = typeName->next();
|
|
|
|
typeOf = true;
|
|
|
|
}
|
|
|
|
|
2010-11-11 17:02:04 +01:00
|
|
|
// function: typedef ... ( .... type )( ... );
|
|
|
|
else if (tok->tokAt(offset)->str() == "(" &&
|
|
|
|
Token::Match(tok->tokAt(offset)->link()->previous(), "%type% ) (") &&
|
|
|
|
Token::Match(tok->tokAt(offset)->link()->next()->link(), ") const|volatile|;"))
|
2010-01-20 21:16:40 +01:00
|
|
|
{
|
2010-11-11 17:02:04 +01:00
|
|
|
funcStart = tok->tokAt(offset + 1);
|
|
|
|
funcEnd = tok->tokAt(offset)->link()->tokAt(-2);
|
|
|
|
typeName = tok->tokAt(offset)->link()->previous();
|
|
|
|
argStart = tok->tokAt(offset)->link()->next();
|
|
|
|
argEnd = tok->tokAt(offset)->link()->next()->link();
|
|
|
|
tok = argEnd->next();
|
2010-05-23 10:46:39 +02:00
|
|
|
Token *spec = tok;
|
|
|
|
if (Token::Match(spec, "const|volatile"))
|
|
|
|
{
|
|
|
|
specStart = spec;
|
|
|
|
specEnd = spec;
|
|
|
|
while (Token::Match(spec->next(), "const|volatile"))
|
|
|
|
{
|
|
|
|
specEnd = spec->next();
|
|
|
|
spec = specEnd;
|
|
|
|
}
|
|
|
|
tok = specEnd->next();
|
|
|
|
}
|
2010-05-11 21:41:33 +02:00
|
|
|
}
|
2010-11-11 17:02:04 +01:00
|
|
|
|
2010-06-20 20:51:36 +02:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( %type% ("))
|
2010-05-10 17:50:40 +02:00
|
|
|
{
|
|
|
|
function = true;
|
|
|
|
if (tok->tokAt(offset)->link()->next())
|
|
|
|
{
|
|
|
|
typeName = tok->tokAt(offset + 1);
|
|
|
|
argStart = tok->tokAt(offset + 2);
|
|
|
|
argEnd = tok->tokAt(offset + 2)->link();
|
|
|
|
tok = tok->tokAt(offset)->link()->next();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// internal error
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-05-25 20:43:44 +02:00
|
|
|
|
|
|
|
// pointer to function returning pointer to function
|
2010-06-20 20:51:36 +02:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( * ( * %type% ) ("))
|
2010-05-25 20:43:44 +02:00
|
|
|
{
|
|
|
|
functionPtrRetFuncPtr = true;
|
|
|
|
|
|
|
|
typeName = tok->tokAt(offset + 4);
|
|
|
|
argStart = tok->tokAt(offset + 6);
|
|
|
|
argEnd = tok->tokAt(offset + 6)->link();
|
|
|
|
|
|
|
|
argFuncRetStart = argEnd->tokAt(2);
|
|
|
|
argFuncRetEnd = argEnd->tokAt(2)->link();
|
|
|
|
|
|
|
|
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-02-02 17:12:46 +01:00
|
|
|
Token::simpleMatch(tok->tokAt(offset + 3)->link(), ") ) ("))
|
2010-05-25 20:43:44 +02:00
|
|
|
{
|
|
|
|
functionRetFuncPtr = true;
|
|
|
|
|
|
|
|
typeName = tok->tokAt(offset + 2);
|
|
|
|
argStart = tok->tokAt(offset + 3);
|
|
|
|
argEnd = tok->tokAt(offset + 3)->link();
|
|
|
|
|
|
|
|
argFuncRetStart = argEnd->tokAt(2);
|
|
|
|
argFuncRetEnd = argEnd->tokAt(2)->link();
|
|
|
|
|
|
|
|
tok = argFuncRetEnd->next();
|
|
|
|
}
|
2010-06-20 20:51:36 +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);
|
|
|
|
argEnd = tok->tokAt(offset + 5)->link();
|
|
|
|
|
|
|
|
argFuncRetStart = argEnd->tokAt(2);
|
|
|
|
argFuncRetEnd = argEnd->tokAt(2)->link();
|
|
|
|
|
|
|
|
tok = argFuncRetEnd->next();
|
|
|
|
}
|
|
|
|
|
2010-05-28 06:55:54 +02:00
|
|
|
// pointer/reference to array
|
2010-06-20 20:51:36 +02:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( *|& %type% ) ["))
|
2010-05-28 06:55:54 +02:00
|
|
|
{
|
|
|
|
ptrToArray = (tok->tokAt(offset + 1)->str() == "*");
|
|
|
|
refToArray = (tok->tokAt(offset + 1)->str() == "&");
|
|
|
|
typeName = tok->tokAt(offset + 2);
|
|
|
|
arrayStart = tok->tokAt(offset + 4);
|
|
|
|
arrayEnd = arrayStart->link();
|
|
|
|
tok = arrayEnd->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// pointer to class member
|
2010-06-20 20:51:36 +02:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "( %type% :: * %type% ) ;"))
|
2010-05-28 06:55:54 +02:00
|
|
|
{
|
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
|
2010-01-17 08:16:17 +01:00
|
|
|
else
|
|
|
|
{
|
2010-08-26 20:44:13 +02:00
|
|
|
unsupportedTypedef(typeDef);
|
2011-01-04 07:43:40 +01:00
|
|
|
tok = deleteInvalidTypedef(typeDef);
|
2010-01-17 08:16:17 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
bool ok = true;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (!done)
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
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;
|
2010-12-31 09:38:03 +01:00
|
|
|
std::size_t classLevel = spaceInfo.size();
|
2010-02-03 07:58:36 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2011-01-02 17:28:47 +01:00
|
|
|
// check for end of scope
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "}")
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2011-01-02 17:28:47 +01:00
|
|
|
// check for end of member function
|
|
|
|
if (inMemberFunc)
|
|
|
|
{
|
|
|
|
memberScope--;
|
|
|
|
if (memberScope == 0)
|
|
|
|
inMemberFunc = false;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (classLevel > 0 && tok2 == spaceInfo[classLevel - 1].classEnd)
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
|
|
|
--classLevel;
|
|
|
|
pattern.clear();
|
|
|
|
|
2010-12-31 09:38:03 +01: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();
|
2010-02-03 07:58:36 +01:00
|
|
|
}
|
|
|
|
else
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-02-03 07:58:36 +01:00
|
|
|
scope--;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (scope < 0)
|
2010-02-03 07:58:36 +01:00
|
|
|
inScope = false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (exitThisScope)
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
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) == "(" &&
|
|
|
|
Token::Match(tok2->tokAt(2)->link(), ") const| {"))
|
|
|
|
{
|
|
|
|
// check for qualifier
|
|
|
|
if (tok2->previous()->str() == "::")
|
|
|
|
{
|
|
|
|
// check for available and matching class name
|
|
|
|
if (!spaceInfo.empty() && classLevel < spaceInfo.size() &&
|
|
|
|
tok2->strAt(-2) == spaceInfo[classLevel].className)
|
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
|
|
|
simplifyType = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-02 17:28:47 +01:00
|
|
|
// check for member functions
|
|
|
|
else if (Token::Match(tok2, ") const| {"))
|
|
|
|
{
|
|
|
|
const Token *func = tok2->link()->previous();
|
|
|
|
|
|
|
|
/** @todo add support for multi-token operators */
|
|
|
|
if (func->previous()->str() == "operator")
|
|
|
|
func = func->previous();
|
|
|
|
|
|
|
|
// check for qualifier
|
|
|
|
if (func->previous()->str() == "::")
|
|
|
|
{
|
|
|
|
// check for available and matching class name
|
2011-01-04 07:34:32 +01:00
|
|
|
if (!spaceInfo.empty() && classLevel < spaceInfo.size() &&
|
2011-01-02 17:28:47 +01:00
|
|
|
func->strAt(-2) == spaceInfo[classLevel].className)
|
|
|
|
{
|
|
|
|
memberScope = 0;
|
|
|
|
inMemberFunc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for entering a new scope
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "{")
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
2011-01-02 17:28:47 +01:00
|
|
|
// keep track of scopes within member function
|
|
|
|
if (inMemberFunc)
|
|
|
|
memberScope++;
|
|
|
|
|
2010-02-03 07:58:36 +01: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()) ||
|
|
|
|
(inMemberFunc && tok2->str() == typeName->str()))
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
if (pattern1.find("::") != std::string::npos) // has a "something ::"
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-12-31 09:38:03 +01:00
|
|
|
for (std::size_t i = classLevel; i < spaceInfo.size(); i++)
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
|
|
|
tok2->deleteNext();
|
|
|
|
tok2->deleteNext();
|
|
|
|
}
|
|
|
|
simplifyType = true;
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
2011-01-02 17:28:47 +01:00
|
|
|
else if ((inScope && !exitThisScope) || inMemberFunc)
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
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
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!spaceInfo.empty() && (tok2->tokAt(-2)->str() == spaceInfo[0].className) && spaceInfo[0].isNamespace)
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(-3);
|
|
|
|
tok2->deleteNext();
|
|
|
|
tok2->deleteNext();
|
|
|
|
tok2 = tok2->next();
|
|
|
|
simplifyType = true;
|
|
|
|
}
|
|
|
|
}
|
2011-01-01 20:20:03 +01:00
|
|
|
else if (Token::Match(tok2->previous(), "case %type% :"))
|
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2011-02-12 01:09:24 +01:00
|
|
|
else if (duplicateTypedef(&tok2, typeName, typeDef))
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
2010-02-20 09:07:29 +01:00
|
|
|
exitScope = scope;
|
|
|
|
|
|
|
|
// skip to end of scope if not already there
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() != "}")
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
2010-02-24 17:50:02 +01:00
|
|
|
int level = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2->next() && (tok2->next()->str() != "}" || level))
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->next()->str() == "{")
|
2010-02-24 17:50:02 +01:00
|
|
|
level++;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->next()->str() == "}")
|
2010-02-24 17:50:02 +01:00
|
|
|
level--;
|
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
2010-02-03 07:58:36 +01:00
|
|
|
}
|
2010-05-17 19:58:27 +02:00
|
|
|
else if (tok2->previous()->str() != ".")
|
2010-02-03 07:58:36 +01:00
|
|
|
{
|
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
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (simplifyType)
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
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
|
|
|
|
if (isDerived && Token::Match(typeStart, "class|struct"))
|
|
|
|
typeStart = typeStart->next();
|
|
|
|
|
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-02-10 04:02:17 +01:00
|
|
|
tok2 = copyTokens(tok2, typeStart->next(), typeEnd);
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2010-05-18 18:20:11 +02:00
|
|
|
if (!pointers.empty())
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-05-18 18:20:11 +02:00
|
|
|
std::list<std::string>::const_iterator iter;
|
|
|
|
for (iter = pointers.begin(); iter != pointers.end(); ++iter)
|
|
|
|
{
|
|
|
|
tok2->insertToken(*iter);
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
2010-01-20 21:16:40 +01:00
|
|
|
|
2010-11-11 17:02:04 +01:00
|
|
|
if (funcStart && funcEnd)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
if (specStart)
|
|
|
|
{
|
|
|
|
Token *spec = specStart;
|
|
|
|
tok2->insertToken(spec->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
while (spec != specEnd)
|
|
|
|
{
|
|
|
|
spec = spec->next();
|
|
|
|
tok2->insertToken(spec->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (functionPtr || functionRef || function)
|
2010-01-20 21:16:40 +01:00
|
|
|
{
|
2010-05-18 07:11:23 +02:00
|
|
|
// don't add parenthesis around function names because it
|
|
|
|
// confuses other simplifications
|
|
|
|
bool needParen = true;
|
2010-07-02 15:22:29 +02:00
|
|
|
if (!inTemplate && function && tok2->next()->str() != "*")
|
2010-05-18 07:11:23 +02:00
|
|
|
needParen = false;
|
|
|
|
if (needParen)
|
|
|
|
{
|
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2010-01-20 21:16:40 +01:00
|
|
|
Token *tok3 = tok2;
|
2010-05-29 07:52:06 +02:00
|
|
|
if (namespaceStart)
|
2010-05-11 21:41:33 +02:00
|
|
|
{
|
2010-05-29 07:52:06 +02:00
|
|
|
const Token *tok4 = namespaceStart;
|
|
|
|
|
|
|
|
while (tok4 != namespaceEnd)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (functionPtr)
|
2010-07-02 15:22:29 +02:00
|
|
|
{
|
2010-01-27 19:03:24 +01:00
|
|
|
tok2->insertToken("*");
|
2010-07-02 15:22:29 +02:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2010-05-10 07:12:06 +02:00
|
|
|
else if (functionRef)
|
2010-07-02 15:22:29 +02:00
|
|
|
{
|
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-05-27 07:00:33 +02:00
|
|
|
if (const1)
|
|
|
|
{
|
|
|
|
tok2->insertToken(const1->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
if (const2)
|
|
|
|
{
|
|
|
|
tok2->insertToken(const2->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-05-18 07:11:23 +02:00
|
|
|
if (needParen)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2010-05-23 10:46:39 +02:00
|
|
|
if (specStart)
|
|
|
|
{
|
|
|
|
Token *spec = specStart;
|
|
|
|
tok2->insertToken(spec->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
while (spec != specEnd)
|
|
|
|
{
|
|
|
|
spec = spec->next();
|
|
|
|
tok2->insertToken(spec->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
2010-01-20 21:16:40 +01:00
|
|
|
}
|
2010-05-25 20:43:44 +02:00
|
|
|
else if (functionRetFuncPtr || functionPtrRetFuncPtr)
|
|
|
|
{
|
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token *tok3 = tok2;
|
|
|
|
tok2->insertToken("*");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
Token * tok4 = 0;
|
|
|
|
if (functionPtrRetFuncPtr)
|
|
|
|
{
|
|
|
|
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
|
2010-08-31 17:48:19 +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
|
|
|
|
|
|
|
if (tok4 && functionPtrRetFuncPtr)
|
|
|
|
{
|
|
|
|
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);
|
2010-05-25 20:43:44 +02:00
|
|
|
}
|
2010-05-28 06:55:54 +02:00
|
|
|
else if (ptrToArray || refToArray)
|
|
|
|
{
|
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token *tok3 = tok2;
|
|
|
|
|
|
|
|
if (ptrToArray)
|
|
|
|
tok2->insertToken("*");
|
|
|
|
else
|
|
|
|
tok2->insertToken("&");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// skip over name
|
2010-06-10 07:18:55 +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();
|
2010-06-10 07:18:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// syntax error
|
|
|
|
}
|
2010-05-28 06:55:54 +02:00
|
|
|
|
|
|
|
tok2->insertToken(")");
|
|
|
|
Token::createMutualLinks(tok2->next(), tok3);
|
|
|
|
}
|
|
|
|
else if (ptrMember)
|
|
|
|
{
|
2010-12-29 20:22:06 +01:00
|
|
|
if (Token::simpleMatch(tok2, "* ("))
|
2010-05-29 07:52:06 +02:00
|
|
|
{
|
2010-12-29 20:22:06 +01:00
|
|
|
tok2->insertToken("*");
|
2010-05-29 07:52:06 +02:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2010-12-29 20:22:06 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2010-12-29 20:22:06 +01:00
|
|
|
while (tok4 != namespaceEnd)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2010-05-28 06:55:54 +02:00
|
|
|
}
|
2010-11-11 06:40:49 +01:00
|
|
|
else if (typeOf)
|
|
|
|
{
|
2011-02-10 04:02:17 +01:00
|
|
|
tok2 = copyTokens(tok2, argStart, argEnd);
|
2010-11-11 06:40:49 +01:00
|
|
|
}
|
2010-08-20 07:11:02 +02:00
|
|
|
else if (tok2->tokAt(2) && tok2->tokAt(2)->str() == "[")
|
|
|
|
{
|
|
|
|
while (tok2->tokAt(2) && tok2->tokAt(2)->str() == "[")
|
|
|
|
tok2 = tok2->tokAt(2)->link()->previous();
|
|
|
|
}
|
2010-01-20 21:16:40 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (arrayStart && arrayEnd)
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-10-13 20:02:37 +02:00
|
|
|
do
|
2010-01-30 19:41:22 +01:00
|
|
|
{
|
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?
|
|
|
|
if (tok2->str() == "&")
|
|
|
|
{
|
|
|
|
tok2 = tok2->previous();
|
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->tokAt(3);
|
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok2->tokAt(-3));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if (tok2->str() == "=")
|
|
|
|
{
|
|
|
|
if (tok2->next()->str() == "{")
|
|
|
|
tok2 = tok2->next()->link()->next();
|
|
|
|
else if (tok2->next()->str().at(0) == '\"')
|
|
|
|
tok2 = tok2->next()->next();
|
|
|
|
}
|
2010-01-30 19:41:22 +01:00
|
|
|
}
|
2010-11-06 13:28:44 +01: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;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == ",")
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
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), "*|&"))
|
2010-01-18 19:06:50 +01:00
|
|
|
pointers.push_back(tok->tokAt(offset++)->str());
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2010-06-20 20:51:36 +02:00
|
|
|
if (Token::Match(tok->tokAt(offset), "%type%"))
|
2010-01-18 19:06:50 +01:00
|
|
|
{
|
2010-02-20 09:07:29 +01:00
|
|
|
typeName = tok->tokAt(offset++);
|
2010-01-11 17:09:04 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(offset) && tok->tokAt(offset)->str() == "[")
|
2010-01-18 19:06:50 +01:00
|
|
|
{
|
2010-01-31 07:16:19 +01:00
|
|
|
arrayStart = tok->tokAt(offset);
|
|
|
|
|
2010-01-30 19:41:22 +01:00
|
|
|
bool atEnd = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (!atEnd)
|
2010-01-30 19:41:22 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), ";|,"))
|
2010-01-30 19:41:22 +01:00
|
|
|
offset++;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->tokAt(offset + 1))
|
2010-01-30 19:41:22 +01:00
|
|
|
return; // invalid input
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->tokAt(offset + 1)->str() == ";")
|
2010-01-30 19:41:22 +01:00
|
|
|
atEnd = true;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->tokAt(offset)->str() == "]")
|
2010-01-30 19:41:22 +01:00
|
|
|
atEnd = true;
|
|
|
|
else
|
|
|
|
offset++;
|
|
|
|
}
|
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);
|
2010-01-17 08:16:17 +01: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
|
|
|
}
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// we encountered a typedef we don't support yet so just continue
|
|
|
|
done = true;
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ok)
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-01-16 09:18:21 +01:00
|
|
|
// remove typedef but leave ;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (typeDef->next() && typeDef->next() != tok)
|
2010-01-16 09:18:21 +01:00
|
|
|
typeDef->deleteNext();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (typeDef != _tokens)
|
2010-01-16 09:18:21 +01:00
|
|
|
{
|
|
|
|
tok = typeDef->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_tokens->deleteThis();
|
|
|
|
tok = _tokens;
|
|
|
|
}
|
2009-09-30 13:35:00 +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);
|
|
|
|
|
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);
|
|
|
|
|
2010-10-19 21:54:15 +02:00
|
|
|
// Convert C# code
|
|
|
|
if (_files[0].find(".cs"))
|
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "[;{}] %type% [ ] %var% [=;]"))
|
|
|
|
{
|
|
|
|
tok = tok->next()->next();
|
|
|
|
tok->str("*");
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-27 21:16:25 +01:00
|
|
|
// if MACRO
|
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "if|for|while %var% ("))
|
|
|
|
{
|
|
|
|
syntaxError(tok);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-28 18:51:55 +02:00
|
|
|
// Simplify JAVA/C# code
|
|
|
|
if (isJavaOrCSharp() && _files[0].find(".java") != std::string::npos)
|
2010-10-20 22:15:35 +02:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, ") throws %var% {"))
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
|
|
|
else if (tok->str() == "private")
|
|
|
|
tok->str("private:");
|
|
|
|
else if (tok->str() == "protected")
|
|
|
|
tok->str("protected:");
|
|
|
|
else if (tok->str() == "public")
|
|
|
|
tok->str("public:");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 20:16:14 +01:00
|
|
|
// Replace NULL with 0..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2011-02-05 09:03:31 +01:00
|
|
|
if (tok->str() == "NULL" || tok->str() == "__null" ||
|
|
|
|
tok->str() == "'\\0'" || tok->str() == "'\\x0'")
|
2011-01-06 20:16:14 +01:00
|
|
|
{
|
|
|
|
tok->str("0");
|
|
|
|
}
|
|
|
|
else if (tok->isNumber() &&
|
|
|
|
MathLib::isInt(tok->str()) &&
|
|
|
|
MathLib::toLongNumber(tok->str()) == 0)
|
|
|
|
{
|
|
|
|
tok->str("0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-30 08:34:58 +01:00
|
|
|
// replace inline SQL with "asm()" (Oracle PRO*C). Ticket: #1959
|
2010-08-31 21:40:51 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok, "EXEC SQL"))
|
|
|
|
{
|
|
|
|
// delete all tokens until ";"
|
2010-09-02 20:51:01 +02:00
|
|
|
const Token *end = tok;
|
|
|
|
while (end && end->str() != ";")
|
|
|
|
end = end->next();
|
|
|
|
Token::eraseTokens(tok, end);
|
2010-08-31 21:40:51 +02:00
|
|
|
|
|
|
|
if (tok)
|
|
|
|
{
|
2010-09-02 20:51:01 +02:00
|
|
|
// insert "asm ( ) ;"
|
|
|
|
tok->str("asm");
|
2010-08-31 21:40:51 +02:00
|
|
|
tok->insertToken("(");
|
|
|
|
tok = tok->next();
|
|
|
|
tok->insertToken(")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!createLinks())
|
2009-05-07 22:17:29 +02:00
|
|
|
{
|
|
|
|
// Source has syntax errors, can't proceed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-22 19:57:48 +02:00
|
|
|
// check for simple syntax errors..
|
2010-08-25 21:57:57 +02:00
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next())
|
2010-07-22 19:57:48 +02:00
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok, "> struct {") &&
|
|
|
|
Token::simpleMatch(tok->tokAt(2)->link(), "} ;"))
|
|
|
|
{
|
|
|
|
syntaxError(tok);
|
|
|
|
deallocateTokens();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-30 08:34:58 +01:00
|
|
|
// remove some unhandled macros in global scope
|
|
|
|
removeMacrosInGlobalScope();
|
|
|
|
|
2010-02-21 09:47:41 +01:00
|
|
|
// specify array size..
|
2010-02-20 18:13:09 +01:00
|
|
|
arraySize();
|
|
|
|
|
2010-02-21 09:47:41 +01:00
|
|
|
// simplify labels..
|
|
|
|
labels();
|
|
|
|
|
2009-09-30 20:42:14 +02:00
|
|
|
simplifyDoWhileAddBraces();
|
|
|
|
simplifyIfAddBraces();
|
|
|
|
|
2009-03-17 20:50:06 +01:00
|
|
|
// Combine "- %num%" ..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-03-17 20:50:06 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "[([+-*/=,] - %num%") && tok->strAt(2)[0] != '-')
|
2009-03-17 20:50:06 +01:00
|
|
|
{
|
2009-08-02 10:54:46 +02:00
|
|
|
tok->next()->str(std::string("-") + tok->strAt(2));
|
2009-03-17 20:50:06 +01:00
|
|
|
tok->next()->deleteNext();
|
|
|
|
}
|
2009-03-18 19:48:06 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "return|case - %num%") && tok->strAt(2)[0] != '-')
|
2009-03-18 19:48:06 +01:00
|
|
|
{
|
2009-08-02 10:54:46 +02:00
|
|
|
tok->next()->str(std::string("-") + tok->strAt(2));
|
2009-03-18 19:48:06 +01:00
|
|
|
tok->next()->deleteNext();
|
|
|
|
}
|
2009-03-17 20:50:06 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Combine tokens..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok && tok->next(); tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-08-11 17:29:33 +02:00
|
|
|
const char c1 = tok->str()[0];
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-08-11 17:29:33 +02:00
|
|
|
if (tok->str().length() == 1 && tok->next()->str().length() == 1)
|
|
|
|
{
|
|
|
|
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..
|
|
|
|
if (c1 == c2 && (c1 == '<' || c1 == '|' || c1 == ':'))
|
|
|
|
{
|
|
|
|
tok->str(tok->str() + c2);
|
|
|
|
tok->deleteNext();
|
2011-03-06 09:33:46 +01: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 =
|
2010-10-31 08:47:13 +01: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 "."
|
|
|
|
else if (c1 == '-' && c2 == '>')
|
|
|
|
{
|
|
|
|
tok->str(".");
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-10-31 08:47:13 +01:00
|
|
|
else if (tok->str() == ">>" && tok->next()->str() == "=")
|
|
|
|
{
|
|
|
|
tok->str(">>=");
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
|
2010-08-31 19:48:04 +02:00
|
|
|
else if ((c1 == 'p' || c1 == '_') && tok->next()->str() == ":")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-08-11 17:29:33 +02:00
|
|
|
if (tok->str() == "private" || tok->str() == "protected" || tok->str() == "public" || tok->str() == "__published")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-31 08:47:13 +01:00
|
|
|
// ";a+=b;" => ";a=a+b;"
|
|
|
|
simplifyCompoundAssignment();
|
|
|
|
|
2010-08-25 21:57:57 +02:00
|
|
|
// check for more complicated syntax errors when using templates..
|
2010-09-02 23:01:12 +02:00
|
|
|
if (!preprocessorCondition)
|
2010-08-25 21:57:57 +02:00
|
|
|
{
|
2010-09-02 23:01:12 +02:00
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next())
|
2010-08-25 21:57:57 +02:00
|
|
|
{
|
2010-09-02 23:01:12 +02:00
|
|
|
// skip executing scopes..
|
2010-10-26 19:39:48 +02:00
|
|
|
if (Token::Match(tok, ") const| {") || Token::simpleMatch(tok, ", {"))
|
2010-09-02 23:01:12 +02:00
|
|
|
{
|
|
|
|
while (tok->str() != "{")
|
|
|
|
tok = tok->next();
|
|
|
|
tok = tok->link();
|
2010-11-28 07:35:42 +01:00
|
|
|
if (!tok)
|
|
|
|
break;
|
2010-09-02 23:01:12 +02:00
|
|
|
}
|
2010-08-25 21:57:57 +02:00
|
|
|
|
2010-09-02 23:01:12 +02:00
|
|
|
// skip executing scopes (ticket #1984)..
|
|
|
|
if (Token::simpleMatch(tok, "; {"))
|
|
|
|
{
|
|
|
|
tok = tok->next()->link();
|
2010-11-28 07:35:42 +01:00
|
|
|
if (!tok)
|
|
|
|
break;
|
2010-09-02 23:01:12 +02:00
|
|
|
}
|
2010-08-26 23:24:01 +02:00
|
|
|
|
2010-09-02 23:01:12 +02:00
|
|
|
// skip executing scopes (ticket #1985)..
|
|
|
|
if (Token::simpleMatch(tok, "try {"))
|
2010-08-26 23:19:18 +02:00
|
|
|
{
|
2010-09-02 23:01:12 +02:00
|
|
|
tok = tok->next()->link();
|
|
|
|
while (Token::simpleMatch(tok, "} catch ("))
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(2)->link();
|
|
|
|
if (Token::simpleMatch(tok, ") {"))
|
|
|
|
tok = tok->next()->link();
|
|
|
|
}
|
2010-11-28 07:35:42 +01:00
|
|
|
if (!tok)
|
|
|
|
break;
|
2010-08-26 23:19:18 +02:00
|
|
|
}
|
|
|
|
|
2010-09-02 23:01:12 +02:00
|
|
|
// not start of statement?
|
|
|
|
if (tok->previous() && !Token::Match(tok, "[;{}]"))
|
|
|
|
continue;
|
2010-08-25 21:57:57 +02:00
|
|
|
|
2010-09-02 23:01:12 +02:00
|
|
|
// 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;
|
2010-08-25 21:57:57 +02:00
|
|
|
|
2010-09-02 23:01:12 +02:00
|
|
|
// template variable or type..
|
|
|
|
if (Token::Match(tok, "%type% <"))
|
2010-08-25 21:57:57 +02:00
|
|
|
{
|
2010-09-02 23:01:12 +02:00
|
|
|
// 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())
|
2010-08-25 21:57:57 +02:00
|
|
|
{
|
2010-09-02 23:01:12 +02:00
|
|
|
if (tok2->str() == "(")
|
|
|
|
tok2 = tok2->link();
|
|
|
|
else if (tok2->str() == "<")
|
2010-08-25 21:57:57 +02:00
|
|
|
{
|
2010-09-02 23:01:12 +02:00
|
|
|
bool inclevel = false;
|
|
|
|
if (Token::simpleMatch(tok2->previous(), "operator <"))
|
|
|
|
;
|
|
|
|
else if (level == 0)
|
2010-08-25 21:57:57 +02:00
|
|
|
inclevel = true;
|
2010-09-02 23:01:12 +02:00
|
|
|
else if (tok2->next()->isStandardType())
|
|
|
|
inclevel = true;
|
|
|
|
else if (Token::simpleMatch(tok2, "< typename"))
|
|
|
|
inclevel = true;
|
|
|
|
else if (Token::Match(tok2->tokAt(-2), "<|, %type% <") && usedtypes.find(tok2->strAt(-1)) != usedtypes.end())
|
|
|
|
inclevel = true;
|
|
|
|
else if (Token::Match(tok2, "< %type%") && usedtypes.find(tok2->strAt(1)) != 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;
|
|
|
|
}
|
2010-08-25 21:57:57 +02:00
|
|
|
|
2010-09-02 23:01:12 +02:00
|
|
|
if (inclevel)
|
|
|
|
{
|
|
|
|
++level;
|
|
|
|
if (Token::Match(tok2->tokAt(-2), "<|, %type% <"))
|
|
|
|
usedtypes.insert(tok2->strAt(-1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tok2->str() == ">")
|
2010-08-25 21:57:57 +02:00
|
|
|
{
|
2010-09-02 23:01:12 +02:00
|
|
|
if (level > 0)
|
|
|
|
--level;
|
2010-08-25 21:57:57 +02:00
|
|
|
}
|
2010-09-05 08:16:19 +02:00
|
|
|
else if (tok2->str() == ">>")
|
|
|
|
{
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
}
|
2010-08-25 21:57:57 +02:00
|
|
|
}
|
2010-09-02 23:01:12 +02:00
|
|
|
if (level > 0)
|
2010-08-25 21:57:57 +02:00
|
|
|
{
|
2010-09-02 23:01:12 +02:00
|
|
|
syntaxError(tok);
|
|
|
|
deallocateTokens();
|
|
|
|
return false;
|
2010-08-25 21:57:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-17 19:50:21 +02:00
|
|
|
// 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;
|
|
|
|
for (Token * tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
else if (indentlevel == 1 && Token::Match(tok2, ") = delete|default ;"))
|
|
|
|
{
|
2010-08-18 22:22:14 +02:00
|
|
|
Token * const end = tok2->tokAt(4);
|
2010-08-17 19:50:21 +02:00
|
|
|
tok2 = tok2->link()->previous();
|
2010-08-18 22:22:14 +02:00
|
|
|
|
|
|
|
// operator ==|>|<|..
|
2010-11-20 17:20:25 +01:00
|
|
|
if (Token::Match(tok2->previous(), "operator %any%"))
|
2010-08-18 22:22:14 +02:00
|
|
|
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);
|
|
|
|
|
2010-08-17 19:50:21 +02:00
|
|
|
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);
|
2010-08-18 22:22:14 +02:00
|
|
|
tok2 = end;
|
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
|
|
|
|
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();
|
|
|
|
|
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-01-16 11:54:28 +01:00
|
|
|
// #2449: syntax error: enum with typedef in it
|
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "enum %var% {"))
|
|
|
|
{
|
|
|
|
for (const Token *tok2 = tok->tokAt(3); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "typedef")
|
|
|
|
{
|
|
|
|
syntaxError(tok2);
|
|
|
|
deallocateTokens();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "(")
|
|
|
|
tok = tok->link();
|
|
|
|
else if (tok->str()[0] == '@')
|
|
|
|
{
|
|
|
|
deallocateTokens();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-07-14 15:30:23 +02:00
|
|
|
/**
|
|
|
|
* @todo simplify "for"
|
|
|
|
* - move out start-statement "for (a;b;c);" => "{ a; for(;b;c); }"
|
|
|
|
* - try to change "for" loop to a "while" loop instead
|
|
|
|
*/
|
|
|
|
|
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.
|
2009-03-18 20:32:05 +01:00
|
|
|
simplifyVarDecl();
|
|
|
|
|
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
|
2010-01-30 09:33:16 +01:00
|
|
|
simplifyRedundantParanthesis();
|
|
|
|
|
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.
|
|
|
|
simplifyTemplates2();
|
|
|
|
|
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();
|
|
|
|
|
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.
|
|
|
|
simplifyVarDecl();
|
2011-01-06 20:16:14 +01:00
|
|
|
|
2010-09-02 23:01:12 +02:00
|
|
|
if (!preprocessorCondition)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2010-08-03 16:36:21 +02:00
|
|
|
_tokens->assignProgressValues();
|
2009-09-19 10:54:10 +02:00
|
|
|
|
2010-10-27 10:34:06 +02:00
|
|
|
removeRedundantSemicolons();
|
2010-10-26 21:05:20 +02:00
|
|
|
|
2010-08-03 16:36:21 +02:00
|
|
|
return validate();
|
2009-05-03 21:23:47 +02: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()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-02-20 18:13:09 +01:00
|
|
|
{
|
2010-10-12 21:52:02 +02:00
|
|
|
if (Token::Match(tok, "%var% [ ] = { %str% }"))
|
|
|
|
{
|
|
|
|
tok->tokAt(4)->deleteThis();
|
|
|
|
tok->tokAt(5)->deleteThis();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "%var% [ ] = {"))
|
2010-02-20 18:13:09 +01:00
|
|
|
{
|
|
|
|
unsigned int sz = 1;
|
|
|
|
const Token *tok2 = tok->tokAt(5);
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok2, "%any% ,"))
|
2010-02-20 18:13:09 +01:00
|
|
|
{
|
2011-02-01 21:46:07 +01:00
|
|
|
if (tok2->isName())
|
|
|
|
break;
|
2010-02-20 18:13:09 +01:00
|
|
|
sz++;
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
|
|
|
|
2011-02-01 21:46:07 +01:00
|
|
|
if (!tok2->isName() && Token::Match(tok2, "%any% } ;"))
|
2010-08-06 22:37:48 +02:00
|
|
|
tok->next()->insertToken(MathLib::toString<unsigned int>(sz));
|
2010-02-20 18:13:09 +01:00
|
|
|
}
|
2010-04-24 21:48:58 +02:00
|
|
|
|
|
|
|
else if (Token::Match(tok, "%var% [ ] = %str% ;"))
|
|
|
|
{
|
2010-12-31 09:38:03 +01:00
|
|
|
std::size_t sz = tok->strAt(4).length() - 1;
|
2010-12-31 09:51:27 +01:00
|
|
|
tok->next()->insertToken(MathLib::toString<unsigned int>((unsigned int)sz));
|
2010-04-24 21:48:58 +02:00
|
|
|
}
|
2010-02-20 18:13:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-21 09:47:41 +01:00
|
|
|
/** simplify labels in the code.. add an ";" */
|
|
|
|
|
|
|
|
void Tokenizer::labels()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-02-21 09:47:41 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, ") const| {"))
|
2010-02-21 09:47:41 +01:00
|
|
|
{
|
|
|
|
// Simplify labels in the executable scope..
|
|
|
|
unsigned int indentlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (0 != (tok = tok->next()))
|
2010-02-21 09:47:41 +01:00
|
|
|
{
|
|
|
|
// indentations..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{")
|
2010-02-21 09:47:41 +01:00
|
|
|
++indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "}")
|
2010-02-21 09:47:41 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel <= 1)
|
2010-02-21 09:47:41 +01:00
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// simplify label..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "[;{}] %var% : %var%"))
|
2010-08-18 22:42:04 +02:00
|
|
|
{
|
|
|
|
if (!Token::Match(tok->next(), "public|protected|private"))
|
|
|
|
tok->tokAt(2)->insertToken(";");
|
|
|
|
}
|
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();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok)
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2011-02-20 12:17:05 +01:00
|
|
|
++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();
|
|
|
|
|
|
|
|
// ,/>
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ">")
|
2011-02-20 12:17:05 +01:00
|
|
|
return numberOfParameters;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != ",")
|
2009-11-01 15:56:00 +01:00
|
|
|
break;
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
2011-02-20 12:17:05 +01:00
|
|
|
return 0;
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
/**
|
|
|
|
* Remove "template < ..." they can cause false positives because they are not expanded
|
|
|
|
*/
|
|
|
|
static void removeTemplates(Token *tok)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (; tok; tok = tok->next())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! Token::simpleMatch(tok, "template <"))
|
2009-11-01 19:03:52 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "{")
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->link();
|
|
|
|
tok2 = tok2 ? tok2->next() : 0;
|
|
|
|
Token::eraseTokens(tok, tok2);
|
|
|
|
tok->str(";");
|
|
|
|
break;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(")
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok2)
|
2009-11-01 19:03:52 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == ";")
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
2009-11-01 20:49:43 +01:00
|
|
|
Token::eraseTokens(tok, tok2->next());
|
2009-11-01 19:03:52 +01:00
|
|
|
tok->str(";");
|
2009-11-01 20:49:43 +01:00
|
|
|
break;
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-12 19:43:33 +01:00
|
|
|
std::set<std::string> Tokenizer::simplifyTemplatesExpandSpecialized()
|
2009-05-03 21:23:47 +02:00
|
|
|
{
|
2009-11-01 15:56:00 +01:00
|
|
|
std::set<std::string> expandedtemplates;
|
|
|
|
|
2009-10-31 12:34:52 +01:00
|
|
|
// Locate specialized templates..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-10-31 12:34:52 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != "template")
|
2009-10-31 12:34:52 +01:00
|
|
|
continue;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::simpleMatch(tok->next(), "< >"))
|
2009-10-31 12:34:52 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// what kind of template is this?
|
|
|
|
Token *tok2 = tok->tokAt(3);
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2 && (tok2->isName() || tok2->str() == "*"))
|
2009-10-31 12:34:52 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!templateParameters(tok2))
|
2009-11-01 15:56:00 +01:00
|
|
|
continue;
|
|
|
|
|
2009-10-31 12:34:52 +01:00
|
|
|
// unknown template.. bail out
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok2->previous()->isName())
|
2009-10-31 12:34:52 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
tok2 = tok2->previous();
|
2009-11-01 19:03:52 +01:00
|
|
|
std::string s;
|
2009-10-31 12:34:52 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
std::ostringstream ostr;
|
|
|
|
const Token *tok3 = tok2;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (tok3 = tok2; tok3 && tok3->str() != ">"; tok3 = tok3->next())
|
2009-10-31 12:34:52 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3 != tok2)
|
2009-11-01 19:03:52 +01:00
|
|
|
ostr << " ";
|
|
|
|
ostr << tok3->str();
|
2009-10-31 12:34:52 +01:00
|
|
|
}
|
2011-02-02 17:12:46 +01:00
|
|
|
if (!Token::simpleMatch(tok3, "> ("))
|
2009-10-31 12:34:52 +01:00
|
|
|
continue;
|
2009-11-01 19:03:52 +01:00
|
|
|
s = ostr.str();
|
2009-10-31 12:34:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// save search pattern..
|
|
|
|
const std::string pattern(s + " > (");
|
|
|
|
|
|
|
|
// remove spaces to create new name
|
2010-04-02 07:30:58 +02:00
|
|
|
while (s.find(" ") != std::string::npos)
|
2009-10-31 12:34:52 +01:00
|
|
|
s.erase(s.find(" "), 1);
|
|
|
|
const std::string name(s + ">");
|
2009-11-01 15:56:00 +01:00
|
|
|
expandedtemplates.insert(name);
|
2009-10-31 12:34:52 +01:00
|
|
|
|
|
|
|
// Rename template..
|
|
|
|
Token::eraseTokens(tok2, Token::findmatch(tok2, "("));
|
|
|
|
tok2->str(name);
|
|
|
|
|
|
|
|
// delete the "template < >"
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
// Use this special template in the code..
|
2010-04-02 07:30:58 +02:00
|
|
|
while (0 != (tok2 = const_cast<Token *>(Token::findmatch(tok2, pattern.c_str()))))
|
2009-10-31 12:34:52 +01:00
|
|
|
{
|
|
|
|
Token::eraseTokens(tok2, Token::findmatch(tok2, "("));
|
|
|
|
tok2->str(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-12 19:43:33 +01:00
|
|
|
return expandedtemplates;
|
|
|
|
}
|
|
|
|
|
2011-02-12 20:12:07 +01:00
|
|
|
std::list<Token *> Tokenizer::simplifyTemplatesGetTemplateDeclarations()
|
2011-02-12 19:43:33 +01:00
|
|
|
{
|
2009-05-05 20:16:57 +02:00
|
|
|
std::list<Token *> templates;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-03-11 18:50:24 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "template <"))
|
2009-05-07 16:05:07 +02:00
|
|
|
{
|
2011-01-01 11:40:32 +01:00
|
|
|
// set member variable, the code has templates.
|
|
|
|
// this info is used by checks
|
2010-09-30 21:22:49 +02:00
|
|
|
_codeWithTemplates = true;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-05-07 16:05:07 +02:00
|
|
|
{
|
|
|
|
// Just a declaration => ignore this
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == ";")
|
2009-05-07 16:05:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Implementation => add to "templates"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "{")
|
2009-05-07 16:05:07 +02:00
|
|
|
{
|
|
|
|
templates.push_back(tok);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-05 20:16:57 +02:00
|
|
|
}
|
2011-02-12 20:12:07 +01:00
|
|
|
return templates;
|
|
|
|
}
|
|
|
|
|
2011-02-12 20:17:58 +01:00
|
|
|
std::list<Token *> Tokenizer::simplifyTemplatesGetTemplateInstantiations()
|
2011-02-12 20:12:07 +01:00
|
|
|
{
|
2009-05-05 20:16:57 +02:00
|
|
|
std::list<Token *> used;
|
2011-02-12 20:17:58 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-05-05 20:16:57 +02:00
|
|
|
{
|
2009-05-06 21:03:11 +02:00
|
|
|
// template definition.. skip it
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "template <"))
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
2011-01-01 11:40:32 +01:00
|
|
|
// Goto the end of the template definition
|
2010-04-02 07:30:58 +02:00
|
|
|
for (; tok; tok = tok->next())
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
2011-01-01 11:40:32 +01:00
|
|
|
// skip inner '(' .. ')' and '{' .. '}'
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{" || tok->str() == "(")
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
2011-01-01 11:40:32 +01:00
|
|
|
// skip inner tokens. goto ')' or '}'
|
2009-11-01 19:03:52 +01:00
|
|
|
tok = tok->link();
|
2011-01-01 11:40:32 +01:00
|
|
|
|
|
|
|
// this should be impossible. but break out anyway
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-11-01 19:03:52 +01:00
|
|
|
break;
|
2011-01-01 11:40:32 +01:00
|
|
|
|
|
|
|
// the end '}' for the template definition => break
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "}")
|
2009-05-06 21:03:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-01-01 11:40:32 +01:00
|
|
|
|
|
|
|
// the end ';' for the template definition
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == ";")
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-05-06 21:03:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-03-06 19:28:51 +01:00
|
|
|
else if (Token::Match(tok->previous(), "[({};=] %type% <") ||
|
2010-04-02 07:30:58 +02:00
|
|
|
Token::Match(tok->tokAt(-2), "[,:] private|protected|public %var% <"))
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (templateParameters(tok->next()))
|
2009-11-01 15:56:00 +01:00
|
|
|
used.push_back(tok);
|
2009-05-06 21:03:11 +02:00
|
|
|
}
|
2009-05-05 20:16:57 +02:00
|
|
|
}
|
2011-01-01 11:40:32 +01:00
|
|
|
|
2011-02-12 20:17:58 +01:00
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-12 20:27:44 +01:00
|
|
|
void Tokenizer::simplifyTemplatesUseDefaultArgumentValues(const std::list<Token *> &templates,
|
|
|
|
const std::list<Token *> &instantiations)
|
|
|
|
{
|
|
|
|
for (std::list<Token *>::const_iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2011-01-01 11:40:32 +01:00
|
|
|
// template parameters with default value has syntax such as:
|
|
|
|
// x = y
|
|
|
|
// this list will contain all the '=' tokens for such arguments
|
2009-09-06 08:22:45 +02:00
|
|
|
std::list<Token *> eq;
|
2011-01-01 11:40:32 +01:00
|
|
|
|
|
|
|
// parameter number. 1,2,3,..
|
2010-12-31 09:38:03 +01:00
|
|
|
std::size_t templatepar = 1;
|
2011-01-01 11:40:32 +01:00
|
|
|
|
|
|
|
// the template classname. This will be empty for template functions
|
2009-09-06 08:22:45 +02:00
|
|
|
std::string classname;
|
2011-01-01 11:40:32 +01:00
|
|
|
|
|
|
|
// Scan template declaration..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = *iter1; tok; tok = tok->next())
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2011-01-01 11:40:32 +01:00
|
|
|
// end of template parameters?
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ">")
|
2009-09-06 08:22:45 +02:00
|
|
|
{
|
2010-06-16 07:13:52 +02:00
|
|
|
if (Token::Match(tok, "> class|struct %var%"))
|
2009-09-06 08:22:45 +02:00
|
|
|
classname = tok->strAt(2);
|
2009-09-03 21:46:07 +02:00
|
|
|
break;
|
2009-09-06 08:22:45 +02:00
|
|
|
}
|
2009-09-03 21:46:07 +02:00
|
|
|
|
2011-01-01 11:40:32 +01:00
|
|
|
// next template parameter
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ",")
|
2009-09-06 08:22:45 +02:00
|
|
|
++templatepar;
|
|
|
|
|
2011-01-01 11:40:32 +01:00
|
|
|
// default parameter value
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "=")
|
2009-09-06 08:22:45 +02:00
|
|
|
eq.push_back(tok);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (eq.empty() || classname.empty())
|
2009-09-06 08:22:45 +02:00
|
|
|
continue;
|
|
|
|
|
2011-01-01 11:40:32 +01:00
|
|
|
// iterate through all template instantiations
|
2011-02-12 20:27:44 +01:00
|
|
|
for (std::list<Token *>::const_iterator iter2 = instantiations.begin(); iter2 != instantiations.end(); ++iter2)
|
2009-09-06 08:22:45 +02:00
|
|
|
{
|
|
|
|
Token *tok = *iter2;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(tok, (classname + " < %any%").c_str()))
|
2009-09-06 08:22:45 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// count the parameters..
|
|
|
|
unsigned int usedpar = 1;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (tok = tok->tokAt(3); tok; tok = tok->tokAt(2))
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ">")
|
2009-09-06 08:22:45 +02:00
|
|
|
break;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ",")
|
2009-09-06 08:22:45 +02:00
|
|
|
++usedpar;
|
|
|
|
|
2009-09-03 21:46:07 +02:00
|
|
|
else
|
2009-09-06 08:22:45 +02:00
|
|
|
break;
|
2009-09-03 21:46:07 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok && tok->str() == ">")
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
tok = tok->previous();
|
|
|
|
std::list<Token *>::const_iterator it = eq.begin();
|
2010-12-31 09:38:03 +01:00
|
|
|
for (std::size_t i = (templatepar - eq.size()); it != eq.end() && i < usedpar; ++i)
|
2009-09-06 08:22:45 +02:00
|
|
|
++it;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (it != eq.end())
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
tok->insertToken(",");
|
|
|
|
tok = tok->next();
|
2011-02-12 16:51:59 +01:00
|
|
|
const Token *from = (*it)->next();
|
|
|
|
std::stack<Token *> links;
|
|
|
|
while (from && (!links.empty() || (from->str() != "," && from->str() != ">")))
|
|
|
|
{
|
|
|
|
tok->insertToken(from->str());
|
|
|
|
tok = tok->next();
|
|
|
|
if (Token::Match(tok, "(|["))
|
|
|
|
links.push(tok);
|
|
|
|
else if (!links.empty() && Token::Match(tok, ")|]"))
|
|
|
|
{
|
|
|
|
Token::createMutualLinks(links.top(), tok);
|
|
|
|
links.pop();
|
|
|
|
}
|
|
|
|
from = from->next();
|
|
|
|
}
|
2009-09-06 08:22:45 +02:00
|
|
|
++it;
|
2009-09-03 21:46:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<Token *>::iterator it = eq.begin(); it != eq.end(); ++it)
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
(*it)->deleteThis();
|
|
|
|
(*it)->deleteThis();
|
2009-09-03 21:46:07 +02:00
|
|
|
}
|
|
|
|
}
|
2011-02-12 20:27:44 +01:00
|
|
|
}
|
|
|
|
|
2011-02-20 12:17:05 +01:00
|
|
|
/**
|
|
|
|
* Match template declaration/instantiation
|
|
|
|
* @param instance template instantiation
|
|
|
|
* @param name name of template
|
|
|
|
* @param numberOfArguments number of template arguments
|
|
|
|
* @param patternAfter pattern that must match the tokens after the ">"
|
|
|
|
* @return match => true
|
|
|
|
*/
|
|
|
|
static bool simplifyTemplatesInstantiateMatch(const Token *instance, const std::string &name, unsigned int numberOfArguments, const char patternAfter[])
|
|
|
|
{
|
|
|
|
if (!Token::simpleMatch(instance, (name + " <").c_str()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (numberOfArguments != templateParameters(instance->next()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (patternAfter)
|
|
|
|
{
|
|
|
|
const Token *tok = Token::findmatch(instance, ">");
|
|
|
|
if (!tok || !Token::Match(tok->next(), patternAfter))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing mismatching was found..
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-12 21:11:20 +01:00
|
|
|
void Tokenizer::simplifyTemplatesInstantiate(const Token *tok,
|
|
|
|
std::list<Token *> &used,
|
2011-02-12 20:58:45 +01:00
|
|
|
std::set<std::string> &expandedtemplates)
|
2011-02-12 20:27:44 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
// this variable is not used at the moment. the intention was to
|
|
|
|
// allow continous instantiations until all templates has been expanded
|
|
|
|
bool done = false;
|
2011-02-12 20:27:44 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
std::vector<const Token *> type;
|
|
|
|
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next())
|
2011-02-12 20:27:44 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
if (Token::Match(tok, "%var% ,|>"))
|
|
|
|
type.push_back(tok);
|
2011-02-12 20:27:44 +01:00
|
|
|
}
|
2011-02-12 20:58:45 +01:00
|
|
|
// bail out if the end of the file was reached
|
|
|
|
if (!tok)
|
|
|
|
return;
|
2011-02-12 20:27:44 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
// get the position of the template name
|
|
|
|
unsigned char namepos = 0;
|
|
|
|
if (Token::Match(tok, "> class|struct %type% {|:"))
|
|
|
|
namepos = 2;
|
|
|
|
else if (Token::Match(tok, "> %type% *|&| %type% ("))
|
|
|
|
namepos = 2;
|
|
|
|
else if (Token::Match(tok, "> %type% %type% *|&| %type% ("))
|
|
|
|
namepos = 3;
|
|
|
|
else
|
2011-02-12 20:27:44 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
// debug message that we bail out..
|
|
|
|
if (_settings->debugwarnings)
|
2011-02-12 20:27:44 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok->linenr();
|
|
|
|
loc.setfile(file(tok));
|
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
Severity::debug,
|
|
|
|
"simplifyTemplates: bailing out",
|
|
|
|
"debug");
|
|
|
|
|
|
|
|
if (_errorLogger)
|
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
2011-02-12 20:27:44 +01:00
|
|
|
}
|
2011-02-12 20:58:45 +01:00
|
|
|
return;
|
2011-02-12 20:27:44 +01:00
|
|
|
}
|
2011-02-12 20:58:45 +01:00
|
|
|
if ((tok->tokAt(namepos)->str() == "*" || tok->tokAt(namepos)->str() == "&"))
|
|
|
|
++namepos;
|
2009-09-03 21:46:07 +02:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
// name of template function/class..
|
|
|
|
const std::string name(tok->strAt(namepos));
|
2011-02-12 20:27:44 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
const bool isfunc(tok->strAt(namepos + 1) == "(");
|
|
|
|
|
|
|
|
// locate template usage..
|
|
|
|
std::string::size_type sz1 = used.size();
|
|
|
|
unsigned int recursiveCount = 0;
|
2009-09-03 21:46:07 +02:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
for (std::list<Token *>::const_iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
|
2009-05-05 20:16:57 +02:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
// If the size of "used" has changed, simplify calculations
|
|
|
|
if (sz1 != used.size())
|
2009-03-14 21:26:32 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
sz1 = used.size();
|
|
|
|
simplifyCalculations();
|
|
|
|
recursiveCount++;
|
|
|
|
if (recursiveCount > 100)
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
// bail out..
|
2009-11-01 19:03:52 +01:00
|
|
|
break;
|
2010-03-02 22:55:05 +01:00
|
|
|
}
|
2011-02-12 20:58:45 +01:00
|
|
|
}
|
2010-03-02 22:55:05 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
Token * const tok2 = *iter2;
|
|
|
|
if (tok2->str() != name)
|
|
|
|
continue;
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
if (Token::Match(tok2->previous(), "[;{}=]") &&
|
2011-02-20 12:17:05 +01:00
|
|
|
!simplifyTemplatesInstantiateMatch(*iter2, name, type.size(), isfunc ? "(" : "*| %var%"))
|
2011-02-12 20:58:45 +01:00
|
|
|
continue;
|
2009-11-01 19:03:52 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
// New type..
|
2011-02-20 12:17:05 +01:00
|
|
|
std::vector<const Token *> types2;
|
|
|
|
std::string s;
|
2011-02-12 20:58:45 +01:00
|
|
|
std::string s1(name + " < ");
|
|
|
|
for (const Token *tok3 = tok2->tokAt(2); tok3 && tok3->str() != ">"; tok3 = tok3->next())
|
|
|
|
{
|
|
|
|
if (!tok3->next())
|
2009-03-14 21:26:32 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
s.clear();
|
|
|
|
break;
|
2009-03-14 21:26:32 +01:00
|
|
|
}
|
2011-02-12 20:58:45 +01:00
|
|
|
s1 += tok3->str();
|
|
|
|
s1 += " ";
|
2011-02-20 12:17:05 +01:00
|
|
|
if (Token::Match(tok3->previous(), "[<,]"))
|
|
|
|
types2.push_back(tok3);
|
2011-02-12 20:58:45 +01:00
|
|
|
// add additional type information
|
|
|
|
if (tok3->isUnsigned())
|
|
|
|
s += "unsigned";
|
|
|
|
else if (tok3->isSigned())
|
|
|
|
s += "signed";
|
|
|
|
if (tok3->isLong())
|
|
|
|
s += "long";
|
|
|
|
s += tok3->str();
|
|
|
|
}
|
|
|
|
s1 += ">";
|
|
|
|
const std::string type2(s);
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
if (type2.empty() || type.size() != types2.size())
|
|
|
|
{
|
|
|
|
if (_settings->debugwarnings)
|
|
|
|
{
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok2->linenr();
|
|
|
|
loc.setfile(file(tok2));
|
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
Severity::debug,
|
|
|
|
"Failed to instantiate template. The checking continues anyway.",
|
|
|
|
"debug");
|
|
|
|
|
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
}
|
|
|
|
if (type2.empty())
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// New classname/funcname..
|
|
|
|
const std::string name2(name + "<" + type2 + ">");
|
2009-03-11 18:50:24 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
if (expandedtemplates.find(name2) == expandedtemplates.end())
|
|
|
|
{
|
|
|
|
expandedtemplates.insert(name2);
|
|
|
|
// Copy template..
|
|
|
|
int _indentlevel = 0;
|
|
|
|
int _parlevel = 0;
|
|
|
|
for (const Token *tok3 = _tokens; tok3; tok3 = tok3->next())
|
2009-03-11 19:14:45 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
if (tok3->str() == "{")
|
|
|
|
++_indentlevel;
|
|
|
|
else if (tok3->str() == "}")
|
|
|
|
--_indentlevel;
|
|
|
|
else if (tok3->str() == "(")
|
|
|
|
++_parlevel;
|
|
|
|
else if (tok3->str() == ")")
|
|
|
|
--_parlevel;
|
|
|
|
|
|
|
|
// Start of template..
|
|
|
|
if (tok3 == tok)
|
2009-03-11 18:50:24 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
tok3 = tok3->next();
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
2009-05-09 08:02:59 +02:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
// member function implemented outside class definition
|
2011-02-20 12:22:01 +01:00
|
|
|
else if (_indentlevel == 0 &&
|
|
|
|
_parlevel == 0 &&
|
2011-02-20 12:17:05 +01:00
|
|
|
simplifyTemplatesInstantiateMatch(tok3, name, type.size(), ":: ~| %var% ("))
|
2011-02-12 20:58:45 +01:00
|
|
|
{
|
|
|
|
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
while (tok3->str() != "::")
|
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
2009-03-17 18:55:28 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
// not part of template.. go on to next token
|
|
|
|
else
|
2009-11-01 19:03:52 +01:00
|
|
|
continue;
|
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
int indentlevel = 0;
|
|
|
|
std::stack<Token *> braces; // holds "{" tokens
|
|
|
|
std::stack<Token *> brackets; // holds "(" tokens
|
|
|
|
std::stack<Token *> brackets2; // holds "[" tokens
|
2009-11-01 19:03:52 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
for (; tok3; tok3 = tok3->next())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
if (tok3->str() == "{")
|
|
|
|
++indentlevel;
|
2009-03-17 18:55:28 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
else if (tok3->str() == "}")
|
2010-11-08 18:01:28 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
if (indentlevel <= 1 && brackets.empty() && brackets2.empty())
|
|
|
|
{
|
|
|
|
// there is a bug if indentlevel is 0
|
|
|
|
// the "}" token should only be added if indentlevel is 1 but I add it always intentionally
|
|
|
|
// if indentlevel ever becomes 0, cppcheck will write:
|
|
|
|
// ### Error: Invalid number of character {
|
|
|
|
addtoken("}", tok3->linenr(), tok3->fileIndex());
|
|
|
|
Token::createMutualLinks(braces.top(), _tokensBack);
|
|
|
|
braces.pop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--indentlevel;
|
2010-11-08 18:01:28 +01:00
|
|
|
}
|
2009-11-22 13:38:45 +01:00
|
|
|
|
2009-03-17 18:55:28 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
if (tok3->isName())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2011-02-12 20:58:45 +01:00
|
|
|
// search for this token in the type vector
|
|
|
|
unsigned int itype = 0;
|
|
|
|
while (itype < type.size() && type[itype]->str() != tok3->str())
|
|
|
|
++itype;
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
// replace type with given type..
|
|
|
|
if (itype < type.size())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2011-02-20 12:22:01 +01:00
|
|
|
for (const Token *typetok = types2[itype];
|
|
|
|
typetok && !Token::Match(typetok, "[,>]");
|
2011-02-20 12:17:05 +01:00
|
|
|
typetok = typetok->next())
|
|
|
|
{
|
|
|
|
addtoken(typetok, tok3->linenr(), tok3->fileIndex());
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
continue;
|
2011-02-12 20:58:45 +01:00
|
|
|
}
|
|
|
|
}
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
// replace name..
|
|
|
|
if (Token::Match(tok3, (name + " !!<").c_str()))
|
|
|
|
{
|
|
|
|
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
continue;
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
// copy
|
|
|
|
addtoken(tok3, tok3->linenr(), tok3->fileIndex());
|
|
|
|
if (Token::Match(tok3, "%type% <"))
|
|
|
|
{
|
|
|
|
if (!Token::Match(tok3, (name + " <").c_str()))
|
|
|
|
done = false;
|
|
|
|
used.push_back(_tokensBack);
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
|
2011-02-12 20:58:45 +01:00
|
|
|
// link() newly tokens manually
|
2011-03-06 19:28:51 +01:00
|
|
|
|