2009-01-24 18:15:38 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 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-01-24 18:15:38 +01:00
|
|
|
#include "filelister.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"
|
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
|
|
|
{
|
|
|
|
_tokens = 0;
|
|
|
|
_tokensBack = 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
_tokens = 0;
|
|
|
|
_tokensBack = 0;
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
Tokenizer::~Tokenizer()
|
|
|
|
{
|
2009-07-05 22:16:43 +02:00
|
|
|
deallocateTokens();
|
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-04-02 07:30:58 +02:00
|
|
|
if (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());
|
|
|
|
}
|
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
|
|
|
|
|
|
|
std::map<std::string, unsigned int>::const_iterator it = _typeSize.find(type->strAt(0));
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// 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
|
|
|
|
// every time an include file is complitely parsed, last item in the vector
|
|
|
|
// 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
|
|
|
|
// every time an include file is complitely parsed, last item in the vector
|
|
|
|
// 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
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (getFileLister()->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-03-11 20:58:59 +01:00
|
|
|
_files.push_back(getFileLister()->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
|
|
|
}
|
|
|
|
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
|
|
|
{
|
|
|
|
std::cerr << "####### Preprocessor bug! #######\n";
|
|
|
|
std::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
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-04-02 07:30:58 +02:00
|
|
|
if (!(_settings && _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();
|
|
|
|
loc.file = file(tok1);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
loc.line = tok2->linenr();
|
|
|
|
loc.file = file(tok2);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
"style",
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!(_settings && _settings->_checkCodingStyle))
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok1->linenr();
|
|
|
|
loc.file = file(tok1);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
loc.line = tok2->linenr();
|
|
|
|
loc.file = file(tok2);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
"style",
|
|
|
|
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
|
|
|
|
bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name)
|
|
|
|
{
|
|
|
|
// check for an end of definition
|
|
|
|
const Token * tok = *tokPtr;
|
2010-04-12 19:05:31 +02:00
|
|
|
if (tok && tok->next() && 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
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous()->str() == "operator")
|
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-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
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(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-04-14 19:06:51 +02:00
|
|
|
if (Token::Match(tok->previous(), "typedef|}|>") ||
|
2010-04-02 07:30:58 +02:00
|
|
|
(Token::Match(tok->previous(), "%type%") &&
|
2010-04-14 19:06:51 +02:00
|
|
|
(!Token::Match(tok->previous(), "return|new|const|friend|public|private|protected") &&
|
2010-04-02 07:30:58 +02:00
|
|
|
!Token::Match(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
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
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")
|
|
|
|
{
|
|
|
|
if (tok->next()->str() != ";")
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SpaceInfo
|
|
|
|
{
|
|
|
|
bool isNamespace;
|
|
|
|
std::string className;
|
|
|
|
const Token * classEnd;
|
|
|
|
};
|
|
|
|
|
2009-09-30 13:35:00 +02:00
|
|
|
void Tokenizer::simplifyTypedef()
|
|
|
|
{
|
2010-02-03 07:58:36 +01:00
|
|
|
std::vector<SpaceInfo> spaceInfo;
|
|
|
|
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-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "class|struct|namespace %any%"))
|
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
|
|
|
{
|
|
|
|
SpaceInfo info;
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
if (Token::Match(tok->next(), "struct|enum|union|class %type% {") ||
|
|
|
|
Token::Match(tok->next(), "struct|enum|union|class {"))
|
2009-12-28 17:57:52 +01:00
|
|
|
{
|
|
|
|
Token *tok1;
|
2010-01-10 08:49:02 +01:00
|
|
|
std::string name;
|
2009-12-28 17:57:52 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(2)->str() == "{") // unnamed
|
2009-12-28 17:57:52 +01:00
|
|
|
{
|
2010-01-10 08:49:02 +01:00
|
|
|
tok1 = tok->tokAt(2)->link();
|
2009-12-28 17:57:52 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok1 && tok1->next())
|
2009-12-28 17:57:52 +01:00
|
|
|
{
|
2010-01-10 08:49:02 +01:00
|
|
|
// use typedef name if available
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok1->next(), "%type%"))
|
2010-01-10 08:49:02 +01:00
|
|
|
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());
|
2009-12-28 17:57:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
2010-01-10 08:49:02 +01:00
|
|
|
else // has a name
|
|
|
|
{
|
|
|
|
tok1 = tok->tokAt(3)->link();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok1)
|
2010-01-10 08:49:02 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
name = tok->tokAt(2)->str();
|
|
|
|
}
|
2009-12-28 17:57:52 +01:00
|
|
|
|
|
|
|
tok1->insertToken(";");
|
|
|
|
tok1 = tok1->next();
|
|
|
|
tok1->insertToken("typedef");
|
|
|
|
tok1 = tok1->next();
|
|
|
|
Token * tok3 = tok1;
|
2010-01-11 22:58:57 +01:00
|
|
|
tok1->insertToken(tok->next()->strAt(0)); // struct, union or enum
|
2009-12-28 17:57:52 +01:00
|
|
|
tok1 = tok1->next();
|
2010-01-10 08:49:02 +01:00
|
|
|
tok1->insertToken(name.c_str());
|
2009-12-28 17:57:52 +01:00
|
|
|
tok->deleteThis();
|
|
|
|
tok = tok3;
|
|
|
|
}
|
2009-12-22 19:10:33 +01:00
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
Token *typeName;
|
2010-01-18 19:06:50 +01:00
|
|
|
std::list<std::string> pointers;
|
2010-01-30 08:00:11 +01:00
|
|
|
Token *typeStart = 0;
|
|
|
|
Token *typeEnd = 0;
|
|
|
|
Token *argStart = 0;
|
|
|
|
Token *argEnd = 0;
|
2010-01-30 19:41:22 +01:00
|
|
|
Token *arrayStart = 0;
|
|
|
|
Token *arrayEnd = 0;
|
2010-05-23 10:46:39 +02:00
|
|
|
Token *specStart = 0;
|
|
|
|
Token *specEnd = 0;
|
2010-01-16 09:18:21 +01:00
|
|
|
Token *typeDef = tok;
|
2010-05-25 20:43:44 +02:00
|
|
|
Token *argFuncRetStart = 0;
|
|
|
|
Token *argFuncRetEnd = 0;
|
2010-05-28 06:55:54 +02:00
|
|
|
Token *const1 = 0;
|
|
|
|
Token *const2 = 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-05-29 07:52:06 +02:00
|
|
|
Token *namespaceStart = 0;
|
|
|
|
Token *namespaceEnd = 0;
|
2009-12-29 07:28:00 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "::") ||
|
|
|
|
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-05-23 10:46:39 +02:00
|
|
|
if (Token::Match(typeStart, "const"))
|
|
|
|
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
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "::"))
|
2010-01-31 21:46:18 +01:00
|
|
|
typeEnd = tok->tokAt(offset++);
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(offset) && 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++);
|
2010-05-23 10:46:39 +02:00
|
|
|
else if (Token::Match(tok->tokAt(offset), "const ("))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
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-04-02 07:30:58 +02:00
|
|
|
while (typeEnd && typeEnd->next() && 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-04-02 07:30:58 +02:00
|
|
|
while (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "*|&"))
|
2010-01-18 19:06:50 +01:00
|
|
|
pointers.push_back(tok->tokAt(offset++)->str());
|
2009-10-03 17:02:23 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(offset) && 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-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(offset) && 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
|
|
|
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "("))
|
|
|
|
{
|
|
|
|
function = true;
|
|
|
|
if (tok->tokAt(offset)->link()->next())
|
|
|
|
{
|
|
|
|
argStart = tok->tokAt(offset);
|
|
|
|
argEnd = tok->tokAt(offset)->link();
|
|
|
|
tok = argEnd->next();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// internal error
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-01-18 19:06:50 +01:00
|
|
|
else
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-01-18 19:06:50 +01:00
|
|
|
// unhandled typedef, skip it and continue
|
2010-01-31 07:16:19 +01:00
|
|
|
tok = tok->tokAt(offset);
|
2010-01-18 19:06:50 +01:00
|
|
|
continue;
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
|
|
|
}
|
2010-05-27 07:00:33 +02:00
|
|
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( *|&| const|volatile| const|volatile| %type% ) ("))
|
2010-01-20 21:16:40 +01:00
|
|
|
{
|
2010-05-10 07:12:06 +02:00
|
|
|
functionPtr = tok->tokAt(offset + 1)->str() == "*";
|
|
|
|
functionRef = tok->tokAt(offset + 1)->str() == "&";
|
|
|
|
function = tok->tokAt(offset + 2)->str() == ")";
|
|
|
|
if (!function)
|
2010-05-27 07:00:33 +02:00
|
|
|
{
|
2010-05-10 07:12:06 +02:00
|
|
|
offset++;
|
2010-05-27 07:00:33 +02:00
|
|
|
if (Token::Match(tok->tokAt(offset + 1), "const|volatile"))
|
|
|
|
{
|
|
|
|
const1= tok->tokAt(offset + 1);
|
|
|
|
offset++;
|
|
|
|
if (Token::Match(tok->tokAt(offset + 1), "const|volatile"))
|
|
|
|
{
|
|
|
|
const2 = tok->tokAt(offset + 1);
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-10 07:12:06 +02:00
|
|
|
if (tok->tokAt(offset + 3)->link()->next())
|
2010-01-20 21:16:40 +01:00
|
|
|
{
|
2010-05-10 07:12:06 +02:00
|
|
|
typeName = tok->tokAt(offset + 1);
|
|
|
|
argStart = tok->tokAt(offset + 3);
|
|
|
|
argEnd = tok->tokAt(offset + 3)->link();
|
2010-01-30 08:00:11 +01:00
|
|
|
tok = argEnd->next();
|
2010-01-20 21:16:40 +01:00
|
|
|
}
|
|
|
|
else
|
2010-01-27 19:03:24 +01:00
|
|
|
{
|
|
|
|
// internal error
|
2010-01-20 21:16:40 +01:00
|
|
|
continue;
|
2010-01-27 19:03:24 +01:00
|
|
|
}
|
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-01-20 21:16:40 +01:00
|
|
|
}
|
2010-05-29 07:52:06 +02:00
|
|
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( ::| %var% :: *|&| const|volatile| const|volatile| %type% ) ("))
|
2010-05-11 21:41:33 +02:00
|
|
|
{
|
2010-05-29 07:52:06 +02:00
|
|
|
namespaceStart = tok->tokAt(offset + 1);
|
|
|
|
if (tok->tokAt(offset + 1)->str() == "::")
|
|
|
|
offset++;
|
|
|
|
namespaceEnd = tok->tokAt(offset + 2);
|
2010-05-11 21:41:33 +02:00
|
|
|
functionPtr = tok->tokAt(offset + 3)->str() == "*";
|
|
|
|
functionRef = tok->tokAt(offset + 3)->str() == "&";
|
|
|
|
function = tok->tokAt(offset + 4)->str() == ")";
|
|
|
|
if (!function)
|
2010-05-27 07:00:33 +02:00
|
|
|
{
|
2010-05-11 21:41:33 +02:00
|
|
|
offset++;
|
2010-05-27 07:00:33 +02:00
|
|
|
if (Token::Match(tok->tokAt(offset + 3), "const|volatile"))
|
|
|
|
{
|
|
|
|
const1 = tok->tokAt(offset + 3);
|
|
|
|
offset++;
|
|
|
|
if (Token::Match(tok->tokAt(offset + 3), "const|volatile"))
|
|
|
|
{
|
|
|
|
const2 = tok->tokAt(offset + 3);
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-11 21:41:33 +02:00
|
|
|
if (tok->tokAt(offset + 5)->link()->next())
|
|
|
|
{
|
|
|
|
typeName = tok->tokAt(offset + 3);
|
|
|
|
argStart = tok->tokAt(offset + 5);
|
|
|
|
argEnd = tok->tokAt(offset + 5)->link();
|
|
|
|
tok = argEnd->next();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// internal error
|
|
|
|
continue;
|
|
|
|
}
|
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-05-10 17:50:40 +02:00
|
|
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( %type% ("))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( * ( * %type% ) ("))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( * %type% ("))
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( * ( %type% ) ("))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( *|& %type% ) ["))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( %type% :: * %type% ) ;"))
|
|
|
|
{
|
2010-05-29 07:52:06 +02:00
|
|
|
namespaceStart = tok->tokAt(offset + 1);
|
|
|
|
namespaceEnd = tok->tokAt(offset + 2);
|
2010-05-28 06:55:54 +02:00
|
|
|
ptrMember = true;
|
|
|
|
typeName = tok->tokAt(offset + 4);
|
|
|
|
tok = tok->tokAt(offset + 6);
|
|
|
|
}
|
|
|
|
|
2010-05-25 20:43:44 +02:00
|
|
|
// unhandled typedef, skip it and continue
|
2010-01-17 08:16:17 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
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-02-03 07:58:36 +01:00
|
|
|
std::string pattern;
|
|
|
|
int scope = 0;
|
2010-01-17 08:16:17 +01:00
|
|
|
bool inScope = true;
|
|
|
|
|
|
|
|
bool exitThisScope = false;
|
|
|
|
int exitScope = 0;
|
|
|
|
bool simplifyType = false;
|
2010-02-03 07:58:36 +01:00
|
|
|
unsigned int classLevel = spaceInfo.size();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = classLevel; i < spaceInfo.size(); i++)
|
2010-02-03 07:58:36 +01:00
|
|
|
pattern += (spaceInfo[i].className + " :: ");
|
|
|
|
|
2010-02-20 09:07:29 +01:00
|
|
|
pattern += typeName->str();
|
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
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "}")
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
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-04-02 07:30:58 +02:00
|
|
|
for (unsigned int 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
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "{")
|
2010-02-20 09:07:29 +01:00
|
|
|
{
|
2010-02-03 07:58:36 +01:00
|
|
|
scope++;
|
2010-02-20 09:07:29 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, pattern.c_str()))
|
2010-01-17 08:16:17 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pattern != typeName->str()) // has a "something ::"
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int 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
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (inScope && !exitThisScope)
|
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;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (duplicateTypedef(&tok2, typeName))
|
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-04-14 19:06:51 +02:00
|
|
|
bool isDerived = false;
|
|
|
|
isDerived = Token::Match(tok2->previous(), "public|protected|private %type% {|,");
|
|
|
|
|
2010-01-22 17:27:40 +01:00
|
|
|
bool inCast = false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((tok2->previous()->str() == "(" && tok2->next()->str() == ")") ||
|
|
|
|
(Token::Match(tok2->tokAt(-1), "<") &&
|
|
|
|
Token::Match(tok2->next(), "> (")))
|
2010-01-22 17:27:40 +01:00
|
|
|
inCast = 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-01-30 08:00:11 +01:00
|
|
|
tok2->str(typeStart->str());
|
|
|
|
Token * nextToken;
|
|
|
|
std::stack<Token *> links;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (nextToken = typeStart->next(); nextToken != typeEnd->next(); nextToken = nextToken->next())
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-01-30 08:00:11 +01:00
|
|
|
tok2->insertToken(nextToken->strAt(0));
|
|
|
|
tok2 = tok2->next();
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2010-01-30 08:00:11 +01:00
|
|
|
// Check for links and fix them up
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(" || tok2->str() == "[")
|
2010-01-30 08:00:11 +01:00
|
|
|
links.push(tok2);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == ")" || tok2->str() == "]")
|
2010-01-30 08:00:11 +01:00
|
|
|
{
|
|
|
|
Token * link = links.top();
|
2009-12-30 20:56:16 +01:00
|
|
|
|
2010-01-30 08:00:11 +01:00
|
|
|
tok2->link(link);
|
|
|
|
link->link(tok2);
|
2009-12-30 20:56:16 +01:00
|
|
|
|
2010-01-30 08:00:11 +01:00
|
|
|
links.pop();
|
2010-01-27 19:03:24 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
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-05-10 07:12:06 +02:00
|
|
|
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;
|
|
|
|
if (function && tok2->next()->str() != "*")
|
|
|
|
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-01-27 19:03:24 +01:00
|
|
|
tok2->insertToken("*");
|
2010-05-10 07:12:06 +02:00
|
|
|
else if (functionRef)
|
2010-01-27 19:03:24 +01:00
|
|
|
tok2->insertToken("&");
|
2010-01-20 21:16:40 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
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)
|
2010-01-22 17:27:40 +01:00
|
|
|
{
|
2010-05-18 07:11:23 +02:00
|
|
|
if (tok2->next() && tok2->next()->str() != ")" && tok2->next()->str() != ",")
|
2010-01-22 20:26:07 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2->next(), "( * %type% ) ("))
|
2010-01-24 22:40:35 +01:00
|
|
|
tok2 = tok2->tokAt(5)->link();
|
|
|
|
else
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->next()->str() == "(")
|
2010-01-24 22:40:35 +01:00
|
|
|
tok2 = tok2->next()->link();
|
2010-05-18 07:11:23 +02:00
|
|
|
else if (!Token::Match(tok2->next(), "[|>|;"))
|
2010-01-27 19:03:24 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
2010-01-24 22:40:35 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok2, "*|&") && tok2->next()->str() != ")")
|
2010-02-18 07:24:24 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
2010-01-27 19:03:24 +01:00
|
|
|
// skip over typedef parameter
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->next()->str() == "(")
|
2010-01-27 19:03:24 +01:00
|
|
|
{
|
2010-01-24 22:40:35 +01:00
|
|
|
tok2 = tok2->next()->link();
|
2010-01-27 19:03:24 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->next()->str() == "(")
|
2010-01-27 19:03:24 +01:00
|
|
|
tok2 = tok2->next()->link();
|
|
|
|
}
|
2010-01-24 22:40:35 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-22 20:26:07 +01:00
|
|
|
}
|
2010-01-22 17:27:40 +01:00
|
|
|
}
|
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-01-20 21:16:40 +01:00
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok3 = tok2;
|
2010-04-11 13:49:50 +02:00
|
|
|
Token * nextArgTok;
|
|
|
|
std::stack<Token *> argLinks;
|
|
|
|
for (nextArgTok = argStart->next(); nextArgTok != argEnd; nextArgTok = nextArgTok->next())
|
2010-01-20 21:16:40 +01:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
tok2->insertToken(nextArgTok->strAt(0));
|
2010-01-20 21:16:40 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// Check for links and fix them up
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(" || tok2->str() == "[")
|
2010-04-11 13:49:50 +02:00
|
|
|
argLinks.push(tok2);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == ")" || tok2->str() == "]")
|
2010-01-20 21:16:40 +01:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
Token * link = argLinks.top();
|
2010-01-20 21:16:40 +01:00
|
|
|
|
|
|
|
tok2->link(link);
|
|
|
|
link->link(tok2);
|
|
|
|
|
2010-04-11 13:49:50 +02:00
|
|
|
argLinks.pop();
|
2010-01-20 21:16:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok3);
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip over variable name
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
if (tok4 && functionPtrRetFuncPtr)
|
|
|
|
{
|
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok4);
|
|
|
|
}
|
|
|
|
|
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token *tok5 = tok2;
|
|
|
|
|
|
|
|
Token *nextArgTok;
|
|
|
|
std::stack<Token *> argLinks;
|
|
|
|
for (nextArgTok = argStart->next(); nextArgTok != argEnd; nextArgTok = nextArgTok->next())
|
|
|
|
{
|
|
|
|
tok2->insertToken(nextArgTok->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// Check for links and fix them up
|
|
|
|
if (tok2->str() == "(" || tok2->str() == "[")
|
|
|
|
argLinks.push(tok2);
|
|
|
|
if (tok2->str() == ")" || tok2->str() == "]")
|
|
|
|
{
|
|
|
|
Token * link = argLinks.top();
|
|
|
|
|
|
|
|
tok2->link(link);
|
|
|
|
link->link(tok2);
|
|
|
|
|
|
|
|
argLinks.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok5);
|
|
|
|
|
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok3);
|
|
|
|
|
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token *tok6 = tok2;
|
|
|
|
|
|
|
|
for (nextArgTok = argFuncRetStart->next(); nextArgTok != argFuncRetEnd; nextArgTok = nextArgTok->next())
|
|
|
|
{
|
|
|
|
tok2->insertToken(nextArgTok->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// Check for links and fix them up
|
|
|
|
if (tok2->str() == "(" || tok2->str() == "[")
|
|
|
|
argLinks.push(tok2);
|
|
|
|
if (tok2->str() == ")" || tok2->str() == "]")
|
|
|
|
{
|
|
|
|
Token * link = argLinks.top();
|
|
|
|
|
|
|
|
tok2->link(link);
|
|
|
|
link->link(tok2);
|
|
|
|
|
|
|
|
argLinks.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok6);
|
|
|
|
}
|
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
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
tok2->insertToken(")");
|
|
|
|
Token::createMutualLinks(tok2->next(), tok3);
|
|
|
|
}
|
|
|
|
else if (ptrMember)
|
|
|
|
{
|
|
|
|
tok2->insertToken("(");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token *tok3 = tok2;
|
|
|
|
|
2010-05-29 07:52:06 +02:00
|
|
|
const Token *tok4 = namespaceStart;
|
2010-05-28 06:55:54 +02:00
|
|
|
|
2010-05-29 07:52:06 +02:00
|
|
|
while (tok4 != namespaceEnd)
|
|
|
|
{
|
|
|
|
tok2->insertToken(tok4->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok4 = tok4->next();
|
|
|
|
}
|
|
|
|
tok2->insertToken(namespaceEnd->str());
|
2010-05-28 06:55:54 +02:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
tok2->insertToken("*");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// skip over name
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
tok2->insertToken(")");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok3);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
2010-04-11 13:49:50 +02:00
|
|
|
Token * nextArrTok;
|
|
|
|
std::stack<Token *> arrLinks;
|
|
|
|
for (nextArrTok = arrayStart; nextArrTok != arrayEnd->next(); nextArrTok = nextArrTok->next())
|
2010-01-30 19:41:22 +01:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
tok2->insertToken(nextArrTok->strAt(0));
|
2010-01-30 19:41:22 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// Check for links and fix them up
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(" || tok2->str() == "[")
|
2010-04-11 13:49:50 +02:00
|
|
|
arrLinks.push(tok2);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == ")" || tok2->str() == "]")
|
2010-01-30 19:41:22 +01:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
Token * link = arrLinks.top();
|
2010-01-30 19:41:22 +01:00
|
|
|
|
|
|
|
tok2->link(link);
|
|
|
|
link->link(tok2);
|
|
|
|
|
2010-04-11 13:49:50 +02:00
|
|
|
arrLinks.pop();
|
2010-01-30 19:41:22 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
tok2 = tok2->next();
|
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-04-02 07:30:58 +02:00
|
|
|
while (tok->tokAt(offset) && 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-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(offset) && 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-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(offset) && 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-01-23 22:44:08 +01:00
|
|
|
bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::string &configuration)
|
2009-05-09 21:32:29 +02:00
|
|
|
{
|
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-03-11 20:58:59 +01:00
|
|
|
_files.push_back(getFileLister()->simplifyPath(FileName));
|
2009-05-09 21:32:29 +02:00
|
|
|
|
|
|
|
createTokens(code);
|
|
|
|
|
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-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
|
|
|
{
|
2009-05-31 15:48:40 +02:00
|
|
|
static const char * const combineWithNext[][3] =
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
{ "<", "<", "<<" },
|
|
|
|
|
|
|
|
{ "&", "&", "&&" },
|
|
|
|
{ "|", "|", "||" },
|
|
|
|
|
|
|
|
{ "+", "=", "+=" },
|
|
|
|
{ "-", "=", "-=" },
|
|
|
|
{ "*", "=", "*=" },
|
|
|
|
{ "/", "=", "/=" },
|
|
|
|
{ "&", "=", "&=" },
|
|
|
|
{ "|", "=", "|=" },
|
|
|
|
|
|
|
|
{ "=", "=", "==" },
|
|
|
|
{ "!", "=", "!=" },
|
|
|
|
{ "<", "=", "<=" },
|
|
|
|
{ ">", "=", ">=" },
|
|
|
|
|
|
|
|
{ ":", ":", "::" },
|
|
|
|
{ "-", ">", "." }, // Replace "->" with "."
|
|
|
|
|
|
|
|
{ "private", ":", "private:" },
|
|
|
|
{ "protected", ":", "protected:" },
|
2009-08-31 19:40:49 +02:00
|
|
|
{ "public", ":", "public:" },
|
|
|
|
{ "__published", ":", "__published:" }
|
2009-01-24 18:15:38 +01:00
|
|
|
};
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned ui = 0; ui < sizeof(combineWithNext) / sizeof(combineWithNext[0]); ui++)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == combineWithNext[ui][0] && tok->next()->str() == combineWithNext[ui][1])
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
tok->str(combineWithNext[ui][2]);
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
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
|
|
|
|
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-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "__asm|_asm|asm {") &&
|
|
|
|
tok->tokAt(2)->link() &&
|
|
|
|
tok->tokAt(2)->link()->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(2)->link()->next());
|
2009-12-30 08:24:27 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok->next(), "__asm__ __volatile__ (") &&
|
|
|
|
tok->tokAt(3)->link() &&
|
|
|
|
tok->tokAt(3)->link()->next())
|
2009-12-30 08:24:27 +01:00
|
|
|
{
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(3)->link()->next());
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
2009-12-30 08:24:27 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::simpleMatch(tok->next(), "__asm"))
|
2010-03-31 20:20:51 +02:00
|
|
|
{
|
|
|
|
const Token *tok2 = tok->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2 && (tok2->isNumber() || tok2->isName() || tok2->str() == ","))
|
2010-03-31 20:20:51 +02:00
|
|
|
tok2 = tok2->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2 && tok2->str() == ";")
|
2010-03-31 20:20:51 +02:00
|
|
|
Token::eraseTokens(tok, tok2);
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-30 08:24:27 +01:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// insert "asm ( )"
|
|
|
|
tok->insertToken(")");
|
|
|
|
tok->insertToken("(");
|
|
|
|
tok->insertToken("asm");
|
|
|
|
|
|
|
|
Token::createMutualLinks(tok->tokAt(2), tok->tokAt(3));
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:43:58 +02:00
|
|
|
// Remove "volatile", "inline" and "register"
|
|
|
|
while (Token::Match(_tokens, "volatile|inline|__inline|__forceinline|register"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-04-20 20:38:05 +02:00
|
|
|
_tokens->deleteThis();
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-05-29 07:43:58 +02:00
|
|
|
while (Token::Match(tok->next(), "volatile|inline|__inline|__forceinline|register"))
|
2009-02-21 14:35:55 +01:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-29 07:17:38 +02:00
|
|
|
// Remove __builtin_expect, likely and unlikely
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok->next(), "__builtin_expect ("))
|
|
|
|
{
|
|
|
|
unsigned int parlevel = 0;
|
|
|
|
for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel <= 1)
|
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
if (parlevel == 1 && tok2->str() == ",")
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2, ", %num% )"))
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
Token::eraseTokens(tok2->previous(), tok2->tokAt(2));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok->next(), "likely|unlikely ("))
|
|
|
|
{
|
|
|
|
// remove closing ')'
|
|
|
|
tok->tokAt(2)->link()->previous()->deleteNext();
|
|
|
|
|
|
|
|
// remove "likely|unlikely ("
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-28 15:56:13 +02:00
|
|
|
// colapse compound standard types into a single token
|
|
|
|
// unsigned long long int => long _isUnsigned=true,_isLong=true
|
|
|
|
simplifyStdType();
|
|
|
|
|
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();
|
|
|
|
|
2009-06-10 19:36:00 +02:00
|
|
|
// Split up variable declarations.
|
2009-03-18 20:32:05 +01:00
|
|
|
simplifyVarDecl();
|
|
|
|
|
2010-05-01 10:26:15 +02:00
|
|
|
simplifyVariableMultipleAssign();
|
|
|
|
|
2010-01-30 09:33:16 +01:00
|
|
|
// Change initialisation of variable to assignment
|
|
|
|
simplifyInitVar();
|
|
|
|
|
|
|
|
// Remove redundant parantheses
|
|
|
|
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);
|
|
|
|
|
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-01-29 22:22:18 +01:00
|
|
|
//updateClassList();
|
2009-09-19 10:54:10 +02:00
|
|
|
setVarId();
|
2010-03-13 11:52:48 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!validate())
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
2009-09-19 10:54:10 +02:00
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
return true;
|
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-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
|
|
|
{
|
|
|
|
sz++;
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "%any% } ;"))
|
2010-02-20 18:13:09 +01:00
|
|
|
tok->next()->insertToken(MathLib::toString<long>(sz));
|
|
|
|
}
|
2010-04-24 21:48:58 +02:00
|
|
|
|
|
|
|
else if (Token::Match(tok, "%var% [ ] = %str% ;"))
|
|
|
|
{
|
|
|
|
unsigned int sz = tok->strAt(4).length() - 1;
|
|
|
|
tok->next()->insertToken(MathLib::toString<long>(sz));
|
|
|
|
}
|
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-02-21 09:47:41 +01:00
|
|
|
tok->tokAt(2)->insertToken(";");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 "<"
|
|
|
|
* \return true if the tokens look like template parameters
|
|
|
|
*/
|
|
|
|
static bool templateParameters(const Token *tok)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-11-01 15:56:00 +01:00
|
|
|
return false;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != "<")
|
2009-11-01 15:56:00 +01:00
|
|
|
return false;
|
|
|
|
tok = tok->next();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok)
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
|
|
|
// num/type ..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->isNumber() && !tok->isName())
|
2009-11-01 15:56:00 +01:00
|
|
|
return false;
|
|
|
|
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() == ">")
|
2009-11-01 15:56:00 +01:00
|
|
|
return true;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != ",")
|
2009-11-01 15:56:00 +01:00
|
|
|
break;
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-03 21:23:47 +02:00
|
|
|
void Tokenizer::simplifyTemplates()
|
|
|
|
{
|
2009-09-03 22:19:44 +02:00
|
|
|
// Remove "typename" unless used in template arguments..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-09-03 22:19:44 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "typename")
|
2009-09-03 22:19:44 +02:00
|
|
|
tok->deleteThis();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "template <"))
|
2009-09-03 22:19:44 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok && tok->str() != ">")
|
2009-09-03 22:19:44 +02:00
|
|
|
tok = tok->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-09-06 08:22:45 +02:00
|
|
|
break;
|
2009-09-03 22:19:44 +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
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-05 20:16:57 +02:00
|
|
|
// Locate templates..
|
|
|
|
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
|
|
|
{
|
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
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (templates.empty())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
removeTemplates(_tokens);
|
2009-05-05 20:16:57 +02:00
|
|
|
return;
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
2009-05-05 20:16:57 +02:00
|
|
|
|
|
|
|
// Locate possible instantiations of templates..
|
|
|
|
std::list<Token *> used;
|
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
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (; tok; tok = tok->next())
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{" || tok->str() == "(")
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
tok = tok->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-11-01 19:03:52 +01:00
|
|
|
break;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "}")
|
2009-05-06 21:03:11 +02:00
|
|
|
break;
|
|
|
|
}
|
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;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok->previous(), "[{};=] %var% <") ||
|
|
|
|
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
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (used.empty())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
removeTemplates(_tokens);
|
2009-05-05 20:16:57 +02:00
|
|
|
return;
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
|
|
|
|
2009-05-05 20:16:57 +02:00
|
|
|
|
2009-09-03 21:46:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Template arguments with default values
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
std::list<Token *> eq;
|
|
|
|
unsigned int templatepar = 1;
|
|
|
|
std::string classname;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = *iter1; tok; tok = tok->next())
|
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
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "> class %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
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ",")
|
2009-09-06 08:22:45 +02:00
|
|
|
++templatepar;
|
|
|
|
|
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;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.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-04-02 07:30:58 +02:00
|
|
|
for (unsigned int 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();
|
|
|
|
tok->insertToken((*it)->strAt(1));
|
|
|
|
tok = tok->next();
|
|
|
|
++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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-05 20:16:57 +02:00
|
|
|
// expand templates
|
2009-11-01 19:03:52 +01:00
|
|
|
bool done = false;
|
2009-12-22 20:10:08 +01:00
|
|
|
//while (!done)
|
2009-05-05 20:16:57 +02:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
done = true;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
|
2009-03-14 21:26:32 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
Token *tok = *iter1;
|
2010-03-31 17:14:49 +02:00
|
|
|
std::vector<const Token *> type;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "%var% ,|>"))
|
2010-03-31 17:14:49 +02:00
|
|
|
type.push_back(tok);
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
// bail out if the end of the file was reached
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-11-01 19:03:52 +01:00
|
|
|
break;
|
|
|
|
|
2010-03-04 17:34:14 +01:00
|
|
|
// get the position of the template name
|
|
|
|
unsigned int namepos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "> class %type% {|:"))
|
2010-03-04 17:34:14 +01:00
|
|
|
namepos = 2;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "> %type% *|&| %type% ("))
|
2010-03-04 17:34:14 +01:00
|
|
|
namepos = 2;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "> %type% %type% *|&| %type% ("))
|
2010-03-04 17:34:14 +01:00
|
|
|
namepos = 3;
|
|
|
|
else
|
2010-03-02 22:55:05 +01:00
|
|
|
{
|
2010-03-04 18:20:08 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// debug message that we bail out..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_settings && _settings->_debug)
|
2010-03-02 22:55:05 +01:00
|
|
|
{
|
2010-03-04 18:20:08 +01:00
|
|
|
std::cout << "simplifyTemplates debug-information: bailing out: "
|
2010-03-04 17:34:14 +01:00
|
|
|
<< file(tok->tokAt(namepos))
|
2010-03-02 22:55:05 +01:00
|
|
|
<< ": "
|
2010-03-04 17:34:14 +01:00
|
|
|
<< tok->tokAt(namepos)->linenr()
|
2010-03-02 22:55:05 +01:00
|
|
|
<< ": "
|
2010-03-04 17:34:14 +01:00
|
|
|
<< tok->tokAt(namepos)->str()
|
2010-03-02 22:55:05 +01:00
|
|
|
<< std::endl;
|
|
|
|
}
|
2010-03-04 18:20:08 +01:00
|
|
|
#endif
|
|
|
|
continue;
|
2010-03-02 22:55:05 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((tok->tokAt(namepos)->str() == "*" || tok->tokAt(namepos)->str() == "&"))
|
2010-03-04 18:20:08 +01:00
|
|
|
++namepos;
|
2010-03-02 22:55:05 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// name of template function/class..
|
2010-03-04 17:34:14 +01:00
|
|
|
const std::string name(tok->strAt(namepos));
|
2009-04-14 22:21:52 +02:00
|
|
|
|
2010-03-04 17:34:14 +01:00
|
|
|
const bool isfunc(tok->strAt(namepos + 1) == "(");
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// locate template usage..
|
|
|
|
|
|
|
|
std::string s(name + " <");
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 0; i < type.size(); ++i)
|
2009-03-14 21:26:32 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (i > 0)
|
2009-11-01 19:03:52 +01:00
|
|
|
s += ",";
|
|
|
|
s += " %any% ";
|
2009-03-14 21:26:32 +01:00
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
const std::string pattern(s + "> ");
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
std::string::size_type sz1 = used.size();
|
|
|
|
unsigned int recursiveCount = 0;
|
2009-03-11 18:50:24 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
|
2009-03-11 19:14:45 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
// If the size of "used" has changed, simplify calculations
|
2010-04-02 07:30:58 +02:00
|
|
|
if (sz1 != used.size())
|
2009-03-11 18:50:24 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
sz1 = used.size();
|
|
|
|
simplifyCalculations();
|
|
|
|
recursiveCount++;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (recursiveCount > 100)
|
2009-05-09 08:02:59 +02:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
// bail out..
|
|
|
|
break;
|
2009-05-09 08:02:59 +02:00
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
2009-05-09 08:02:59 +02:00
|
|
|
|
2009-12-22 20:10:08 +01:00
|
|
|
Token * const tok2 = *iter2;
|
2009-03-17 18:55:28 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() != name)
|
2009-11-01 19:03:52 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2->previous(), "[;{}=]") &&
|
|
|
|
!Token::Match(tok2, (pattern + (isfunc ? "(" : "*| %var%")).c_str()))
|
2009-11-01 19:03:52 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// New type..
|
2010-03-31 17:14:49 +02:00
|
|
|
std::vector<Token> types2;
|
2009-11-01 19:03:52 +01:00
|
|
|
s = "";
|
2010-03-31 17:14:49 +02:00
|
|
|
std::string s1(name + " < ");
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok3 = tok2->tokAt(2); tok3 && tok3->str() != ">"; tok3 = tok3->next())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok3->next())
|
2009-12-20 12:45:34 +01:00
|
|
|
{
|
|
|
|
s.clear();
|
|
|
|
break;
|
|
|
|
}
|
2010-03-31 17:14:49 +02:00
|
|
|
s1 += tok3->str();
|
|
|
|
s1 += " ";
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->str() != ",")
|
2010-03-31 17:14:49 +02:00
|
|
|
types2.push_back(*tok3);
|
|
|
|
// add additional type information
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->isUnsigned())
|
2010-03-31 17:14:49 +02:00
|
|
|
s += "unsigned";
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->isSigned())
|
2010-03-31 17:14:49 +02:00
|
|
|
s += "signed";
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->isLong())
|
2010-03-31 17:14:49 +02:00
|
|
|
s += "long";
|
2009-11-01 19:03:52 +01:00
|
|
|
s += tok3->str();
|
|
|
|
}
|
2010-03-31 17:14:49 +02:00
|
|
|
s1 += ">";
|
2009-11-01 19:03:52 +01:00
|
|
|
const std::string type2(s);
|
2009-03-17 18:55:28 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type2.empty() || type.size() != types2.size())
|
2009-11-22 13:38:45 +01:00
|
|
|
{
|
2009-12-06 11:44:53 +01:00
|
|
|
#ifndef NDEBUG
|
2009-11-22 13:38:45 +01:00
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok2->linenr();
|
|
|
|
loc.file = file(tok2);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
2010-02-27 17:06:33 +01:00
|
|
|
"information",
|
|
|
|
"Failed to instantiate template. The checking continues anyway.",
|
|
|
|
"templateInstantiate");
|
2009-11-22 13:38:45 +01:00
|
|
|
|
|
|
|
_errorLogger->reportErr(errmsg);
|
2009-12-06 11:44:53 +01:00
|
|
|
#endif
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type2.empty())
|
2009-12-20 12:45:34 +01:00
|
|
|
continue;
|
2009-11-22 13:38:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// New classname/funcname..
|
|
|
|
const std::string name2(name + "<" + type2 + ">");
|
2009-03-17 18:55:28 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (expandedtemplates.find(name2) == expandedtemplates.end())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
expandedtemplates.insert(name2);
|
|
|
|
// Copy template..
|
|
|
|
int _indentlevel = 0;
|
|
|
|
int _parlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok3 = _tokens; tok3; tok3 = tok3->next())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->str() == "{")
|
2009-11-01 19:03:52 +01:00
|
|
|
++_indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "}")
|
2009-11-01 19:03:52 +01:00
|
|
|
--_indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "(")
|
2009-11-01 19:03:52 +01:00
|
|
|
++_parlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == ")")
|
2009-11-01 19:03:52 +01:00
|
|
|
--_parlevel;
|
|
|
|
|
|
|
|
// Start of template..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3 == tok)
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
tok3 = tok3->next();
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// member function implemented outside class definition
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (_indentlevel == 0 && _parlevel == 0 && Token::Match(tok3, (pattern + " :: %var% (").c_str()))
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok3->str() != "::")
|
2009-11-01 19:03:52 +01:00
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// not part of template.. go on to next token
|
|
|
|
else
|
|
|
|
continue;
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
int indentlevel = 0;
|
|
|
|
std::stack<Token *> braces; // holds "{" tokens
|
|
|
|
std::stack<Token *> brackets; // holds "(" tokens
|
2009-11-11 19:16:05 +01:00
|
|
|
std::stack<Token *> brackets2; // holds "[" tokens
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (; tok3; tok3 = tok3->next())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->str() == "{")
|
2009-11-01 19:03:52 +01:00
|
|
|
++indentlevel;
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "}")
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel <= 1 && brackets.empty() && brackets2.empty())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
// 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());
|
2009-11-01 15:56:00 +01:00
|
|
|
Token::createMutualLinks(braces.top(), _tokensBack);
|
|
|
|
braces.pop();
|
2009-11-01 19:03:52 +01:00
|
|
|
break;
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->isName())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
// search for this token in the type vector
|
|
|
|
unsigned int itype = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (itype < type.size() && type[itype]->str() != tok3->str())
|
2009-11-01 19:03:52 +01:00
|
|
|
++itype;
|
|
|
|
|
|
|
|
// replace type with given type..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (itype < type.size())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2010-03-31 17:14:49 +02:00
|
|
|
addtoken(&types2[itype], tok3->linenr(), tok3->fileIndex());
|
2009-11-01 19:03:52 +01:00
|
|
|
continue;
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
2009-08-27 18:33:42 +02:00
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
|
|
|
|
// replace name..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok3, (name + " !!<").c_str()))
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy
|
2010-03-31 17:14:49 +02:00
|
|
|
addtoken(tok3, tok3->linenr(), tok3->fileIndex());
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok3, "%type% <"))
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(tok3, (name + " <").c_str()))
|
2009-11-01 19:03:52 +01:00
|
|
|
done = false;
|
|
|
|
used.push_back(_tokensBack);
|
|
|
|
}
|
|
|
|
|
|
|
|
// link() newly tokens manually
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->str() == "{")
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
braces.push(_tokensBack);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "}")
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
assert(braces.empty() == false);
|
|
|
|
Token::createMutualLinks(braces.top(), _tokensBack);
|
|
|
|
braces.pop();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "(")
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
brackets.push(_tokensBack);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "[")
|
2009-11-11 19:16:05 +01:00
|
|
|
{
|
|
|
|
brackets2.push(_tokensBack);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == ")")
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
assert(brackets.empty() == false);
|
|
|
|
Token::createMutualLinks(brackets.top(), _tokensBack);
|
|
|
|
brackets.pop();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "]")
|
2009-11-11 19:16:05 +01:00
|
|
|
{
|
|
|
|
assert(brackets2.empty() == false);
|
|
|
|
Token::createMutualLinks(brackets2.top(), _tokensBack);
|
|
|
|
brackets2.pop();
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
|
2009-08-27 18:33:42 +02:00
|
|
|
}
|
2009-09-13 15:00:48 +02:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
assert(braces.empty());
|
|
|
|
assert(brackets.empty());
|
|
|
|
}
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// Replace all these template usages..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok4 = tok2; tok4; tok4 = tok4->next())
|
2009-03-11 18:50:24 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok4, s1.c_str()))
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
2010-03-31 17:14:49 +02:00
|
|
|
bool match = true;
|
|
|
|
Token * tok5 = tok4->tokAt(2);
|
|
|
|
unsigned int count = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok5->str() != ">")
|
2010-03-31 17:14:49 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok5->str() != ",")
|
2010-03-31 17:14:49 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok5->isUnsigned() != types2[count].isUnsigned() ||
|
|
|
|
tok5->isSigned() != types2[count].isSigned() ||
|
|
|
|
tok5->isLong() != types2[count].isLong())
|
2010-03-31 17:14:49 +02:00
|
|
|
{
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
tok5 = tok5->next();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (match)
|
2009-12-22 20:10:08 +01:00
|
|
|
{
|
2010-03-31 17:14:49 +02:00
|
|
|
tok4->str(name2);
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok4->next()->str() != ">")
|
2010-03-31 17:14:49 +02:00
|
|
|
{
|
|
|
|
used.remove(tok4->next());
|
|
|
|
tok4->deleteNext();
|
|
|
|
}
|
2010-01-06 21:29:39 +01:00
|
|
|
used.remove(tok4->next());
|
2009-11-01 19:03:52 +01:00
|
|
|
tok4->deleteNext();
|
2009-12-22 20:10:08 +01:00
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
2009-03-11 18:50:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
|
|
|
|
removeTemplates(_tokens);
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2010-03-16 19:53:09 +01:00
|
|
|
void Tokenizer::simplifyTemplates2()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-03-16 19:53:09 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "(")
|
2010-03-16 19:53:09 +01:00
|
|
|
tok = tok->link();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "; %type% <"))
|
2010-03-16 19:53:09 +01:00
|
|
|
{
|
2010-03-19 16:12:51 +01:00
|
|
|
const Token *tok2 = tok->tokAt(3);
|
|
|
|
std::string type;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok2, "%type% ,") || Token::Match(tok2, "%num% ,"))
|
2010-03-19 16:12:51 +01:00
|
|
|
{
|
|
|
|
type += tok2->str() + ",";
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "%type% > (") || Token::Match(tok2, "%num% > ("))
|
2010-03-19 16:12:51 +01:00
|
|
|
{
|
|
|
|
type += tok2->str();
|
|
|
|
tok = tok->next();
|
|
|
|
tok->str(tok->str() + "<" + type + ">");
|
|
|
|
Token::eraseTokens(tok, tok2->tokAt(2));
|
|
|
|
}
|
2010-03-16 19:53:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2010-01-29 22:22:18 +01:00
|
|
|
std::string Tokenizer::getNameForFunctionParams(const Token *start)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (start->next() == start->link())
|
2010-01-29 22:22:18 +01:00
|
|
|
return "";
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
bool findNextComma = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok = start->next(); tok && tok != start->link(); tok = tok->next())
|
2010-01-29 22:22:18 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (findNextComma)
|
2010-01-29 22:22:18 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ",")
|
2010-01-29 22:22:18 +01:00
|
|
|
findNextComma = false;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.append(tok->str() + ",");
|
|
|
|
findNextComma = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-08-12 22:50:03 +02:00
|
|
|
void Tokenizer::updateClassList()
|
|
|
|
{
|
|
|
|
_classInfoList.clear();
|
|
|
|
|
|
|
|
// Locate class
|
|
|
|
const Token *tok1 = tokens();
|
2010-01-29 22:22:18 +01:00
|
|
|
|
|
|
|
// Locate implementation
|
|
|
|
tok1 = tokens();
|
|
|
|
std::map<std::string, std::map<std::string, std::map<std::string, const Token*> > > implementations;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((tok1 = Token::findmatch(tok1, "%var% :: ~| %var% (")) != 0)
|
2010-01-29 22:22:18 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok1->tokAt(2)->str() == "~")
|
2010-01-29 22:22:18 +01:00
|
|
|
implementations[tok1->str()]["~ "+tok1->tokAt(3)->str()][getNameForFunctionParams(tok1->tokAt(4))] = tok1;
|
|
|
|
else
|
|
|
|
implementations[tok1->str()][tok1->tokAt(2)->str()][getNameForFunctionParams(tok1->tokAt(3))] = tok1;
|
|
|
|
//std::cout <<"1."<< tok1->str() << "," << tok1->tokAt(2)->str() << ","<<getNameForFunctionParams(tok1->tokAt(3));
|
|
|
|
tok1 = tok1->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
tok1 = tokens();
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((tok1 = Token::findmatch(tok1, "%var% :: operator %any% (")) != 0)
|
2009-08-12 22:50:03 +02:00
|
|
|
{
|
2010-01-29 22:22:18 +01:00
|
|
|
implementations[tok1->str()][tok1->tokAt(2)->str()+" "+tok1->tokAt(3)->str()][getNameForFunctionParams(tok1->tokAt(4))] = tok1;
|
|
|
|
//std::cout <<"3."<< tok1->str() << "," << tok1->tokAt(2)->str()+" "+tok1->tokAt(3)->str() << ","<<getNameForFunctionParams(tok1->tokAt(4));
|
2009-08-12 22:50:03 +02:00
|
|
|
tok1 = tok1->next();
|
2010-01-29 22:22:18 +01:00
|
|
|
}
|
2009-08-12 22:50:03 +02:00
|
|
|
|
2010-01-29 22:22:18 +01:00
|
|
|
tok1 = tokens();
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((tok1 = Token::findmatch(tok1, "class|struct %var% [{:]")) != 0)
|
2010-01-29 22:22:18 +01:00
|
|
|
{
|
2009-08-12 22:50:03 +02:00
|
|
|
ClassInfo::MemberType memberType = ClassInfo::PRIVATE;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok1->str() == "struct")
|
2010-01-29 22:22:18 +01:00
|
|
|
memberType = ClassInfo::PUBLIC;
|
|
|
|
|
2010-02-14 19:58:17 +01:00
|
|
|
const std::string className = tok1->strAt(1);
|
2010-01-29 22:22:18 +01:00
|
|
|
tok1 = tok1->next();
|
2009-08-12 22:50:03 +02:00
|
|
|
int indentlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok = tok1; tok; tok = tok->next())
|
2009-08-12 22:50:03 +02:00
|
|
|
{
|
|
|
|
// Indentation
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{")
|
2009-08-12 22:50:03 +02:00
|
|
|
{
|
|
|
|
++indentlevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "}")
|
2009-08-12 22:50:03 +02:00
|
|
|
{
|
|
|
|
--indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel <= 0)
|
2009-08-12 22:50:03 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse class contents (indentlevel == 1)..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel == 1)
|
2009-08-12 22:50:03 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "private:")
|
2009-08-12 22:50:03 +02:00
|
|
|
memberType = ClassInfo::PRIVATE;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "protected:")
|
2009-08-12 22:50:03 +02:00
|
|
|
memberType = ClassInfo::PROTECTED;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "public:")
|
2009-08-12 22:50:03 +02:00
|
|
|
memberType = ClassInfo::PUBLIC;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "typedef %type% ("))
|
2009-08-12 22:50:03 +02:00
|
|
|
tok = tok->tokAt(2);
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "[:,] %var% ("))
|
2009-08-12 22:50:03 +02:00
|
|
|
tok = tok->tokAt(2);
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "%var% ("))
|
2009-08-12 22:50:03 +02:00
|
|
|
{
|
2010-01-29 22:22:18 +01:00
|
|
|
ClassInfo::MemberFunctionInfo func;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous()->str() == "~")
|
2010-01-29 22:22:18 +01:00
|
|
|
{
|
|
|
|
// destructor
|
|
|
|
func._declaration = tok->previous();
|
|
|
|
func._name = "~ " + tok->str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// member function
|
|
|
|
func._declaration = tok;
|
|
|
|
func._name = tok->str();
|
|
|
|
}
|
|
|
|
|
|
|
|
func._type = memberType;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->next()->link()->next()->str() == "{")
|
2010-01-29 22:22:18 +01:00
|
|
|
func._implementation = tok;
|
|
|
|
else
|
|
|
|
func._implementation = implementations[className][func._name][getNameForFunctionParams(tok->next())];
|
|
|
|
//std::cout <<"\n2."<< className << "," << func._name << ","<<getNameForFunctionParams(tok->next())<<"\n";
|
|
|
|
_classInfoList[className]._memberFunctions.push_back(func);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "operator %any% ("))
|
2010-01-29 22:22:18 +01:00
|
|
|
{
|
2009-08-12 22:50:03 +02:00
|
|
|
ClassInfo::MemberFunctionInfo func;
|
|
|
|
func._declaration = tok;
|
2010-01-29 22:22:18 +01:00
|
|
|
func._name = tok->str() + " " + tok->next()->str();
|
2009-08-12 22:50:03 +02:00
|
|
|
func._type = memberType;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(2)->link()->next()->str() == "{")
|
2010-01-29 22:22:18 +01:00
|
|
|
func._implementation = tok;
|
|
|
|
else
|
|
|
|
func._implementation = implementations[className][func._name][getNameForFunctionParams(tok->tokAt(2))];
|
2009-08-12 22:50:03 +02:00
|
|
|
|
2010-01-29 22:22:18 +01:00
|
|
|
//std::cout <<"\n4."<< className << "," << func._name << ","<<getNameForFunctionParams(tok->tokAt(2))<<"\n";
|
2009-08-12 22:50:03 +02:00
|
|
|
_classInfoList[className]._memberFunctions.push_back(func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
void Tokenizer::setVarId()
|
|
|
|
{
|
2010-04-12 21:17:31 +02:00
|
|
|
// Clear all variable ids
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
tok->varId(0);
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Set variable ids..
|
2010-04-12 21:17:31 +02:00
|
|
|
unsigned int _varId = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-12 21:17:31 +02:00
|
|
|
if (tok != _tokens && !Token::Match(tok, "[,;{}(] %type%"))
|
2009-02-02 18:35:46 +01:00
|
|
|
continue;
|
2009-02-02 19:59:32 +01:00
|
|
|
|
2010-05-06 18:40:52 +02:00
|
|
|
// If pattern is "( %type% *|& %var% )" then check if it's a
|
|
|
|
// variable declaration or a multiplication / mask
|
|
|
|
if (Token::Match(tok, "( %type% *|& %var% )") && !tok->next()->isStandardType())
|
2010-05-01 10:09:53 +02:00
|
|
|
{
|
|
|
|
if (!Token::Match(tok->previous(), "%type%"))
|
|
|
|
continue;
|
2010-05-06 18:40:52 +02:00
|
|
|
if (tok->strAt(-1) == "return")
|
|
|
|
continue;
|
2010-05-01 10:09:53 +02:00
|
|
|
if (!Token::Match(tok->tokAt(5), "const|{|;"))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-12 21:17:31 +02:00
|
|
|
if (Token::Match(tok, "[,;{}(] %type%"))
|
2009-02-02 20:19:36 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "new")
|
2010-02-07 13:34:39 +01:00
|
|
|
continue;
|
|
|
|
|
2010-06-03 07:05:57 +02:00
|
|
|
if (tok->str() == "throw")
|
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "unsigned")
|
2010-02-12 21:24:06 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
2010-04-12 21:17:31 +02:00
|
|
|
if (Token::Match(tok, "class|struct %type% :|{|;"))
|
2009-09-22 17:49:13 +02:00
|
|
|
continue;
|
|
|
|
|
2010-04-18 07:53:39 +02:00
|
|
|
if (Token::Match(tok, "using namespace %type% ;"))
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "else|return|typedef|delete|sizeof"))
|
2009-02-28 21:21:48 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "const|static|extern|public:|private:|protected:"))
|
2009-06-18 18:30:04 +02:00
|
|
|
tok = tok->next();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok, "%var% ::"))
|
2009-03-25 18:10:03 +01:00
|
|
|
tok = tok->tokAt(2);
|
|
|
|
|
2009-04-25 16:55:00 +02:00
|
|
|
// Skip template arguments..
|
2010-04-12 21:17:31 +02:00
|
|
|
if (Token::Match(tok, "%type% <"))
|
2009-04-25 16:55:00 +02:00
|
|
|
{
|
2009-12-29 19:24:48 +01:00
|
|
|
int level = 1;
|
|
|
|
bool again;
|
2009-04-25 16:55:00 +02:00
|
|
|
Token *tok2 = tok->tokAt(2);
|
2009-07-28 19:54:13 +02:00
|
|
|
|
2009-12-29 19:24:48 +01:00
|
|
|
do // Look for start of templates or template arguments
|
|
|
|
{
|
|
|
|
again = false;
|
2009-07-28 19:54:13 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok2, "%var% ::"))
|
2009-12-29 19:24:48 +01:00
|
|
|
tok2 = tok2->tokAt(2);
|
2009-04-25 16:55:00 +02:00
|
|
|
|
2010-04-12 21:17:31 +02:00
|
|
|
if (Token::Match(tok2, "%type% <"))
|
2009-12-29 19:24:48 +01:00
|
|
|
{
|
|
|
|
level++;
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
again = true;
|
|
|
|
}
|
2010-04-12 21:17:31 +02:00
|
|
|
else if (Token::Match(tok2, "%type% ,"))
|
2009-12-29 19:24:48 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2 && (tok2->isName() || tok2->isNumber() || tok2->str() == "*" || tok2->str() == ","))
|
2009-12-29 19:24:48 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
while (again);
|
2009-12-29 19:24:48 +01:00
|
|
|
|
|
|
|
do // Look for end of templates
|
|
|
|
{
|
|
|
|
again = false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (level == 1 && Token::Match(tok2, "> %var%"))
|
2009-12-29 19:24:48 +01:00
|
|
|
tok = tok2;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (level > 1 && tok2->str() == ">")
|
2009-12-29 19:24:48 +01:00
|
|
|
{
|
|
|
|
level--;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (level == 0)
|
2009-12-29 19:24:48 +01:00
|
|
|
tok = tok2;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(1);
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (level == 1 && Token::Match(tok2, "> ::|*|& %var%"))
|
2009-12-29 19:24:48 +01:00
|
|
|
tok = tok2->next();
|
|
|
|
else
|
|
|
|
continue; // Not code that I understand / not a variable declaration
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
while (again);
|
2009-04-25 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
2010-04-12 21:17:31 +02:00
|
|
|
// Determine name of declared variable..
|
|
|
|
std::string varname;
|
2009-03-01 21:02:24 +01:00
|
|
|
Token *tok2 = tok->tokAt(1);
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->isName())
|
2010-04-12 21:17:31 +02:00
|
|
|
varname = tok2->str();
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() != "*" && tok2->str() != "&")
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
2009-03-27 15:12:49 +01:00
|
|
|
// End of tokens reached..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok2)
|
2009-03-27 15:12:49 +01:00
|
|
|
break;
|
|
|
|
|
2010-04-12 21:17:31 +02:00
|
|
|
if (varname == "operator" && Token::Match(tok2, "=|+|-|*|/|[| ]| ("))
|
2010-02-07 13:04:33 +01:00
|
|
|
continue;
|
|
|
|
|
2009-03-25 18:10:03 +01:00
|
|
|
// Is it a function?
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(")
|
2009-03-25 18:10:03 +01:00
|
|
|
{
|
2009-05-27 22:34:08 +02:00
|
|
|
// Search for function declaration, e.g. void f();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok2->next(), ") ;"))
|
2009-05-27 22:34:08 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Search for function declaration, e.g. void f( int c );
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2->next(), "%num%") ||
|
|
|
|
Token::Match(tok2->next(), "%bool%") ||
|
|
|
|
tok2->next()->str()[0] == '"' ||
|
2010-04-12 21:17:31 +02:00
|
|
|
tok2->next()->varId() != 0)
|
2009-03-25 18:10:03 +01:00
|
|
|
{
|
2009-05-27 22:34:08 +02:00
|
|
|
// This is not a function
|
2009-03-25 18:10:03 +01:00
|
|
|
}
|
2009-05-27 22:34:08 +02:00
|
|
|
else
|
|
|
|
{
|
2009-03-25 18:10:03 +01:00
|
|
|
continue;
|
2009-05-27 22:34:08 +02:00
|
|
|
}
|
2009-03-25 18:10:03 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Variable declaration found => Set variable ids
|
2010-04-12 21:17:31 +02:00
|
|
|
if (Token::Match(tok2, "[,();[=]") && !varname.empty())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
++_varId;
|
|
|
|
int indentlevel = 0;
|
|
|
|
int parlevel = 0;
|
|
|
|
bool dot = false;
|
2009-09-30 13:40:10 +02:00
|
|
|
bool funcDeclaration = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (tok2 = tok->next(); tok2; tok2 = tok2->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-12 21:17:31 +02:00
|
|
|
if (!dot && tok2->str() == varname && !Token::Match(tok2->previous(), "struct|union"))
|
2009-01-24 18:15:38 +01:00
|
|
|
tok2->varId(_varId);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "{")
|
2009-01-24 18:15:38 +01:00
|
|
|
++indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "}")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
--indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel < 0)
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
2009-09-30 13:40:10 +02:00
|
|
|
|
|
|
|
// We have reached the end of a loop: "for( int i;;) { }"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (funcDeclaration && indentlevel <= 0)
|
2009-09-30 13:40:10 +02:00
|
|
|
break;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "(")
|
2009-01-24 18:15:38 +01:00
|
|
|
++parlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == ")")
|
2009-02-16 18:41:33 +01:00
|
|
|
{
|
|
|
|
// Is this a function parameter or a variable declared in for example a for loop?
|
2010-04-02 07:30:58 +02:00
|
|
|
if (parlevel == 0 && indentlevel == 0 && Token::Match(tok2, ") const| {"))
|
2009-09-30 13:40:10 +02:00
|
|
|
funcDeclaration = true;
|
2009-02-16 18:41:33 +01:00
|
|
|
else
|
|
|
|
--parlevel;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (parlevel < 0 && tok2->str() == ";")
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
|
|
|
dot = bool(tok2->str() == ".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Struct/Class members
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-05-27 22:34:08 +02:00
|
|
|
// str.clear is a variable
|
|
|
|
// str.clear() is a member function
|
2010-04-12 21:17:31 +02:00
|
|
|
if (tok->varId() != 0 &&
|
2010-04-02 07:30:58 +02:00
|
|
|
Token::Match(tok->next(), ". %var% !!(") &&
|
2010-04-12 21:17:31 +02:00
|
|
|
tok->tokAt(2)->varId() == 0)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
++_varId;
|
|
|
|
|
2010-04-12 21:17:31 +02:00
|
|
|
const std::string pattern(std::string("%varid% . ") + tok->strAt(2));
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, pattern.c_str(), tok->varId()))
|
2009-06-14 08:20:51 +02:00
|
|
|
tok2->tokAt(2)->varId(_varId);
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-02 22:57:18 +02:00
|
|
|
|
2010-03-06 13:35:04 +01:00
|
|
|
// Member functions and variables in this source
|
2009-08-07 23:49:37 +02:00
|
|
|
std::list<Token *> allMemberFunctions;
|
2010-03-06 13:35:04 +01:00
|
|
|
std::list<Token *> allMemberVars;
|
2009-08-07 23:49:37 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = _tokens; tok2; tok2 = tok2->next())
|
2009-08-07 23:49:37 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "%var% :: %var%"))
|
2009-08-07 23:49:37 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok2->tokAt(3), "("))
|
2010-03-06 13:35:04 +01:00
|
|
|
allMemberFunctions.push_back(tok2);
|
2010-04-12 21:17:31 +02:00
|
|
|
else if (tok2->tokAt(2)->varId() != 0)
|
2010-03-06 13:35:04 +01:00
|
|
|
allMemberVars.push_back(tok2);
|
2009-08-07 23:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-02 22:57:18 +02:00
|
|
|
// class members..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "class %var% {"))
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
|
|
|
const std::string &classname(tok->next()->str());
|
|
|
|
|
2010-04-12 21:17:31 +02:00
|
|
|
|
2009-05-02 22:57:18 +02:00
|
|
|
// What member variables are there in this class?
|
2009-08-07 23:49:37 +02:00
|
|
|
std::map<std::string, unsigned int> varlist;
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
2009-05-03 14:31:54 +02:00
|
|
|
unsigned int indentlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
2009-05-03 14:31:54 +02:00
|
|
|
// Indentation..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "{")
|
2009-05-03 14:31:54 +02:00
|
|
|
++indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "}")
|
2009-05-03 14:31:54 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel <= 1)
|
2009-05-03 14:31:54 +02:00
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a member variable..
|
2010-04-12 21:17:31 +02:00
|
|
|
else if (indentlevel == 1 && tok2->varId() > 0)
|
2009-08-07 23:49:37 +02:00
|
|
|
varlist[tok2->str()] = tok2->varId();
|
2009-05-02 22:57:18 +02:00
|
|
|
}
|
2009-05-03 14:31:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Are there any member variables in this class?
|
2010-04-02 07:30:58 +02:00
|
|
|
if (varlist.empty())
|
2009-05-03 14:31:54 +02:00
|
|
|
continue;
|
2009-05-02 22:57:18 +02:00
|
|
|
|
2010-03-06 13:35:04 +01:00
|
|
|
// Member variables
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<Token *>::iterator func = allMemberVars.begin(); func != allMemberVars.end(); ++func)
|
2010-03-06 13:35:04 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::simpleMatch(*func, classname.c_str()))
|
2010-03-06 13:35:04 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Token *tok2 = *func;
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
tok2->varId(varlist[tok2->str()]);
|
|
|
|
}
|
2009-05-03 14:31:54 +02:00
|
|
|
|
|
|
|
// Member functions for this class..
|
|
|
|
std::list<Token *> funclist;
|
|
|
|
{
|
|
|
|
const std::string funcpattern(classname + " :: %var% (");
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<Token *>::iterator func = allMemberFunctions.begin(); func != allMemberFunctions.end(); ++func)
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
2009-08-07 23:49:37 +02:00
|
|
|
Token *tok2 = *func;
|
|
|
|
|
2009-05-03 14:31:54 +02:00
|
|
|
// Found a class function..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, funcpattern.c_str()))
|
2009-05-03 14:31:54 +02:00
|
|
|
{
|
|
|
|
// Goto the end paranthesis..
|
2009-08-29 22:17:01 +02:00
|
|
|
tok2 = tok2->tokAt(3)->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok2)
|
2009-05-03 14:31:54 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
// If this is a function implementation.. add it to funclist
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, ") const|volatile| {"))
|
2009-05-03 14:31:54 +02:00
|
|
|
funclist.push_back(tok2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the variable ids..
|
2009-08-07 23:49:37 +02:00
|
|
|
// Parse each function..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<Token *>::iterator func = funclist.begin(); func != funclist.end(); ++func)
|
2009-05-03 14:31:54 +02:00
|
|
|
{
|
2009-08-07 23:49:37 +02:00
|
|
|
unsigned int indentlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = *func; tok2; tok2 = tok2->next())
|
2009-05-03 14:31:54 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "{")
|
2009-08-07 23:49:37 +02:00
|
|
|
++indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "}")
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel <= 1)
|
2009-08-07 23:49:37 +02:00
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (indentlevel > 0 &&
|
2010-04-12 21:17:31 +02:00
|
|
|
tok2->varId() == 0 &&
|
2010-04-02 07:30:58 +02:00
|
|
|
varlist.find(tok2->str()) != varlist.end())
|
2009-08-07 23:49:37 +02:00
|
|
|
{
|
|
|
|
tok2->varId(varlist[tok2->str()]);
|
2009-05-02 22:57:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-07 23:49:37 +02:00
|
|
|
|
2009-05-02 22:57:18 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:39:45 +01:00
|
|
|
bool Tokenizer::createLinks()
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-11-20 20:10:42 +01:00
|
|
|
std::list<const Token*> type;
|
2009-03-13 22:25:56 +01:00
|
|
|
std::list<Token*> links;
|
2009-05-01 12:07:10 +02:00
|
|
|
std::list<Token*> links2;
|
2009-09-20 22:13:06 +02:00
|
|
|
std::list<Token*> links3;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *token = _tokens; token; token = token->next())
|
2009-03-13 22:25:56 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (token->link())
|
2009-03-15 00:39:45 +01:00
|
|
|
{
|
|
|
|
token->link(0);
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (token->str() == "{")
|
2009-03-13 22:25:56 +01:00
|
|
|
{
|
|
|
|
links.push_back(token);
|
2009-11-20 20:10:42 +01:00
|
|
|
type.push_back(token);
|
2009-03-13 22:25:56 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (token->str() == "}")
|
2009-03-13 22:25:56 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (links.empty())
|
2009-03-13 22:25:56 +01:00
|
|
|
{
|
|
|
|
// Error, { and } don't match.
|
2009-05-07 22:17:29 +02:00
|
|
|
syntaxError(token, '{');
|
2009-03-15 00:39:45 +01:00
|
|
|
return false;
|
2009-03-13 22:25:56 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type.back()->str() != "{")
|
2009-11-20 20:10:42 +01:00
|
|
|
{
|
|
|
|
syntaxError(type.back(), type.back()->str()[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
type.pop_back();
|
2009-03-13 22:25:56 +01:00
|
|
|
|
2009-08-22 16:22:50 +02:00
|
|
|
Token::createMutualLinks(links.back(), token);
|
2009-03-13 22:25:56 +01:00
|
|
|
links.pop_back();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (token->str() == "(")
|
2009-05-01 12:07:10 +02:00
|
|
|
{
|
|
|
|
links2.push_back(token);
|
2009-11-20 20:10:42 +01:00
|
|
|
type.push_back(token);
|
2009-05-01 12:07:10 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (token->str() == ")")
|
2009-05-01 12:07:10 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (links2.empty())
|
2009-05-01 12:07:10 +02:00
|
|
|
{
|
|
|
|
// Error, ( and ) don't match.
|
2009-05-07 22:17:29 +02:00
|
|
|
syntaxError(token, '(');
|
2009-05-01 12:07:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type.back()->str() != "(")
|
2009-11-20 20:10:42 +01:00
|
|
|
{
|
|
|
|
syntaxError(type.back(), type.back()->str()[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
type.pop_back();
|
2009-05-01 12:07:10 +02:00
|
|
|
|
2009-08-22 16:22:50 +02:00
|
|
|
Token::createMutualLinks(links2.back(), token);
|
2009-05-01 12:07:10 +02:00
|
|
|
links2.pop_back();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (token->str() == "[")
|
2009-09-20 22:13:06 +02:00
|
|
|
{
|
|
|
|
links3.push_back(token);
|
2009-11-20 20:10:42 +01:00
|
|
|
type.push_back(token);
|
2009-09-20 22:13:06 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (token->str() == "]")
|
2009-09-20 22:13:06 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (links3.empty())
|
2009-09-20 22:13:06 +02:00
|
|
|
{
|
2009-09-22 18:36:12 +02:00
|
|
|
// Error, [ and ] don't match.
|
2009-09-20 22:13:06 +02:00
|
|
|
syntaxError(token, '[');
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type.back()->str() != "[")
|
2009-11-20 20:10:42 +01:00
|
|
|
{
|
|
|
|
syntaxError(type.back(), type.back()->str()[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
type.pop_back();
|
2009-09-20 22:13:06 +02:00
|
|
|
|
|
|
|
Token::createMutualLinks(links3.back(), token);
|
|
|
|
links3.pop_back();
|
|
|
|
}
|
2009-03-13 22:25:56 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!links.empty())
|
2009-05-07 22:17:29 +02:00
|
|
|
{
|
|
|
|
// Error, { and } don't match.
|
2009-11-03 21:21:28 +01:00
|
|
|
syntaxError(links.back(), '{');
|
2009-05-07 22:17:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!links2.empty())
|
2009-03-13 22:25:56 +01:00
|
|
|
{
|
2009-06-12 15:47:15 +02:00
|
|
|
// Error, ( and ) don't match.
|
2009-11-03 21:21:28 +01:00
|
|
|
syntaxError(links2.back(), '(');
|
2009-03-15 00:39:45 +01:00
|
|
|
return false;
|
2009-03-13 22:25:56 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!links3.empty())
|
2009-09-20 22:13:06 +02:00
|
|
|
{
|
|
|
|
// Error, [ and ] don't match.
|
2009-11-03 21:21:28 +01:00
|
|
|
syntaxError(links3.back(), '[');
|
2009-09-20 22:13:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-15 00:39:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
void Tokenizer::simplifySizeof()
|
2009-03-15 00:39:45 +01:00
|
|
|
{
|
2009-01-24 18:15:38 +01:00
|
|
|
// Fill the map _typeSize..
|
|
|
|
_typeSize.clear();
|
|
|
|
_typeSize["char"] = sizeof(char);
|
|
|
|
_typeSize["short"] = sizeof(short);
|
|
|
|
_typeSize["int"] = sizeof(int);
|
|
|
|
_typeSize["long"] = sizeof(long);
|
|
|
|
_typeSize["float"] = sizeof(float);
|
|
|
|
_typeSize["double"] = sizeof(double);
|
2009-11-12 22:31:13 +01:00
|
|
|
_typeSize["size_t"] = sizeof(size_t);
|
2009-05-31 21:33:44 +02:00
|
|
|
_typeSize["*"] = sizeof(void *);
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "class|struct %var%"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-02-24 17:30:57 +01:00
|
|
|
_typeSize[tok->strAt(1)] = 100;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
// Locate variable declarations and calculate the size
|
|
|
|
std::map<unsigned int, std::string> sizeOfVar;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-02-28 09:34:02 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->varId() != 0 && sizeOfVar.find(tok->varId()) == sizeOfVar.end())
|
2009-06-01 11:27:04 +02:00
|
|
|
{
|
2009-10-07 09:54:34 +02:00
|
|
|
const unsigned int varId = tok->varId();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->tokAt(-3), "[;{}(,] %type% * %var% [;,)]") ||
|
|
|
|
Token::Match(tok->tokAt(-4), "[;{}(,] const %type% * %var% [;),]") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "[;{}(,] %type% %var% [;),]") ||
|
|
|
|
Token::Match(tok->tokAt(-3), "[;{}(,] const %type% %var% [;),]"))
|
2009-02-28 09:34:02 +01:00
|
|
|
{
|
2009-10-10 21:54:58 +02:00
|
|
|
const unsigned int size = sizeOfType(tok->previous());
|
2010-04-02 07:30:58 +02:00
|
|
|
if (size == 0)
|
2009-10-10 14:26:03 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sizeOfVar[varId] = MathLib::toString<long>(size);
|
2009-06-01 11:27:04 +02:00
|
|
|
}
|
2009-10-07 09:54:34 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ %num% ] [;=]") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [;=]"))
|
2009-06-01 11:27:04 +02:00
|
|
|
{
|
2009-10-10 21:54:58 +02:00
|
|
|
unsigned int size = sizeOfType(tok->tokAt(-1));
|
2010-04-02 07:30:58 +02:00
|
|
|
if (size == 0)
|
2009-10-07 09:54:34 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
sizeOfVar[varId] = MathLib::toString<long>(size * MathLib::toLongNumber(tok->strAt(2)));
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ %num% ] [,)]") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [,)]"))
|
2009-10-07 11:05:44 +02:00
|
|
|
{
|
2010-01-06 20:19:27 +01:00
|
|
|
Token tempTok(0);
|
2009-10-07 11:05:44 +02:00
|
|
|
tempTok.str("*");
|
|
|
|
sizeOfVar[varId] = MathLib::toString<long>(sizeOfType(&tempTok));
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ ] = %str% ;"))
|
2009-10-07 09:54:34 +02:00
|
|
|
{
|
2009-10-10 21:54:58 +02:00
|
|
|
unsigned int size = sizeOfType(tok->tokAt(4));
|
2010-04-02 07:30:58 +02:00
|
|
|
if (size == 0)
|
2009-10-07 09:54:34 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
sizeOfVar[varId] = MathLib::toString<long>(size);
|
2009-02-28 09:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != "sizeof")
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
2010-05-14 18:40:15 +02:00
|
|
|
if (Token::simpleMatch(tok->next(), "sizeof"))
|
|
|
|
continue;
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
// sizeof "text"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "%str%"))
|
2009-10-07 09:54:34 +02:00
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << (Token::getStrLength(tok) + 1);
|
|
|
|
tok->str(ostr.str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sizeof ("text")
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "( %str% )"))
|
2009-10-07 09:54:34 +02:00
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << (Token::getStrLength(tok) + 1);
|
|
|
|
tok->str(ostr.str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-11-29 12:14:42 +01:00
|
|
|
// sizeof * (...) -> sizeof(*...)
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok->next(), "* (") && !Token::simpleMatch(tok->tokAt(2)->link(), ") ."))
|
2009-11-29 12:14:42 +01:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->next()->insertToken("*");
|
|
|
|
}
|
|
|
|
|
2009-11-29 17:25:35 +01:00
|
|
|
// sizeof a++ -> sizeof(a++)
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "++|-- %var% !!.") || Token::Match(tok->next(), "%var% ++|--"))
|
2009-11-29 17:25:35 +01:00
|
|
|
{
|
|
|
|
tok->insertToken("(");
|
|
|
|
tok->tokAt(3)->insertToken(")");
|
|
|
|
Token::createMutualLinks(tok->next(), tok->tokAt(4));
|
|
|
|
}
|
|
|
|
|
2009-12-06 08:46:59 +01:00
|
|
|
// sizeof 1 => sizeof ( 1 )
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->next()->isNumber())
|
2009-12-06 08:46:59 +01:00
|
|
|
{
|
|
|
|
Token *tok2 = tok->next();
|
|
|
|
tok->insertToken("(");
|
|
|
|
tok2->insertToken(")");
|
|
|
|
Token::createMutualLinks(tok->next(), tok2->next());
|
|
|
|
}
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
// sizeof int -> sizeof( int )
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->next()->str() != "(")
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
|
|
|
// Add parenthesis around the sizeof
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tempToken = tok->next(); tempToken; tempToken = tempToken->next())
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tempToken, "%var%"))
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tempToken->next()->str() == "[")
|
2009-09-20 22:38:32 +02:00
|
|
|
{
|
|
|
|
tempToken = tempToken->next()->link();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tempToken->next()->str() == ".")
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
|
|
|
// We are checking a class or struct, search next varname
|
|
|
|
tempToken = tempToken->tokAt(1);
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::simpleMatch(tempToken->next(), "- >"))
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
|
|
|
// We are checking a class or struct, search next varname
|
|
|
|
tempToken = tempToken->tokAt(2);
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tempToken->next(), "++|--"))
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
|
|
|
// We have variable++ or variable--, there should be
|
|
|
|
// nothing after this
|
|
|
|
tempToken = tempToken->tokAt(2);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::simpleMatch(tempToken->next(), ") ."))
|
2009-11-13 20:07:44 +01:00
|
|
|
{
|
|
|
|
tempToken = tempToken->tokAt(2);
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-27 21:47:00 +01:00
|
|
|
|
|
|
|
// Ok, we should be clean. Add ) after tempToken
|
|
|
|
tok->insertToken("(");
|
|
|
|
tempToken->insertToken(")");
|
2009-08-22 16:22:50 +02:00
|
|
|
Token::createMutualLinks(tok->next(), tempToken->next());
|
2009-01-27 21:47:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 12:01:45 +02:00
|
|
|
// sizeof(type *) => sizeof(*)
|
2010-04-13 22:17:27 +02:00
|
|
|
if (Token::Match(tok->next(), "( %type% * )"))
|
2009-06-01 12:01:45 +02:00
|
|
|
{
|
|
|
|
tok->next()->deleteNext();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "( * )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-10-07 09:54:34 +02:00
|
|
|
tok->str(MathLib::toString<long>(sizeOfType(tok->tokAt(2))));
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(4));
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
// sizeof( a )
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok->next(), "( %var% )") && tok->tokAt(2)->varId() != 0)
|
2009-06-05 04:50:06 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (sizeOfVar.find(tok->tokAt(2)->varId()) != sizeOfVar.end())
|
2009-10-07 09:54:34 +02:00
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->str(sizeOfVar[tok->varId()]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// don't try to replace size of variable if variable has
|
|
|
|
// similar name with type (#329)
|
|
|
|
}
|
2009-06-05 04:50:06 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "sizeof ( %type% )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-10-10 21:54:58 +02:00
|
|
|
unsigned int size = sizeOfType(tok->tokAt(2));
|
2010-04-02 07:30:58 +02:00
|
|
|
if (size > 0)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 13:44:12 +02:00
|
|
|
tok->str(MathLib::toString<long>(size));
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(4));
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "sizeof ( * %var% )") || Token::Match(tok, "sizeof ( %var% [ %num% ] )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-01-28 18:38:32 +01:00
|
|
|
// Some default value..
|
2009-10-21 21:08:00 +02:00
|
|
|
unsigned int sz = 0;
|
2009-01-28 18:38:32 +01:00
|
|
|
|
2009-02-02 19:59:32 +01:00
|
|
|
unsigned int varid = tok->tokAt((tok->tokAt(2)->str() == "*") ? 3 : 2)->varId();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (varid != 0)
|
2009-01-28 18:38:32 +01:00
|
|
|
{
|
2009-02-03 22:42:50 +01:00
|
|
|
// Try to locate variable declaration..
|
2009-10-21 21:08:00 +02:00
|
|
|
const Token *decltok = Token::findmatch(_tokens, "%varid%", varid);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(decltok->previous(), "%type% %var% ["))
|
2009-02-03 22:42:50 +01:00
|
|
|
{
|
2009-10-21 21:08:00 +02:00
|
|
|
sz = sizeOfType(decltok->previous());
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(decltok->previous(), "* %var% ["))
|
2009-10-21 21:08:00 +02:00
|
|
|
{
|
|
|
|
sz = sizeOfType(decltok->previous());
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(decltok->tokAt(-2), "%type% * %var%"))
|
2009-10-21 21:08:00 +02:00
|
|
|
{
|
|
|
|
sz = sizeOfType(decltok->tokAt(-2));
|
2009-02-03 22:42:50 +01:00
|
|
|
}
|
2009-01-28 18:38:32 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (sz > 0)
|
2009-03-29 16:36:34 +02:00
|
|
|
{
|
2009-08-22 13:44:12 +02:00
|
|
|
tok->str(MathLib::toString<long>(sz));
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
2009-03-29 16:36:34 +02:00
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
}
|
|
|
|
|
2009-11-10 22:10:56 +01:00
|
|
|
bool Tokenizer::simplifyTokenList()
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
2010-01-16 13:56:57 +01:00
|
|
|
// clear the _functionList so it can't contain dead pointers
|
|
|
|
_functionList.clear();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-11-05 21:30:05 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "* const"))
|
2009-11-05 21:30:05 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
|
2010-01-30 09:33:16 +01:00
|
|
|
|
|
|
|
// Replace NULL with 0..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-01-30 09:33:16 +01:00
|
|
|
{
|
2010-04-29 18:30:23 +02:00
|
|
|
if (tok->str() == "NULL" || tok->str() == "'\\0'" || tok->str() == "'\\x0'")
|
2010-01-30 09:33:16 +01:00
|
|
|
{
|
|
|
|
tok->str("0");
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->isNumber() &&
|
|
|
|
MathLib::isInt(tok->str()) &&
|
|
|
|
MathLib::toLongNumber(tok->str()) == 0)
|
2010-01-30 09:33:16 +01:00
|
|
|
{
|
|
|
|
tok->str("0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-31 16:29:12 +01:00
|
|
|
// simplify references
|
|
|
|
simplifyReference();
|
|
|
|
|
2010-01-12 21:25:31 +01:00
|
|
|
simplifyStd();
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
simplifyGoto();
|
|
|
|
|
|
|
|
// Combine wide strings
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok->str() == "L" && tok->next() && tok->next()->str()[0] == '"')
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
|
|
|
// Combine 'L "string"'
|
|
|
|
tok->str(tok->next()->str());
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine strings
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str()[0] != '"')
|
2009-09-27 16:04:10 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
tok->str(simplifyString(tok->str()));
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok->next() && tok->next()->str()[0] == '"')
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
2009-09-27 16:04:10 +02:00
|
|
|
tok->next()->str(simplifyString(tok->next()->str()));
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
// Two strings after each other, combine them
|
|
|
|
tok->concatStr(tok->next()->str());
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-12 22:54:47 +02:00
|
|
|
// Convert e.g. atol("0") into 0
|
|
|
|
simplifyMathFunctions();
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
// Convert + + into + and + - into -
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok->next())
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "+")
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->next()->str() == "+")
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->next()->str() == "-")
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
|
|
|
tok->str("-");
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "-")
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->next()->str() == "-")
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
|
|
|
tok->str("+");
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->next()->str() == "+")
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-04 22:33:41 +02:00
|
|
|
// 0[a] -> a[0]
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-10-04 22:33:41 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "%num% [ %var% ]"))
|
2009-10-04 22:33:41 +02:00
|
|
|
{
|
|
|
|
const std::string temp = tok->str();
|
|
|
|
tok->str(tok->tokAt(2)->str());
|
|
|
|
tok->tokAt(2)->str(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
simplifySizeof();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-11-07 08:25:39 +01:00
|
|
|
// replace strlen(str)
|
|
|
|
simplifyKnownVariables();
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-11-07 08:25:39 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "strlen ( %str% )"))
|
2009-11-07 08:25:39 +01:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << Token::getStrLength(tok->tokAt(2));
|
|
|
|
tok->str(ostr.str());
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-07 10:08:53 +02:00
|
|
|
// change array to pointer..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-10-07 10:08:53 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "%type% %var% [ ] [,;=]"))
|
2009-10-07 10:08:53 +02:00
|
|
|
{
|
|
|
|
Token::eraseTokens(tok->next(), tok->tokAt(4));
|
|
|
|
tok->insertToken("*");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-14 22:33:28 +01:00
|
|
|
// Replace constants..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-02-14 22:33:28 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "const %type% %var% = %num% ;"))
|
2009-02-14 22:33:28 +01:00
|
|
|
{
|
2009-06-19 18:03:09 +02:00
|
|
|
unsigned int varId = tok->tokAt(2)->varId();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (varId == 0)
|
2009-08-01 15:57:54 +02:00
|
|
|
{
|
|
|
|
tok = tok->tokAt(5);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-02-14 19:58:17 +01:00
|
|
|
const std::string num = tok->strAt(4);
|
2009-02-14 22:33:28 +01:00
|
|
|
int indent = 1;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next())
|
2009-02-14 22:33:28 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "{")
|
2009-02-14 22:33:28 +01:00
|
|
|
{
|
|
|
|
++indent;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "}")
|
2009-02-14 22:33:28 +01:00
|
|
|
{
|
|
|
|
--indent;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indent == 0)
|
2009-02-14 22:33:28 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-02-14 22:33:28 +01:00
|
|
|
// Compare constants, but don't touch members of other structures
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->varId() == varId)
|
2009-02-14 22:33:28 +01:00
|
|
|
{
|
|
|
|
tok2->str(num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-11-17 19:03:23 +01:00
|
|
|
simplifyCasts();
|
2009-03-05 20:32:02 +01:00
|
|
|
|
2010-02-27 19:41:14 +01:00
|
|
|
// simplify "x=realloc(y,0);" => "free(y); x=0;"..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-02-27 19:41:14 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "; %var% = realloc ( %var% , 0 ) ;"))
|
2010-02-27 19:41:14 +01:00
|
|
|
{
|
|
|
|
const std::string varname(tok->next()->str());
|
|
|
|
const unsigned int varid(tok->next()->varId());
|
|
|
|
|
|
|
|
// Delete the "%var% ="
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
|
|
|
|
// Change function name "realloc" to "free"
|
|
|
|
tok->next()->str("free");
|
|
|
|
|
|
|
|
// delete the ", 0"
|
|
|
|
Token::eraseTokens(tok->tokAt(3), tok->tokAt(6));
|
|
|
|
|
|
|
|
// goto the ";"
|
|
|
|
tok = tok->tokAt(5);
|
|
|
|
|
|
|
|
// insert "var=0;"
|
|
|
|
tok->insertToken(";");
|
|
|
|
tok->insertToken("0");
|
|
|
|
tok->insertToken("=");
|
|
|
|
tok->insertToken(varname);
|
|
|
|
tok->next()->varId(varid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 07:24:03 +01:00
|
|
|
// Simplify simple calculations..
|
2010-04-02 07:30:58 +02:00
|
|
|
while (simplifyCalculations())
|
2009-03-04 07:24:03 +01:00
|
|
|
;
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Replace "*(str + num)" => "str[num]"
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! strchr(";{}(=<>", tok->str()[0]))
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Token *next = tok->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! next)
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(next, "* ( %var% + %num% )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-02-14 19:58:17 +01:00
|
|
|
// var
|
|
|
|
tok = tok->next();
|
|
|
|
tok->str(tok->strAt(2));
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-02-14 19:58:17 +01:00
|
|
|
// [
|
|
|
|
tok = tok->next();
|
|
|
|
tok->str("[");
|
|
|
|
|
|
|
|
// num
|
|
|
|
tok = tok->next();
|
|
|
|
tok->str(tok->strAt(2));
|
|
|
|
|
|
|
|
// ]
|
|
|
|
tok = tok->next();
|
|
|
|
tok->str("]");
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
2009-11-11 19:16:05 +01:00
|
|
|
|
|
|
|
Token::createMutualLinks(next->tokAt(1), next->tokAt(3));
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-30 09:33:16 +01:00
|
|
|
// Change initialisation of variable to assignment
|
|
|
|
simplifyInitVar();
|
|
|
|
|
|
|
|
// Simplify variable declarations
|
|
|
|
simplifyVarDecl();
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
simplifyFunctionParameters();
|
2009-02-17 21:03:08 +01:00
|
|
|
elseif();
|
2010-05-30 13:15:15 +02:00
|
|
|
simplifyErrNoInWhile();
|
2009-11-04 23:58:15 +01:00
|
|
|
simplifyIfAssign();
|
2009-10-05 13:10:09 +02:00
|
|
|
simplifyRedundantParanthesis();
|
2009-03-24 18:23:21 +01:00
|
|
|
simplifyIfNot();
|
2009-07-25 13:11:29 +02:00
|
|
|
simplifyIfNotNull();
|
2010-01-22 20:20:43 +01:00
|
|
|
simplifyIfSameInnerCondition();
|
2009-11-04 23:58:15 +01:00
|
|
|
simplifyComparisonOrder();
|
2009-09-05 18:46:27 +02:00
|
|
|
simplifyNestedStrcat();
|
2009-12-28 08:37:34 +01:00
|
|
|
simplifyWhile0();
|
2010-03-06 15:28:52 +01:00
|
|
|
simplifyFuncInWhile();
|
2009-09-05 18:46:27 +02:00
|
|
|
|
2010-02-26 22:14:56 +01:00
|
|
|
simplifyIfAssign(); // could be affected by simplifyIfNot
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "case %any% : %var%"))
|
2009-06-14 08:20:51 +02:00
|
|
|
tok->tokAt(2)->insertToken(";");
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "default : %var%"))
|
2009-01-24 18:15:38 +01:00
|
|
|
tok->next()->insertToken(";");
|
|
|
|
}
|
|
|
|
|
2009-03-23 18:20:56 +01:00
|
|
|
// In case variable declarations have been updated...
|
|
|
|
setVarId();
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
bool modified = true;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (modified)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
modified = false;
|
|
|
|
modified |= simplifyConditions();
|
|
|
|
modified |= simplifyFunctionReturn();
|
|
|
|
modified |= simplifyKnownVariables();
|
|
|
|
modified |= removeReduntantConditions();
|
2009-01-26 17:38:08 +01:00
|
|
|
modified |= simplifyRedundantParanthesis();
|
2009-06-05 23:33:13 +02:00
|
|
|
modified |= simplifyQuestionMark();
|
2009-10-05 16:34:38 +02:00
|
|
|
modified |= simplifyCalculations();
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
2009-03-15 00:39:45 +01:00
|
|
|
|
2009-07-24 21:55:35 +02:00
|
|
|
// Remove redundant parantheses in return..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-07-24 21:55:35 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::simpleMatch(tok, "return ("))
|
2009-07-24 21:55:35 +02:00
|
|
|
{
|
2009-08-30 10:27:26 +02:00
|
|
|
Token *tok2 = tok->next()->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok2, ") ;"))
|
2009-07-24 21:55:35 +02:00
|
|
|
{
|
2009-08-30 10:27:26 +02:00
|
|
|
tok->deleteNext();
|
|
|
|
tok2->deleteThis();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
2009-07-24 21:55:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-31 09:33:57 +01:00
|
|
|
removeRedundantAssignment();
|
|
|
|
|
2009-07-05 20:29:09 +02:00
|
|
|
simplifyComma();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_settings && _settings->_debug)
|
2009-03-16 22:31:52 +01:00
|
|
|
{
|
2009-11-27 23:04:04 +01:00
|
|
|
_tokens->printOut(0, _files);
|
2009-03-16 22:31:52 +01:00
|
|
|
}
|
2009-11-10 22:10:56 +01:00
|
|
|
|
|
|
|
return validate();
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2010-01-31 09:33:57 +01:00
|
|
|
void Tokenizer::removeRedundantAssignment()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{")
|
2010-01-31 09:33:57 +01:00
|
|
|
tok = tok->link();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, ") const| {"))
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
|
|
|
// parse in this function..
|
|
|
|
std::set<unsigned int> localvars;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->next()->str() == "const")
|
2010-01-31 09:33:57 +01:00
|
|
|
tok = tok->next();
|
|
|
|
const Token * const end = tok->next()->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok->next(); tok2 && tok2 != end; tok2 = tok2->next())
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "[;{}] %type% * %var% ;"))
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(3);
|
|
|
|
localvars.insert(tok2->varId());
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "[;{}] %type% %var% ;") && tok2->next()->isStandardType())
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
localvars.insert(tok2->varId());
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->varId() &&
|
|
|
|
!Token::Match(tok2->previous(), "[;{}] %var% = %var% ;") &&
|
|
|
|
!Token::Match(tok2->previous(), "[;{}] %var% = %num% ;"))
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
|
|
|
localvars.erase(tok2->varId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
localvars.erase(0);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!localvars.empty())
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok->next(); tok2 && tok2 != end; tok2 = tok2->next())
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "[;{}] %type% %var% ;") && localvars.find(tok2->tokAt(2)->varId()) != localvars.end())
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(3));
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "[;{}] %type% * %var% ;") && localvars.find(tok2->tokAt(3)->varId()) != localvars.end())
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "[;{}] %var% = %any% ;") && localvars.find(tok2->next()->varId()) != localvars.end())
|
2010-01-31 09:33:57 +01:00
|
|
|
{
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
bool Tokenizer::removeReduntantConditions()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != "if")
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(tok->tokAt(1), "( %bool% ) {"))
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Find matching else
|
|
|
|
const Token *elseTag = 0;
|
|
|
|
|
|
|
|
// Find the closing "}"
|
2009-10-05 14:06:50 +02:00
|
|
|
elseTag = tok->tokAt(4)->link()->next();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
bool boolValue = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(2)->str() == "true")
|
2009-01-24 18:15:38 +01:00
|
|
|
boolValue = true;
|
|
|
|
|
|
|
|
// Handle if with else
|
2010-04-02 07:30:58 +02:00
|
|
|
if (elseTag && elseTag->str() == "else")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(elseTag->next(), "if ("))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Handle "else if"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (boolValue == false)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Convert "if( false ) {aaa;} else if() {bbb;}" => "if() {bbb;}"
|
|
|
|
Token::eraseTokens(tok, elseTag->tokAt(2));
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Keep first if, remove every else if and else after it
|
|
|
|
const Token *lastTagInIf = elseTag->tokAt(2);
|
2010-04-02 07:30:58 +02:00
|
|
|
while (lastTagInIf)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (lastTagInIf->str() == "(")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-10-05 14:06:50 +02:00
|
|
|
lastTagInIf = lastTagInIf->link()->next();
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-10-05 14:06:50 +02:00
|
|
|
lastTagInIf = lastTagInIf->link()->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::simpleMatch(lastTagInIf, "else"))
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
lastTagInIf = lastTagInIf->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (lastTagInIf->str() == "if")
|
2009-01-24 18:15:38 +01:00
|
|
|
lastTagInIf = lastTagInIf->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::eraseTokens(elseTag->previous(), lastTagInIf);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Handle else
|
2010-04-02 07:30:58 +02:00
|
|
|
if (boolValue == false)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Convert "if( false ) {aaa;} else {bbb;}" => "{bbb;}" or ";{bbb;}"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous())
|
2009-01-24 18:15:38 +01:00
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, elseTag->tokAt(1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (elseTag->tokAt(1)->str() == "{")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}"
|
2009-10-05 14:06:50 +02:00
|
|
|
const Token *end = elseTag->tokAt(1)->link();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Remove the "else { aaa; }"
|
|
|
|
Token::eraseTokens(elseTag->previous(), end->tokAt(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove "if( true )"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous())
|
2009-01-24 18:15:38 +01:00
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle if without else
|
|
|
|
else
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (boolValue == false)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Remove if and its content
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous())
|
2009-01-24 18:15:38 +01:00
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, elseTag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// convert "if( true ) {aaa;}" => "{aaa;}"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous())
|
2009-01-24 18:15:38 +01:00
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-02-20 18:13:09 +01:00
|
|
|
|
2009-08-29 21:25:15 +02:00
|
|
|
void Tokenizer::simplifyIfAddBraces()
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok ? tok->next() : NULL)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous() && !Token::Match(tok->previous(), ";|{|}|else|)|:"))
|
2009-12-12 19:58:19 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "if|for|while ("))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 09:49:45 +02:00
|
|
|
// don't add "{}" around ";" in "do {} while();" (#609)
|
|
|
|
const Token *prev = tok->previous();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(prev, "} while") &&
|
|
|
|
prev->link() &&
|
|
|
|
prev->link()->previous() &&
|
|
|
|
prev->link()->previous()->str() == "do")
|
2009-08-22 09:49:45 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Goto the ending ')'
|
2009-08-22 09:54:28 +02:00
|
|
|
tok = tok->next()->link();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// ')' should be followed by '{'
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok || Token::simpleMatch(tok, ") {"))
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "else")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// An else followed by an if or brace don't need to be processed further
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "else if|{"))
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-05-09 22:12:14 +02:00
|
|
|
// If there is no code after he if(), abort
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->next())
|
2009-08-29 21:25:15 +02:00
|
|
|
return;
|
2009-05-09 22:12:14 +02:00
|
|
|
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// insert open brace..
|
|
|
|
tok->insertToken("{");
|
|
|
|
tok = tok->next();
|
2009-03-15 13:44:57 +01:00
|
|
|
Token *tempToken = tok;
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-02-20 11:43:53 +01:00
|
|
|
bool innerIf = Token::simpleMatch(tempToken->next(), "if");
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tempToken->next(), "do {"))
|
2010-02-20 13:24:50 +01:00
|
|
|
tempToken = tempToken->tokAt(2)->link();
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// insert close brace..
|
|
|
|
// In most cases it would work to just search for the next ';' and insert a closing brace after it.
|
|
|
|
// But here are special cases..
|
|
|
|
// * if (cond) for (;;) break;
|
|
|
|
// * if (cond1) if (cond2) { }
|
2010-02-20 11:43:53 +01:00
|
|
|
// * if (cond1) if (cond2) ; else ;
|
2009-01-24 18:15:38 +01:00
|
|
|
int parlevel = 0;
|
|
|
|
int indentlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((tempToken = tempToken->next()) != NULL)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tempToken->str() == "{")
|
2009-01-24 18:15:38 +01:00
|
|
|
++indentlevel;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tempToken->str() == "}")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
--indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel == 0 && parlevel == 0)
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
2009-11-20 20:38:30 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (indentlevel < 0 && parlevel == 0)
|
2009-11-20 20:38:30 +01:00
|
|
|
{
|
|
|
|
// insert closing brace before this
|
|
|
|
tempToken = tempToken->previous();
|
|
|
|
break;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tempToken->str() == "(")
|
2009-01-24 18:15:38 +01:00
|
|
|
++parlevel;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tempToken->str() == ")")
|
2010-02-07 15:00:19 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (parlevel == 0)
|
2010-02-07 15:00:19 +01:00
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
tempToken = 0;
|
|
|
|
break;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
--parlevel;
|
2010-02-07 15:00:19 +01:00
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (indentlevel == 0 && parlevel == 0 && tempToken->str() == ";")
|
2010-02-20 11:43:53 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!innerIf)
|
2010-02-20 11:43:53 +01:00
|
|
|
break;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tempToken, "; else if"))
|
2010-02-20 11:43:53 +01:00
|
|
|
;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tempToken, "; else"))
|
2010-02-20 11:43:53 +01:00
|
|
|
innerIf = false;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tempToken)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-03-15 13:44:57 +01:00
|
|
|
tempToken->insertToken("}");
|
2009-08-23 02:38:00 +02:00
|
|
|
Token::createMutualLinks(tok, tempToken->next());
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-03 23:06:03 +01:00
|
|
|
bool Tokenizer::simplifyDoWhileAddBracesHelper(Token *tok)
|
2009-08-22 12:42:19 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "[),]"))
|
2009-08-22 12:42:19 +02:00
|
|
|
{
|
2009-12-03 23:06:03 +01:00
|
|
|
// fix for #988
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *tok1 = tok; // token with "do"
|
|
|
|
Token *tok2 = NULL; // token with "while"
|
|
|
|
Token *tok3 = tok->next();
|
|
|
|
|
|
|
|
// skip loop body
|
|
|
|
bool result = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok3)
|
2009-12-03 23:06:03 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->str() == "{")
|
2009-08-22 12:42:19 +02:00
|
|
|
{
|
2009-12-03 23:06:03 +01:00
|
|
|
tok3 = tok3->link();
|
2009-08-22 12:42:19 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "while")
|
2009-11-21 15:44:15 +01:00
|
|
|
{
|
2009-12-03 23:06:03 +01:00
|
|
|
tok2 = tok3;
|
|
|
|
break;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok3, "do {"))
|
2009-12-03 23:06:03 +01:00
|
|
|
{
|
|
|
|
// Skip do{}while inside the current "do"
|
|
|
|
tok3 = tok3->next()->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok3->next(), "while"))
|
2009-12-03 23:06:03 +01:00
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok3, "do !!{") &&
|
|
|
|
!Token::Match(tok3->next(), "[),]"))
|
2009-12-03 23:06:03 +01:00
|
|
|
{
|
|
|
|
// Handle do-while inside the current "do"
|
|
|
|
// first and return true to get the outer
|
|
|
|
// "do" to be handled later.
|
|
|
|
tok1 = tok3;
|
|
|
|
result = true;
|
2009-11-21 15:44:15 +01:00
|
|
|
}
|
|
|
|
|
2009-12-03 23:06:03 +01:00
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
2009-08-22 12:42:19 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2)
|
2009-12-03 23:06:03 +01:00
|
|
|
{
|
|
|
|
// insert "{" after "do"
|
|
|
|
tok1->insertToken("{");
|
2009-08-22 12:42:19 +02:00
|
|
|
|
2009-12-03 23:06:03 +01:00
|
|
|
// insert "}" before "while"
|
|
|
|
tok2->previous()->insertToken("}");
|
2009-08-22 12:42:19 +02:00
|
|
|
|
2009-12-03 23:06:03 +01:00
|
|
|
Token::createMutualLinks(tok1->next(), tok2->previous());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = false;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2009-08-22 12:42:19 +02:00
|
|
|
|
2009-12-03 23:06:03 +01:00
|
|
|
void Tokenizer::simplifyDoWhileAddBraces()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-12-03 23:06:03 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "do !!{"))
|
2009-12-03 23:06:03 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (simplifyDoWhileAddBracesHelper(tok))
|
2009-12-03 23:06:03 +01:00
|
|
|
{
|
|
|
|
// Call until the function returns false to
|
|
|
|
// handle do-while inside do-while
|
2009-08-22 12:42:19 +02:00
|
|
|
|
2009-12-03 23:06:03 +01:00
|
|
|
}
|
2009-08-22 12:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-29 21:30:13 +02:00
|
|
|
void Tokenizer::simplifyConditionOperator()
|
2009-07-14 12:06:38 +02:00
|
|
|
{
|
|
|
|
int parlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-07-14 12:06:38 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "(")
|
2009-07-14 12:06:38 +02:00
|
|
|
++parlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == ")")
|
2009-07-14 12:06:38 +02:00
|
|
|
--parlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (parlevel == 0 && Token::Match(tok, "; %var% = %var% ? %var% : %var% ;"))
|
2009-07-14 12:06:38 +02:00
|
|
|
{
|
|
|
|
const std::string var(tok->strAt(1));
|
|
|
|
const std::string condition(tok->strAt(3));
|
|
|
|
const std::string value1(tok->strAt(5));
|
|
|
|
const std::string value2(tok->strAt(7));
|
|
|
|
|
2009-09-29 22:27:10 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(9));
|
2009-07-14 12:06:38 +02:00
|
|
|
|
2009-09-29 22:27:10 +02:00
|
|
|
std::string str("if ( " + condition + " ) { " + var + " = " + value1 + " ; } else { " + var + " = " + value2 + " ; }");
|
2009-07-14 12:06:38 +02:00
|
|
|
std::string::size_type pos1 = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (pos1 != std::string::npos)
|
2009-07-14 12:06:38 +02:00
|
|
|
{
|
|
|
|
std::string::size_type pos2 = str.find(" ", pos1);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos2 == std::string::npos)
|
2009-07-14 12:06:38 +02:00
|
|
|
{
|
|
|
|
tok->insertToken(str.substr(pos1).c_str());
|
|
|
|
pos1 = pos2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-16 07:20:29 +02:00
|
|
|
tok->insertToken(str.substr(pos1, pos2 - pos1).c_str());
|
2009-07-14 12:06:38 +02:00
|
|
|
pos1 = pos2 + 1;
|
|
|
|
}
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
2009-08-23 02:38:00 +02:00
|
|
|
|
2009-09-29 22:27:10 +02:00
|
|
|
Token::createMutualLinks(tok->tokAt(-15), tok->tokAt(-13));
|
|
|
|
Token::createMutualLinks(tok->tokAt(-12), tok->tokAt(-7));
|
|
|
|
Token::createMutualLinks(tok->tokAt(-5), tok);
|
2009-07-14 12:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
bool Tokenizer::simplifyConditions()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "! %num%") || Token::Match(tok, "! %bool%"))
|
2009-06-03 21:02:16 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->next()->str() == "0" || tok->next()->str() == "false")
|
2009-06-03 21:02:16 +02:00
|
|
|
tok->str("true");
|
|
|
|
else
|
|
|
|
tok->str("false");
|
|
|
|
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "( true &&") ||
|
|
|
|
Token::simpleMatch(tok, "&& true &&") ||
|
|
|
|
Token::simpleMatch(tok->next(), "&& true )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-01-24 18:15:38 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::simpleMatch(tok, "( false ||") ||
|
|
|
|
Token::simpleMatch(tok, "|| false ||") ||
|
|
|
|
Token::simpleMatch(tok->next(), "|| false )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-01-24 18:15:38 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change numeric constant in condition to "true" or "false"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "if|while ( %num%") &&
|
|
|
|
(tok->tokAt(3)->str() == ")" || tok->tokAt(3)->str() == "||" || tok->tokAt(3)->str() == "&&"))
|
2009-02-07 11:54:39 +01:00
|
|
|
{
|
2009-06-14 08:20:51 +02:00
|
|
|
tok->tokAt(2)->str((tok->tokAt(2)->str() != "0") ? "true" : "false");
|
2009-02-07 11:54:39 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
Token *tok2 = tok->tokAt(2);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2 &&
|
|
|
|
(tok->str() == "&&" || tok->str() == "||") &&
|
|
|
|
Token::Match(tok->next(), "%num%") &&
|
|
|
|
(tok2->str() == ")" || tok2->str() == "&&" || tok2->str() == "||"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-02-07 20:15:10 +01:00
|
|
|
tok->next()->str((tok->next()->str() != "0") ? "true" : "false");
|
2009-01-24 18:15:38 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "(%num% == %num%)" => "(true)"/"(false)"
|
|
|
|
const Token *tok4 = tok->tokAt(4);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! tok4)
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((tok->str() == "&&" || tok->str() == "||" || tok->str() == "(") &&
|
|
|
|
(Token::Match(tok->tokAt(1), "%num% %any% %num%") ||
|
|
|
|
Token::Match(tok->tokAt(1), "%bool% %any% %bool%")) &&
|
|
|
|
(tok4->str() == "&&" || tok4->str() == "||" || tok4->str() == ")" || tok4->str() == "?"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
std::string cmp = tok->strAt(2);
|
|
|
|
bool result = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->tokAt(1), "%num%"))
|
2009-05-29 23:04:01 +02:00
|
|
|
{
|
|
|
|
// Compare numbers
|
|
|
|
|
2010-05-21 12:28:13 +02:00
|
|
|
if (cmp == "==" || cmp == "!=")
|
|
|
|
{
|
|
|
|
const std::string op1(tok->strAt(1));
|
|
|
|
const std::string op2(tok->strAt(3));
|
|
|
|
|
|
|
|
bool eq = false;
|
|
|
|
if (MathLib::isInt(op1) && MathLib::isInt(op2))
|
|
|
|
eq = (MathLib::toLongNumber(op1) == MathLib::toLongNumber(op2));
|
|
|
|
else
|
|
|
|
eq = (op1 == op2);
|
|
|
|
|
|
|
|
if (cmp == "==")
|
|
|
|
result = eq;
|
|
|
|
else
|
|
|
|
result = !eq;
|
|
|
|
}
|
2009-05-29 23:04:01 +02:00
|
|
|
else
|
2010-05-21 12:28:13 +02:00
|
|
|
{
|
|
|
|
double op1 = MathLib::toDoubleNumber(tok->strAt(1));
|
|
|
|
double op2 = MathLib::toDoubleNumber(tok->strAt(3));
|
|
|
|
if (cmp == ">=")
|
|
|
|
result = (op1 >= op2);
|
|
|
|
else if (cmp == ">")
|
|
|
|
result = (op1 > op2);
|
|
|
|
else if (cmp == "<=")
|
|
|
|
result = (op1 <= op2);
|
|
|
|
else if (cmp == "<")
|
|
|
|
result = (op1 < op2);
|
|
|
|
else
|
|
|
|
cmp = "";
|
|
|
|
}
|
2009-05-29 23:04:01 +02:00
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
else
|
2009-05-29 23:04:01 +02:00
|
|
|
{
|
|
|
|
// Compare boolean
|
|
|
|
bool op1 = (tok->strAt(1) == std::string("true"));
|
|
|
|
bool op2 = (tok->strAt(3) == std::string("true"));
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (cmp == "==")
|
2009-05-29 23:04:01 +02:00
|
|
|
result = (op1 == op2);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (cmp == "!=")
|
2009-05-29 23:04:01 +02:00
|
|
|
result = (op1 != op2);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (cmp == ">=")
|
2009-05-29 23:04:01 +02:00
|
|
|
result = (op1 >= op2);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (cmp == ">")
|
2009-05-29 23:04:01 +02:00
|
|
|
result = (op1 > op2);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (cmp == "<=")
|
2009-05-29 23:04:01 +02:00
|
|
|
result = (op1 <= op2);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (cmp == "<")
|
2009-05-29 23:04:01 +02:00
|
|
|
result = (op1 < op2);
|
|
|
|
else
|
|
|
|
cmp = "";
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! cmp.empty())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
|
|
|
|
tok->str(result ? "true" : "false");
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-06-05 23:33:13 +02:00
|
|
|
bool Tokenizer::simplifyQuestionMark()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-06-05 23:33:13 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != "?")
|
2009-06-05 23:33:13 +02:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->tokAt(-2))
|
2009-06-05 23:33:13 +02:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(tok->tokAt(-2), "[=,(]"))
|
2009-06-05 23:33:13 +02:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(tok->previous(), "%bool%") &&
|
|
|
|
!Token::Match(tok->previous(), "%num%"))
|
2009-06-05 23:33:13 +02:00
|
|
|
continue;
|
|
|
|
|
2009-07-23 10:12:02 +02:00
|
|
|
// Find the ":" token..
|
|
|
|
Token *semicolon = 0;
|
2009-06-05 23:33:13 +02:00
|
|
|
{
|
2009-07-22 13:29:42 +02:00
|
|
|
unsigned int parlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-07-22 13:29:42 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(")
|
2009-07-22 13:29:42 +02:00
|
|
|
++parlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == ")")
|
2009-07-22 13:29:42 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (parlevel == 0)
|
2009-07-22 13:29:42 +02:00
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (parlevel == 0 && tok2->str() == ":")
|
2009-07-22 13:29:42 +02:00
|
|
|
{
|
2009-07-23 10:12:02 +02:00
|
|
|
semicolon = tok2;
|
2009-07-22 13:29:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-23 10:12:02 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!semicolon || !semicolon->next())
|
2009-07-23 10:12:02 +02:00
|
|
|
continue;
|
2009-07-22 13:29:42 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous()->str() == "false" ||
|
|
|
|
tok->previous()->str() == "0")
|
2009-07-23 10:12:02 +02:00
|
|
|
{
|
|
|
|
// Use code after semicolon, remove code before it.
|
|
|
|
semicolon = semicolon->next();
|
2009-06-14 08:26:20 +02:00
|
|
|
tok = tok->tokAt(-2);
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, semicolon);
|
2009-06-05 23:33:13 +02:00
|
|
|
|
|
|
|
tok = tok->next();
|
2009-06-12 23:17:06 +02:00
|
|
|
ret = true;
|
2009-06-05 23:33:13 +02:00
|
|
|
}
|
2009-07-23 10:12:02 +02:00
|
|
|
|
|
|
|
// The condition is true. Delete the operator after the ":"..
|
2009-10-05 16:34:38 +02:00
|
|
|
else
|
2009-06-05 23:33:13 +02:00
|
|
|
{
|
2010-02-07 09:14:59 +01:00
|
|
|
const Token *end = 0;
|
|
|
|
|
|
|
|
// check the operator after the :
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(semicolon, ": ("))
|
2010-02-07 09:14:59 +01:00
|
|
|
{
|
|
|
|
end = semicolon->next()->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(end, ") !!."))
|
2010-02-07 09:14:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-07-23 10:12:02 +02:00
|
|
|
// delete the condition token and the "?"
|
|
|
|
tok = tok->tokAt(-2);
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2010-02-07 09:14:59 +01:00
|
|
|
|
|
|
|
// delete operator after the :
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end)
|
2010-02-07 09:14:59 +01:00
|
|
|
{
|
|
|
|
Token::eraseTokens(semicolon->previous(), end->next());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-10-05 16:34:38 +02:00
|
|
|
int ind = 0;
|
2010-04-11 13:49:50 +02:00
|
|
|
for (const Token *endTok = semicolon; endTok; endTok = endTok->next())
|
2009-10-05 16:34:38 +02:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
if (endTok->str() == ";")
|
2009-10-05 16:34:38 +02:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
Token::eraseTokens(semicolon->previous(), endTok->next());
|
2009-10-05 16:34:38 +02:00
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-23 10:12:02 +02:00
|
|
|
|
2010-04-11 13:49:50 +02:00
|
|
|
else if (Token::Match(endTok, "[({[]"))
|
2009-10-05 16:34:38 +02:00
|
|
|
{
|
|
|
|
++ind;
|
|
|
|
}
|
|
|
|
|
2010-04-11 13:49:50 +02:00
|
|
|
else if (Token::Match(endTok, "[)}]]"))
|
2009-10-05 16:34:38 +02:00
|
|
|
{
|
|
|
|
--ind;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ind < 0)
|
2009-10-05 16:34:38 +02:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
Token::eraseTokens(semicolon->previous(), endTok);
|
2009-10-05 16:34:38 +02:00
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-05 23:33:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-08-29 21:06:14 +02:00
|
|
|
void Tokenizer::simplifyCasts()
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok->next(), "( %type% *| ) *|&| %var%") ||
|
|
|
|
Token::Match(tok->next(), "( %type% %type% *| ) *|&| %var%"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->isName() && tok->str() != "return")
|
2009-10-04 23:12:12 +02:00
|
|
|
break;
|
2009-03-10 21:26:08 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok->previous(), "operator"))
|
2009-10-29 20:54:51 +01:00
|
|
|
break;
|
|
|
|
|
2009-03-10 21:26:08 +01:00
|
|
|
// Remove cast..
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
2009-10-04 23:12:12 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ")" && tok->link()->previous())
|
2009-10-04 23:12:12 +02:00
|
|
|
{
|
|
|
|
// If there was another cast before this, go back
|
|
|
|
// there to check it also. e.g. "(int)(char)x"
|
|
|
|
tok = tok->link()->previous();
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2010-04-13 21:57:52 +02:00
|
|
|
// Replace pointer casts of 0.. "(char *)0" => "0"
|
|
|
|
while (Token::Match(tok->next(), "( %type% * ) 0") ||
|
|
|
|
Token::Match(tok->next(), "( %type% %type% * ) 0"))
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
|
|
|
if (tok->str() == ")" && tok->link()->previous())
|
|
|
|
{
|
|
|
|
// If there was another cast before this, go back
|
|
|
|
// there to check it also. e.g. "(char*)(char*)0"
|
|
|
|
tok = tok->link()->previous();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-06-19 19:25:56 +02:00
|
|
|
Token *tok2 = tok->next();
|
2009-05-27 20:06:19 +02:00
|
|
|
unsigned int level = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2)
|
2009-05-27 20:06:19 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "<")
|
2009-05-27 20:06:19 +02:00
|
|
|
++level;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == ">")
|
2009-05-27 20:06:19 +02:00
|
|
|
{
|
|
|
|
--level;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (level == 0)
|
2009-05-27 20:06:19 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok2, "> ("))
|
2009-06-19 19:25:56 +02:00
|
|
|
{
|
2009-09-13 12:31:32 +02:00
|
|
|
Token *closeBracket = tok2->next()->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (closeBracket)
|
2009-06-19 19:25:56 +02:00
|
|
|
{
|
2009-09-13 12:31:32 +02:00
|
|
|
Token::eraseTokens(tok, tok2->tokAt(2));
|
|
|
|
closeBracket->deleteThis();
|
2009-06-19 19:25:56 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-29 21:37:15 +02:00
|
|
|
void Tokenizer::simplifyFunctionParameters()
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{" || tok->str() == "[" || tok->str() == "(")
|
2009-12-30 15:12:38 +01:00
|
|
|
{
|
|
|
|
tok = tok->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-12-30 15:12:38 +01:00
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-26 23:26:50 +01:00
|
|
|
|
|
|
|
// Find the function e.g. foo( x ) or foo( x, y )
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "%var% ( %var% [,)]"))
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
|
|
|
// We have found old style function, now we need to change it
|
|
|
|
|
2010-01-21 20:05:32 +01:00
|
|
|
// backup pointer to the '(' token
|
|
|
|
Token * const tok1 = tok->next();
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
// Get list of argument names
|
|
|
|
std::map<std::string, Token*> argumentNames;
|
|
|
|
bool bailOut = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (tok = tok->tokAt(2); tok; tok = tok->tokAt(2))
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(tok, "%var% [,)]"))
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
|
|
|
bailOut = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (argumentNames.find(tok->str()) != argumentNames.end())
|
2009-09-05 21:21:25 +02:00
|
|
|
{
|
|
|
|
// Invalid code, two arguments with the same name.
|
|
|
|
// TODO, print error perhaps?
|
|
|
|
bailOut = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
argumentNames[tok->str()] = tok;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->next()->str() == ")")
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (bailOut)
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
2010-01-21 20:05:32 +01:00
|
|
|
tok = tok1->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2010-01-21 20:05:32 +01:00
|
|
|
return;
|
2009-01-26 23:26:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *start = tok;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok && tok->str() != "{")
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ";")
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
|
|
|
tok = tok->previous();
|
|
|
|
// Move tokens from start to tok into the place of
|
|
|
|
// argumentNames[tok->str()] and remove the ";"
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (argumentNames.find(tok->str()) == argumentNames.end())
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
|
|
|
bailOut = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the following ";"
|
|
|
|
Token *temp = tok->tokAt(2);
|
|
|
|
tok->deleteNext();
|
|
|
|
|
|
|
|
// Replace "x" with "int x" or similar
|
|
|
|
Token::replace(argumentNames[tok->str()], start, tok);
|
2009-09-05 22:41:39 +02:00
|
|
|
argumentNames.erase(tok->str());
|
2009-01-26 23:26:50 +01:00
|
|
|
tok = temp;
|
|
|
|
start = tok;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "{"))
|
2010-01-01 13:26:54 +01:00
|
|
|
tok = tok->link();
|
2009-12-30 15:12:38 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok == NULL)
|
2009-01-27 08:39:11 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (bailOut)
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-01-20 21:19:06 +01:00
|
|
|
void Tokenizer:: simplifyFunctionPointers()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-01-20 21:19:06 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous() && !Token::Match(tok->previous(), "[{};]"))
|
2010-01-20 21:19:06 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "%type% *| *| ( * %var% ) ("))
|
2010-01-20 21:19:06 +01:00
|
|
|
;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "%type% %type% *| *| ( * %var% ) ("))
|
2010-01-20 21:19:06 +01:00
|
|
|
tok = tok->next();
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok->next()->str() == "*")
|
2010-01-20 21:19:06 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// check that the declaration ends with ;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::simpleMatch(tok->tokAt(5)->link(), ") ;"))
|
2010-01-20 21:19:06 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// ok simplify this function pointer to an ordinary pointer
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->tokAt(2)->deleteNext();
|
|
|
|
const Token *tok2 = tok->tokAt(3)->link();
|
|
|
|
Token::eraseTokens(tok->tokAt(2), tok2 ? tok2->next() : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
bool Tokenizer::simplifyFunctionReturn()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
int indentlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok = tokens(); tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{")
|
2009-01-24 18:15:38 +01:00
|
|
|
++indentlevel;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "}")
|
2009-01-24 18:15:38 +01:00
|
|
|
--indentlevel;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (indentlevel == 0 && Token::Match(tok, "%var% ( ) { return %num% ; }"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
std::ostringstream pattern;
|
|
|
|
pattern << "[(=+-*/] " << tok->str() << " ( ) [;)+-*/]";
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = _tokens; tok2; tok2 = tok2->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, pattern.str().c_str()))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok2->str(tok->strAt(5));
|
|
|
|
tok2->deleteNext();
|
|
|
|
tok2->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-24 17:11:37 +01:00
|
|
|
|
|
|
|
static void incdec(std::string &value, const std::string &op)
|
|
|
|
{
|
|
|
|
int ivalue = 0;
|
|
|
|
std::istringstream istr(value.c_str());
|
|
|
|
istr >> ivalue;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (op == "++")
|
2009-02-24 17:11:37 +01:00
|
|
|
++ivalue;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (op == "--")
|
2009-02-24 17:11:37 +01:00
|
|
|
--ivalue;
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << ivalue;
|
|
|
|
value = ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-18 20:32:05 +01:00
|
|
|
|
2009-08-29 21:07:09 +02:00
|
|
|
void Tokenizer::simplifyVarDecl()
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
// Split up variable declarations..
|
|
|
|
// "int a=4;" => "int a; a=4;"
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "= {"))
|
2009-11-22 09:06:39 +01:00
|
|
|
{
|
|
|
|
tok = tok->next()->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-11-22 09:06:39 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous() && !Token::Match(tok->previous(), "[{};)]"))
|
2009-03-18 20:32:05 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Token *type0 = tok;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(type0, "%type%"))
|
2009-03-18 20:32:05 +01:00
|
|
|
continue;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(type0, "else|return"))
|
2009-03-18 20:32:05 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
bool isconst = false;
|
2009-10-03 12:03:54 +02:00
|
|
|
bool isstatic = false;
|
2009-03-18 20:32:05 +01:00
|
|
|
Token *tok2 = type0;
|
|
|
|
unsigned int typelen = 1;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok2, "%type% %type% *| *| %var%"))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "const")
|
2009-03-18 20:32:05 +01:00
|
|
|
isconst = true;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "static")
|
2009-10-03 12:03:54 +02:00
|
|
|
isstatic = true;
|
|
|
|
|
2009-03-18 20:32:05 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
++typelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't split up const declaration..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (isconst && Token::Match(tok2, "%type% %var% ="))
|
2009-03-18 20:32:05 +01:00
|
|
|
continue;
|
|
|
|
|
2009-11-22 09:06:39 +01:00
|
|
|
// strange looking variable declaration => don't split up.
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "%type% *| %var% , %type% *| %var%"))
|
2009-11-22 09:06:39 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "%type% *| %var% ,|="))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
2009-10-03 12:11:52 +02:00
|
|
|
const bool isPointer = (tok2->next()->str() == "*");
|
|
|
|
const Token *varName = tok2->tokAt((isPointer ? 2 : 1));
|
|
|
|
Token *endDeclaration = varName->next();
|
2009-03-18 20:32:05 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (varName->str() != "operator")
|
2009-10-03 12:03:54 +02:00
|
|
|
{
|
2009-10-03 12:11:52 +02:00
|
|
|
tok2 = endDeclaration; // The ',' or '=' token
|
2009-10-03 12:03:54 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (isstatic && tok2->str() == "=")
|
2009-10-03 12:03:54 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2->next(), "%num% ,"))
|
2009-10-03 12:03:54 +02:00
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
else
|
|
|
|
tok2 = NULL;
|
|
|
|
}
|
|
|
|
}
|
2009-03-18 20:32:05 +01:00
|
|
|
else
|
|
|
|
tok2 = NULL;
|
2009-08-06 22:11:29 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "%type% * * %var% ,|="))
|
2009-08-06 22:11:29 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->tokAt(3)->str() != "operator")
|
2009-08-06 22:11:29 +02:00
|
|
|
tok2 = tok2->tokAt(4); // The ',' token
|
|
|
|
else
|
|
|
|
tok2 = NULL;
|
2009-03-18 20:32:05 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "%type% * const %var% ,|="))
|
2009-06-14 19:32:34 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->tokAt(3)->str() != "operator")
|
2009-06-14 19:32:34 +02:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(4); // The ',' token
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok2 = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-16 20:21:22 +02:00
|
|
|
else if (Token::Match(tok2, "%type% %var% [ %num% ] ,|=|[") ||
|
|
|
|
Token::Match(tok2, "%type% %var% [ %var% ] ,|=|["))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(5); // The ',' token
|
2010-05-16 20:21:22 +02:00
|
|
|
while (Token::Match(tok2, "[ %num% ]") || Token::Match(tok2, "[ %var% ]"))
|
|
|
|
tok2 = tok2->tokAt(3);
|
|
|
|
if (!Token::Match(tok2, "=|,"))
|
|
|
|
{
|
|
|
|
tok2 = NULL;
|
|
|
|
}
|
2009-09-20 12:28:15 +02:00
|
|
|
|
2010-05-16 20:21:22 +02:00
|
|
|
if (tok2 && tok2->str() == "=")
|
2009-09-20 12:28:15 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2 && tok2->str() != ",")
|
2009-09-20 12:28:15 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "{")
|
2009-09-20 13:28:56 +02:00
|
|
|
tok2 = tok2->link();
|
2009-09-26 12:02:13 +02:00
|
|
|
|
2009-09-20 12:28:15 +02:00
|
|
|
tok2 = tok2->next();
|
2009-09-26 12:02:13 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == ";")
|
2009-09-26 12:02:13 +02:00
|
|
|
tok2 = NULL;
|
2009-09-20 12:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-18 20:32:05 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "%type% * %var% [ %num% ] ,") ||
|
|
|
|
Token::Match(tok2, "%type% * %var% [ %var% ] ,"))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(6); // The ',' token
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "std :: %type% <") || Token::Match(tok2, "%type% <"))
|
2009-07-28 21:46:33 +02:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// Deal with templates and standart types
|
|
|
|
//
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok2, "std ::"))
|
2009-07-28 21:46:33 +02:00
|
|
|
{
|
2009-12-05 21:15:14 +01:00
|
|
|
typelen += 2;
|
2009-07-28 21:46:33 +02:00
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
typelen += 2;
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
size_t indentlevel = 1;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok3 = tok2; tok3; tok3 = tok3->next())
|
2009-07-28 21:46:33 +02:00
|
|
|
{
|
|
|
|
++typelen;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->str() == "<")
|
2009-07-28 21:46:33 +02:00
|
|
|
{
|
|
|
|
++indentlevel;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == ">")
|
2009-07-28 21:46:33 +02:00
|
|
|
{
|
|
|
|
--indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel == 0)
|
2009-07-28 21:46:33 +02:00
|
|
|
{
|
|
|
|
tok2 = tok3->next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == ";")
|
2009-08-23 08:26:16 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2009-07-28 21:46:33 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, ":: %type%"))
|
2009-07-28 21:46:33 +02:00
|
|
|
{
|
|
|
|
typelen += 2;
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "*" || tok2->str() == "&")
|
2009-07-28 21:46:33 +02:00
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "%var% ,"))
|
2009-07-28 21:46:33 +02:00
|
|
|
{
|
|
|
|
tok2 = tok2->next(); // The ',' token
|
2009-12-05 21:15:14 +01:00
|
|
|
typelen--;
|
2009-07-28 21:46:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok2 = NULL;
|
|
|
|
typelen = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-03-18 20:32:05 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tok2 = NULL;
|
|
|
|
typelen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2)
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == ",")
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
tok2->str(";");
|
2009-07-05 22:16:43 +02:00
|
|
|
insertTokens(tok2, type0, typelen);
|
2009-12-05 22:19:54 +01:00
|
|
|
std::stack<Token *> link1;
|
|
|
|
std::stack<Token *> link2;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (((typelen--) > 0) && (0 != (tok2 = tok2->next())))
|
2009-12-05 21:15:14 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(")
|
2009-12-05 22:19:54 +01:00
|
|
|
link1.push(tok2);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == ")" && !link1.empty())
|
2009-12-05 21:15:14 +01:00
|
|
|
{
|
2009-12-05 22:19:54 +01:00
|
|
|
Token::createMutualLinks(tok2, link1.top());
|
|
|
|
link1.pop();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "[")
|
2009-12-05 22:19:54 +01:00
|
|
|
link2.push(tok2);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "]" && !link2.empty())
|
2009-12-05 22:19:54 +01:00
|
|
|
{
|
|
|
|
Token::createMutualLinks(tok2, link2.top());
|
|
|
|
link2.pop();
|
2009-12-05 21:15:14 +01:00
|
|
|
}
|
|
|
|
}
|
2009-03-18 20:32:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Token *eq = tok2;
|
|
|
|
|
|
|
|
int parlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2)
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "[{(<]"))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
++parlevel;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "[})>]"))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (parlevel <= 0)
|
2009-03-18 20:32:05 +01:00
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (parlevel == 0 && strchr(";,", tok2->str()[0]))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
// "type var =" => "type var; var ="
|
|
|
|
Token *VarTok = type0->tokAt(typelen);
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(VarTok, "*|const"))
|
2009-03-18 20:32:05 +01:00
|
|
|
VarTok = VarTok->next();
|
2009-07-05 22:16:43 +02:00
|
|
|
insertTokens(eq, VarTok, 2);
|
2009-03-18 20:32:05 +01:00
|
|
|
eq->str(";");
|
|
|
|
|
|
|
|
// "= x, " => "= x; type "
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == ",")
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
tok2->str(";");
|
2009-07-05 22:16:43 +02:00
|
|
|
insertTokens(tok2, type0, typelen);
|
2009-03-18 20:32:05 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-28 15:56:13 +02:00
|
|
|
void Tokenizer::simplifyStdType()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-03-28 15:56:13 +02:00
|
|
|
{
|
|
|
|
// long unsigned => unsigned long
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "long|short|int|char|_int64 unsigned|signed"))
|
2010-03-28 15:56:13 +02:00
|
|
|
{
|
|
|
|
std::string temp = tok->str();
|
|
|
|
tok->str(tok->next()->str());
|
|
|
|
tok->next()->str(temp);
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(tok, "unsigned|signed|long|char|short|int|__int64"))
|
2010-03-28 15:56:13 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// check if signed or unsigned specified
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "unsigned|signed"))
|
2010-03-28 15:56:13 +02:00
|
|
|
{
|
|
|
|
bool isUnsigned = tok->str() == "unsigned";
|
2010-03-31 17:14:49 +02:00
|
|
|
|
|
|
|
// unsigned i => unsigned int i
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->next()->isIntegerType())
|
2010-03-31 17:14:49 +02:00
|
|
|
tok->str("int");
|
|
|
|
else
|
|
|
|
tok->deleteThis();
|
2010-03-28 15:56:13 +02:00
|
|
|
tok->isUnsigned(isUnsigned);
|
|
|
|
tok->isSigned(!isUnsigned);
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "__int64"))
|
2010-03-28 15:56:13 +02:00
|
|
|
{
|
|
|
|
tok->str("long");
|
|
|
|
tok->isLong(true);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "long"))
|
2010-03-28 15:56:13 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "long"))
|
2010-03-28 15:56:13 +02:00
|
|
|
{
|
|
|
|
tok->isLong(true);
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "int"))
|
2010-03-28 15:56:13 +02:00
|
|
|
tok->deleteNext();
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok->next(), "double"))
|
2010-03-28 15:56:13 +02:00
|
|
|
{
|
|
|
|
tok->str("double");
|
|
|
|
tok->isLong(true);
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "short"))
|
2010-03-28 15:56:13 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "int"))
|
2010-03-28 15:56:13 +02:00
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-18 20:32:05 +01:00
|
|
|
|
2009-08-29 21:17:17 +02:00
|
|
|
void Tokenizer::simplifyIfAssign()
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(tok->next(), "if|while ( !| (| %var% =") &&
|
|
|
|
!Token::Match(tok->next(), "if|while ( !| (| %var% . %var% ="))
|
2009-08-05 19:45:05 +02:00
|
|
|
continue;
|
2009-06-20 13:58:30 +02:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
// simplifying a "while" condition ?
|
|
|
|
const bool iswhile(tok->next()->str() == "while");
|
2009-06-20 13:58:30 +02:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
// delete the "if"
|
|
|
|
tok->deleteNext();
|
|
|
|
|
|
|
|
// Remember if there is a "!" or not. And delete it if there are.
|
|
|
|
const bool isNot(tok->tokAt(2)->str() == "!");
|
2010-04-02 07:30:58 +02:00
|
|
|
if (isNot)
|
2009-08-05 19:45:05 +02:00
|
|
|
tok->next()->deleteNext();
|
|
|
|
|
2009-08-29 11:08:11 +02:00
|
|
|
// Delete paranthesis.. and remember how many there are with
|
|
|
|
// their links.
|
|
|
|
std::list<Token *> braces;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok->next()->str() == "(")
|
2009-08-05 19:45:05 +02:00
|
|
|
{
|
2009-08-29 11:08:11 +02:00
|
|
|
braces.push_back(tok->next()->link());
|
2009-03-23 18:20:56 +01:00
|
|
|
tok->deleteNext();
|
2009-08-05 19:45:05 +02:00
|
|
|
}
|
2009-03-23 18:20:56 +01:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
// Skip the "%var% = ..."
|
|
|
|
Token *tok2;
|
|
|
|
unsigned int indentlevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (tok2 = tok->next(); tok2; tok2 = tok2->next())
|
2009-08-05 19:45:05 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(")
|
2009-08-05 19:45:05 +02:00
|
|
|
++indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == ")")
|
2009-03-24 18:23:21 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel <= 0)
|
2009-08-05 19:45:05 +02:00
|
|
|
break;
|
|
|
|
--indentlevel;
|
2009-03-24 18:23:21 +01:00
|
|
|
}
|
2009-08-05 19:45:05 +02:00
|
|
|
}
|
2009-03-24 18:23:21 +01:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
// Insert "; if|while ( .."
|
|
|
|
tok2 = tok2->previous();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok->tokAt(2), "."))
|
2009-08-05 19:45:05 +02:00
|
|
|
{
|
|
|
|
tok2->insertToken(tok->strAt(3));
|
|
|
|
tok2->insertToken(tok->strAt(2));
|
|
|
|
}
|
|
|
|
tok2->insertToken(tok->strAt(1));
|
2009-11-04 23:58:15 +01:00
|
|
|
tok2->next()->varId(tok->tokAt(1)->varId());
|
2009-08-29 11:08:11 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (! braces.empty())
|
2009-08-29 11:08:11 +02:00
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
tok2->insertToken("(");
|
2009-08-29 11:08:11 +02:00
|
|
|
Token::createMutualLinks(tok2->next(), braces.back());
|
|
|
|
braces.pop_back();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (isNot)
|
2009-08-05 19:45:05 +02:00
|
|
|
tok2->next()->insertToken("!");
|
|
|
|
tok2->insertToken(iswhile ? "while" : "if");
|
|
|
|
tok2->insertToken(";");
|
2009-03-23 18:20:56 +01:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
// If it's a while loop.. insert the assignment in the loop
|
2010-04-02 07:30:58 +02:00
|
|
|
if (iswhile)
|
2009-08-05 19:45:05 +02:00
|
|
|
{
|
|
|
|
indentlevel = 0;
|
|
|
|
Token *tok3 = tok2;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (tok3 = tok2; tok3; tok3 = tok3->next())
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->str() == "{")
|
2009-03-23 18:20:56 +01:00
|
|
|
++indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "}")
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel <= 1)
|
2009-03-23 18:20:56 +01:00
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
}
|
2009-03-24 18:23:21 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3 && indentlevel == 1)
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
tok3 = tok3->previous();
|
2009-11-12 19:41:35 +01:00
|
|
|
std::list<Token *> braces2;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (tok2 = tok2->next(); tok2 && tok2 != tok; tok2 = tok2->previous())
|
2009-06-20 13:58:30 +02:00
|
|
|
{
|
2009-08-29 13:09:24 +02:00
|
|
|
tok3->insertToken(tok2->strAt(0));
|
|
|
|
|
|
|
|
Token *newTok = tok3->next();
|
|
|
|
newTok->fileIndex(tok2->fileIndex());
|
|
|
|
newTok->linenr(tok2->linenr());
|
|
|
|
|
|
|
|
// link() newly tokens manually
|
2010-04-02 07:30:58 +02:00
|
|
|
if (newTok->str() == ")")
|
2009-08-29 13:09:24 +02:00
|
|
|
{
|
|
|
|
braces.push_back(newTok);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (newTok->str() == "]")
|
2009-11-12 19:41:35 +01:00
|
|
|
{
|
|
|
|
braces2.push_back(newTok);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (newTok->str() == "(")
|
2009-08-29 13:09:24 +02:00
|
|
|
{
|
|
|
|
Token::createMutualLinks(newTok, braces.back());
|
|
|
|
braces.pop_back();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (newTok->str() == "[")
|
2009-11-12 19:41:35 +01:00
|
|
|
{
|
|
|
|
Token::createMutualLinks(newTok, braces2.back());
|
|
|
|
braces2.pop_back();
|
|
|
|
}
|
2009-06-20 13:58:30 +02:00
|
|
|
}
|
2009-03-23 18:20:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-01 10:26:15 +02:00
|
|
|
void Tokenizer::simplifyVariableMultipleAssign()
|
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "%var% = %var% = %num% ;") ||
|
|
|
|
Token::Match(tok, "%var% = %var% = %var% ;"))
|
|
|
|
{
|
|
|
|
|
|
|
|
// skip intermediate assignments
|
|
|
|
Token *tok2 = tok->previous();
|
|
|
|
while (tok2 &&
|
|
|
|
tok2->str() == "=" &&
|
|
|
|
Token::Match(tok2->previous(), "%var%"))
|
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok2->str() != ";")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *stopAt = tok->tokAt(2);
|
|
|
|
const Token *valueTok = tok->tokAt(4);
|
|
|
|
const std::string value(valueTok->str());
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
while (tok2 != stopAt)
|
|
|
|
{
|
|
|
|
tok2->next()->insertToken(";");
|
|
|
|
tok2->next()->insertToken(value);
|
|
|
|
tok2 = tok2->tokAt(4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-24 18:23:21 +01:00
|
|
|
|
2009-08-29 21:19:45 +02:00
|
|
|
void Tokenizer::simplifyIfNot()
|
2009-03-24 18:23:21 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-03-24 18:23:21 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "(" || tok->str() == "||" || tok->str() == "&&")
|
2009-03-24 18:23:21 +01:00
|
|
|
{
|
2009-07-18 10:18:46 +02:00
|
|
|
tok = tok->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok && tok->str() == "(")
|
2009-07-29 23:37:01 +02:00
|
|
|
tok = tok->next();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-07-29 23:37:01 +02:00
|
|
|
break;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "0|false == (") ||
|
|
|
|
Token::Match(tok, "0|false == %var%"))
|
2009-07-18 10:18:46 +02:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->str("!");
|
|
|
|
}
|
2009-05-01 12:07:10 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "%var% == 0|false"))
|
2009-07-18 10:18:46 +02:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->next()->str(tok->str());
|
|
|
|
tok->str("!");
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "%var% .|:: %var% == 0|false"))
|
2009-07-18 10:18:46 +02:00
|
|
|
{
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->insertToken("!");
|
|
|
|
tok = tok->tokAt(4);
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-07-18 10:18:46 +02:00
|
|
|
}
|
2009-10-05 13:10:09 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "* %var% == 0|false"))
|
2009-10-05 13:10:09 +02:00
|
|
|
{
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->insertToken("!");
|
|
|
|
tok = tok->tokAt(3);
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
|
|
|
}
|
2009-05-01 12:07:10 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->link() && Token::Match(tok, ") == 0|false"))
|
2009-05-01 12:07:10 +02:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->link()->previous(), "%var%"))
|
2009-08-20 21:37:05 +02:00
|
|
|
{
|
|
|
|
// if( foo(x) == 0 )
|
|
|
|
tok->link()->previous()->insertToken(tok->link()->previous()->str().c_str());
|
|
|
|
tok->link()->previous()->previous()->str("!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if( (x) == 0 )
|
|
|
|
tok->link()->insertToken("(");
|
|
|
|
tok->link()->str("!");
|
2009-10-05 13:10:09 +02:00
|
|
|
Token *temp = tok->link();
|
2009-08-29 11:08:11 +02:00
|
|
|
Token::createMutualLinks(tok->link()->next(), tok);
|
2009-10-05 13:10:09 +02:00
|
|
|
temp->link(0);
|
2009-08-20 21:37:05 +02:00
|
|
|
}
|
2009-03-24 18:23:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-31 10:42:27 +02:00
|
|
|
|
2009-08-29 21:21:06 +02:00
|
|
|
void Tokenizer::simplifyIfNotNull()
|
2009-07-25 13:11:29 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-07-25 13:11:29 +02:00
|
|
|
{
|
|
|
|
Token *deleteFrom = NULL;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "(" || tok->str() == "||" || tok->str() == "&&")
|
2009-07-25 13:11:29 +02:00
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "0 != (") ||
|
|
|
|
Token::Match(tok, "0 != %var%"))
|
2009-07-25 13:11:29 +02:00
|
|
|
{
|
|
|
|
deleteFrom = tok->previous();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "%var% != 0"))
|
2009-07-25 13:11:29 +02:00
|
|
|
{
|
|
|
|
deleteFrom = tok;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "%var% .|:: %var% != 0"))
|
2009-07-25 13:11:29 +02:00
|
|
|
{
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
deleteFrom = tok;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->link() && Token::simpleMatch(tok, ") != 0"))
|
2009-07-25 13:11:29 +02:00
|
|
|
{
|
|
|
|
deleteFrom = tok;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (deleteFrom)
|
2009-07-25 16:27:18 +02:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(deleteFrom, deleteFrom->tokAt(3));
|
2009-07-31 18:06:16 +02:00
|
|
|
tok = deleteFrom;
|
2009-07-25 13:11:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-22 20:20:43 +01:00
|
|
|
void Tokenizer::simplifyIfSameInnerCondition()
|
|
|
|
{
|
|
|
|
// same inner condition
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-01-22 20:20:43 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "if ( %var% ) {"))
|
2010-01-22 20:20:43 +01:00
|
|
|
{
|
|
|
|
const unsigned int varid(tok->tokAt(2)->varId());
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!varid)
|
2010-01-22 20:20:43 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok->tokAt(5); tok2; tok2 = tok2->next())
|
2010-01-22 20:20:43 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "{" || tok2->str() == "}")
|
2010-01-22 20:20:43 +01:00
|
|
|
break;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok2, "if ("))
|
2010-01-22 20:20:43 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(2);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "%varid% )", varid))
|
2010-01-22 20:20:43 +01:00
|
|
|
tok2->str("true");
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "! %varid% )", varid))
|
2010-01-22 20:20:43 +01:00
|
|
|
tok2->next()->varId(varid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-13 13:59:41 +02:00
|
|
|
bool Tokenizer::simplifyLogicalOperators()
|
2009-05-31 10:42:27 +02:00
|
|
|
{
|
2010-05-13 13:59:41 +02:00
|
|
|
bool ret = false;
|
|
|
|
|
2009-05-31 10:42:27 +02:00
|
|
|
// "if (not p)" => "if (!p)"
|
2010-05-13 13:59:41 +02:00
|
|
|
// "if (p and q)" => "if (p && q)"
|
|
|
|
// "if (p or q)" => "if (p || q)"
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-05-31 10:42:27 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "if|while ( not %var%"))
|
2009-08-29 15:11:47 +02:00
|
|
|
{
|
2009-06-14 08:20:51 +02:00
|
|
|
tok->tokAt(2)->str("!");
|
2010-05-13 13:59:41 +02:00
|
|
|
ret = true;
|
2009-08-29 15:11:47 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "&& not %var%"))
|
2009-08-29 15:11:47 +02:00
|
|
|
{
|
2009-05-31 10:42:27 +02:00
|
|
|
tok->next()->str("!");
|
2010-05-13 13:59:41 +02:00
|
|
|
ret = true;
|
2009-08-29 15:11:47 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "|| not %var%"))
|
2009-08-29 15:11:47 +02:00
|
|
|
{
|
2009-05-31 10:42:27 +02:00
|
|
|
tok->next()->str("!");
|
2010-05-13 13:59:41 +02:00
|
|
|
ret = true;
|
2009-08-29 15:11:47 +02:00
|
|
|
}
|
2009-08-29 15:29:19 +02:00
|
|
|
// "%var%|) and %var%|("
|
2010-05-13 13:59:41 +02:00
|
|
|
else if (Token::Match(tok, "and|or") &&
|
2010-04-02 07:30:58 +02:00
|
|
|
((Token::Match(tok->previous(), "%var%") || tok->previous()->str() == ")") ||
|
|
|
|
(Token::Match(tok->next(), "%var%") || tok->next()->str() == "(")))
|
2009-08-29 15:29:19 +02:00
|
|
|
{
|
2010-05-13 13:59:41 +02:00
|
|
|
if (tok->str() == "and")
|
|
|
|
tok->str("&&");
|
|
|
|
else
|
|
|
|
tok->str("||");
|
|
|
|
ret = true;
|
2009-08-29 15:29:19 +02:00
|
|
|
}
|
2009-05-31 10:42:27 +02:00
|
|
|
}
|
2010-05-13 13:59:41 +02:00
|
|
|
return ret;
|
2009-05-31 10:42:27 +02:00
|
|
|
}
|
|
|
|
|
2010-04-14 19:04:16 +02:00
|
|
|
// int i(0); => int i; i = 0;
|
2010-01-30 09:33:16 +01:00
|
|
|
void Tokenizer::simplifyInitVar()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-01-30 09:33:16 +01:00
|
|
|
{
|
2010-04-14 19:04:16 +02:00
|
|
|
if (Token::Match(tok, "[{};] class|struct|union| %type% *| %var% ( &| %any% ) ;"))
|
|
|
|
tok = initVar(tok->next());
|
2010-04-15 18:42:11 +02:00
|
|
|
else if (tok == _tokens && Token::Match(tok, "class|struct|union| %type% *| %var% ( &| %any% ) ;"))
|
2010-04-14 19:04:16 +02:00
|
|
|
tok = initVar(tok);
|
|
|
|
}
|
|
|
|
}
|
2010-01-30 09:33:16 +01:00
|
|
|
|
2010-04-14 19:04:16 +02:00
|
|
|
Token * Tokenizer::initVar(Token * tok)
|
|
|
|
{
|
|
|
|
// call constructor of class => no simplification
|
|
|
|
if (Token::Match(tok, "class|struct|union"))
|
|
|
|
{
|
|
|
|
if (tok->tokAt(2)->str() != "*")
|
|
|
|
return tok;
|
2010-01-30 09:33:16 +01:00
|
|
|
|
2010-04-14 19:04:16 +02:00
|
|
|
tok = tok->next();
|
2010-01-30 09:33:16 +01:00
|
|
|
}
|
2010-04-14 19:04:16 +02:00
|
|
|
else if (!tok->isStandardType() && tok->tokAt(1)->str() != "*")
|
|
|
|
return tok;
|
|
|
|
|
|
|
|
// goto variable name..
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok->str() == "*")
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// check initializer..
|
|
|
|
if (tok->tokAt(2)->isStandardType() || tok->tokAt(2)->str() == "void")
|
|
|
|
return tok;
|
2010-04-15 18:42:11 +02:00
|
|
|
else if (!tok->tokAt(2)->isNumber() && tok->tokAt(2)->str() != "&" && tok->tokAt(2)->varId() == 0)
|
|
|
|
return tok;
|
2010-04-14 19:04:16 +02:00
|
|
|
|
|
|
|
// insert '; var ='
|
|
|
|
tok->insertToken(";");
|
|
|
|
tok->next()->insertToken(tok->str());
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
tok->insertToken("=");
|
|
|
|
|
|
|
|
// remove parantheses..
|
|
|
|
tok = tok->next();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok->str() == "&")
|
|
|
|
tok = tok->next();
|
|
|
|
tok->deleteNext();
|
|
|
|
|
|
|
|
return tok;
|
2010-01-30 09:33:16 +01:00
|
|
|
}
|
|
|
|
|
2009-05-31 10:42:27 +02:00
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
bool Tokenizer::simplifyKnownVariables()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Search for a block of code
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! Token::Match(tok, ") const| {"))
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// parse the block of code..
|
|
|
|
int indentlevel = 0;
|
2009-12-08 22:12:49 +01:00
|
|
|
Token *tok2 = tok;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (; tok2; tok2 = tok2->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "{")
|
2009-01-24 18:15:38 +01:00
|
|
|
++indentlevel;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "}")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
--indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel <= 0)
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->previous()->str() != "*" &&
|
|
|
|
(Token::Match(tok2, "%var% = %num% ;") ||
|
|
|
|
Token::Match(tok2, "%var% = %str% ;") ||
|
|
|
|
Token::Match(tok2, "%var% [ ] = %str% ;") ||
|
|
|
|
Token::Match(tok2, "%var% = %bool% ;") ||
|
|
|
|
Token::Match(tok2, "%var% = %var% ;") ||
|
2010-04-25 15:12:06 +02:00
|
|
|
Token::Match(tok2, "%var% = & %var% ;") ||
|
|
|
|
Token::Match(tok2, "%var% = & %var% [ 0 ] ;")))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-11-12 22:16:24 +01:00
|
|
|
const unsigned int varid = tok2->varId();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (varid == 0)
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == tok2->strAt(2))
|
2009-12-30 20:15:44 +01:00
|
|
|
continue;
|
|
|
|
|
2010-01-31 16:29:12 +01:00
|
|
|
const bool pointeralias(tok2->tokAt(2)->isName() || tok2->tokAt(2)->str() == "&");
|
2009-10-17 18:21:13 +02:00
|
|
|
|
2010-05-02 14:41:21 +02:00
|
|
|
std::string value;
|
|
|
|
|
|
|
|
Token *tok3 = NULL;
|
2010-05-16 19:38:44 +02:00
|
|
|
bool valueIsPointer = false;
|
2010-05-02 14:41:21 +02:00
|
|
|
|
|
|
|
if (Token::Match(tok2->tokAt(-2), "for ( %varid% = %num% ; %varid% <|<= %num% ; ++| %varid% ++| ) {", varid))
|
|
|
|
{
|
2010-05-18 19:55:23 +02:00
|
|
|
// is there a "break" in the for loop?
|
|
|
|
bool hasbreak = false;
|
|
|
|
unsigned int indentlevel4 = 0; // indentlevel for tok4
|
|
|
|
for (const Token *tok4 = tok2->previous()->link(); tok4; tok4 = tok4->next())
|
|
|
|
{
|
|
|
|
if (tok4->str() == "{")
|
|
|
|
++indentlevel4;
|
|
|
|
else if (tok4->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel4 <= 1)
|
|
|
|
break;
|
|
|
|
--indentlevel4;
|
|
|
|
}
|
|
|
|
else if (tok4->str() == "break")
|
|
|
|
{
|
|
|
|
hasbreak = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasbreak)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// no break => the value of the counter value is known after the for loop..
|
2010-05-02 14:41:21 +02:00
|
|
|
const std::string compareop = tok2->strAt(5);
|
|
|
|
if (compareop == "<")
|
|
|
|
value = tok2->strAt(6);
|
|
|
|
else
|
|
|
|
value = MathLib::toString(MathLib::toLongNumber(tok2->strAt(6)) + 1);
|
|
|
|
|
|
|
|
// Skip for-body..
|
|
|
|
tok3 = tok2->previous()->link()->next()->link()->next();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = tok2->strAt(2);
|
|
|
|
if (value == "]")
|
|
|
|
value = tok2->strAt(4);
|
|
|
|
else if (value == "&")
|
2010-05-16 19:38:44 +02:00
|
|
|
{
|
2010-05-02 14:41:21 +02:00
|
|
|
value = tok2->strAt(3);
|
2010-05-16 19:38:44 +02:00
|
|
|
|
|
|
|
// *ptr = &var; *ptr = 5;
|
|
|
|
// equals
|
|
|
|
// var = 5; not *var = 5;
|
|
|
|
if (tok2->strAt(4) == ";")
|
|
|
|
valueIsPointer = true;
|
|
|
|
}
|
2010-05-02 14:41:21 +02:00
|
|
|
if (Token::simpleMatch(tok2->next(), "= &"))
|
|
|
|
tok2 = tok2->tokAt(3);
|
|
|
|
tok3 = tok2->next();
|
|
|
|
}
|
2009-05-03 20:57:27 +02:00
|
|
|
Token* bailOutFromLoop = 0;
|
2009-05-25 14:21:58 +02:00
|
|
|
int indentlevel3 = indentlevel; // indentlevel for tok3
|
2010-05-05 18:29:56 +02:00
|
|
|
bool ret3 = false;
|
2010-05-02 14:41:21 +02:00
|
|
|
for (; tok3; tok3 = tok3->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->str() == "{")
|
2009-05-25 14:21:58 +02:00
|
|
|
{
|
|
|
|
++indentlevel3;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "}")
|
2009-05-25 14:21:58 +02:00
|
|
|
{
|
|
|
|
--indentlevel3;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel3 < indentlevel)
|
2010-01-31 16:29:12 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2->tokAt(-7), "%type% * %var% ; %var% = & %var% ;") &&
|
|
|
|
tok2->tokAt(-5)->str() == tok2->tokAt(-3)->str())
|
2010-01-31 16:29:12 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(-4);
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(5));
|
|
|
|
}
|
2009-05-25 14:21:58 +02:00
|
|
|
break;
|
2010-01-31 16:29:12 +01:00
|
|
|
}
|
2009-05-25 14:21:58 +02:00
|
|
|
}
|
|
|
|
|
2010-05-04 20:02:47 +02:00
|
|
|
// Stop if label is found
|
|
|
|
if (Token::Match(tok3, "; %type% : ;"))
|
|
|
|
break;
|
|
|
|
|
2010-05-05 18:29:56 +02:00
|
|
|
// Stop if return is found ..
|
|
|
|
if (indentlevel3 == 1)
|
|
|
|
{
|
|
|
|
if (tok3->str() == "return")
|
|
|
|
ret3 = true;
|
|
|
|
else if (tok3->str() == ";")
|
|
|
|
{
|
|
|
|
if (ret3 && !Token::simpleMatch(tok3->next(), "}"))
|
|
|
|
break;
|
|
|
|
ret3 = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str()))
|
2009-10-17 18:21:13 +02:00
|
|
|
break;
|
|
|
|
|
2009-08-22 10:23:55 +02:00
|
|
|
// Stop if something like 'while (--var)' is found
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->str() == "while" || tok3->str() == "do")
|
2009-08-22 10:23:55 +02:00
|
|
|
{
|
|
|
|
const Token *endpar = tok3->next()->link();
|
|
|
|
bool bailout = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok4 = tok3; tok4 && tok4 != endpar; tok4 = tok4->next())
|
2009-08-22 10:23:55 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok4, "++|-- %varid%", varid) ||
|
|
|
|
Token::Match(tok4, "%varid% ++|--|=", varid))
|
2009-08-22 10:23:55 +02:00
|
|
|
{
|
|
|
|
bailout = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (bailout)
|
2009-08-22 10:23:55 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (bailOutFromLoop)
|
2009-05-03 20:57:27 +02:00
|
|
|
{
|
|
|
|
// This could be a loop, skip it, but only if it doesn't contain
|
|
|
|
// the variable we are checking for. If it contains the variable
|
|
|
|
// we will bail out.
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->varId() == varid)
|
2009-05-03 20:57:27 +02:00
|
|
|
{
|
|
|
|
// Continue
|
|
|
|
tok2 = bailOutFromLoop;
|
|
|
|
break;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3 == bailOutFromLoop)
|
2009-05-03 20:57:27 +02:00
|
|
|
{
|
|
|
|
// We have skipped the loop
|
|
|
|
bailOutFromLoop = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "{" && tok3->previous()->str() == ")")
|
2009-05-03 20:57:27 +02:00
|
|
|
{
|
|
|
|
// There is a possible loop after the assignment. Try to skip it.
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->previous()->link() &&
|
|
|
|
!Token::simpleMatch(tok3->previous()->link()->previous(), "if"))
|
2010-02-20 15:50:44 +01:00
|
|
|
bailOutFromLoop = tok3->link();
|
2009-05-03 20:57:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->str() == "}" && tok3->link() && tok3->link()->previous()->str() == ")")
|
2009-05-03 20:57:27 +02:00
|
|
|
{
|
|
|
|
// Assignment was in the middle of possible loop, bail out.
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
2009-05-03 20:57:27 +02:00
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-05-18 07:46:48 +02:00
|
|
|
// Variable used in realloc (see Ticket #1649)
|
|
|
|
if (Token::Match(tok3, "%var% = realloc ( %var% ,") &&
|
|
|
|
tok3->varId() == varid &&
|
|
|
|
tok3->tokAt(4)->varId() == varid)
|
|
|
|
{
|
|
|
|
tok3->tokAt(4)->str(value);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Variable is used somehow in a non-defined pattern => bail out
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->varId() == varid)
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
|
|
|
|
2009-03-25 07:10:17 +01:00
|
|
|
// Using the variable in condition..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok3->previous(), "if ( %varid% ==|!=|<|<=|>|>=|)", varid) ||
|
|
|
|
Token::Match(tok3, "( %varid% ==|!=|<|<=|>|>=", varid) ||
|
|
|
|
Token::Match(tok3, "!|==|!=|<|<=|>|>= %varid% ==|!=|<|<=|>|>=|)", varid) ||
|
|
|
|
Token::Match(tok3->previous(), "strlen|free ( %varid% )", varid))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-03-25 07:10:17 +01:00
|
|
|
tok3 = tok3->next();
|
2009-08-02 10:54:46 +02:00
|
|
|
tok3->str(value);
|
2009-01-24 18:15:38 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
2009-02-14 07:11:37 +01:00
|
|
|
|
|
|
|
// Variable is used in calculation..
|
2010-04-25 15:12:06 +02:00
|
|
|
if (Token::Match(tok3, "[=+-*/[] %varid% [=?+-*/;]]", varid) ||
|
2010-04-24 07:59:53 +02:00
|
|
|
Token::Match(tok3, "[(=+-*/[] %varid% <<|>>", varid) ||
|
|
|
|
Token::Match(tok3, "<< %varid% [+-*/;])]", varid) ||
|
|
|
|
Token::Match(tok3, ">> %varid% [+-*/])]", varid))
|
2009-02-14 07:11:37 +01:00
|
|
|
{
|
|
|
|
tok3 = tok3->next();
|
2009-08-02 10:54:46 +02:00
|
|
|
tok3->str(value);
|
2010-05-16 19:38:44 +02:00
|
|
|
if (tok3->previous()->str() == "*" && valueIsPointer)
|
2010-05-17 19:50:36 +02:00
|
|
|
{
|
|
|
|
tok3 = tok3->previous();
|
|
|
|
tok3->deleteThis();
|
|
|
|
}
|
2009-02-14 07:11:37 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
2009-02-24 17:11:37 +01:00
|
|
|
|
2010-03-14 09:57:34 +01:00
|
|
|
// Using the variable in for-condition..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok3, "for ("))
|
2010-03-14 09:57:34 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok4 = tok3->tokAt(2); tok4; tok4 = tok4->next())
|
2010-03-14 09:57:34 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok4->str() == "(" || tok4->str() == ")")
|
2010-03-14 09:57:34 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Replace variable used in condition..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok4, "; %var% <|<=|!= %var% ; ++| %var% ++| )"))
|
2010-03-14 09:57:34 +01:00
|
|
|
{
|
|
|
|
const Token *inctok = tok4->tokAt(5);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (inctok->str() == "++")
|
2010-03-14 09:57:34 +01:00
|
|
|
inctok = inctok->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (inctok->varId() == varid)
|
2010-03-14 09:57:34 +01:00
|
|
|
break;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok4->next()->varId() == varid)
|
2010-03-14 09:57:34 +01:00
|
|
|
{
|
|
|
|
tok4->next()->str(value);
|
|
|
|
ret = true;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok4->tokAt(3)->varId() == varid)
|
2010-03-14 09:57:34 +01:00
|
|
|
{
|
|
|
|
tok4->tokAt(3)->str(value);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->next()->varId() == varid)
|
2010-03-14 09:57:34 +01:00
|
|
|
{
|
|
|
|
tok3->next()->str(value);
|
|
|
|
ret = true;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok3->tokAt(3)->varId() == varid)
|
2010-03-14 09:57:34 +01:00
|
|
|
{
|
|
|
|
tok3->tokAt(3)->str(value);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-15 19:09:19 +02:00
|
|
|
if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value))
|
2009-02-24 17:11:37 +01:00
|
|
|
{
|
2009-03-04 07:03:51 +01:00
|
|
|
const std::string op(tok3->strAt(2));
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok3, "[{};] %any% %any% ;"))
|
2009-03-04 07:03:51 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok3, tok3->tokAt(3));
|
2009-03-04 07:03:51 +01:00
|
|
|
}
|
|
|
|
else
|
2009-02-27 19:25:47 +01:00
|
|
|
{
|
2009-03-04 07:03:51 +01:00
|
|
|
tok3 = tok3->next();
|
2009-08-02 10:54:46 +02:00
|
|
|
tok3->str(value);
|
2009-02-27 19:25:47 +01:00
|
|
|
tok3->deleteNext();
|
|
|
|
}
|
|
|
|
incdec(value, op);
|
2009-08-02 10:54:46 +02:00
|
|
|
tok2->tokAt(2)->str(value);
|
2009-03-04 07:24:03 +01:00
|
|
|
ret = true;
|
2009-02-24 17:11:37 +01:00
|
|
|
}
|
|
|
|
|
2010-04-15 19:09:19 +02:00
|
|
|
if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "++|-- %varid%", varid) && MathLib::isInt(value) &&
|
2010-04-02 07:30:58 +02:00
|
|
|
!Token::Match(tok3->tokAt(3), "[.[]"))
|
2009-02-24 17:11:37 +01:00
|
|
|
{
|
|
|
|
incdec(value, tok3->strAt(1));
|
2009-08-02 10:54:46 +02:00
|
|
|
tok2->tokAt(2)->str(value);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok3, "[;{}] %any% %any% ;"))
|
2009-03-04 07:03:51 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok3, tok3->tokAt(3));
|
2009-03-04 07:03:51 +01:00
|
|
|
}
|
|
|
|
else
|
2009-02-27 19:25:47 +01:00
|
|
|
{
|
|
|
|
tok3->deleteNext();
|
2009-08-02 10:54:46 +02:00
|
|
|
tok3->next()->str(value);
|
2009-02-27 19:25:47 +01:00
|
|
|
}
|
2009-02-24 17:11:37 +01:00
|
|
|
tok3 = tok3->next();
|
2009-03-04 07:24:03 +01:00
|
|
|
ret = true;
|
2009-02-24 17:11:37 +01:00
|
|
|
}
|
2009-12-27 13:08:41 +01:00
|
|
|
|
|
|
|
// return variable..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok3, "return %varid% ;", varid))
|
2009-12-27 13:08:41 +01:00
|
|
|
{
|
|
|
|
tok3->next()->str(value);
|
|
|
|
}
|
2010-01-31 16:29:12 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (pointeralias && Token::Match(tok3, "return * %varid% ;", varid))
|
2010-01-31 16:29:12 +01:00
|
|
|
{
|
|
|
|
tok3->deleteNext();
|
|
|
|
tok3->next()->str(value);
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-08 22:12:49 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2)
|
2009-12-08 22:12:49 +01:00
|
|
|
tok = tok2->previous();
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:18:26 +01:00
|
|
|
|
2009-08-29 21:28:39 +02:00
|
|
|
void Tokenizer::elseif()
|
2009-02-17 20:18:26 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-02-17 20:18:26 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::simpleMatch(tok, "else if"))
|
2009-02-17 20:18:26 +01:00
|
|
|
continue;
|
|
|
|
int indent = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok; indent >= 0 && tok2; tok2 = tok2->next())
|
2009-02-17 20:18:26 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "(|{"))
|
2009-02-17 20:18:26 +01:00
|
|
|
++indent;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, ")|}"))
|
2009-02-17 20:18:26 +01:00
|
|
|
--indent;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indent == 0 && Token::Match(tok2, "}|;"))
|
2009-02-17 20:18:26 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->next() && tok2->next()->str() != "else")
|
2009-02-17 20:18:26 +01:00
|
|
|
{
|
|
|
|
tok->insertToken("{");
|
|
|
|
tok2->insertToken("}");
|
2009-08-23 02:38:00 +02:00
|
|
|
Token::createMutualLinks(tok->next(), tok2->next());
|
2009-02-17 20:18:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
bool Tokenizer::simplifyRedundantParanthesis()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-26 17:38:08 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != "(")
|
2009-05-29 23:04:01 +02:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::simpleMatch(tok, "( (") &&
|
|
|
|
tok->link()->previous() == tok->next()->link())
|
2009-01-26 17:38:08 +01:00
|
|
|
{
|
2009-05-29 23:04:01 +02:00
|
|
|
// We have "(( *something* ))", remove the inner
|
|
|
|
// paranthesis
|
|
|
|
tok->deleteNext();
|
2009-06-14 08:26:20 +02:00
|
|
|
tok->link()->tokAt(-2)->deleteNext();
|
2009-05-29 23:04:01 +02:00
|
|
|
ret = true;
|
|
|
|
}
|
2009-05-28 22:03:36 +02:00
|
|
|
|
2010-05-29 22:20:10 +02:00
|
|
|
while (Token::Match(tok->previous(), "[;{(] ( %var% (") &&
|
2010-04-02 07:30:58 +02:00
|
|
|
tok->link()->previous() == tok->tokAt(2)->link())
|
2009-06-12 15:04:58 +02:00
|
|
|
{
|
|
|
|
// We have "( func ( *something* ))", remove the outer
|
|
|
|
// paranthesis
|
|
|
|
tok->link()->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok->previous(), "[;{] ( delete %var% ) ;"))
|
2009-06-12 16:14:01 +02:00
|
|
|
{
|
|
|
|
// We have "( delete var )", remove the outer
|
|
|
|
// paranthesis
|
|
|
|
tok->tokAt(3)->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok->previous(), "[;{] ( delete [ ] %var% ) ;"))
|
2009-06-12 16:14:01 +02:00
|
|
|
{
|
|
|
|
// We have "( delete [] var )", remove the outer
|
|
|
|
// paranthesis
|
|
|
|
tok->tokAt(5)->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2010-05-07 18:37:50 +02:00
|
|
|
if ((Token::simpleMatch(tok->previous(), "delete (") && Token::Match(tok->link(), ") ;|,")) ||
|
|
|
|
(Token::simpleMatch(tok->previous(), "; (") && Token::Match(tok->link(), ") ;")))
|
|
|
|
{
|
|
|
|
tok->link()->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous(), "[(!*;{}] ( %var% )") && tok->next()->varId() != 0)
|
2009-07-03 20:57:44 +02:00
|
|
|
{
|
|
|
|
// We have "( var )", remove the paranthesis
|
2009-11-15 09:28:08 +01:00
|
|
|
tok->deleteThis();
|
2009-07-03 20:57:44 +02:00
|
|
|
tok->deleteNext();
|
2009-11-15 09:28:08 +01:00
|
|
|
ret = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous(), "[(!] ( %var% . %var% )"))
|
2009-11-15 09:28:08 +01:00
|
|
|
{
|
|
|
|
// We have "( var . var )", remove the paranthesis
|
|
|
|
tok->deleteThis();
|
|
|
|
tok = tok->tokAt(2);
|
2009-07-03 20:57:44 +02:00
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "( ( %bool% )") ||
|
|
|
|
Token::Match(tok, "( ( %num% )"))
|
2009-05-29 23:04:01 +02:00
|
|
|
{
|
|
|
|
tok->tokAt(2)->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
2009-01-26 17:38:08 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-01-31 16:29:12 +01:00
|
|
|
void Tokenizer::simplifyReference()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-01-31 16:29:12 +01:00
|
|
|
{
|
|
|
|
// starting executable scope..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, ") const| {"))
|
2010-01-31 16:29:12 +01:00
|
|
|
{
|
|
|
|
// replace references in this scope..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->next()->str() != "{")
|
2010-01-31 16:29:12 +01:00
|
|
|
tok = tok->next();
|
|
|
|
Token * const end = tok->next()->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok; tok2 && tok2 != end; tok2 = tok2->next())
|
2010-01-31 16:29:12 +01:00
|
|
|
{
|
|
|
|
// found a reference..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "[;{}] %type% & %var% (|= %var% )| ;"))
|
2010-01-31 16:29:12 +01:00
|
|
|
{
|
|
|
|
const unsigned int ref_id = tok2->tokAt(3)->varId();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!ref_id)
|
2010-01-31 16:29:12 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// replace reference in the code..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok3 = tok2->tokAt(7); tok3 && tok3 != end; tok3 = tok3->next())
|
2010-01-31 16:29:12 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->varId() == ref_id)
|
2010-01-31 16:29:12 +01:00
|
|
|
{
|
|
|
|
tok3->str(tok2->strAt(5));
|
|
|
|
tok3->varId(tok2->tokAt(5)->varId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(7));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 07:24:03 +01:00
|
|
|
bool Tokenizer::simplifyCalculations()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-03-04 07:24:03 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok->next(), "* 1") || Token::simpleMatch(tok->next(), "1 *"))
|
2009-03-04 07:24:03 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-03-04 07:24:03 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (1-2)
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% [],);=<>+-*/]") ||
|
|
|
|
Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/]") ||
|
2010-05-29 20:27:08 +02:00
|
|
|
Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% <<|>>") ||
|
2010-04-02 07:30:58 +02:00
|
|
|
Token::Match(tok, "<< %num% [+-*/] %num% <<"))
|
2009-03-04 07:24:03 +01:00
|
|
|
{
|
2009-04-06 19:23:30 +02:00
|
|
|
tok = tok->next();
|
2009-03-04 07:24:03 +01:00
|
|
|
|
2009-04-06 19:43:54 +02:00
|
|
|
// Don't simplify "%num% / 0"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok->next(), "/ 0"))
|
2009-04-06 19:43:54 +02:00
|
|
|
continue;
|
|
|
|
|
2009-12-25 14:47:15 +01:00
|
|
|
// + and - are calculated after * and /
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "[+-/]"))
|
2009-08-28 12:13:46 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous()->str() == "*")
|
2009-08-28 12:13:46 +02:00
|
|
|
continue;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->tokAt(3), "[*/]"))
|
2009-08-28 12:13:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous(), "- %num% - %num%"))
|
2009-12-25 14:47:15 +01:00
|
|
|
tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), '+'));
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok->previous(), "- %num% + %num%"))
|
2010-01-20 20:26:54 +01:00
|
|
|
tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), '-'));
|
2009-12-25 14:47:15 +01:00
|
|
|
else
|
2010-02-14 19:58:17 +01:00
|
|
|
tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), tok->strAt(1)[0]));
|
2009-04-06 19:23:30 +02:00
|
|
|
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-03-04 07:24:03 +01:00
|
|
|
|
2009-08-02 10:45:33 +02:00
|
|
|
// evaluate "2 + 2 - 2 - 2"
|
|
|
|
// as (((2 + 2) - 2) - 2) = 0
|
|
|
|
// instead of ((2 + 2) - (2 - 2)) = 4
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "[+-*/]"))
|
2009-08-02 21:11:17 +02:00
|
|
|
{
|
2009-12-15 20:18:32 +01:00
|
|
|
tok = tok->previous();
|
2009-08-02 10:45:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-03-04 07:24:03 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove parantheses around number..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->isName() && Token::Match(tok->next(), "( %num% )"))
|
2009-03-04 07:24:03 +01:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok = tok->next();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove parantheses around variable..
|
|
|
|
// keep parantheses here: dynamic_cast<Fred *>(p);
|
2009-10-29 20:54:51 +01:00
|
|
|
// keep parantheses here: A operator * (int);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->isName() && tok->str() != ">" && Token::Match(tok->next(), "( %var% ) [;),+-*/><]]") && !Token::simpleMatch(tok->previous(), "operator"))
|
2009-03-04 07:24:03 +01:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok = tok->next();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
2009-12-11 19:28:37 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok->previous(), "( 0 ||") ||
|
|
|
|
Token::simpleMatch(tok, "|| 0 )") ||
|
|
|
|
Token::simpleMatch(tok->previous(), "( 1 &&") ||
|
|
|
|
Token::simpleMatch(tok, "&& 1 )"))
|
2009-12-11 19:28:37 +01:00
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "%num% ==|!=|<=|>=|<|> %num%") &&
|
|
|
|
MathLib::isInt(tok->str()) &&
|
|
|
|
MathLib::isInt(tok->tokAt(2)->str()))
|
2009-12-11 19:28:37 +01:00
|
|
|
{
|
2010-02-20 08:53:33 +01:00
|
|
|
const std::string prev(tok->previous() ? tok->strAt(-1).c_str() : "");
|
|
|
|
const std::string after(tok->tokAt(3) ? tok->strAt(3).c_str() : "");
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((prev == "(" || prev == "&&" || prev == "||") && (after == ")" || after == "&&" || after == "||"))
|
2009-12-11 19:28:37 +01:00
|
|
|
{
|
|
|
|
const int op1(MathLib::toLongNumber(tok->str()));
|
|
|
|
const std::string &cmp(tok->next()->str());
|
|
|
|
const int op2(MathLib::toLongNumber(tok->tokAt(2)->str()));
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (cmp == "==")
|
2009-12-11 19:28:37 +01:00
|
|
|
result = (op1 == op2) ? "1" : "0";
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (cmp == "!=")
|
2009-12-11 19:28:37 +01:00
|
|
|
result = (op1 != op2) ? "1" : "0";
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (cmp == "<=")
|
2009-12-11 19:28:37 +01:00
|
|
|
result = (op1 <= op2) ? "1" : "0";
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (cmp == ">=")
|
2009-12-11 19:28:37 +01:00
|
|
|
result = (op1 >= op2) ? "1" : "0";
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (cmp == "<")
|
2009-12-11 19:28:37 +01:00
|
|
|
result = (op1 < op2) ? "1" : "0";
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (cmp == ">")
|
2009-12-11 19:28:37 +01:00
|
|
|
result = (op1 > op2) ? "1" : "0";
|
|
|
|
|
|
|
|
tok->str(result);
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
2010-03-27 07:21:08 +01:00
|
|
|
|
2010-05-29 20:27:08 +02:00
|
|
|
if (Token::Match(tok->previous(), "[([,=] %num% <<|>> %num%"))
|
2010-03-27 07:21:08 +01:00
|
|
|
{
|
|
|
|
const int op1(MathLib::toLongNumber(tok->str()));
|
|
|
|
const int op2(MathLib::toLongNumber(tok->tokAt(2)->str()));
|
|
|
|
int result;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->next()->str() == "<<")
|
2010-03-27 07:21:08 +01:00
|
|
|
result = op1 << op2;
|
|
|
|
else
|
|
|
|
result = op1 >> op2;
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << result;
|
|
|
|
|
|
|
|
tok->str(ss.str());
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
2009-03-04 07:24:03 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2009-08-24 23:10:12 +02:00
|
|
|
void Tokenizer::simplifyGoto()
|
|
|
|
{
|
|
|
|
std::list<Token *> gotos;
|
|
|
|
unsigned int indentlevel = 0;
|
|
|
|
Token *beginfunction = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{")
|
2009-08-26 18:46:56 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (beginfunction == 0 && indentlevel == 0 && tok->link())
|
2009-08-26 18:46:56 +02:00
|
|
|
tok = tok->link();
|
|
|
|
else
|
|
|
|
++indentlevel;
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "}")
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel == 0)
|
2009-08-24 23:10:12 +02:00
|
|
|
break; // break out - it seems the code is wrong
|
|
|
|
--indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel == 0)
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
gotos.clear();
|
|
|
|
beginfunction = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (indentlevel == 0 && Token::Match(tok, ") const| {"))
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
gotos.clear();
|
|
|
|
beginfunction = tok;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "goto %var% ;"))
|
2009-08-24 23:10:12 +02:00
|
|
|
gotos.push_back(tok);
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (indentlevel == 1 && Token::Match(tok->previous(), "[};] %var% :"))
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
// Is this label at the end..
|
|
|
|
bool end = false;
|
2010-04-11 13:49:50 +02:00
|
|
|
int level = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "}")
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
--level;
|
|
|
|
if (level < 0)
|
2009-09-30 13:30:53 +02:00
|
|
|
{
|
|
|
|
end = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "{")
|
2009-09-30 13:30:53 +02:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
++level;
|
2009-08-24 23:10:12 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "%var% :") || tok2->str() == "goto")
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!end)
|
2009-08-24 23:10:12 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const std::string name(tok->str());
|
|
|
|
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "; %any%"))
|
2010-02-21 09:47:41 +01:00
|
|
|
tok->deleteThis();
|
2009-08-24 23:10:12 +02:00
|
|
|
|
|
|
|
// This label is at the end of the function.. replace all matching goto statements..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<Token *>::iterator it = gotos.begin(); it != gotos.end(); ++it)
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
Token *token = *it;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (token->next()->str() == name)
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
// Delete the "goto name;"
|
|
|
|
token = token->previous();
|
|
|
|
token->deleteNext();
|
|
|
|
token->deleteNext();
|
|
|
|
token->deleteNext();
|
|
|
|
|
|
|
|
// Insert the statements..
|
2010-03-06 13:48:43 +01:00
|
|
|
bool ret = false; // is there return
|
2010-02-25 20:03:39 +01:00
|
|
|
bool ret2 = false; // is there return in indentlevel 0
|
2009-08-28 22:31:11 +02:00
|
|
|
std::list<Token*> links;
|
2009-10-01 09:25:32 +02:00
|
|
|
std::list<Token*> links2;
|
|
|
|
std::list<Token*> links3;
|
2009-09-30 13:30:53 +02:00
|
|
|
int lev = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "}")
|
2009-09-30 13:30:53 +02:00
|
|
|
{
|
|
|
|
--lev;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (lev < 0)
|
2009-09-30 13:30:53 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "{")
|
2009-09-30 13:30:53 +02:00
|
|
|
{
|
|
|
|
++lev;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "return")
|
2010-02-25 20:03:39 +01:00
|
|
|
{
|
2009-08-24 23:10:12 +02:00
|
|
|
ret = true;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel == 1 && lev == 0)
|
2010-02-25 20:03:39 +01:00
|
|
|
ret2 = true;
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
token->insertToken(tok2->str().c_str());
|
|
|
|
token = token->next();
|
2009-12-02 19:47:35 +01:00
|
|
|
token->linenr(tok2->linenr());
|
2010-01-22 20:20:43 +01:00
|
|
|
token->varId(tok2->varId());
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ret2 && tok2->str() == ";")
|
2010-02-25 20:03:39 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (token->str() == "(")
|
2009-08-28 22:31:11 +02:00
|
|
|
{
|
|
|
|
links.push_back(token);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (token->str() == ")")
|
2009-08-28 22:31:11 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (links.empty())
|
2009-08-28 22:31:11 +02:00
|
|
|
{
|
|
|
|
// This should never happen at this point
|
|
|
|
syntaxError(token, ')');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::createMutualLinks(links.back(), token);
|
|
|
|
links.pop_back();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (token->str() == "{")
|
2009-10-01 09:25:32 +02:00
|
|
|
{
|
|
|
|
links2.push_back(token);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (token->str() == "}")
|
2009-10-01 09:25:32 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (links2.empty())
|
2009-10-01 09:25:32 +02:00
|
|
|
{
|
|
|
|
// This should never happen at this point
|
|
|
|
syntaxError(token, '}');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::createMutualLinks(links2.back(), token);
|
|
|
|
links2.pop_back();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (token->str() == "[")
|
2009-10-01 09:25:32 +02:00
|
|
|
{
|
|
|
|
links3.push_back(token);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (token->str() == "]")
|
2009-10-01 09:25:32 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (links3.empty())
|
2009-10-01 09:25:32 +02:00
|
|
|
{
|
|
|
|
// This should never happen at this point
|
|
|
|
syntaxError(token, ']');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::createMutualLinks(links3.back(), token);
|
|
|
|
links3.pop_back();
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
}
|
2009-08-28 22:31:11 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!ret)
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
token->insertToken("return");
|
|
|
|
token = token->next();
|
|
|
|
token->insertToken(";");
|
|
|
|
token = token->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gotos.clear();
|
|
|
|
tok = beginfunction;
|
|
|
|
indentlevel = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-05 18:46:27 +02:00
|
|
|
void Tokenizer::simplifyNestedStrcat()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-09-05 18:46:27 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! Token::Match(tok, "[;{}] strcat ( strcat ("))
|
2009-09-05 18:46:27 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find inner strcat call
|
|
|
|
Token *tok2 = tok->tokAt(3);
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::simpleMatch(tok2, "strcat ( strcat"))
|
2009-09-05 18:46:27 +02:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
|
|
|
|
2009-11-27 22:21:13 +01:00
|
|
|
// If we have this code:
|
|
|
|
// strcat(strcat(dst, foo), bar);
|
|
|
|
// We move this part of code before all strcat() calls: strcat(dst, foo)
|
|
|
|
// And place "dst" token where the code was.
|
|
|
|
Token *prevTok = tok2->previous();
|
2009-08-24 23:10:12 +02:00
|
|
|
|
2009-11-27 22:21:13 +01:00
|
|
|
// Move tokens to new place
|
|
|
|
Token::move(tok2, tok2->next()->link(), tok);
|
|
|
|
tok = tok2->next()->link();
|
2009-09-05 18:46:27 +02:00
|
|
|
|
2009-11-27 22:21:13 +01:00
|
|
|
// Insert the "dst" token
|
|
|
|
prevTok->insertToken(tok2->strAt(2));
|
2009-09-05 18:46:27 +02:00
|
|
|
|
2009-11-27 22:21:13 +01:00
|
|
|
// Insert semicolon after the moved strcat()
|
|
|
|
tok->insertToken(";");
|
2009-09-05 18:46:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
|
2010-02-16 07:33:23 +01:00
|
|
|
void Tokenizer::duplicateEnumError(const Token * tok1, const Token * tok2, const std::string & type)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!(_settings && _settings->_checkCodingStyle))
|
2010-02-16 07:33:23 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok1->linenr();
|
|
|
|
loc.file = file(tok1);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
loc.line = tok2->linenr();
|
|
|
|
loc.file = file(tok2);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
"style",
|
|
|
|
std::string(type + " '" + tok2->str() +
|
2010-04-15 20:08:51 +02:00
|
|
|
"' hides enumerator with same name"),
|
2010-02-16 07:33:23 +01:00
|
|
|
"variableHidingEnum");
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_errorLogger)
|
2010-02-16 07:33:23 +01:00
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this statement is a duplicate definition. A duplicate
|
|
|
|
// definition will hide the enumerator within it's scope so just
|
|
|
|
// skip the entire scope of the duplicate.
|
|
|
|
bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name)
|
|
|
|
{
|
|
|
|
// check for an end of definition
|
|
|
|
const Token * tok = *tokPtr;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok && tok->next() && Token::Match(tok->next(), ";|,|[|=|)|>"))
|
2010-02-16 07:33:23 +01:00
|
|
|
{
|
|
|
|
const Token * end = tok->next();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end->str() == "[")
|
2010-02-16 07:33:23 +01:00
|
|
|
{
|
|
|
|
end = end->link()->next();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (end->str() == ",")
|
2010-02-16 07:33:23 +01:00
|
|
|
{
|
2010-02-26 21:40:57 +01:00
|
|
|
// check for function argument
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous(), "(|,"))
|
2010-02-26 21:40:57 +01:00
|
|
|
return false;
|
|
|
|
|
2010-02-16 07:33:23 +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-16 07:33:23 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end->next()->str() == "(")
|
2010-02-16 07:33:23 +01:00
|
|
|
level++;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (end->next()->str() == ")")
|
2010-02-16 07:33:23 +01:00
|
|
|
level--;
|
|
|
|
|
|
|
|
end = end->next();
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (end->str() == ")")
|
2010-02-26 21:40:57 +01:00
|
|
|
{
|
|
|
|
// check of function argument
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous()->str() == ",")
|
2010-02-26 21:40:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-16 07:33:23 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end)
|
2010-02-16 07:33:23 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(end, ") {")) // function parameter ?
|
2010-02-16 07:33:23 +01:00
|
|
|
{
|
|
|
|
// look backwards
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous()->str() == "enum" ||
|
|
|
|
(Token::Match(tok->previous(), "%type%") &&
|
|
|
|
tok->previous()->str() != "return"))
|
2010-02-16 07:33:23 +01:00
|
|
|
{
|
|
|
|
duplicateEnumError(*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-16 07:33:23 +01:00
|
|
|
{
|
|
|
|
// look backwards
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous()->str() == "enum" ||
|
|
|
|
(Token::Match(tok->previous(), "%type%") &&
|
|
|
|
tok->previous()->str() != "return"))
|
2010-02-16 07:33:23 +01:00
|
|
|
{
|
|
|
|
// duplicate definition so skip entire template
|
2010-04-02 07:30:58 +02:00
|
|
|
while (end && end->str() != "{")
|
2010-02-16 07:33:23 +01:00
|
|
|
end = end->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end)
|
2010-02-16 07:33:23 +01:00
|
|
|
{
|
|
|
|
duplicateEnumError(*tokPtr, name, "Template parameter");
|
|
|
|
*tokPtr = end->link();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// look backwards
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous(), "enum|,") ||
|
|
|
|
(Token::Match(tok->previous(), "%type%") &&
|
|
|
|
tok->previous()->str() != "return"))
|
2010-02-16 07:33:23 +01:00
|
|
|
{
|
|
|
|
duplicateEnumError(*tokPtr, name, "Variable");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
|
2010-01-04 17:22:06 +01:00
|
|
|
void Tokenizer::simplifyEnum()
|
|
|
|
{
|
|
|
|
std::string className;
|
|
|
|
int classLevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "class|struct|namespace %any%"))
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
|
|
|
className = tok->next()->str();
|
|
|
|
classLevel = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "}")
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
|
|
|
--classLevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (classLevel < 0)
|
2010-01-04 17:22:06 +01:00
|
|
|
className = "";
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "{")
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
|
|
|
++classLevel;
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "enum {") ||
|
|
|
|
Token::Match(tok, "enum %type% {"))
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
|
|
|
Token * tok1;
|
2010-01-29 15:57:26 +01:00
|
|
|
Token * start = tok;
|
2010-01-04 17:22:06 +01:00
|
|
|
Token * end;
|
2010-01-29 15:57:26 +01:00
|
|
|
Token * enumType = 0;
|
2010-01-04 17:22:06 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(1)->str() == "{")
|
2010-01-04 17:22:06 +01:00
|
|
|
tok1 = tok->tokAt(2);
|
|
|
|
else
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
|
|
|
enumType = tok->tokAt(1);
|
2010-01-04 17:22:06 +01:00
|
|
|
tok1 = tok->tokAt(3);
|
2010-01-29 15:57:26 +01:00
|
|
|
}
|
2010-01-04 17:22:06 +01:00
|
|
|
|
|
|
|
end = tok1->tokAt(-1)->link();
|
|
|
|
|
2010-02-05 07:37:17 +01:00
|
|
|
long lastValue = -1;
|
|
|
|
Token * lastEnumValueStart = 0;
|
|
|
|
Token * lastEnumValueEnd = 0;
|
2010-01-04 17:22:06 +01:00
|
|
|
|
2010-02-16 07:33:23 +01:00
|
|
|
// iterate over all enumerators between { and }
|
|
|
|
// Give each enumerator the const value specified or if not specified, 1 + the
|
|
|
|
// previous value or 0 if it is the first one.
|
2010-04-02 07:30:58 +02:00
|
|
|
for (; tok1 && tok1 != end; tok1 = tok1->next())
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
|
|
|
Token * enumName = 0;
|
|
|
|
Token * enumValue = 0;
|
2010-02-05 07:37:17 +01:00
|
|
|
Token * enumValueStart = 0;
|
|
|
|
Token * enumValueEnd = 0;
|
2010-01-04 17:22:06 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok1->str() == "(")
|
2010-03-07 12:50:45 +01:00
|
|
|
{
|
|
|
|
tok1 = tok1->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok1->previous(), ",|{ %type% ,|}"))
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
2010-02-16 07:33:23 +01:00
|
|
|
// no value specified
|
2010-01-04 17:22:06 +01:00
|
|
|
enumName = tok1;
|
2010-02-05 07:37:17 +01:00
|
|
|
lastValue++;
|
2010-01-04 17:22:06 +01:00
|
|
|
tok1->insertToken("=");
|
|
|
|
tok1 = tok1->next();
|
2010-02-05 07:37:17 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (lastEnumValueStart && lastEnumValueEnd)
|
2010-02-05 07:37:17 +01:00
|
|
|
{
|
2010-02-16 07:33:23 +01:00
|
|
|
// previous value was an expression
|
2010-02-05 07:37:17 +01:00
|
|
|
Token * valueStart = tok1;
|
|
|
|
std::stack<Token *> links;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = lastEnumValueStart; tok2 != lastEnumValueEnd->next(); tok2 = tok2->next())
|
2010-02-05 07:37:17 +01:00
|
|
|
{
|
2010-02-16 07:33:23 +01:00
|
|
|
tok1->insertToken(tok2->str());
|
2010-02-05 07:37:17 +01:00
|
|
|
tok1 = tok1->next();
|
|
|
|
|
|
|
|
// Check for links and fix them up
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok1->str() == "(" || tok1->str() == "[" || tok1->str() == "{")
|
2010-02-05 07:37:17 +01:00
|
|
|
links.push(tok1);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok1->str() == ")" || tok1->str() == "]" || tok1->str() == "}")
|
2010-02-05 07:37:17 +01:00
|
|
|
{
|
|
|
|
Token * link = links.top();
|
|
|
|
|
|
|
|
tok1->link(link);
|
|
|
|
link->link(tok1);
|
|
|
|
|
|
|
|
links.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-16 07:33:23 +01:00
|
|
|
// value is previous expression + 1
|
2010-02-05 07:37:17 +01:00
|
|
|
tok1->insertToken("+");
|
|
|
|
tok1 = tok1->next();
|
2010-02-16 07:33:23 +01:00
|
|
|
tok1->insertToken(MathLib::toString<long>(lastValue));
|
2010-02-05 07:37:17 +01:00
|
|
|
enumValue = 0;
|
|
|
|
enumValueStart = valueStart->next();
|
|
|
|
enumValueEnd = tok1->next();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-16 07:33:23 +01:00
|
|
|
// value is previous numeric value + 1
|
2010-02-14 19:58:17 +01:00
|
|
|
tok1->insertToken(MathLib::toString<long>(lastValue));
|
2010-02-05 07:37:17 +01:00
|
|
|
enumValue = tok1->next();
|
|
|
|
}
|
2010-01-04 17:22:06 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok1->previous(), ",|{ %type% = %num% ,|}"))
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
2010-02-16 07:33:23 +01:00
|
|
|
// value is specified numeric value
|
2010-01-04 17:22:06 +01:00
|
|
|
enumName = tok1;
|
2010-02-14 19:58:17 +01:00
|
|
|
lastValue = MathLib::toLongNumber(tok1->strAt(2));
|
2010-01-04 17:22:06 +01:00
|
|
|
enumValue = tok1->tokAt(2);
|
2010-02-05 07:37:17 +01:00
|
|
|
lastEnumValueStart = 0;
|
|
|
|
lastEnumValueEnd = 0;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok1->previous(), ",|{ %type% = "))
|
2010-02-05 07:37:17 +01:00
|
|
|
{
|
2010-02-16 07:33:23 +01:00
|
|
|
// value is specified expression
|
2010-02-05 07:37:17 +01:00
|
|
|
enumName = tok1;
|
|
|
|
lastValue = 0;
|
|
|
|
tok1 = tok1->tokAt(2);
|
|
|
|
enumValueStart = tok1;
|
|
|
|
enumValueEnd = tok1;
|
2010-02-06 09:49:56 +01:00
|
|
|
int level = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (enumValueEnd->str() == "(" ||
|
|
|
|
enumValueEnd->str() == "[" ||
|
|
|
|
enumValueEnd->str() == "{")
|
2010-02-25 07:24:32 +01:00
|
|
|
level++;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (enumValueEnd->next() &&
|
|
|
|
(!Token::Match(enumValueEnd->next(), "}|,") || level))
|
2010-02-06 09:49:56 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (enumValueEnd->next()->str() == "(" ||
|
|
|
|
enumValueEnd->next()->str() == "[" ||
|
|
|
|
enumValueEnd->next()->str() == "{")
|
2010-02-06 09:49:56 +01:00
|
|
|
level++;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (enumValueEnd->next()->str() == ")" ||
|
|
|
|
enumValueEnd->next()->str() == "]" ||
|
|
|
|
enumValueEnd->next()->str() == "}")
|
2010-02-06 09:49:56 +01:00
|
|
|
level--;
|
|
|
|
|
2010-02-05 07:37:17 +01:00
|
|
|
enumValueEnd = enumValueEnd->next();
|
2010-02-06 09:49:56 +01:00
|
|
|
}
|
2010-02-16 07:33:23 +01:00
|
|
|
// remember this expression in case it needs to be incremented
|
2010-02-05 07:37:17 +01:00
|
|
|
lastEnumValueStart = enumValueStart;
|
|
|
|
lastEnumValueEnd = enumValueEnd;
|
2010-02-16 07:33:23 +01:00
|
|
|
// skip over expression
|
2010-02-06 09:49:56 +01:00
|
|
|
tok1 = enumValueEnd;
|
2010-01-04 17:22:06 +01:00
|
|
|
}
|
|
|
|
|
2010-02-16 07:33:23 +01:00
|
|
|
// find all uses of this enumerator and substitute it's value for it's name
|
2010-04-02 07:30:58 +02:00
|
|
|
if (enumName && (enumValue || (enumValueStart && enumValueEnd)))
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
|
|
|
const std::string pattern(className.empty() ? "" : (className + " :: " + enumName->str()).c_str());
|
2010-02-05 07:37:17 +01:00
|
|
|
int level = 1;
|
2010-01-04 17:22:06 +01:00
|
|
|
bool inScope = true;
|
|
|
|
|
|
|
|
bool exitThisScope = false;
|
|
|
|
int exitScope = 0;
|
2010-04-11 13:49:50 +02:00
|
|
|
bool simplify = false;
|
2010-01-29 15:57:26 +01:00
|
|
|
bool hasClass = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok1->next(); tok2; tok2 = tok2->next())
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "}")
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
|
|
|
--level;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (level < 0)
|
2010-01-04 17:22:06 +01:00
|
|
|
inScope = false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (exitThisScope)
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (level < exitScope)
|
2010-01-04 17:22:06 +01:00
|
|
|
exitThisScope = false;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "{")
|
2010-02-16 07:33:23 +01:00
|
|
|
{
|
2010-01-04 17:22:06 +01:00
|
|
|
++level;
|
2010-02-16 07:33:23 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (!pattern.empty() && Token::Match(tok2, pattern.c_str()))
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
simplify = true;
|
2010-01-29 15:57:26 +01:00
|
|
|
hasClass = true;
|
2010-01-04 17:22:06 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (inScope && !exitThisScope && tok2->str() == enumName->str())
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok2->previous(), "::") ||
|
|
|
|
(tok2->next() && Token::simpleMatch(tok2->next(), "::")))
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
2010-02-17 22:42:08 +01:00
|
|
|
// Don't replace this enum if it's preceded or followed by "::"
|
2010-01-04 17:22:06 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (!duplicateDefinition(&tok2, enumName))
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
simplify = true;
|
2010-01-29 15:57:26 +01:00
|
|
|
hasClass = false;
|
|
|
|
}
|
2010-02-16 07:33:23 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// something with the same name.
|
|
|
|
exitScope = level;
|
|
|
|
}
|
2010-01-04 17:22:06 +01:00
|
|
|
}
|
|
|
|
|
2010-04-11 13:49:50 +02:00
|
|
|
if (simplify)
|
2010-01-04 17:22:06 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (enumValue)
|
2010-02-05 07:37:17 +01:00
|
|
|
tok2->str(enumValue->strAt(0));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::stack<Token *> links;
|
|
|
|
tok2->str(enumValueStart->strAt(0));
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(" || tok2->str() == "[" || tok2->str() == "{")
|
2010-02-05 07:37:17 +01:00
|
|
|
links.push(tok2);
|
|
|
|
Token * nextToken = enumValueStart->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
for (; nextToken != enumValueEnd->next(); nextToken = nextToken->next())
|
2010-02-05 07:37:17 +01:00
|
|
|
{
|
2010-02-16 07:33:23 +01:00
|
|
|
tok2->insertToken(nextToken->str());
|
2010-02-05 07:37:17 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
// Check for links and fix them up
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(" || tok2->str() == "[" || tok2->str() == "{")
|
2010-02-05 07:37:17 +01:00
|
|
|
links.push(tok2);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == ")" || tok2->str() == "]" || tok2->str() == "}")
|
2010-02-05 07:37:17 +01:00
|
|
|
{
|
|
|
|
Token * link = links.top();
|
|
|
|
|
|
|
|
tok2->link(link);
|
|
|
|
link->link(tok2);
|
|
|
|
|
|
|
|
links.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 17:22:06 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (hasClass)
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
|
|
|
tok2->deleteNext();
|
|
|
|
tok2->deleteNext();
|
|
|
|
}
|
|
|
|
|
2010-04-11 13:49:50 +02:00
|
|
|
simplify = false;
|
2010-01-04 17:22:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-29 15:57:26 +01:00
|
|
|
|
2010-02-25 07:24:32 +01:00
|
|
|
// check for a variable definition: enum {} x;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (end->next()->str() != ";")
|
2010-02-25 07:24:32 +01:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
Token * tempTok = end;
|
2010-02-25 07:24:32 +01:00
|
|
|
|
2010-04-11 13:49:50 +02:00
|
|
|
tempTok->insertToken(";");
|
|
|
|
tempTok = tempTok->next();
|
|
|
|
tempTok->insertToken("int");
|
2010-02-25 07:24:32 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (enumType)
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
|
|
|
const std::string pattern(className.empty() ? "" : (className + " :: " + enumType->str()).c_str());
|
|
|
|
int level = 0;
|
|
|
|
bool inScope = true;
|
|
|
|
|
|
|
|
bool exitThisScope = false;
|
|
|
|
int exitScope = 0;
|
2010-04-11 13:49:50 +02:00
|
|
|
bool simplify = false;
|
2010-01-29 15:57:26 +01:00
|
|
|
bool hasClass = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = end->next(); tok2; tok2 = tok2->next())
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "}")
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
|
|
|
--level;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (level < 0)
|
2010-01-29 15:57:26 +01:00
|
|
|
inScope = false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (exitThisScope)
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (level < exitScope)
|
2010-01-29 15:57:26 +01:00
|
|
|
exitThisScope = false;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "{")
|
2010-01-29 15:57:26 +01:00
|
|
|
++level;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (!pattern.empty() && ((Token::Match(tok2, "enum") && Token::Match(tok2->next(), pattern.c_str())) || Token::Match(tok2, pattern.c_str())))
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
simplify = true;
|
2010-01-29 15:57:26 +01:00
|
|
|
hasClass = true;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (inScope && !exitThisScope && (tok2->str() == enumType->str() || (tok2->str() == "enum" && tok2->next()->str() == enumType->str())))
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok2->previous(), "::"))
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
|
|
|
// Don't replace this enum if it's preceded by "::"
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->next() && tok2->next()->isName())
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
2010-04-11 13:49:50 +02:00
|
|
|
simplify = true;
|
2010-01-29 15:57:26 +01:00
|
|
|
hasClass = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-11 13:49:50 +02:00
|
|
|
if (simplify)
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "enum")
|
2010-01-29 15:57:26 +01:00
|
|
|
tok2->deleteNext();
|
|
|
|
tok2->str("int");
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (hasClass)
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
|
|
|
tok2->deleteNext();
|
|
|
|
tok2->deleteNext();
|
|
|
|
}
|
|
|
|
|
2010-04-11 13:49:50 +02:00
|
|
|
simplify = false;
|
2010-01-29 15:57:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tok1 = start;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok1->next() && tok1->next() != end)
|
2010-01-29 15:57:26 +01:00
|
|
|
tok1->deleteNext();
|
2010-01-31 07:13:09 +01:00
|
|
|
tok1->deleteNext();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (start != _tokens)
|
2010-01-29 15:57:26 +01:00
|
|
|
{
|
|
|
|
tok1 = start->previous();
|
|
|
|
tok1->deleteNext();
|
2010-01-31 07:13:09 +01:00
|
|
|
tok = tok1;
|
2010-01-29 15:57:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
_tokens->deleteThis();
|
2010-01-04 17:22:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-12 21:25:31 +01:00
|
|
|
|
|
|
|
void Tokenizer::simplifyStd()
|
|
|
|
{
|
|
|
|
std::set<std::string> f;
|
|
|
|
f.insert("strcat");
|
|
|
|
f.insert("strcpy");
|
|
|
|
f.insert("strncat");
|
|
|
|
f.insert("strncpy");
|
|
|
|
f.insert("free");
|
|
|
|
f.insert("malloc");
|
|
|
|
f.insert("strdup");
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-01-12 21:25:31 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != "std")
|
2010-01-12 21:25:31 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous(), "[(,{};] std :: %var% (") &&
|
|
|
|
f.find(tok->strAt(2)) != f.end())
|
2010-01-12 21:25:31 +01:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Helper functions for handling the tokens list
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-07-05 22:16:43 +02:00
|
|
|
const Token *Tokenizer::getFunctionTokenByName(const char funcname[]) const
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 0; i < _functionList.size(); ++i)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_functionList[i]->str() == funcname)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
return _functionList[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Tokenizer::fillFunctionList()
|
|
|
|
{
|
|
|
|
_functionList.clear();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-01-16 13:56:57 +01:00
|
|
|
tok = tok->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2010-01-16 13:56:57 +01:00
|
|
|
break;
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "%var% ("))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Check if this is the first token of a function implementation..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == ";")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
tok = tok2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "{")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == ")")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, ") const| {"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
_functionList.push_back(tok);
|
|
|
|
tok = tok2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok = tok2;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok->next() && !Token::Match(tok->next(), "[;{]"))
|
2009-01-24 18:15:38 +01:00
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the _functionList functions with duplicate names, remove them
|
2009-07-14 08:17:12 +02:00
|
|
|
/** @todo handle when functions with the same name */
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int func1 = 0; func1 < _functionList.size();)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
bool hasDuplicates = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int func2 = func1 + 1; func2 < _functionList.size();)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_functionList[func1]->str() == _functionList[func2]->str())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
hasDuplicates = true;
|
|
|
|
_functionList.erase(_functionList.begin() + func2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++func2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! hasDuplicates)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
++func1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_functionList.erase(_functionList.begin() + func1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Deallocate lists..
|
2009-07-05 22:16:43 +02:00
|
|
|
void Tokenizer::deallocateTokens()
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
deleteTokens(_tokens);
|
|
|
|
_tokens = 0;
|
|
|
|
_tokensBack = 0;
|
|
|
|
_files.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tokenizer::deleteTokens(Token *tok)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
Token *next = tok->next();
|
|
|
|
delete tok;
|
|
|
|
tok = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const char *Tokenizer::getParameterName(const Token *ftok, int par)
|
|
|
|
{
|
|
|
|
int _par = 1;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (; ftok; ftok = ftok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ftok->str() == ",")
|
2009-01-24 18:15:38 +01:00
|
|
|
++_par;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (par == _par && Token::Match(ftok, "%var% [,)]"))
|
2009-05-03 20:10:59 +02:00
|
|
|
return ftok->str().c_str();
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
std::string Tokenizer::fileLine(const Token *tok) const
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "[" << _files.at(tok->fileIndex()) << ":" << tok->linenr() << "]";
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:51:04 +01:00
|
|
|
std::string Tokenizer::file(const Token *tok) const
|
|
|
|
{
|
|
|
|
return _files.at(tok->fileIndex());
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2009-03-13 22:28:44 +01:00
|
|
|
|
2010-02-14 19:58:17 +01:00
|
|
|
const Token * Tokenizer::findClassFunction(const Token *tok, const std::string &classname, const std::string &funcname, int &indentlevel, bool isStruct) const
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel < 0 || tok == NULL)
|
2009-03-13 22:28:44 +01:00
|
|
|
return NULL;
|
2010-01-29 22:22:18 +01:00
|
|
|
/*
|
|
|
|
// TODO: This is currently commented out as updateClassList doesn't
|
|
|
|
// fully work yet and call to updateClassList is currently also
|
|
|
|
// commented out.
|
|
|
|
|
|
|
|
//std::cout << tok->str()<<"--\n";
|
|
|
|
if( tok == _tokens || tok->str() == "class" || tok->str() == "struct")
|
|
|
|
tok = 0;
|
|
|
|
|
|
|
|
std::map<std::string, ClassInfo>::const_iterator iter;
|
|
|
|
iter = _classInfoList.find(classname);
|
|
|
|
if( iter == _classInfoList.end() )
|
|
|
|
return NULL;
|
2009-03-13 22:28:44 +01:00
|
|
|
|
2010-01-29 22:22:18 +01:00
|
|
|
for( size_t i = 0; i < iter->second._memberFunctions.size(); i++ )
|
|
|
|
if( Token::Match( iter->second._memberFunctions[i]._declaration, funcname ) )
|
|
|
|
{
|
|
|
|
if( tok != 0 )
|
|
|
|
{
|
|
|
|
if( tok == iter->second._memberFunctions[i]._implementation ||
|
|
|
|
tok->previous() == iter->second._memberFunctions[i]._implementation)
|
|
|
|
tok = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iter->second._memberFunctions[i]._implementation;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
*/
|
2010-01-02 17:29:55 +01:00
|
|
|
const std::string classPattern(std::string(isStruct ? "struct " : "class ") + classname + " :|{");
|
2010-01-02 08:33:54 +01:00
|
|
|
const std::string internalPattern(std::string("!!~ ") + funcname + " (");
|
2010-04-02 19:29:54 +02:00
|
|
|
const std::string externalPattern(classname + " :: " + funcname + " (");
|
2009-03-13 22:28:44 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (; tok; tok = tok->next())
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel == 0 && Token::Match(tok, classPattern.c_str()))
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok && tok->str() != "{")
|
2009-03-13 22:28:44 +01:00
|
|
|
tok = tok->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok)
|
2009-03-13 22:28:44 +01:00
|
|
|
tok = tok->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! tok)
|
2009-03-13 22:28:44 +01:00
|
|
|
break;
|
|
|
|
indentlevel = 1;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{")
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
|
|
|
// If indentlevel==0 don't go to indentlevel 1. Skip the block.
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel > 0)
|
2009-03-13 22:28:44 +01:00
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2010-01-02 08:33:54 +01:00
|
|
|
// skip the block
|
|
|
|
tok = tok->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok == NULL)
|
2009-03-13 22:28:44 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "}")
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
|
|
|
--indentlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel < 0)
|
2009-03-13 22:28:44 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (indentlevel == 1)
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
|
|
|
// Member function implemented in the class declaration?
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous(), internalPattern.c_str()))
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
2010-01-02 08:33:54 +01:00
|
|
|
const Token *tok2 = tok;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2 && tok2->str() != "{" && tok2->str() != ";")
|
2009-03-13 22:28:44 +01:00
|
|
|
tok2 = tok2->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2 && tok2->str() == "{")
|
2010-01-02 08:33:54 +01:00
|
|
|
return tok;
|
2009-03-13 22:28:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (indentlevel == 0 && Token::Match(tok, externalPattern.c_str()))
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
2010-04-02 19:29:54 +02:00
|
|
|
if (!Token::simpleMatch(tok->previous(), "::"))
|
|
|
|
return tok;
|
2009-03-13 22:28:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2009-05-07 22:17:29 +02:00
|
|
|
// Error message for bad iterator usage..
|
2009-03-13 22:28:44 +01:00
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
void Tokenizer::syntaxError(const Token *tok, char c)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_settings && _settings->_debug)
|
2009-05-12 23:01:53 +02:00
|
|
|
{
|
|
|
|
_tokens->printOut();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!_errorLogger && tok)
|
2009-05-11 21:52:04 +02:00
|
|
|
{
|
2009-09-01 19:30:54 +02:00
|
|
|
std::ostringstream err;
|
|
|
|
err << "### Unlogged error at Tokenizer::syntaxError: Invalid number of character (" << c << ")";
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_settings && _settings->_debug)
|
2009-09-01 19:30:54 +02:00
|
|
|
{
|
|
|
|
throw std::runtime_error(err.str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << err.str() << std::endl;
|
|
|
|
}
|
2009-05-11 21:52:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok)
|
2009-10-17 23:11:48 +02:00
|
|
|
{
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok->linenr();
|
|
|
|
loc.file = file(tok);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
"error",
|
2010-01-23 22:18:11 +01:00
|
|
|
std::string("Invalid number of character (") +
|
|
|
|
c +
|
|
|
|
") " +
|
|
|
|
"when these macros are defined: '" +
|
|
|
|
_configuration +
|
|
|
|
"'.",
|
2009-10-17 23:11:48 +02:00
|
|
|
"syntaxError");
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_errorLogger)
|
2009-10-17 23:11:48 +02:00
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
2009-05-11 21:52:04 +02:00
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
}
|
2009-03-13 22:28:44 +01:00
|
|
|
|
2009-11-28 22:08:43 +01:00
|
|
|
void Tokenizer::cppcheckError(const Token *tok) const
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!_errorLogger && tok)
|
2009-11-28 22:08:43 +01:00
|
|
|
{
|
|
|
|
std::ostringstream err;
|
|
|
|
err << "### Unlogged error at Tokenizer::cppcheckError";
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_settings && _settings->_debug)
|
2009-11-28 22:08:43 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error(err.str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << err.str() << std::endl;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok)
|
2009-11-28 22:08:43 +01:00
|
|
|
{
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok->linenr();
|
|
|
|
loc.file = file(tok);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
"error",
|
2009-11-29 16:13:14 +01:00
|
|
|
"### Internal error in Cppcheck. Please report it.",
|
2009-11-28 22:08:43 +01:00
|
|
|
"cppcheckError");
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_errorLogger)
|
2009-11-28 22:08:43 +01:00
|
|
|
_errorLogger->reportErr(errmsg);
|
2010-04-04 22:55:28 +02:00
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
2009-11-28 22:08:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-13 09:35:02 +02:00
|
|
|
void Tokenizer::simplifyMathFunctions()
|
2009-09-12 22:54:47 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-09-12 22:54:47 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "atol ( %str% )"))
|
2009-09-12 22:54:47 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!MathLib::isInt(tok->tokAt(2)->strValue()))
|
2009-09-12 22:54:47 +02:00
|
|
|
{
|
|
|
|
// Ignore strings which we can't convert
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous() &&
|
|
|
|
Token::simpleMatch(tok->previous()->previous(), "std ::"))
|
2009-09-12 22:54:47 +02:00
|
|
|
{
|
|
|
|
// Delete "std ::"
|
|
|
|
tok = tok->previous()->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete atol(
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
// Convert string into a number
|
|
|
|
tok->str(MathLib::toString(MathLib::toLongNumber(tok->strValue())));
|
|
|
|
|
|
|
|
// Delete remaining )
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-29 21:23:39 +02:00
|
|
|
void Tokenizer::simplifyComma()
|
2009-06-10 23:12:26 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-06-10 23:12:26 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "for (") ||
|
|
|
|
Token::Match(tok, "=|enum {"))
|
2009-07-28 18:40:15 +02:00
|
|
|
{
|
2009-08-05 22:02:40 +02:00
|
|
|
tok = tok->next()->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-08-05 22:02:40 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
continue;
|
2009-07-28 18:40:15 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "(")
|
2010-01-29 22:28:49 +01:00
|
|
|
{
|
|
|
|
tok = tok->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-12 08:12:00 +02:00
|
|
|
// Skip unhandled template specifiers..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "%var% <"))
|
2009-09-12 08:12:00 +02:00
|
|
|
{
|
|
|
|
// Todo.. use the link instead.
|
|
|
|
unsigned int comparelevel = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-09-12 08:12:00 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "<")
|
2009-09-12 08:12:00 +02:00
|
|
|
++comparelevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == ">")
|
2009-09-12 08:12:00 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (comparelevel <= 1)
|
2009-09-12 08:12:00 +02:00
|
|
|
{
|
|
|
|
tok = tok2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++comparelevel;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "[;{}]"))
|
2009-09-12 08:12:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 14:38:41 +02:00
|
|
|
// If token after the comma is a constant number, simplification is not required.
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() != "," || Token::Match(tok->next(), "%num%"))
|
2009-06-10 23:12:26 +02:00
|
|
|
continue;
|
2009-03-13 22:28:44 +01:00
|
|
|
|
2009-06-10 23:12:26 +02:00
|
|
|
// We must not accept just any keyword, e.g. accepting int
|
|
|
|
// would cause function parameters to corrupt.
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "delete"))
|
2009-06-18 23:00:16 +02:00
|
|
|
{
|
|
|
|
// Handle "delete a, delete b;"
|
|
|
|
tok->str(";");
|
|
|
|
}
|
2009-07-05 20:29:09 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->previous() && tok->previous()->previous())
|
2009-06-18 23:00:16 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->previous()->previous(), "delete") &&
|
|
|
|
tok->next()->varId() != 0)
|
2009-07-05 20:29:09 +02:00
|
|
|
{
|
|
|
|
// Handle "delete a, b;"
|
|
|
|
tok->str(";");
|
|
|
|
tok->insertToken("delete");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous())
|
2009-07-05 20:29:09 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "=")
|
2009-07-05 20:29:09 +02:00
|
|
|
{
|
|
|
|
// Handle "a = 0, b = 0;"
|
|
|
|
tok->str(";");
|
|
|
|
break;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "delete %var%") ||
|
|
|
|
Token::Match(tok2, "delete [ ] %var%"))
|
2009-07-27 21:13:08 +02:00
|
|
|
{
|
|
|
|
// Handle "delete a, a = 0;"
|
|
|
|
tok->str(";");
|
|
|
|
break;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, "[;,{}()]"))
|
2009-07-05 20:29:09 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-18 23:00:16 +02:00
|
|
|
}
|
2009-07-26 13:00:11 +02:00
|
|
|
|
|
|
|
bool inReturn = false;
|
|
|
|
Token *startFrom = NULL; // next tokean after "; return"
|
|
|
|
Token *endAt = NULL; // first ";" token after "; return"
|
|
|
|
|
|
|
|
// find "; return" pattern before comma
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->previous())
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, "[;{}]"))
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
break;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "return" && Token::Match(tok2->previous(), "[;{}]"))
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
inReturn = true;
|
|
|
|
startFrom = tok2->next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find token where return ends and also count commas
|
2010-04-02 07:30:58 +02:00
|
|
|
if (inReturn)
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
size_t commaCounter = 0;
|
|
|
|
size_t indentlevel = 0;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = startFrom; tok2; tok2 = tok2->next())
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == ";")
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
endAt = tok2;
|
|
|
|
break;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "(")
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
++indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == ")")
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
--indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "," && indentlevel == 0)
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
++commaCounter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (commaCounter)
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
indentlevel = 0;
|
|
|
|
|
|
|
|
// change tokens:
|
|
|
|
// "; return a ( ) , b ( ) , c ;"
|
|
|
|
// to
|
|
|
|
// "; return a ( ) ; b ( ) ; c ;"
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok2 = startFrom; tok2 != endAt; tok2 = tok2->next())
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2->str() == "(")
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
++indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == ")")
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
--indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok2->str() == "," && indentlevel == 0)
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
tok2->str(";");
|
|
|
|
--commaCounter;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (commaCounter == 0)
|
2009-07-26 22:13:08 +02:00
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
tok2->insertToken("return");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete old "return"
|
|
|
|
startFrom->previous()->deleteThis();
|
2010-05-21 08:26:53 +02:00
|
|
|
startFrom = 0; // give dead pointer a value
|
2009-07-26 13:00:11 +02:00
|
|
|
|
|
|
|
tok = endAt;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok)
|
2009-12-06 09:02:13 +01:00
|
|
|
return;
|
2009-07-26 13:00:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
2009-09-13 15:35:37 +02:00
|
|
|
|
|
|
|
|
2009-10-01 19:45:48 +02:00
|
|
|
void Tokenizer::removeExceptionSpecifications(Token *tok) const
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok)
|
2009-10-01 19:45:48 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == "{")
|
2009-10-01 19:45:48 +02:00
|
|
|
tok = tok->link();
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->str() == "}")
|
2009-10-01 19:45:48 +02:00
|
|
|
break;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::simpleMatch(tok, ") throw ("))
|
2009-10-01 19:45:48 +02:00
|
|
|
{
|
2010-02-02 19:56:41 +01:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(2)->link());
|
|
|
|
tok->deleteNext();
|
2009-10-01 19:45:48 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "class %type%"))
|
2009-10-01 19:45:48 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok && !Token::Match(tok, "[;{]"))
|
2009-10-01 19:45:48 +02:00
|
|
|
tok = tok->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok && tok->str() == "{")
|
2009-10-01 19:45:48 +02:00
|
|
|
{
|
|
|
|
removeExceptionSpecifications(tok->next());
|
|
|
|
tok = tok->link();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = tok ? tok->next() : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-13 15:35:37 +02:00
|
|
|
bool Tokenizer::validate() const
|
|
|
|
{
|
|
|
|
std::stack<const Token *> linktok;
|
2010-01-06 00:32:05 +01:00
|
|
|
const Token *lastTok = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok = tokens(); tok; tok = tok->next())
|
2009-09-13 15:35:37 +02:00
|
|
|
{
|
2010-01-06 00:32:05 +01:00
|
|
|
lastTok = tok;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "[{([]"))
|
2009-09-13 15:35:37 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->link() == 0)
|
2009-11-10 22:10:56 +01:00
|
|
|
{
|
2009-11-28 22:08:43 +01:00
|
|
|
cppcheckError(tok);
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-13 15:35:37 +02:00
|
|
|
linktok.push(tok);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "[})]]"))
|
2009-09-13 15:35:37 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->link() == 0)
|
2009-11-10 22:10:56 +01:00
|
|
|
{
|
2009-11-28 22:08:43 +01:00
|
|
|
cppcheckError(tok);
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (linktok.empty() == true)
|
2009-11-10 22:10:56 +01:00
|
|
|
{
|
2009-11-28 22:08:43 +01:00
|
|
|
cppcheckError(tok);
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->link() != linktok.top())
|
2009-11-10 22:10:56 +01:00
|
|
|
{
|
2009-11-28 22:08:43 +01:00
|
|
|
cppcheckError(tok);
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok != tok->link()->link())
|
2009-11-10 22:10:56 +01:00
|
|
|
{
|
2009-11-28 22:08:43 +01:00
|
|
|
cppcheckError(tok);
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-13 15:35:37 +02:00
|
|
|
linktok.pop();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->link() != 0)
|
2009-11-10 22:10:56 +01:00
|
|
|
{
|
2009-11-28 22:08:43 +01:00
|
|
|
cppcheckError(tok);
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-13 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!linktok.empty())
|
2009-11-10 22:10:56 +01:00
|
|
|
{
|
2009-11-28 22:08:43 +01:00
|
|
|
cppcheckError(linktok.top());
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-13 15:35:37 +02:00
|
|
|
|
2010-01-06 21:18:42 +01:00
|
|
|
// Validate that the Tokenizer::_tokensBack is updated correctly during simplifications
|
2010-04-02 07:30:58 +02:00
|
|
|
if (lastTok != _tokensBack)
|
2010-01-06 00:32:05 +01:00
|
|
|
{
|
2010-01-06 21:18:42 +01:00
|
|
|
cppcheckError(lastTok);
|
|
|
|
return false;
|
2010-01-06 00:32:05 +01:00
|
|
|
}
|
|
|
|
|
2009-09-13 15:35:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-27 16:04:10 +02:00
|
|
|
|
|
|
|
std::string Tokenizer::simplifyString(const std::string &source)
|
|
|
|
{
|
|
|
|
std::string str = source;
|
|
|
|
bool escaped = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::string::size_type i = 0; i + 2 < str.size(); i++)
|
2009-09-27 16:04:10 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!escaped)
|
2009-09-27 16:04:10 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str[i] == '\\')
|
2009-09-27 16:04:10 +02:00
|
|
|
escaped = true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str[i] == 'x')
|
2009-09-27 16:04:10 +02:00
|
|
|
{
|
|
|
|
// Hex value
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str[i+1] == '0' && str[i+2] == '0')
|
2009-09-27 16:04:10 +02:00
|
|
|
str.replace(i, 3, "0");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We will replace all other character as 'a'
|
|
|
|
// If that causes problems in the future, this can
|
|
|
|
// be improved. But for now, this should be OK.
|
2009-10-09 21:11:29 +02:00
|
|
|
int n = 1;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (n < 2 && std::isxdigit(str[i+1+n]))
|
2009-10-09 21:11:29 +02:00
|
|
|
++n;
|
2009-09-27 16:04:10 +02:00
|
|
|
--i;
|
2009-10-09 21:11:29 +02:00
|
|
|
str.replace(i, 2 + n, "a");
|
2009-09-27 16:04:10 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (MathLib::isOctalDigit(str[i]))
|
2009-09-27 16:04:10 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (MathLib::isOctalDigit(str[i+1]) &&
|
|
|
|
MathLib::isOctalDigit(str[i+2]))
|
2009-09-27 16:04:10 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str[i+1] == '0' && str[i+2] == '0')
|
2009-09-27 16:04:10 +02:00
|
|
|
str.replace(i, 3, "0");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We will replace all other character as 'a'
|
|
|
|
// If that causes problems in the future, this can
|
|
|
|
// be improved. But for now, this should be OK.
|
|
|
|
--i;
|
|
|
|
str.replace(i, 4, "a");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
escaped = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2009-10-17 23:11:48 +02:00
|
|
|
|
2010-03-13 11:52:48 +01:00
|
|
|
|
|
|
|
void Tokenizer::simplifyStructInit()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-03-13 11:52:48 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "[;{}] struct| %type% %var% = { . %type% ="))
|
2010-03-13 11:52:48 +01:00
|
|
|
{
|
|
|
|
// Goto "." and check if the initializations have an expected format
|
|
|
|
const Token *tok2 = tok;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2->str() != ".")
|
2010-03-13 11:52:48 +01:00
|
|
|
tok2 = tok2->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok2 && tok2->str() == ".")
|
2010-03-13 11:52:48 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok2, ". %type% = %num% [,}]"))
|
2010-03-13 11:52:48 +01:00
|
|
|
tok2 = tok2->tokAt(4);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, ". %type% = %var% [,}]"))
|
2010-03-13 11:52:48 +01:00
|
|
|
tok2 = tok2->tokAt(4);
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok2, ". %type% = & %var% [,}]"))
|
2010-03-13 11:52:48 +01:00
|
|
|
tok2 = tok2->tokAt(5);
|
2010-03-27 20:52:31 +01:00
|
|
|
else
|
|
|
|
break;
|
2010-03-13 11:52:48 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok2, ", ."))
|
2010-03-13 11:52:48 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(tok2, "} ;"))
|
2010-03-13 11:52:48 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Known expression format => Perform simplification
|
|
|
|
Token *vartok = tok->tokAt(3);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (vartok->str() == "=")
|
2010-03-13 11:52:48 +01:00
|
|
|
vartok = vartok->previous();
|
|
|
|
vartok->next()->str(";");
|
|
|
|
|
|
|
|
Token *tok3 = vartok->tokAt(2);
|
|
|
|
tok3->link(0);
|
2010-04-02 07:30:58 +02:00
|
|
|
while (Token::Match(tok3, "[{,] . %type% ="))
|
2010-03-13 11:52:48 +01:00
|
|
|
{
|
|
|
|
tok3->str(vartok->str());
|
|
|
|
tok3->varId(vartok->varId());
|
|
|
|
tok3 = tok3->tokAt(5);
|
2010-04-02 07:30:58 +02:00
|
|
|
while (!Token::Match(tok3, "[,}]"))
|
2010-03-13 11:52:48 +01:00
|
|
|
tok3 = tok3->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok3->str() == "}")
|
2010-03-13 11:52:48 +01:00
|
|
|
{
|
|
|
|
tok3->deleteThis();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tok3->previous()->insertToken(";");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 23:58:15 +01:00
|
|
|
void Tokenizer::simplifyComparisonOrder()
|
|
|
|
{
|
|
|
|
// Use "<" comparison instead of ">"
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-11-04 23:58:15 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "[;(] %any% >|>= %any% [);]"))
|
2009-11-04 23:58:15 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->next()->isName() && !tok->next()->isNumber())
|
2009-11-12 18:53:26 +01:00
|
|
|
continue;
|
2009-11-04 23:58:15 +01:00
|
|
|
const std::string op1(tok->strAt(1));
|
|
|
|
tok->next()->str(tok->strAt(3));
|
|
|
|
tok->tokAt(3)->str(op1);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->tokAt(2)->str() == ">")
|
2009-11-04 23:58:15 +01:00
|
|
|
tok->tokAt(2)->str("<");
|
|
|
|
else
|
|
|
|
tok->tokAt(2)->str("<=");
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "( %num% ==|!= %var% )"))
|
2009-11-30 16:45:19 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->next()->isName() && !tok->next()->isNumber())
|
2009-11-30 16:45:19 +01:00
|
|
|
continue;
|
|
|
|
const std::string op1(tok->strAt(1));
|
|
|
|
tok->next()->str(tok->strAt(3));
|
|
|
|
tok->tokAt(3)->str(op1);
|
|
|
|
}
|
2009-11-04 23:58:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-12 22:49:39 +01:00
|
|
|
void Tokenizer::simplifyConst()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-11-12 22:49:39 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "[;{}(,] %type% const") &&
|
|
|
|
tok->next()->str().find(":") == std::string::npos)
|
2009-11-12 22:49:39 +01:00
|
|
|
{
|
|
|
|
tok->tokAt(2)->str(tok->tokAt(1)->str());
|
|
|
|
tok->tokAt(1)->str("const");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 23:11:48 +02:00
|
|
|
void Tokenizer::getErrorMessages()
|
|
|
|
{
|
|
|
|
syntaxError(0, ' ');
|
2010-04-04 22:55:28 +02:00
|
|
|
cppcheckError(0);
|
2009-10-17 23:11:48 +02:00
|
|
|
}
|
2009-12-28 08:37:34 +01:00
|
|
|
|
2009-12-29 12:15:39 +01:00
|
|
|
/** find pattern */
|
|
|
|
static bool findmatch(const Token *tok1, const Token *tok2, const char pattern[])
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok = tok1; tok && tok != tok2; tok = tok->next())
|
2009-12-29 12:15:39 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, pattern))
|
2009-12-29 12:15:39 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-28 08:37:34 +01:00
|
|
|
void Tokenizer::simplifyWhile0()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-12-28 08:37:34 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::simpleMatch(tok, "while ( 0 )"))
|
2009-12-28 08:37:34 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok->previous(), "}"))
|
2009-12-28 08:37:34 +01:00
|
|
|
{
|
|
|
|
// find "do"
|
|
|
|
Token *tok2 = tok->previous()->link();
|
|
|
|
tok2 = tok2 ? tok2->previous() : 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok2 && tok2->str() == "do" && !findmatch(tok2, tok, "continue|break"))
|
2009-12-28 08:37:34 +01:00
|
|
|
{
|
|
|
|
// delete "do {"
|
|
|
|
tok2->deleteThis();
|
|
|
|
tok2->deleteThis();
|
|
|
|
|
|
|
|
// delete "} while ( 0 )"
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteNext(); // while
|
|
|
|
tok->deleteNext(); // (
|
|
|
|
tok->deleteNext(); // 0
|
|
|
|
tok->deleteNext(); // )
|
|
|
|
tok->deleteThis(); // }
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-01-01 22:25:37 +01:00
|
|
|
|
|
|
|
// remove "while (0) { .. }"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok->tokAt(4), "{"))
|
2010-01-01 22:25:37 +01:00
|
|
|
{
|
|
|
|
const Token *end = tok->tokAt(4)->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!findmatch(tok, end, "continue|break"))
|
2010-01-01 22:25:37 +01:00
|
|
|
{
|
|
|
|
Token::eraseTokens(tok, end ? end->next() : 0);
|
|
|
|
tok->deleteThis(); // delete "while"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-28 08:37:34 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-06 15:28:52 +01:00
|
|
|
|
2010-05-30 12:49:39 +02:00
|
|
|
void Tokenizer::simplifyErrNoInWhile()
|
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() != "errno")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Token *endpar = 0;
|
|
|
|
if (Token::Match(tok->previous(), "&& errno == EINTR ) { ;| }"))
|
|
|
|
endpar = tok->tokAt(3);
|
|
|
|
else if (Token::Match(tok->tokAt(-2), "&& ( errno == EINTR ) ) { ;| }"))
|
|
|
|
endpar = tok->tokAt(4);
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::simpleMatch(endpar->link()->previous(), "while ("))
|
|
|
|
{
|
|
|
|
Token *tok1 = tok->previous();
|
|
|
|
if (tok1->str() == "(")
|
|
|
|
tok1 = tok1->previous();
|
|
|
|
|
|
|
|
// erase "&& errno == EINTR"
|
|
|
|
Token::eraseTokens(tok1->previous(), endpar);
|
|
|
|
|
|
|
|
// tok is invalid.. move to endpar
|
|
|
|
tok = endpar;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-06 15:28:52 +01:00
|
|
|
void Tokenizer::simplifyFuncInWhile()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-03-06 15:28:52 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!Token::Match(tok, "while ( %var% ( %var% ) ) {"))
|
2010-03-06 15:28:52 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Token *func = tok->tokAt(2);
|
|
|
|
Token *var = tok->tokAt(4);
|
|
|
|
Token *end = tok->tokAt(7)->link();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!end)
|
2010-03-06 15:28:52 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
tok->str("int");
|
|
|
|
tok->insertToken("cppcheck:r");
|
|
|
|
tok->tokAt(1)->insertToken("=");
|
|
|
|
tok->tokAt(2)->insertToken(func->str());
|
|
|
|
tok->tokAt(3)->insertToken("(");
|
|
|
|
tok->tokAt(4)->insertToken(var->str());
|
|
|
|
tok->tokAt(5)->varId(var->varId());
|
|
|
|
tok->tokAt(5)->insertToken(")");
|
|
|
|
tok->tokAt(6)->insertToken(";");
|
|
|
|
tok->tokAt(7)->insertToken("while");
|
|
|
|
tok->tokAt(9)->insertToken("cppcheck:r");
|
|
|
|
Token::createMutualLinks(tok->tokAt(4), tok->tokAt(6));
|
|
|
|
end->previous()->insertToken("cppcheck:r");
|
|
|
|
end->previous()->insertToken("=");
|
|
|
|
Token::move(func, func->tokAt(3), end->previous());
|
|
|
|
end->previous()->insertToken(";");
|
|
|
|
|
|
|
|
tok = end;
|
|
|
|
}
|
|
|
|
}
|
2010-03-18 18:14:52 +01:00
|
|
|
|
|
|
|
void Tokenizer::simplifyStructDecl()
|
|
|
|
{
|
|
|
|
unsigned int count = 0;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2010-03-18 18:14:52 +01:00
|
|
|
{
|
|
|
|
// check for named struct/union
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "struct|union %type% :|{"))
|
2010-03-18 18:14:52 +01:00
|
|
|
{
|
|
|
|
Token *type = tok->next();
|
|
|
|
Token *next = tok->tokAt(2);
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (next->str() != "{")
|
2010-03-19 16:17:25 +01:00
|
|
|
next = next->next();
|
|
|
|
|
|
|
|
tok = next->link();
|
2010-03-18 18:14:52 +01:00
|
|
|
|
|
|
|
// check for named type
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "*|&| %type% ,|;|["))
|
2010-03-18 18:14:52 +01:00
|
|
|
{
|
|
|
|
tok->insertToken(";");
|
|
|
|
tok = tok->next();
|
|
|
|
tok->insertToken(type->str().c_str());
|
|
|
|
}
|
2010-03-19 16:17:25 +01:00
|
|
|
|
|
|
|
tok = next;
|
2010-03-18 18:14:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for anonymous struct/union
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "struct|union {"))
|
2010-03-18 18:14:52 +01:00
|
|
|
{
|
|
|
|
Token *tok1 = tok;
|
|
|
|
|
|
|
|
tok = tok->next()->link();
|
|
|
|
|
2010-03-19 16:17:25 +01:00
|
|
|
// check for named type
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok->next(), "*|&| %type% ,|;|["))
|
2010-03-18 18:14:52 +01:00
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
|
|
|
|
name = "Anonymous" + MathLib::toString<long>(count++);
|
|
|
|
|
|
|
|
tok1->insertToken(name.c_str());
|
|
|
|
|
|
|
|
tok->insertToken(";");
|
|
|
|
tok = tok->next();
|
|
|
|
tok->insertToken(name.c_str());
|
|
|
|
}
|
2010-04-08 17:40:23 +02:00
|
|
|
|
|
|
|
// unnamed anonymous struct/union so remove it
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (tok->next()->str() == ";")
|
2010-03-19 19:04:56 +01:00
|
|
|
{
|
2010-04-08 17:40:23 +02:00
|
|
|
tok1->deleteThis();
|
|
|
|
if (tok1->next() == tok)
|
|
|
|
{
|
|
|
|
tok1->deleteThis();
|
|
|
|
tok = tok1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tok1->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
if (tok->next())
|
|
|
|
tok->deleteThis();
|
|
|
|
if (!tok->next())
|
|
|
|
return;
|
2010-03-19 19:04:56 +01:00
|
|
|
}
|
2010-03-19 16:17:25 +01:00
|
|
|
|
|
|
|
tok = tok1->next();
|
2010-03-18 18:14:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-21 21:08:47 +02:00
|
|
|
|
|
|
|
void Tokenizer::simplifyCallingConvention()
|
|
|
|
{
|
2010-04-24 09:40:05 +02:00
|
|
|
const char * pattern = "__cdecl|__stdcall|__fastcall|__thiscall|__clrcall|__syscall|__pascal|__fortran|__far|__near|WINAPI|APIENTRY|CALLBACK";
|
2010-04-21 21:08:47 +02:00
|
|
|
while (Token::Match(_tokens, pattern))
|
|
|
|
{
|
|
|
|
_tokens->deleteThis();
|
|
|
|
}
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
while (Token::Match(tok->next(), pattern))
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-24 09:40:05 +02:00
|
|
|
void Tokenizer::simplifyDeclspec()
|
|
|
|
{
|
|
|
|
while (Token::simpleMatch(_tokens, "__declspec (") && _tokens->next()->link() && _tokens->next()->link()->next())
|
|
|
|
{
|
|
|
|
Token::eraseTokens(_tokens, _tokens->next()->link()->next());
|
|
|
|
_tokens->deleteThis();
|
|
|
|
}
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok, "__declspec (") && tok->next()->link() && tok->next()->link()->next())
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-27 18:15:42 +02:00
|
|
|
void Tokenizer::simplifyAttribute()
|
|
|
|
{
|
|
|
|
while (Token::simpleMatch(_tokens, "__attribute__ (") && _tokens->next()->link() && _tokens->next()->link()->next())
|
|
|
|
{
|
|
|
|
Token::eraseTokens(_tokens, _tokens->next()->link()->next());
|
|
|
|
_tokens->deleteThis();
|
|
|
|
}
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok, "__attribute__ (") && tok->next()->link() && tok->next()->link()->next())
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|