2009-01-24 18:15:38 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#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()
|
2009-07-16 07:20:29 +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)
|
2009-07-16 07:20:29 +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'
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsigned int fileno)
|
|
|
|
{
|
|
|
|
if (str[0] == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Replace hexadecimal value with decimal
|
|
|
|
std::ostringstream str2;
|
|
|
|
if (strncmp(str, "0x", 2) == 0)
|
|
|
|
{
|
|
|
|
str2 << std::strtoul(str + 2, NULL, 16);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str2 << str;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_tokensBack)
|
|
|
|
{
|
|
|
|
_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);
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// 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
|
|
|
{
|
2009-10-07 09:54:34 +02:00
|
|
|
if (!type || !type->strAt(0))
|
2009-01-24 18:15:38 +01:00
|
|
|
return 0;
|
|
|
|
|
2009-10-07 09:54:34 +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));
|
2009-01-24 18:15:38 +01:00
|
|
|
if (it == _typeSize.end())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
while (n > 0)
|
|
|
|
{
|
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());
|
|
|
|
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
|
|
|
|
for (char ch = (char)code.get(); code.good(); ch = (char)code.get())
|
|
|
|
{
|
|
|
|
// We are not handling UTF and stuff like that. Code is supposed to plain simple text.
|
|
|
|
if (ch < 0)
|
|
|
|
continue;
|
|
|
|
|
2009-03-04 18:02:45 +01:00
|
|
|
// char/string..
|
|
|
|
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
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
++lineno;
|
|
|
|
|
|
|
|
// Special sequence '\.'
|
|
|
|
if (special)
|
|
|
|
special = false;
|
|
|
|
else
|
|
|
|
special = (c == '\\');
|
|
|
|
|
|
|
|
// Get next character
|
|
|
|
c = (char)code.get();
|
|
|
|
}
|
2009-03-04 18:02:45 +01: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"
|
|
|
|
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);
|
|
|
|
for (unsigned int i = 0; i < _files.size(); i++)
|
|
|
|
{
|
2009-07-05 22:16:43 +02:00
|
|
|
if (FileLister::sameFileName(_files[i].c_str(), line.c_str()))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Use this index
|
|
|
|
foundOurfile = true;
|
|
|
|
FileIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundOurfile)
|
|
|
|
{
|
|
|
|
// The "_files" vector remembers what files have been tokenized..
|
|
|
|
_files.push_back(FileLister::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
|
|
|
}
|
|
|
|
|
2009-03-15 22:09:27 +01:00
|
|
|
if (strchr("+-*/%&|^?!=<>[](){};:,.~\n ", ch))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-02-08 11:56:20 +01: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"
|
|
|
|
}
|
|
|
|
else if (strchr("+-", ch) &&
|
|
|
|
CurrentToken.length() > 0 &&
|
|
|
|
std::isdigit(CurrentToken[0]) &&
|
|
|
|
CurrentToken[CurrentToken.length()-1] == 'e')
|
|
|
|
{
|
|
|
|
// Don't separate doubles "4.2e+10"
|
2009-02-08 10:51:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-15 22:09:27 +01:00
|
|
|
if (CurrentToken == "#file")
|
|
|
|
{
|
|
|
|
// Handle this where strings are handled
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (CurrentToken == "#endfile")
|
|
|
|
{
|
|
|
|
if (lineNumbers.empty() || fileIndexes.empty())
|
|
|
|
{
|
|
|
|
std::cerr << "####### Preprocessor bug! #######\n";
|
|
|
|
std::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
lineno = lineNumbers.back();
|
|
|
|
lineNumbers.pop_back();
|
|
|
|
FileIndex = fileIndexes.back();
|
|
|
|
fileIndexes.pop_back();
|
|
|
|
CurrentToken.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If token contains # characters, split it up
|
2009-12-09 19:14:07 +01:00
|
|
|
if (CurrentToken.find("##") == std::string::npos)
|
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
else
|
2009-03-15 22:09:27 +01:00
|
|
|
{
|
2009-12-09 19:14:07 +01:00
|
|
|
std::string temp;
|
|
|
|
for (std::string::size_type i = 0; i < CurrentToken.length(); ++i)
|
2009-03-15 22:09:27 +01:00
|
|
|
{
|
2009-12-09 19:14:07 +01:00
|
|
|
if (CurrentToken[i] == '#' && CurrentToken.length() + 1 > i && CurrentToken[i+1] == '#')
|
|
|
|
{
|
|
|
|
addtoken(temp.c_str(), lineno, FileIndex);
|
|
|
|
temp.clear();
|
|
|
|
addtoken("##", lineno, FileIndex);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
temp += CurrentToken[i];
|
2009-03-15 22:09:27 +01:00
|
|
|
}
|
2009-12-09 19:14:07 +01:00
|
|
|
addtoken(temp.c_str(), lineno, FileIndex);
|
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
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
++lineno;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (ch == ' ')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-02-08 10:51:45 +01:00
|
|
|
CurrentToken += ch;
|
2009-02-14 11:13:50 +01:00
|
|
|
// Add "++", "--" or ">>" token
|
2009-02-14 19:40:04 +01: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;
|
|
|
|
}
|
2009-12-09 19:14:07 +01:00
|
|
|
if (CurrentToken.find("##") == std::string::npos)
|
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string temp;
|
|
|
|
for (std::string::size_type i = 0; i < CurrentToken.length(); ++i)
|
|
|
|
{
|
|
|
|
if (CurrentToken[i] == '#' && CurrentToken.length() + 1 > i && CurrentToken[i+1] == '#')
|
|
|
|
{
|
|
|
|
addtoken(temp.c_str(), lineno, FileIndex);
|
|
|
|
temp.clear();
|
|
|
|
addtoken("##", lineno, FileIndex);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
temp += CurrentToken[i];
|
|
|
|
}
|
|
|
|
addtoken(temp.c_str(), lineno, FileIndex);
|
|
|
|
}
|
2009-05-09 21:32:29 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 13:35:00 +02:00
|
|
|
void Tokenizer::simplifyTypedef()
|
|
|
|
{
|
|
|
|
std::string className;
|
|
|
|
int classLevel = 0;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-11-19 19:02:16 +01:00
|
|
|
if (Token::Match(tok, "class|namespace %any%"))
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
|
|
|
className = tok->next()->str();
|
|
|
|
classLevel = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
--classLevel;
|
|
|
|
if (classLevel < 0)
|
|
|
|
className = "";
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->str() == "{")
|
|
|
|
{
|
|
|
|
++classLevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->str() != "typedef")
|
|
|
|
continue;
|
|
|
|
|
2010-01-11 22:58:57 +01:00
|
|
|
// pull struct, union or enum definition out of typedef
|
|
|
|
// use typedef name for unnamed struct, union or enum
|
|
|
|
if (Token::Match(tok->next(), "struct|enum|union %type% {") ||
|
|
|
|
Token::Match(tok->next(), "struct|enum|union {"))
|
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-01-10 08:49:02 +01: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-01-10 08:49:02 +01: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
|
|
|
|
if (Token::Match(tok1->next(), "%type%"))
|
|
|
|
name = tok1->next()->str();
|
|
|
|
else // create a unique name
|
|
|
|
{
|
|
|
|
static long count = 0;
|
|
|
|
name = "Unnamed" + MathLib::toString<long>(count++);
|
|
|
|
}
|
|
|
|
tok->tokAt(1)->insertToken(name.c_str());
|
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();
|
|
|
|
|
|
|
|
if (!tok1)
|
|
|
|
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
|
|
|
|
2009-12-29 07:28:00 +01:00
|
|
|
const char *type1 = 0;
|
|
|
|
const char *type2 = 0;
|
|
|
|
const char *typeName = 0;
|
|
|
|
bool pointer = false;
|
2010-01-17 08:16:17 +01:00
|
|
|
bool toPointer = false;
|
2009-12-29 07:28:00 +01:00
|
|
|
Token *start = 0;
|
|
|
|
Token *end = 0;
|
2010-01-11 17:09:04 +01:00
|
|
|
Token *num = 0;
|
2010-01-16 09:18:21 +01:00
|
|
|
Token *typeDef = tok;
|
2009-12-29 07:28:00 +01:00
|
|
|
|
|
|
|
if (Token::Match(tok->next(), "%type% <") ||
|
2010-01-17 08:16:17 +01:00
|
|
|
Token::Match(tok->next(), "%type% %type% <") ||
|
2009-12-29 07:28:00 +01:00
|
|
|
Token::Match(tok->next(), "%type% :: %type% <") ||
|
2010-01-17 08:16:17 +01:00
|
|
|
Token::Match(tok->next(), "%type% %type% :: %type% <"))
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
// template
|
|
|
|
int level = 1;
|
|
|
|
start = tok->next();
|
|
|
|
|
|
|
|
if (tok->tokAt(2)->str() == "<")
|
|
|
|
end = tok->tokAt(3);
|
|
|
|
else if (tok->tokAt(3)->str() == "<")
|
|
|
|
end = tok->tokAt(4);
|
|
|
|
else if (tok->tokAt(4)->str() == "<")
|
|
|
|
end = tok->tokAt(5);
|
|
|
|
else
|
|
|
|
end = tok->tokAt(6);
|
2009-12-29 07:28:00 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
int paren = 0;
|
|
|
|
for (; end ; end = end->next())
|
|
|
|
{
|
|
|
|
if (end->str() == ">")
|
2009-12-29 07:28:00 +01:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
if (paren == 0)
|
2009-12-29 07:28:00 +01:00
|
|
|
{
|
|
|
|
level--;
|
|
|
|
if (level == 0)
|
|
|
|
break;
|
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
|
|
|
else if (end->str() == "<")
|
|
|
|
{
|
|
|
|
if (paren == 0)
|
2009-12-29 07:28:00 +01:00
|
|
|
level++;
|
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
else if (end->str() == "(")
|
|
|
|
paren++;
|
|
|
|
else if (end->str() == ")")
|
|
|
|
paren--;
|
|
|
|
}
|
2009-12-29 07:28:00 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
while (end && end->next() && Token::Match(end->next(), ":: %type%"))
|
|
|
|
end = end->tokAt(2);
|
2010-01-13 07:59:47 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
if (end && end->next() && end->next()->str() == "*")
|
|
|
|
{
|
|
|
|
pointer = true;
|
|
|
|
end = end->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end && end->next() && end->next()->str() == "*")
|
|
|
|
{
|
|
|
|
toPointer = true;
|
|
|
|
end = end->next();
|
2009-12-29 07:28:00 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
|
|
|
|
if (end && end->next() && (Token::Match(end->next(), "%type% ;|,") || Token::Match(end->next(), "%type% [ %num% ] ;|,")))
|
2010-01-11 17:09:04 +01:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
typeName = end->strAt(1);
|
2010-01-11 17:09:04 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
if (end->tokAt(2)->str() == "[")
|
2010-01-11 17:09:04 +01:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
num = end->tokAt(3);
|
|
|
|
tok = end->tokAt(5);
|
2010-01-11 17:09:04 +01:00
|
|
|
}
|
|
|
|
else
|
2010-01-17 08:16:17 +01:00
|
|
|
tok = end->tokAt(2);
|
2010-01-11 17:09:04 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
else
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
// unhandled template typedef, skip it and continue
|
|
|
|
continue;
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
|
|
|
else if (Token::Match(tok->next(), "%type% *| *| %type% [ %num% ] ;|,") ||
|
|
|
|
Token::Match(tok->next(), "%type% %type% *| *| %type% [ %num% ] ;|,"))
|
|
|
|
{
|
|
|
|
// array
|
|
|
|
int offset = 2;
|
|
|
|
type1 = tok->strAt(1);
|
|
|
|
|
|
|
|
if (tok->tokAt(2)->str() != "*" && tok->tokAt(3)->str() != "[")
|
|
|
|
{
|
|
|
|
type2 = tok->strAt(2);
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->tokAt(offset) && (tok->tokAt(offset)->str() == "*"))
|
2009-12-22 19:10:33 +01:00
|
|
|
{
|
|
|
|
pointer = true;
|
2010-01-17 08:16:17 +01:00
|
|
|
offset++;
|
2009-12-22 19:10:33 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
|
|
|
|
if (tok->tokAt(offset) && (tok->tokAt(offset)->str() == "*"))
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
toPointer = true;
|
|
|
|
offset++;
|
|
|
|
}
|
2009-10-03 17:02:23 +02:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
typeName = tok->strAt(offset);
|
|
|
|
num = tok->tokAt(offset + 2);
|
|
|
|
tok = tok->tokAt(offset + 4);
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok->next(), "%type% *| *| %type% ;|,") ||
|
|
|
|
Token::Match(tok->next(), "%type% %type% *| *| %type% ;|,"))
|
|
|
|
{
|
|
|
|
int offset = 2;
|
|
|
|
type1 = tok->strAt(1);
|
|
|
|
|
|
|
|
if (tok->tokAt(2)->str() != "*" && (tok->tokAt(3)->str() != ";" && tok->tokAt(3)->str() != ","))
|
|
|
|
{
|
2009-09-30 13:35:00 +02:00
|
|
|
type2 = tok->strAt(2);
|
2010-01-17 08:16:17 +01:00
|
|
|
offset++;
|
|
|
|
}
|
2009-10-03 17:02:23 +02:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
if (tok->tokAt(offset) && (tok->tokAt(offset)->str() == "*"))
|
|
|
|
{
|
|
|
|
pointer = true;
|
|
|
|
offset++;
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
if (tok->tokAt(offset) && (tok->tokAt(offset)->str() == "*"))
|
|
|
|
{
|
|
|
|
toPointer = true;
|
|
|
|
offset++;
|
|
|
|
}
|
2009-09-30 13:35:00 +02:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
typeName = tok->strAt(offset++);
|
|
|
|
tok = tok->tokAt(offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// unhandled typedef, skip it and continue
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
const std::string pattern(className.empty() ? "" : (className + " :: " + typeName).c_str());
|
|
|
|
int level = 0;
|
|
|
|
bool inScope = true;
|
|
|
|
|
|
|
|
bool exitThisScope = false;
|
|
|
|
int exitScope = 0;
|
|
|
|
bool simplifyType = false;
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
if (tok2->str() == "}")
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
--level;
|
|
|
|
if (level < 0)
|
|
|
|
inScope = false;
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
if (exitThisScope)
|
|
|
|
{
|
|
|
|
if (level < exitScope)
|
|
|
|
exitThisScope = false;
|
2009-11-27 17:32:53 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "{")
|
|
|
|
++level;
|
|
|
|
else if (!pattern.empty() && Token::Match(tok2, pattern.c_str()))
|
|
|
|
{
|
|
|
|
tok2->deleteNext();
|
|
|
|
tok2->deleteNext();
|
|
|
|
simplifyType = true;
|
|
|
|
}
|
|
|
|
else if (inScope && !exitThisScope && tok2->str() == typeName)
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok2->previous(), "::"))
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
// Don't replace this typename if it's preceded by "::"
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
else if (Token::Match(tok2->tokAt(-2), "!!typedef") &&
|
|
|
|
Token::Match(tok2->tokAt(-3), "!!typedef"))
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
// Check for enum and typedef with same name.
|
|
|
|
if (type1 && (tok2->tokAt(-1)->str() != type1))
|
|
|
|
simplifyType = true;
|
|
|
|
else if (!type1)
|
|
|
|
simplifyType = true;
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Typedef with the same name.
|
|
|
|
exitThisScope = true;
|
|
|
|
exitScope = level;
|
|
|
|
}
|
|
|
|
}
|
2009-09-30 13:35:00 +02:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
if (simplifyType)
|
|
|
|
{
|
|
|
|
if (start && end)
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
tok2->str(start->str());
|
|
|
|
Token * nextToken;
|
|
|
|
std::stack<Token *> links;
|
|
|
|
for (nextToken = start->next(); nextToken != end->next(); nextToken = nextToken->next())
|
2009-12-29 07:28:00 +01:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
tok2->insertToken(nextToken->strAt(0));
|
|
|
|
tok2 = tok2->next();
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
// Check for links and fix them up
|
|
|
|
if (tok2->str() == "(" || tok2->str() == "[")
|
|
|
|
links.push(tok2);
|
|
|
|
if (tok2->str() == ")" || tok2->str() == "]")
|
|
|
|
{
|
|
|
|
Token * link = links.top();
|
2009-12-30 20:56:16 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
tok2->link(link);
|
|
|
|
link->link(tok2);
|
2009-12-30 20:56:16 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
links.pop();
|
2009-12-30 20:56:16 +01:00
|
|
|
}
|
2009-12-29 07:28:00 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok2->str(type1);
|
|
|
|
if (type2)
|
2009-12-29 07:28:00 +01:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
tok2->insertToken(type2);
|
|
|
|
tok2 = tok2->next();
|
2009-12-29 07:28:00 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
if (pointer)
|
|
|
|
{
|
|
|
|
tok2->insertToken("*");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
if (toPointer)
|
|
|
|
{
|
|
|
|
tok2->insertToken("*");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
if (num)
|
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok2->insertToken("[");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token *tok3 = tok2;
|
|
|
|
tok2->insertToken(num->strAt(0));
|
|
|
|
tok2 = tok2->next();
|
|
|
|
tok2->insertToken("]");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
Token::createMutualLinks(tok2, tok3);
|
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-01-17 08:16:17 +01:00
|
|
|
if (tok->str() == ";")
|
|
|
|
done = true;
|
|
|
|
else if (tok->str() == ",")
|
|
|
|
{
|
|
|
|
if (Token::Match(tok->next(), "*| *| %type% ;|,") ||
|
|
|
|
Token::Match(tok->next(), "*| *| %type% [ %num% ] ;|,"))
|
2010-01-16 09:18:21 +01:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
num = 0;
|
|
|
|
pointer = (tok->tokAt(1)->str() == "*");
|
|
|
|
toPointer = (tok->tokAt(2)->str() == "*");
|
|
|
|
|
|
|
|
if (pointer)
|
2010-01-16 09:18:21 +01:00
|
|
|
{
|
2010-01-17 08:16:17 +01:00
|
|
|
if (toPointer)
|
|
|
|
{
|
|
|
|
typeName = tok->strAt(3);
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
if (tok->tokAt(4)->str() == "[")
|
|
|
|
{
|
|
|
|
num = tok->tokAt(5);
|
|
|
|
tok = tok->tokAt(7);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tok = tok->tokAt(4);
|
|
|
|
}
|
|
|
|
else
|
2009-12-29 07:28:00 +01:00
|
|
|
{
|
2010-01-16 09:18:21 +01:00
|
|
|
typeName = tok->strAt(2);
|
2010-01-11 17:09:04 +01:00
|
|
|
|
2010-01-16 09:18:21 +01:00
|
|
|
if (tok->tokAt(3)->str() == "[")
|
|
|
|
{
|
|
|
|
num = tok->tokAt(4);
|
|
|
|
tok = tok->tokAt(6);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tok = tok->tokAt(3);
|
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
typeName = tok->strAt(1);
|
2010-01-16 09:18:21 +01:00
|
|
|
|
2010-01-17 08:16:17 +01:00
|
|
|
if (tok->tokAt(2)->str() == "[")
|
|
|
|
{
|
|
|
|
num = tok->tokAt(3);
|
|
|
|
tok = tok->tokAt(5);
|
2009-12-29 07:28:00 +01:00
|
|
|
}
|
2010-01-17 08:16:17 +01:00
|
|
|
else
|
|
|
|
tok = tok->tokAt(2);
|
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-01-17 08:16:17 +01:00
|
|
|
if (ok)
|
|
|
|
{
|
2010-01-16 09:18:21 +01:00
|
|
|
// remove typedef but leave ;
|
|
|
|
while (typeDef->next() && typeDef->next() != tok)
|
|
|
|
typeDef->deleteNext();
|
|
|
|
|
|
|
|
if (typeDef != _tokens)
|
|
|
|
{
|
|
|
|
tok = typeDef->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_tokens->deleteThis();
|
|
|
|
tok = _tokens;
|
|
|
|
}
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-09 21:32:29 +02:00
|
|
|
bool Tokenizer::tokenize(std::istream &code, const char FileName[])
|
|
|
|
{
|
|
|
|
// The "_files" vector remembers what files have been tokenized..
|
|
|
|
_files.push_back(FileLister::simplifyPath(FileName));
|
|
|
|
|
|
|
|
createTokens(code);
|
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
if (!createLinks())
|
|
|
|
{
|
|
|
|
// Source has syntax errors, can't proceed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-30 20:42:14 +02:00
|
|
|
simplifyDoWhileAddBraces();
|
|
|
|
simplifyIfAddBraces();
|
|
|
|
|
2009-03-17 20:50:06 +01:00
|
|
|
// Combine "- %num%" ..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2010-01-11 21:18:07 +01: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
|
|
|
|
2009-12-06 23:09:56 +01: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..
|
|
|
|
for (Token *tok = _tokens; tok && tok->next(); tok = tok->next())
|
|
|
|
{
|
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
|
|
|
};
|
|
|
|
|
|
|
|
for (unsigned ui = 0; ui < sizeof(combineWithNext) / sizeof(combineWithNext[0]); ui++)
|
|
|
|
{
|
|
|
|
if (tok->str() == combineWithNext[ui][0] && tok->next()->str() == combineWithNext[ui][1])
|
|
|
|
{
|
|
|
|
tok->str(combineWithNext[ui][2]);
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// typedef..
|
2009-09-30 13:35:00 +02:00
|
|
|
simplifyTypedef();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2010-01-04 17:22:06 +01:00
|
|
|
simplifyEnum();
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Remove __asm..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-08-22 16:04: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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok->next(), "__asm__ __volatile__ (") &&
|
|
|
|
tok->tokAt(3)->link() &&
|
|
|
|
tok->tokAt(3)->link()->next())
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(3)->link()->next());
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2009-05-03 21:32:22 +02:00
|
|
|
// Remove "volatile" and "mutable"
|
|
|
|
while (_tokens && (_tokens->str() == "volatile" || _tokens->str() == "mutable"))
|
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
|
|
|
}
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-03 21:32:22 +02:00
|
|
|
while (tok->next() && (tok->next()->str() == "volatile" || tok->next()->str() == "mutable"))
|
2009-02-21 14:35:55 +01:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-06 10:40:48 +02:00
|
|
|
// replace "unsigned i" with "unsigned int i"
|
|
|
|
unsignedint();
|
|
|
|
|
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();
|
|
|
|
|
2009-06-10 19:36:00 +02:00
|
|
|
// Split up variable declarations.
|
2009-03-18 20:32:05 +01:00
|
|
|
simplifyVarDecl();
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
|
2009-09-19 10:54:10 +02:00
|
|
|
setVarId();
|
2009-11-10 22:10:56 +01:00
|
|
|
if (!validate())
|
|
|
|
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
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-11-01 15:56:00 +01:00
|
|
|
/**
|
|
|
|
* is the token pointing at a template parameters block..
|
|
|
|
* < 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)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (tok->str() != "<")
|
|
|
|
return false;
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
// num/type ..
|
|
|
|
if (!tok->isNumber() && !tok->isName())
|
|
|
|
return false;
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// optional "*"
|
|
|
|
if (tok->str() == "*")
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// ,/>
|
|
|
|
if (tok->str() == ">")
|
|
|
|
return true;
|
|
|
|
if (tok->str() != ",")
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (! Token::simpleMatch(tok, "template <"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
tok2 = tok2->link();
|
|
|
|
tok2 = tok2 ? tok2->next() : 0;
|
|
|
|
Token::eraseTokens(tok, tok2);
|
|
|
|
tok->str(";");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
{
|
|
|
|
tok2 = tok2->link();
|
|
|
|
if (!tok2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
{
|
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..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "typename")
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, "template <"))
|
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
while (tok && tok->str() != ">")
|
2009-09-03 22:19:44 +02:00
|
|
|
tok = tok->next();
|
2009-09-06 08:22:45 +02:00
|
|
|
if (!tok)
|
|
|
|
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..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() != "template")
|
|
|
|
continue;
|
|
|
|
if (!Token::simpleMatch(tok->next(), "< >"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// what kind of template is this?
|
|
|
|
Token *tok2 = tok->tokAt(3);
|
|
|
|
while (tok2 && (tok2->isName() || tok2->str() == "*"))
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
2009-11-01 15:56:00 +01:00
|
|
|
if (!templateParameters(tok2))
|
|
|
|
continue;
|
|
|
|
|
2009-10-31 12:34:52 +01:00
|
|
|
// unknown template.. bail out
|
2009-11-01 15:56:00 +01: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;
|
|
|
|
for (tok3 = tok2; tok3 && tok3->str() != ">"; tok3 = tok3->next())
|
2009-10-31 12:34:52 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
if (tok3 != tok2)
|
|
|
|
ostr << " ";
|
|
|
|
ostr << tok3->str();
|
2009-10-31 12:34:52 +01:00
|
|
|
}
|
|
|
|
if (!Token::Match(tok3, "> ("))
|
|
|
|
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
|
|
|
|
while (s.find(" ") != std::string::npos)
|
|
|
|
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..
|
|
|
|
while (0 != (tok2 = const_cast<Token *>(Token::findmatch(tok2, pattern.c_str()))))
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok2, Token::findmatch(tok2, "("));
|
|
|
|
tok2->str(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-05 20:16:57 +02:00
|
|
|
// Locate templates..
|
|
|
|
std::list<Token *> templates;
|
2009-03-11 18:50:24 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-05 20:16:57 +02:00
|
|
|
if (Token::simpleMatch(tok, "template <"))
|
2009-05-07 16:05:07 +02:00
|
|
|
{
|
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
// Just a declaration => ignore this
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Implementation => add to "templates"
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
templates.push_back(tok);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-05 20:16:57 +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;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-06 21:03:11 +02:00
|
|
|
// template definition.. skip it
|
|
|
|
if (Token::simpleMatch(tok, "template <"))
|
|
|
|
{
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
2009-11-01 19:03:52 +01: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();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
if (tok->str() == "}")
|
2009-05-06 21:03:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
else if (tok->str() == ";")
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
}
|
2009-11-01 19:03:52 +01: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
|
|
|
{
|
2009-11-01 15:56:00 +01:00
|
|
|
if (templateParameters(tok->next()))
|
|
|
|
used.push_back(tok);
|
2009-05-06 21:03:11 +02:00
|
|
|
}
|
2009-05-05 20:16:57 +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
|
|
|
|
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
|
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
std::list<Token *> eq;
|
|
|
|
unsigned int templatepar = 1;
|
|
|
|
std::string classname;
|
2009-09-03 21:46:07 +02:00
|
|
|
for (Token *tok = *iter1; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == ">")
|
2009-09-06 08:22:45 +02:00
|
|
|
{
|
|
|
|
if (Token::Match(tok, "> class %var%"))
|
|
|
|
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
|
|
|
|
|
|
|
if (tok->str() == ",")
|
2009-09-06 08:22:45 +02:00
|
|
|
++templatepar;
|
|
|
|
|
|
|
|
else if (tok->str() == "=")
|
|
|
|
eq.push_back(tok);
|
|
|
|
}
|
|
|
|
if (eq.empty() || classname.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
|
|
|
|
{
|
|
|
|
Token *tok = *iter2;
|
|
|
|
|
|
|
|
if (!Token::Match(tok, (classname + " < %any%").c_str()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// count the parameters..
|
|
|
|
unsigned int usedpar = 1;
|
|
|
|
for (tok = tok->tokAt(3); tok; tok = tok->tokAt(2))
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
if (tok->str() == ">")
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (tok->str() == ",")
|
|
|
|
++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
|
|
|
}
|
2009-09-06 08:22:45 +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();
|
|
|
|
for (unsigned int i = (templatepar - eq.size()); it != eq.end() && i < usedpar; ++i)
|
|
|
|
++it;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-06 08:22:45 +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;
|
|
|
|
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;
|
2009-03-11 18:50:24 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
std::vector<std::string> type;
|
|
|
|
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
if (Token::Match(tok, "%var% ,|>"))
|
|
|
|
type.push_back(tok->str());
|
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
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// if this is a template function, get the position of the function name
|
|
|
|
unsigned int pos = 0;
|
|
|
|
if (Token::Match(tok, "> %type% *| %var% ("))
|
|
|
|
pos = 2;
|
|
|
|
else if (Token::Match(tok, "> %type% %type% *| %var% ("))
|
|
|
|
pos = 3;
|
|
|
|
if (pos > 0 && tok->tokAt(pos)->str() == "*")
|
|
|
|
++pos;
|
2009-05-05 20:16:57 +02:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// name of template function/class..
|
|
|
|
const std::string name(tok->strAt(pos > 0 ? pos : 2));
|
2009-04-14 22:21:52 +02:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
const bool isfunc(pos > 0);
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// locate template usage..
|
|
|
|
|
|
|
|
std::string s(name + " <");
|
|
|
|
for (unsigned int i = 0; i < type.size(); ++i)
|
2009-03-14 21:26:32 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
if (i > 0)
|
|
|
|
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
|
|
|
|
2009-11-01 19:03:52 +01: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
|
|
|
|
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++;
|
|
|
|
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
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
if (tok2->str() != name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::Match(tok2->previous(), "[;{}=]") &&
|
|
|
|
!Token::Match(tok2, (pattern + (isfunc ? "(" : "%var%")).c_str()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// New type..
|
|
|
|
std::vector<std::string> types2;
|
|
|
|
s = "";
|
2009-12-20 12:45:34 +01:00
|
|
|
for (const Token *tok3 = tok2->tokAt(2); tok3 && tok3->str() != ">"; tok3 = tok3->next())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
2009-12-20 12:45:34 +01:00
|
|
|
if (!tok3->next())
|
|
|
|
{
|
|
|
|
s.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
if (tok3->str() != ",")
|
|
|
|
types2.push_back(tok3->str());
|
|
|
|
s += tok3->str();
|
|
|
|
}
|
|
|
|
const std::string type2(s);
|
2009-03-17 18:55:28 +01:00
|
|
|
|
2009-12-20 12:45:34 +01: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,
|
|
|
|
"error",
|
|
|
|
"Internal error: failed to instantiate template. The checking continues anyway.",
|
|
|
|
"internalError");
|
|
|
|
|
|
|
|
_errorLogger->reportErr(errmsg);
|
2009-12-06 11:44:53 +01:00
|
|
|
#endif
|
2009-12-20 12:45:34 +01:00
|
|
|
if (type2.empty())
|
|
|
|
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
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
if (expandedtemplates.find(name2) == expandedtemplates.end())
|
|
|
|
{
|
|
|
|
expandedtemplates.insert(name2);
|
|
|
|
// Copy template..
|
|
|
|
int _indentlevel = 0;
|
|
|
|
int _parlevel = 0;
|
|
|
|
for (const Token *tok3 = _tokens; tok3; tok3 = tok3->next())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
|
|
|
if (tok3->str() == "{")
|
2009-11-01 19:03:52 +01:00
|
|
|
++_indentlevel;
|
2009-11-01 15:56:00 +01:00
|
|
|
else if (tok3->str() == "}")
|
2009-11-01 19:03:52 +01:00
|
|
|
--_indentlevel;
|
|
|
|
else if (tok3->str() == "(")
|
|
|
|
++_parlevel;
|
|
|
|
else if (tok3->str() == ")")
|
|
|
|
--_parlevel;
|
|
|
|
|
|
|
|
// Start of template..
|
|
|
|
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
|
|
|
|
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());
|
|
|
|
while (tok3->str() != "::")
|
|
|
|
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
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
for (; tok3; tok3 = tok3->next())
|
|
|
|
{
|
|
|
|
if (tok3->str() == "{")
|
|
|
|
++indentlevel;
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
else if (tok3->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tok3->isName())
|
|
|
|
{
|
|
|
|
// search for this token in the type vector
|
|
|
|
unsigned int itype = 0;
|
|
|
|
while (itype < type.size() && type[itype] != tok3->str())
|
|
|
|
++itype;
|
|
|
|
|
|
|
|
// replace type with given type..
|
|
|
|
if (itype < type.size())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
addtoken(types2[itype].c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
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..
|
|
|
|
if (Token::Match(tok3, (name + " !!<").c_str()))
|
|
|
|
{
|
|
|
|
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy
|
|
|
|
addtoken(tok3->str().c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
if (Token::Match(tok3, "%type% <"))
|
|
|
|
{
|
|
|
|
if (!Token::Match(tok3, (name + " <").c_str()))
|
|
|
|
done = false;
|
|
|
|
used.push_back(_tokensBack);
|
|
|
|
}
|
|
|
|
|
|
|
|
// link() newly tokens manually
|
|
|
|
if (tok3->str() == "{")
|
|
|
|
{
|
|
|
|
braces.push(_tokensBack);
|
|
|
|
}
|
|
|
|
else if (tok3->str() == "}")
|
|
|
|
{
|
|
|
|
assert(braces.empty() == false);
|
|
|
|
Token::createMutualLinks(braces.top(), _tokensBack);
|
|
|
|
braces.pop();
|
|
|
|
}
|
|
|
|
else if (tok3->str() == "(")
|
|
|
|
{
|
|
|
|
brackets.push(_tokensBack);
|
|
|
|
}
|
2009-11-11 19:16:05 +01:00
|
|
|
else if (tok3->str() == "[")
|
|
|
|
{
|
|
|
|
brackets2.push(_tokensBack);
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
else if (tok3->str() == ")")
|
|
|
|
{
|
|
|
|
assert(brackets.empty() == false);
|
|
|
|
Token::createMutualLinks(brackets.top(), _tokensBack);
|
|
|
|
brackets.pop();
|
|
|
|
}
|
2009-11-11 19:16:05 +01:00
|
|
|
else if (tok3->str() == "]")
|
|
|
|
{
|
|
|
|
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..
|
|
|
|
s = name + " < " + type2 + " >";
|
|
|
|
for (std::string::size_type pos = s.find(","); pos != std::string::npos; pos = s.find(",", pos + 2))
|
|
|
|
{
|
|
|
|
s.insert(pos + 1, " ");
|
|
|
|
s.insert(pos, " ");
|
|
|
|
}
|
|
|
|
for (Token *tok4 = tok2; tok4; tok4 = tok4->next())
|
2009-03-11 18:50:24 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
if (Token::simpleMatch(tok4, s.c_str()))
|
|
|
|
{
|
|
|
|
tok4->str(name2);
|
|
|
|
while (tok4->next()->str() != ">")
|
2009-12-22 20:10:08 +01:00
|
|
|
{
|
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
|
|
|
}
|
2010-01-06 21:29:39 +01:00
|
|
|
used.remove(tok4->next());
|
2009-03-14 21:26:32 +01:00
|
|
|
tok4->deleteNext();
|
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
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-08-12 22:50:03 +02:00
|
|
|
void Tokenizer::updateClassList()
|
|
|
|
{
|
|
|
|
const char pattern_class[] = "class %var% [{:]";
|
|
|
|
_classInfoList.clear();
|
|
|
|
|
|
|
|
// Locate class
|
|
|
|
const Token *tok1 = tokens();
|
2009-09-13 09:03:48 +02:00
|
|
|
while ((tok1 = Token::findmatch(tok1, pattern_class)) != 0)
|
2009-08-12 22:50:03 +02:00
|
|
|
{
|
|
|
|
const char *className;
|
|
|
|
className = tok1->strAt(1);
|
|
|
|
tok1 = tok1->next();
|
|
|
|
|
|
|
|
ClassInfo::MemberType memberType = ClassInfo::PRIVATE;
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const Token *tok = tok1; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
// Indentation
|
|
|
|
if (tok->str() == "{")
|
|
|
|
{
|
|
|
|
++indentlevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse class contents (indentlevel == 1)..
|
|
|
|
if (indentlevel == 1)
|
|
|
|
{
|
|
|
|
if (tok->str() == "private:")
|
|
|
|
memberType = ClassInfo::PRIVATE;
|
|
|
|
else if (tok->str() == "protected:")
|
|
|
|
memberType = ClassInfo::PROTECTED;
|
|
|
|
else if (tok->str() == "public:")
|
|
|
|
memberType = ClassInfo::PUBLIC;
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "typedef %type% ("))
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "[:,] %var% ("))
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "%var% ("))
|
|
|
|
{
|
|
|
|
// member function
|
|
|
|
ClassInfo::MemberFunctionInfo func;
|
|
|
|
func._declaration = tok;
|
|
|
|
func._name = tok->str();
|
|
|
|
func._type = memberType;
|
|
|
|
|
|
|
|
_classInfoList[className]._memberFunctions.push_back(func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
void Tokenizer::setVarId()
|
|
|
|
{
|
|
|
|
// Clear all variable ids
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
tok->varId(0);
|
|
|
|
|
|
|
|
// Set variable ids..
|
|
|
|
unsigned int _varId = 0;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-03-21 21:58:39 +01: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
|
|
|
|
2009-03-21 21:58:39 +01:00
|
|
|
if (Token::Match(tok, "[,;{}(] %type%"))
|
2009-02-02 20:19:36 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
2009-09-22 17:49:13 +02:00
|
|
|
if (Token::Match(tok, "class|struct %type% :|{|;"))
|
|
|
|
continue;
|
|
|
|
|
2009-10-18 18:06:32 +02:00
|
|
|
if (Token::Match(tok, "else|return|typedef|delete|sizeof"))
|
2009-02-28 21:21:48 +01:00
|
|
|
continue;
|
|
|
|
|
2009-08-09 10:41:31 +02:00
|
|
|
if (Token::Match(tok, "const|static|extern|public:|private:|protected:"))
|
2009-06-18 18:30:04 +02:00
|
|
|
tok = tok->next();
|
|
|
|
|
2009-07-30 19:40:41 +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..
|
|
|
|
if (Token::Match(tok, "%type% <"))
|
|
|
|
{
|
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
|
|
|
|
2009-12-29 19:24:48 +01:00
|
|
|
while (Token::Match(tok2, "%var% ::"))
|
|
|
|
tok2 = tok2->tokAt(2);
|
2009-04-25 16:55:00 +02:00
|
|
|
|
2009-12-29 19:24:48 +01:00
|
|
|
if (Token::Match(tok2, "%type% <"))
|
|
|
|
{
|
|
|
|
level++;
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok2, "%type% ,"))
|
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (tok2 && (tok2->isName() || tok2->isNumber() || tok2->str() == "*" || tok2->str() == ","))
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (again);
|
|
|
|
|
|
|
|
do // Look for end of templates
|
|
|
|
{
|
|
|
|
again = false;
|
|
|
|
|
|
|
|
if (level == 1 && Token::Match(tok2, "> %var%"))
|
|
|
|
tok = tok2;
|
|
|
|
else if (level > 1 && tok2->str() == ">")
|
|
|
|
{
|
|
|
|
level--;
|
|
|
|
if (level == 0)
|
|
|
|
tok = tok2;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(1);
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (level == 1 && Token::Match(tok2, "> ::|*|& %var%"))
|
|
|
|
tok = tok2->next();
|
|
|
|
else
|
|
|
|
continue; // Not code that I understand / not a variable declaration
|
|
|
|
}
|
|
|
|
while (again);
|
2009-04-25 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Determine name of declared variable..
|
|
|
|
const char *varname = 0;
|
2009-03-01 21:02:24 +01:00
|
|
|
Token *tok2 = tok->tokAt(1);
|
2009-03-21 21:58:39 +01:00
|
|
|
while (tok2)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
if (tok2->isName())
|
|
|
|
varname = tok2->strAt(0);
|
2009-06-18 18:30:04 +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..
|
|
|
|
if (!tok2)
|
|
|
|
break;
|
|
|
|
|
2009-03-25 18:10:03 +01:00
|
|
|
// Is it a function?
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
{
|
2009-05-27 22:34:08 +02:00
|
|
|
// Search for function declaration, e.g. void f();
|
|
|
|
if (Token::simpleMatch(tok2->next(), ") ;"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Search for function declaration, e.g. void f( int c );
|
|
|
|
if (Token::Match(tok2->next(), "%num%") ||
|
|
|
|
Token::Match(tok2->next(), "%bool%") ||
|
|
|
|
tok2->next()->str()[0] == '"' ||
|
|
|
|
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
|
2009-03-25 18:10:03 +01:00
|
|
|
if (Token::Match(tok2, "[,();[=]") && varname)
|
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;
|
2009-01-24 18:15:38 +01:00
|
|
|
for (tok2 = tok->next(); tok2; tok2 = tok2->next())
|
|
|
|
{
|
2009-05-31 18:46:32 +02:00
|
|
|
if (!dot && tok2->str() == varname && !Token::Match(tok2->previous(), "struct|union"))
|
2009-01-24 18:15:38 +01:00
|
|
|
tok2->varId(_varId);
|
|
|
|
else if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel < 0)
|
|
|
|
break;
|
2009-09-30 13:40:10 +02:00
|
|
|
|
|
|
|
// We have reached the end of a loop: "for( int i;;) { }"
|
|
|
|
if (funcDeclaration && indentlevel <= 0)
|
|
|
|
break;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
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?
|
2009-02-17 20:18:26 +01: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;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
else if (parlevel < 0 && tok2->str() == ";")
|
|
|
|
break;
|
|
|
|
dot = bool(tok2->str() == ".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Struct/Class members
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-27 22:34:08 +02:00
|
|
|
// str.clear is a variable
|
|
|
|
// str.clear() is a member function
|
2009-01-24 18:15:38 +01:00
|
|
|
if (tok->varId() != 0 &&
|
2009-08-29 22:12:08 +02:00
|
|
|
Token::Match(tok->next(), ". %var% !!(") &&
|
2009-01-24 18:15:38 +01:00
|
|
|
tok->tokAt(2)->varId() == 0)
|
|
|
|
{
|
|
|
|
++_varId;
|
|
|
|
|
2009-08-29 22:25:14 +02:00
|
|
|
const std::string pattern(std::string("%varid% . ") + tok->strAt(2));
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
2009-08-29 22:25:14 +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
|
|
|
|
2009-08-07 23:49:37 +02:00
|
|
|
// Member functions in this source
|
|
|
|
std::list<Token *> allMemberFunctions;
|
|
|
|
{
|
|
|
|
const std::string funcpattern("%var% :: %var% (");
|
|
|
|
for (Token *tok2 = _tokens; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
// Found a class function..
|
|
|
|
if (Token::Match(tok2, funcpattern.c_str()))
|
|
|
|
{
|
|
|
|
allMemberFunctions.push_back(tok2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-02 22:57:18 +02:00
|
|
|
// class members..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "class %var% {"))
|
|
|
|
{
|
|
|
|
const std::string &classname(tok->next()->str());
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
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..
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a member variable..
|
|
|
|
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?
|
|
|
|
if (varlist.empty())
|
|
|
|
continue;
|
2009-05-02 22:57:18 +02:00
|
|
|
|
2009-05-03 14:31:54 +02:00
|
|
|
|
|
|
|
// Member functions for this class..
|
|
|
|
std::list<Token *> funclist;
|
|
|
|
{
|
|
|
|
const std::string funcpattern(classname + " :: %var% (");
|
2009-08-07 23:49:37 +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..
|
|
|
|
if (Token::Match(tok2, funcpattern.c_str()))
|
|
|
|
{
|
|
|
|
// Goto the end paranthesis..
|
2009-08-29 22:17:01 +02:00
|
|
|
tok2 = tok2->tokAt(3)->link();
|
2009-05-03 14:31:54 +02:00
|
|
|
if (!tok2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If this is a function implementation.. add it to funclist
|
|
|
|
if (Token::Match(tok2, ") const|volatile| {"))
|
|
|
|
funclist.push_back(tok2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are there any member functions for this class?
|
|
|
|
if (funclist.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Update the variable ids..
|
2009-08-07 23:49:37 +02:00
|
|
|
// Parse each function..
|
|
|
|
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;
|
|
|
|
for (Token *tok2 = *func; tok2; tok2 = tok2->next())
|
2009-05-03 14:31:54 +02:00
|
|
|
{
|
2009-08-07 23:49:37 +02:00
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}")
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
2009-08-07 23:49:37 +02:00
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
else if (indentlevel > 0 &&
|
|
|
|
tok2->varId() == 0 &&
|
|
|
|
varlist.find(tok2->str()) != varlist.end())
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Simplify token list
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-03-13 00:07:05 +01:00
|
|
|
void Tokenizer::simplifyNamespaces()
|
|
|
|
{
|
|
|
|
for (Token *token = _tokens; token; token = token->next())
|
|
|
|
{
|
2010-01-01 21:06:30 +01:00
|
|
|
while (token && token->str() == "namespace" && token->varId() == 0 &&
|
2009-03-13 00:07:05 +01:00
|
|
|
(!token->previous() || token->previous()->str() != "using"))
|
|
|
|
{
|
|
|
|
// Token is namespace and there is no "using" before it.
|
|
|
|
Token *start = token;
|
2009-03-13 22:25:56 +01:00
|
|
|
Token *tok = token->tokAt(2);
|
2009-03-13 00:07:05 +01:00
|
|
|
if (!tok)
|
|
|
|
return;
|
|
|
|
|
2009-03-13 22:25:56 +01:00
|
|
|
tok = tok->link();
|
|
|
|
if (tok && tok->str() == "}")
|
2009-03-13 00:07:05 +01:00
|
|
|
{
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
start->deleteNext();
|
|
|
|
start->deleteNext();
|
|
|
|
if (start->previous())
|
|
|
|
{
|
2009-03-17 19:16:15 +01:00
|
|
|
token = start->next();
|
2009-03-13 00:07:05 +01:00
|
|
|
start = start->previous();
|
|
|
|
start->deleteNext();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// First token in the list, don't delete
|
|
|
|
// as _tokens is attached to it.
|
|
|
|
start->deleteThis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-03-30 23:59:33 +02:00
|
|
|
|
|
|
|
if (!token)
|
|
|
|
break;
|
2009-03-13 00:07:05 +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;
|
2009-03-13 22:25:56 +01:00
|
|
|
for (Token *token = _tokens; token; token = token->next())
|
|
|
|
{
|
2009-03-15 00:39:45 +01:00
|
|
|
if (token->link())
|
|
|
|
{
|
|
|
|
token->link(0);
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:25:56 +01:00
|
|
|
if (token->str() == "{")
|
|
|
|
{
|
|
|
|
links.push_back(token);
|
2009-11-20 20:10:42 +01:00
|
|
|
type.push_back(token);
|
2009-03-13 22:25:56 +01:00
|
|
|
}
|
|
|
|
else if (token->str() == "}")
|
|
|
|
{
|
|
|
|
if (links.size() == 0)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|
2009-11-20 20:10:42 +01:00
|
|
|
if (type.back()->str() != "{")
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
2009-05-01 12:07:10 +02:00
|
|
|
else if (token->str() == "(")
|
|
|
|
{
|
|
|
|
links2.push_back(token);
|
2009-11-20 20:10:42 +01:00
|
|
|
type.push_back(token);
|
2009-05-01 12:07:10 +02:00
|
|
|
}
|
|
|
|
else if (token->str() == ")")
|
|
|
|
{
|
|
|
|
if (links2.size() == 0)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
2009-11-20 20:10:42 +01:00
|
|
|
if (type.back()->str() != "(")
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
2009-09-20 22:13:06 +02:00
|
|
|
else if (token->str() == "[")
|
|
|
|
{
|
|
|
|
links3.push_back(token);
|
2009-11-20 20:10:42 +01:00
|
|
|
type.push_back(token);
|
2009-09-20 22:13:06 +02:00
|
|
|
}
|
|
|
|
else if (token->str() == "]")
|
|
|
|
{
|
|
|
|
if (links3.size() == 0)
|
|
|
|
{
|
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;
|
|
|
|
}
|
2009-11-20 20:10:42 +01:00
|
|
|
if (type.back()->str() != "[")
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
if (links.size() > 0)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (links2.size() > 0)
|
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
|
|
|
}
|
|
|
|
|
2009-09-20 22:13:06 +02:00
|
|
|
if (links3.size() > 0)
|
|
|
|
{
|
|
|
|
// 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 *);
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-02-24 17:30:57 +01: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;
|
2009-02-28 09:34:02 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-10-07 09:54:34 +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();
|
2009-10-07 11:05:44 +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());
|
|
|
|
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
|
|
|
|
|
|
|
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));
|
|
|
|
if (size == 0)
|
2009-10-07 09:54:34 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
sizeOfVar[varId] = MathLib::toString<long>(size * MathLib::toLongNumber(tok->strAt(2)));
|
|
|
|
}
|
|
|
|
|
2009-10-07 11:05:44 +02:00
|
|
|
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ %num% ] [,)]") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [,)]"))
|
|
|
|
{
|
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));
|
|
|
|
}
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ ] = %str% ;"))
|
|
|
|
{
|
2009-10-10 21:54:58 +02:00
|
|
|
unsigned int size = sizeOfType(tok->tokAt(4));
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() != "sizeof")
|
|
|
|
continue;
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
// sizeof "text"
|
|
|
|
if (Token::Match(tok->next(), "%str%"))
|
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << (Token::getStrLength(tok) + 1);
|
|
|
|
tok->str(ostr.str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sizeof ("text")
|
|
|
|
if (Token::Match(tok->next(), "( %str% )"))
|
|
|
|
{
|
|
|
|
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(*...)
|
|
|
|
if (Token::simpleMatch(tok->next(), "* (") && !Token::simpleMatch(tok->tokAt(2)->link(), ") ."))
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->next()->insertToken("*");
|
|
|
|
}
|
|
|
|
|
2009-11-29 17:25:35 +01:00
|
|
|
// sizeof a++ -> sizeof(a++)
|
|
|
|
if (Token::Match(tok->next(), "++|-- %var% !!.") || Token::Match(tok->next(), "%var% ++|--"))
|
|
|
|
{
|
|
|
|
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 )
|
|
|
|
if (tok->next()->isNumber())
|
|
|
|
{
|
|
|
|
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 )
|
2009-12-06 08:46:59 +01:00
|
|
|
else if (tok->next()->str() != "(")
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
|
|
|
// Add parenthesis around the sizeof
|
|
|
|
for (Token *tempToken = tok->next(); tempToken; tempToken = tempToken->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tempToken, "%var%"))
|
|
|
|
{
|
2009-09-20 22:38:32 +02:00
|
|
|
while (tempToken->next()->str() == "[")
|
|
|
|
{
|
|
|
|
tempToken = tempToken->next()->link();
|
|
|
|
}
|
|
|
|
|
2009-07-27 11:24:24 +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;
|
|
|
|
}
|
2009-05-21 17:55:52 +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;
|
|
|
|
}
|
2009-05-22 17:03:42 +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);
|
|
|
|
}
|
2009-11-13 20:07:44 +01:00
|
|
|
else if (Token::simpleMatch(tempToken->next(), ") ."))
|
|
|
|
{
|
|
|
|
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(*)
|
2009-10-07 09:54:34 +02:00
|
|
|
if (Token::Match(tok->next(), "( %type% *)"))
|
2009-06-01 12:01:45 +02:00
|
|
|
{
|
|
|
|
tok->next()->deleteNext();
|
2009-10-07 09:54:34 +02:00
|
|
|
continue;
|
2009-06-01 12:01:45 +02:00
|
|
|
}
|
|
|
|
|
2009-10-07 09:54:34 +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 )
|
|
|
|
else if (Token::Match(tok->next(), "( %var% )") && tok->tokAt(2)->varId() != 0)
|
2009-06-05 04:50:06 +02:00
|
|
|
{
|
2009-10-07 09:54:34 +02:00
|
|
|
if (sizeOfVar.find(tok->tokAt(2)->varId()) != sizeOfVar.end())
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
else if (Token::Match(tok, "sizeof ( %type% )"))
|
|
|
|
{
|
2009-10-10 21:54:58 +02:00
|
|
|
unsigned int size = sizeOfType(tok->tokAt(2));
|
2009-01-24 18:15:38 +01:00
|
|
|
if (size > 0)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-02 19:59:32 +01: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();
|
2009-02-03 22:42:50 +01: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);
|
2009-10-22 23:00:27 +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());
|
|
|
|
}
|
2009-10-22 23:00:27 +02:00
|
|
|
else if (Token::Match(decltok->previous(), "* %var% ["))
|
2009-10-21 21:08:00 +02:00
|
|
|
{
|
|
|
|
sz = sizeOfType(decltok->previous());
|
|
|
|
}
|
2009-10-22 23:00:27 +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
|
|
|
}
|
|
|
|
|
2009-03-29 16:36:34 +02:00
|
|
|
if (sz > 0)
|
|
|
|
{
|
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();
|
|
|
|
|
2009-11-05 21:30:05 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok, "* const"))
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
|
2010-01-12 21:25:31 +01:00
|
|
|
simplifyStd();
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
simplifyNamespaces();
|
|
|
|
|
|
|
|
simplifyGoto();
|
|
|
|
|
|
|
|
// Combine wide strings
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
while (tok->str() == "L" && tok->next() && tok->next()->str()[0] == '"')
|
|
|
|
{
|
|
|
|
// Combine 'L "string"'
|
|
|
|
tok->str(tok->next()->str());
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine strings
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-09-27 16:04:10 +02:00
|
|
|
if (tok->str()[0] != '"')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tok->str(simplifyString(tok->str()));
|
|
|
|
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
|
|
|
// Remove unwanted keywords
|
|
|
|
static const char * const unwantedWords[] = { "unsigned", "unlikely", "likely" };
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
for (unsigned ui = 0; ui < sizeof(unwantedWords) / sizeof(unwantedWords[0]) && tok->next(); ui++)
|
|
|
|
{
|
|
|
|
if (tok->next()->str() == unwantedWords[ui])
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert + + into + and + - into -
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
while (tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "+")
|
|
|
|
{
|
|
|
|
if (tok->next()->str() == "+")
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->next()->str() == "-")
|
|
|
|
{
|
|
|
|
tok->str("-");
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tok->str() == "-")
|
|
|
|
{
|
|
|
|
if (tok->next()->str() == "-")
|
|
|
|
{
|
|
|
|
tok->str("+");
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->next()->str() == "+")
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-04 22:33:41 +02:00
|
|
|
// 0[a] -> a[0]
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "%num% [ %var% ]"))
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "strlen ( %str% )"))
|
|
|
|
{
|
|
|
|
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..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "%type% %var% [ ] [,;=]"))
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok->next(), tok->tokAt(4));
|
|
|
|
tok->insertToken("*");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-14 22:33:28 +01:00
|
|
|
// Replace constants..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "const %type% %var% = %num% ;"))
|
|
|
|
{
|
2009-06-19 18:03:09 +02:00
|
|
|
unsigned int varId = tok->tokAt(2)->varId();
|
2009-08-01 15:57:54 +02:00
|
|
|
if (varId == 0)
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(5);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-02-14 22:33:28 +01:00
|
|
|
const char *num = tok->strAt(4);
|
|
|
|
int indent = 1;
|
|
|
|
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
++indent;
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
--indent;
|
|
|
|
if (indent == 0)
|
|
|
|
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
|
2009-06-19 18:03:09 +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-08-29 15:29:19 +02:00
|
|
|
simplifyLogicalOperators();
|
2009-11-17 19:03:23 +01:00
|
|
|
simplifyCasts();
|
2009-03-05 20:32:02 +01:00
|
|
|
|
2009-03-04 07:24:03 +01:00
|
|
|
// Simplify simple calculations..
|
|
|
|
while (simplifyCalculations())
|
|
|
|
;
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Replace "*(str + num)" => "str[num]"
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-03 20:10:59 +02:00
|
|
|
if (! strchr(";{}(=<>", tok->str()[0]))
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Token *next = tok->next();
|
|
|
|
if (! next)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (Token::Match(next, "* ( %var% + %num% )"))
|
|
|
|
{
|
|
|
|
const char *str[4] = {"var", "[", "num", "]"};
|
|
|
|
str[0] = tok->strAt(3);
|
|
|
|
str[2] = tok->strAt(5);
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
tok->str(str[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-18 20:32:05 +01:00
|
|
|
// Simplify variable declarations
|
|
|
|
simplifyVarDecl();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Replace NULL with 0..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-09-23 21:44:52 +02:00
|
|
|
if (tok->str() == "NULL" || tok->str() == "'\\0'")
|
|
|
|
{
|
2009-01-24 18:15:38 +01:00
|
|
|
tok->str("0");
|
2009-09-23 21:44:52 +02:00
|
|
|
}
|
|
|
|
else if (tok->isNumber() &&
|
|
|
|
MathLib::isInt(tok->str()) &&
|
|
|
|
MathLib::toLongNumber(tok->str()) == 0)
|
|
|
|
{
|
|
|
|
tok->str("0");
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace pointer casts of 0.. "(char *)0" => "0"
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
if (Token::Match(tok->next(), "( %type% * ) 0") ||
|
|
|
|
Token::Match(tok->next(), "( %type% %type% * ) 0"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
simplifyFunctionParameters();
|
2009-02-17 21:03:08 +01:00
|
|
|
elseif();
|
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();
|
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();
|
2009-09-05 18:46:27 +02:00
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "case %any% : %var%"))
|
2009-06-14 08:20:51 +02:00
|
|
|
tok->tokAt(2)->insertToken(";");
|
2009-01-24 18:15:38 +01:00
|
|
|
if (Token::Match(tok, "default : %var%"))
|
|
|
|
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;
|
|
|
|
while (modified)
|
|
|
|
{
|
|
|
|
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..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
while (Token::simpleMatch(tok, "return ("))
|
|
|
|
{
|
2009-08-30 10:27:26 +02:00
|
|
|
Token *tok2 = tok->next()->link();
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-05 20:29:09 +02:00
|
|
|
simplifyComma();
|
2009-07-13 19:11:31 +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
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool Tokenizer::removeReduntantConditions()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-07-27 11:24:24 +02:00
|
|
|
if (tok->str() != "if")
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Token::Match(tok->tokAt(1), "( %bool% ) {"))
|
|
|
|
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;
|
|
|
|
if (tok->tokAt(2)->str() == "true")
|
|
|
|
boolValue = true;
|
|
|
|
|
|
|
|
// Handle if with else
|
|
|
|
if (elseTag && elseTag->str() == "else")
|
|
|
|
{
|
2009-10-05 14:06:50 +02:00
|
|
|
if (Token::simpleMatch(elseTag->next(), "if ("))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Handle "else if"
|
|
|
|
if (boolValue == false)
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
while (lastTagInIf)
|
|
|
|
{
|
|
|
|
if (lastTagInIf->str() == "(")
|
|
|
|
{
|
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();
|
|
|
|
if (!Token::simpleMatch(lastTagInIf, "else"))
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
lastTagInIf = lastTagInIf->next();
|
2009-07-27 11:24:24 +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
|
|
|
|
if (boolValue == false)
|
|
|
|
{
|
|
|
|
// Convert "if( false ) {aaa;} else {bbb;}" => "{bbb;}" or ";{bbb;}"
|
|
|
|
if (tok->previous())
|
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, elseTag->tokAt(1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-27 11:24:24 +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 )"
|
|
|
|
if (tok->previous())
|
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle if without else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (boolValue == false)
|
|
|
|
{
|
|
|
|
// Remove if and its content
|
|
|
|
if (tok->previous())
|
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, elseTag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// convert "if( true ) {aaa;}" => "{aaa;}"
|
|
|
|
if (tok->previous())
|
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-08-29 21:25:15 +02:00
|
|
|
void Tokenizer::simplifyIfAddBraces()
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok ? tok->next() : NULL)
|
|
|
|
{
|
2009-12-12 19:58:19 +01:00
|
|
|
if (tok->previous() && !Token::Match(tok->previous(), ";|{|}|else|)|:"))
|
|
|
|
continue;
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
if (Token::Match(tok, "if|for|while ("))
|
|
|
|
{
|
2009-08-22 09:49:45 +02:00
|
|
|
// don't add "{}" around ";" in "do {} while();" (#609)
|
|
|
|
const Token *prev = tok->previous();
|
|
|
|
if (Token::simpleMatch(prev, "} while") &&
|
|
|
|
prev->link() &&
|
|
|
|
prev->link()->previous() &&
|
|
|
|
prev->link()->previous()->str() == "do")
|
|
|
|
{
|
|
|
|
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 '{'
|
|
|
|
if (!tok || Token::simpleMatch(tok, ") {"))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok->str() == "else")
|
|
|
|
{
|
|
|
|
// An else followed by an if or brace don't need to be processed further
|
|
|
|
if (Token::Match(tok, "else if|{"))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-05-09 22:12:14 +02:00
|
|
|
// If there is no code after he if(), abort
|
|
|
|
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
|
|
|
|
|
|
|
// 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) { }
|
|
|
|
int parlevel = 0;
|
|
|
|
int indentlevel = 0;
|
2009-03-15 13:44:57 +01:00
|
|
|
while ((tempToken = tempToken->next()) != NULL)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-03-15 13:44:57 +01:00
|
|
|
if (tempToken->str() == "{")
|
2009-01-24 18:15:38 +01:00
|
|
|
++indentlevel;
|
|
|
|
|
2009-03-15 13:44:57 +01:00
|
|
|
else if (tempToken->str() == "}")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
--indentlevel;
|
2009-06-22 22:54:11 +02:00
|
|
|
if (indentlevel == 0 && parlevel == 0)
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
2009-11-20 20:38:30 +01:00
|
|
|
|
|
|
|
else if (indentlevel < 0 && parlevel == 0)
|
|
|
|
{
|
|
|
|
// insert closing brace before this
|
|
|
|
tempToken = tempToken->previous();
|
|
|
|
break;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-03-15 13:44:57 +01:00
|
|
|
else if (tempToken->str() == "(")
|
2009-01-24 18:15:38 +01:00
|
|
|
++parlevel;
|
|
|
|
|
2009-03-15 13:44:57 +01:00
|
|
|
else if (tempToken->str() == ")")
|
2009-01-24 18:15:38 +01:00
|
|
|
--parlevel;
|
|
|
|
|
2009-03-15 13:44:57 +01:00
|
|
|
else if (indentlevel == 0 && parlevel == 0 && tempToken->str() == ";")
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-15 13:44:57 +01: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
|
|
|
{
|
2009-12-03 23:06:03 +01: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;
|
|
|
|
while (tok3)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2009-12-03 23:06:03 +01: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;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok3, "do {"))
|
|
|
|
{
|
|
|
|
// Skip do{}while inside the current "do"
|
|
|
|
tok3 = tok3->next()->link();
|
|
|
|
if (Token::simpleMatch(tok3->next(), "while"))
|
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok3, "do !!{") &&
|
|
|
|
!Token::Match(tok3->next(), "[),]"))
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
2009-12-03 23:06:03 +01:00
|
|
|
if (tok2)
|
|
|
|
{
|
|
|
|
// 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()
|
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "do !!{"))
|
|
|
|
{
|
|
|
|
while (simplifyDoWhileAddBracesHelper(tok))
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok->str() == ")")
|
|
|
|
--parlevel;
|
|
|
|
else if (parlevel == 0 && Token::Match(tok, "; %var% = %var% ? %var% : %var% ;"))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
while (pos1 != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string::size_type pos2 = str.find(" ", pos1);
|
|
|
|
if (pos2 == std::string::npos)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-06-03 21:02:16 +02:00
|
|
|
if (Token::Match(tok, "! %num%") || Token::Match(tok, "! %bool%"))
|
|
|
|
{
|
|
|
|
if (tok->next()->str() == "0" || tok->next()->str() == "false")
|
|
|
|
tok->str("true");
|
|
|
|
else
|
|
|
|
tok->str("false");
|
|
|
|
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2009-08-22 16:04: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;
|
|
|
|
}
|
|
|
|
|
2009-08-22 16:04: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"
|
2009-02-07 11:54:39 +01:00
|
|
|
if (Token::Match(tok, "if|while ( %num%") &&
|
|
|
|
(tok->tokAt(3)->str() == ")" || tok->tokAt(3)->str() == "||" || tok->tokAt(3)->str() == "&&"))
|
|
|
|
{
|
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);
|
|
|
|
if (tok2 &&
|
|
|
|
(tok->str() == "&&" || tok->str() == "||") &&
|
|
|
|
Token::Match(tok->next(), "%num%") &&
|
2009-01-24 18:15:38 +01:00
|
|
|
(tok2->str() == ")" || tok2->str() == "&&" || tok2->str() == "||"))
|
|
|
|
{
|
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);
|
|
|
|
if (! tok4)
|
|
|
|
break;
|
|
|
|
if ((tok->str() == "&&" || tok->str() == "||" || tok->str() == "(") &&
|
2009-05-29 23:04:01 +02:00
|
|
|
(Token::Match(tok->tokAt(1), "%num% %any% %num%") ||
|
|
|
|
Token::Match(tok->tokAt(1), "%bool% %any% %bool%")) &&
|
2009-10-06 11:44:53 +02:00
|
|
|
(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;
|
2009-05-29 23:04:01 +02:00
|
|
|
if (Token::Match(tok->tokAt(1), "%num%"))
|
|
|
|
{
|
|
|
|
// Compare numbers
|
|
|
|
double op1 = (strstr(tok->strAt(1), "0x")) ? std::strtol(tok->strAt(1), 0, 16) : std::atof(tok->strAt(1));
|
|
|
|
double op2 = (strstr(tok->strAt(3), "0x")) ? std::strtol(tok->strAt(3), 0, 16) : std::atof(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 if (cmp == "<=")
|
|
|
|
result = (op1 <= op2);
|
|
|
|
else if (cmp == "<")
|
|
|
|
result = (op1 < op2);
|
|
|
|
else
|
|
|
|
cmp = "";
|
|
|
|
}
|
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"));
|
|
|
|
|
|
|
|
if (cmp == "==")
|
|
|
|
result = (op1 == op2);
|
|
|
|
else if (cmp == "!=")
|
|
|
|
result = (op1 != op2);
|
|
|
|
else 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-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
if (! cmp.empty())
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() != "?")
|
|
|
|
continue;
|
|
|
|
|
2009-10-05 16:34:38 +02:00
|
|
|
if (!tok->tokAt(-2))
|
2009-06-05 23:33:13 +02:00
|
|
|
continue;
|
|
|
|
|
2009-06-14 08:26:20 +02:00
|
|
|
if (!Token::Match(tok->tokAt(-2), "[=,(]"))
|
2009-06-05 23:33:13 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Token::Match(tok->previous(), "%bool%") &&
|
|
|
|
!Token::Match(tok->previous(), "%num%"))
|
|
|
|
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;
|
2009-07-23 10:12:02 +02:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-07-22 13:29:42 +02:00
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel == 0)
|
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
else if (parlevel == 0 && tok2->str() == ":")
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
if (!semicolon || !semicolon->next())
|
|
|
|
continue;
|
2009-07-22 13:29:42 +02:00
|
|
|
|
2009-07-23 10:12:02 +02:00
|
|
|
if (tok->previous()->str() == "false" ||
|
|
|
|
tok->previous()->str() == "0")
|
|
|
|
{
|
|
|
|
// 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
|
|
|
{
|
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));
|
2009-10-05 16:34:38 +02:00
|
|
|
int ind = 0;
|
|
|
|
for (const Token *end = semicolon; end; end = end->next())
|
|
|
|
{
|
|
|
|
if (end->str() == ";")
|
|
|
|
{
|
|
|
|
Token::eraseTokens(semicolon->previous(), end->next());
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-23 10:12:02 +02:00
|
|
|
|
2009-12-06 10:58:27 +01:00
|
|
|
else if (Token::Match(end, "[({[]"))
|
2009-10-05 16:34:38 +02:00
|
|
|
{
|
|
|
|
++ind;
|
|
|
|
}
|
|
|
|
|
2009-12-06 10:58:27 +01:00
|
|
|
else if (Token::Match(end, "[)}]]"))
|
2009-10-05 16:34:38 +02:00
|
|
|
{
|
|
|
|
--ind;
|
|
|
|
if (ind < 0)
|
|
|
|
{
|
|
|
|
Token::eraseTokens(semicolon->previous(), end);
|
|
|
|
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
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-10-04 23:12:12 +02:00
|
|
|
while (Token::Match(tok->next(), "( %type% *| ) *|&| %var%") ||
|
|
|
|
Token::Match(tok->next(), "( %type% %type% *| ) *|&| %var%"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-03-09 19:47:21 +01:00
|
|
|
if (tok->isName() && tok->str() != "return")
|
2009-10-04 23:12:12 +02:00
|
|
|
break;
|
2009-03-10 21:26:08 +01:00
|
|
|
|
2009-10-29 20:54:51 +01:00
|
|
|
if (Token::simpleMatch(tok->previous(), "operator"))
|
|
|
|
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
|
|
|
|
|
|
|
if (tok->str() == ")" && tok->link()->previous())
|
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2009-10-04 23:12:12 +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;
|
2009-06-19 19:25:56 +02:00
|
|
|
while (tok2)
|
2009-05-27 20:06:19 +02:00
|
|
|
{
|
2009-06-19 19:25:56 +02:00
|
|
|
if (tok2->str() == "<")
|
2009-05-27 20:06:19 +02:00
|
|
|
++level;
|
2009-06-19 19:25:56 +02:00
|
|
|
else if (tok2->str() == ">")
|
2009-05-27 20:06:19 +02:00
|
|
|
{
|
|
|
|
--level;
|
|
|
|
if (level == 0)
|
|
|
|
break;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
2009-06-19 19:25:56 +02:00
|
|
|
if (Token::simpleMatch(tok2, "> ("))
|
|
|
|
{
|
2009-09-13 12:31:32 +02:00
|
|
|
Token *closeBracket = tok2->next()->link();
|
|
|
|
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
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-12-31 19:31:21 +01:00
|
|
|
if (tok->str() == "{" || tok->str() == "[" || tok->str() == "(")
|
2009-12-30 15:12:38 +01:00
|
|
|
{
|
|
|
|
tok = tok->link();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-26 23:26:50 +01:00
|
|
|
|
|
|
|
// Find the function e.g. foo( x ) or foo( x, y )
|
2009-12-30 15:12:38 +01: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
|
|
|
|
|
|
|
|
// Get list of argument names
|
|
|
|
std::map<std::string, Token*> argumentNames;
|
|
|
|
bool bailOut = false;
|
|
|
|
for (tok = tok->tokAt(2); tok; tok = tok->tokAt(2))
|
|
|
|
{
|
|
|
|
if (!Token::Match(tok, "%var% [,)]"))
|
|
|
|
{
|
|
|
|
bailOut = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-09-05 21:21:25 +02:00
|
|
|
if (argumentNames.find(tok->str()) != argumentNames.end())
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
if (tok->next()->str() == ")")
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bailOut)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *start = tok;
|
|
|
|
while (tok && tok->str() != "{")
|
|
|
|
{
|
|
|
|
if (tok->str() == ";")
|
|
|
|
{
|
|
|
|
tok = tok->previous();
|
|
|
|
// Move tokens from start to tok into the place of
|
|
|
|
// argumentNames[tok->str()] and remove the ";"
|
|
|
|
|
|
|
|
if (argumentNames.find(tok->str()) == argumentNames.end())
|
|
|
|
{
|
|
|
|
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-01-01 13:26:54 +01:00
|
|
|
if (Token::simpleMatch(tok, "{"))
|
|
|
|
tok = tok->link();
|
2009-12-30 15:12:38 +01:00
|
|
|
|
2009-01-27 08:39:11 +01:00
|
|
|
if (tok == NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
if (bailOut)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
bool Tokenizer::simplifyFunctionReturn()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const Token *tok = tokens(); tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
--indentlevel;
|
|
|
|
|
|
|
|
else if (indentlevel == 0 && Token::Match(tok, "%var% ( ) { return %num% ; }"))
|
|
|
|
{
|
|
|
|
std::ostringstream pattern;
|
|
|
|
pattern << "[(=+-*/] " << tok->str() << " ( ) [;)+-*/]";
|
|
|
|
for (Token *tok2 = _tokens; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2, pattern.str().c_str()))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
if (op == "++")
|
|
|
|
++ivalue;
|
|
|
|
else if (op == "--")
|
|
|
|
--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;"
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-11-22 09:06:39 +01:00
|
|
|
if (Token::simpleMatch(tok, "= {"))
|
|
|
|
{
|
|
|
|
tok = tok->next()->link();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-18 20:32:05 +01:00
|
|
|
if (tok->previous() && !Token::Match(tok->previous(), "[{};)]"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Token *type0 = tok;
|
|
|
|
if (!Token::Match(type0, "%type%"))
|
|
|
|
continue;
|
2009-10-03 12:03:54 +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;
|
|
|
|
|
2009-08-06 23:31:39 +02:00
|
|
|
while (Token::Match(tok2, "%type% %type% *| *| %var%"))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
if (tok2->str() == "const")
|
|
|
|
isconst = true;
|
|
|
|
|
2009-10-03 12:03:54 +02:00
|
|
|
else if (tok2->str() == "static")
|
|
|
|
isstatic = true;
|
|
|
|
|
2009-03-18 20:32:05 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
++typelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't split up const declaration..
|
|
|
|
if (isconst && Token::Match(tok2, "%type% %var% ="))
|
|
|
|
continue;
|
|
|
|
|
2009-11-22 09:06:39 +01:00
|
|
|
// strange looking variable declaration => don't split up.
|
|
|
|
if (Token::Match(tok2, "%type% *| %var% , %type% *| %var%"))
|
|
|
|
continue;
|
|
|
|
|
2009-10-03 12:11:52 +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
|
|
|
|
2009-10-03 12:11:52 +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
|
|
|
|
|
|
|
if (isstatic && tok2->str() == "=")
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2->next(), "%num% ,"))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok2, "%type% * * %var% ,|="))
|
|
|
|
{
|
|
|
|
if (tok2->tokAt(3)->str() != "operator")
|
|
|
|
tok2 = tok2->tokAt(4); // The ',' token
|
|
|
|
else
|
|
|
|
tok2 = NULL;
|
2009-03-18 20:32:05 +01:00
|
|
|
}
|
|
|
|
|
2009-06-14 19:32:34 +02:00
|
|
|
else if (Token::Match(tok2, "%type% * const %var% ,|="))
|
|
|
|
{
|
|
|
|
if (tok2->tokAt(3)->str() != "operator")
|
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(4); // The ',' token
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok2 = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-20 12:28:15 +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
|
2009-09-20 12:28:15 +02:00
|
|
|
|
|
|
|
if (tok2->str() == "=")
|
|
|
|
{
|
|
|
|
while (tok2 && tok2->str() != ",")
|
|
|
|
{
|
2009-09-20 13:28:56 +02:00
|
|
|
if (tok2->str() == "{")
|
|
|
|
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
|
|
|
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
tok2 = NULL;
|
2009-09-20 12:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-18 20:32:05 +01:00
|
|
|
}
|
|
|
|
|
2009-08-09 10:16:37 +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
|
|
|
|
}
|
|
|
|
|
2009-07-28 21:46:33 +02:00
|
|
|
else if (Token::Match(tok2, "std :: %type% <") || Token::Match(tok2, "%type% <"))
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Deal with templates and standart types
|
|
|
|
//
|
|
|
|
if (Token::simpleMatch(tok2, "std ::"))
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
for (Token *tok3 = tok2; tok3; tok3 = tok3->next())
|
|
|
|
{
|
|
|
|
++typelen;
|
|
|
|
|
|
|
|
if (tok3->str() == "<")
|
|
|
|
{
|
|
|
|
++indentlevel;
|
|
|
|
}
|
|
|
|
else if (tok3->str() == ">")
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel == 0)
|
|
|
|
{
|
|
|
|
tok2 = tok3->next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-23 08:26:16 +02:00
|
|
|
else if (tok3->str() == ";")
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2009-07-28 21:46:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(tok2, ":: %type%"))
|
|
|
|
{
|
|
|
|
typelen += 2;
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok2->str() == "*" || tok2->str() == "&")
|
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(tok2, "%var% ,"))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tok2)
|
|
|
|
{
|
|
|
|
if (tok2->str() == ",")
|
|
|
|
{
|
|
|
|
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;
|
2009-12-05 21:15:14 +01:00
|
|
|
while (((typelen--) > 0) && (0 != (tok2 = tok2->next())))
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
2009-12-05 22:19:54 +01:00
|
|
|
link1.push(tok2);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok2->str() == "[")
|
|
|
|
link2.push(tok2);
|
|
|
|
else if (tok2->str() == "]" && !link2.empty())
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
while (tok2)
|
|
|
|
{
|
2009-08-08 12:33:07 +02:00
|
|
|
if (Token::Match(tok2, "[{(<]"))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
++parlevel;
|
|
|
|
}
|
|
|
|
|
2009-08-08 12:33:07 +02:00
|
|
|
else if (Token::Match(tok2, "[})>]"))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
2009-06-14 14:57:47 +02:00
|
|
|
if (parlevel <= 0)
|
2009-03-18 20:32:05 +01:00
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
|
2009-05-03 20:10:59 +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);
|
2009-06-14 19:32:34 +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 "
|
|
|
|
if (tok2->str() == ",")
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-06 10:40:48 +02:00
|
|
|
void Tokenizer::unsignedint()
|
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-09-28 20:25:05 +02:00
|
|
|
if (!Token::Match(tok, "unsigned|signed"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::Match(tok->previous(), "%type% unsigned|signed %var% [;,=)]") &&
|
|
|
|
tok->previous()->isStandardType())
|
|
|
|
{
|
|
|
|
if (tok->str() == "signed")
|
|
|
|
{
|
|
|
|
// int signed a; -> int a;
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// int unsigned a; -> unsigned int a;
|
|
|
|
std::string temp = tok->str();
|
|
|
|
tok->str(tok->previous()->str());
|
|
|
|
tok->previous()->str(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// signed int a; -> int a;
|
|
|
|
if (Token::Match(tok, "signed %type% %var% [;,=)]"))
|
|
|
|
{
|
|
|
|
if (tok->next()->isStandardType())
|
|
|
|
{
|
|
|
|
tok->str(tok->next()->str());
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-06 10:40:48 +02:00
|
|
|
// A variable declaration where the "int" is left out?
|
2009-11-17 19:03:23 +01:00
|
|
|
else if (!Token::Match(tok, "unsigned|signed %var% [;,=)]") &&
|
|
|
|
!Token::Match(tok->previous(), "( unsigned|signed )"))
|
2009-06-06 10:40:48 +02:00
|
|
|
continue;
|
|
|
|
|
2009-09-28 20:25:05 +02:00
|
|
|
// Previous token should either be a symbol or one of "{};(,"
|
2009-06-06 10:40:48 +02:00
|
|
|
if (tok->previous() &&
|
|
|
|
!tok->previous()->isName() &&
|
2009-09-28 20:25:05 +02:00
|
|
|
!Token::Match(tok->previous(), "[{};(,]"))
|
2009-06-06 10:40:48 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// next token should not be a standard type?
|
|
|
|
if (tok->next()->isStandardType())
|
2009-09-28 20:25:05 +02:00
|
|
|
{
|
|
|
|
if (tok->str() == "signed")
|
|
|
|
{
|
|
|
|
tok->str(tok->next()->str());
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
|
2009-06-06 10:40:48 +02:00
|
|
|
continue;
|
2009-09-28 20:25:05 +02:00
|
|
|
}
|
2009-06-06 10:40:48 +02:00
|
|
|
|
|
|
|
// The "int" is missing.. add it
|
2009-09-28 20:25:05 +02:00
|
|
|
if (tok->str() == "signed")
|
|
|
|
tok->str("int");
|
|
|
|
else
|
|
|
|
tok->insertToken("int");
|
2009-06-06 10:40:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-29 21:17:17 +02:00
|
|
|
void Tokenizer::simplifyIfAssign()
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
if (!Token::Match(tok->next(), "if|while ( !| (| %var% =") &&
|
|
|
|
!Token::Match(tok->next(), "if|while ( !| (| %var% . %var% ="))
|
|
|
|
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() == "!");
|
|
|
|
if (isNot)
|
|
|
|
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;
|
2009-08-05 19:45:05 +02:00
|
|
|
while (tok->next()->str() == "(")
|
|
|
|
{
|
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;
|
2009-10-06 20:03:15 +02:00
|
|
|
for (tok2 = tok->next(); tok2; tok2 = tok2->next())
|
2009-08-05 19:45:05 +02:00
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == ")")
|
2009-03-24 18:23:21 +01:00
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
if (indentlevel <= 0)
|
|
|
|
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();
|
|
|
|
if (Token::simpleMatch(tok->tokAt(2), "."))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
while (! braces.empty())
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
if (isNot)
|
|
|
|
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
|
|
|
|
if (iswhile)
|
|
|
|
{
|
|
|
|
indentlevel = 0;
|
|
|
|
Token *tok3 = tok2;
|
|
|
|
for (tok3 = tok2; tok3; tok3 = tok3->next())
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
if (tok3->str() == "{")
|
2009-03-23 18:20:56 +01:00
|
|
|
++indentlevel;
|
2009-08-05 19:45:05 +02:00
|
|
|
else if (tok3->str() == "}")
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
if (indentlevel <= 1)
|
2009-03-23 18:20:56 +01:00
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
}
|
2009-03-24 18:23:21 +01:00
|
|
|
|
2009-08-05 19:45:05 +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;
|
|
|
|
|
2009-08-05 19:45:05 +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
|
|
|
|
if (newTok->str() == ")")
|
|
|
|
{
|
|
|
|
braces.push_back(newTok);
|
|
|
|
}
|
2009-11-12 19:41:35 +01:00
|
|
|
else if (newTok->str() == "]")
|
|
|
|
{
|
|
|
|
braces2.push_back(newTok);
|
|
|
|
}
|
2009-08-29 13:09:24 +02:00
|
|
|
else if (newTok->str() == "(")
|
|
|
|
{
|
|
|
|
Token::createMutualLinks(newTok, braces.back());
|
|
|
|
braces.pop_back();
|
|
|
|
}
|
2009-11-12 19:41:35 +01:00
|
|
|
else if (newTok->str() == "[")
|
|
|
|
{
|
|
|
|
Token::createMutualLinks(newTok, braces2.back());
|
|
|
|
braces2.pop_back();
|
|
|
|
}
|
2009-06-20 13:58:30 +02:00
|
|
|
}
|
2009-03-23 18:20:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-07-18 10:18:46 +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();
|
2009-07-29 23:37:01 +02:00
|
|
|
while (tok && tok->str() == "(")
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
|
2009-07-18 10:18:46 +02:00
|
|
|
if (Token::simpleMatch(tok, "0 == (") ||
|
|
|
|
Token::Match(tok, "0 == %var%"))
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->str("!");
|
|
|
|
}
|
2009-05-01 12:07:10 +02:00
|
|
|
|
2009-07-18 10:18:46 +02:00
|
|
|
else if (Token::Match(tok, "%var% == 0"))
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->next()->str(tok->str());
|
|
|
|
tok->str("!");
|
|
|
|
}
|
|
|
|
|
2009-07-25 13:11:29 +02:00
|
|
|
else if (Token::Match(tok, "%var% .|:: %var% == 0"))
|
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
|
|
|
|
|
|
|
else if (Token::Match(tok, "* %var% == 0"))
|
|
|
|
{
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->insertToken("!");
|
|
|
|
tok = tok->tokAt(3);
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
|
|
|
}
|
2009-05-01 12:07:10 +02:00
|
|
|
}
|
|
|
|
|
2009-07-18 10:18:46 +02:00
|
|
|
else if (tok->link() && Token::simpleMatch(tok, ") == 0"))
|
2009-05-01 12:07:10 +02:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-08-20 21:37:05 +02:00
|
|
|
if (Token::Match(tok->link()->previous(), "%var%"))
|
|
|
|
{
|
|
|
|
// 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
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
Token *deleteFrom = NULL;
|
|
|
|
|
|
|
|
if (tok->str() == "(" || tok->str() == "||" || tok->str() == "&&")
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, "0 != (") ||
|
|
|
|
Token::Match(tok, "0 != %var%"))
|
|
|
|
{
|
|
|
|
deleteFrom = tok->previous();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "%var% != 0"))
|
|
|
|
{
|
|
|
|
deleteFrom = tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "%var% .|:: %var% != 0"))
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
deleteFrom = tok;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok->link() && Token::simpleMatch(tok, ") != 0"))
|
|
|
|
{
|
|
|
|
deleteFrom = tok;
|
|
|
|
}
|
|
|
|
|
2009-07-25 16:27:18 +02:00
|
|
|
if (deleteFrom)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-29 21:03:37 +02:00
|
|
|
void Tokenizer::simplifyLogicalOperators()
|
2009-05-31 10:42:27 +02:00
|
|
|
{
|
|
|
|
// "if (not p)" => "if (!p)"
|
2009-08-29 15:29:19 +02:00
|
|
|
// "if (p and q)" => "if (p and q)"
|
2009-05-31 10:42:27 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
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("!");
|
2009-08-29 15:11:47 +02:00
|
|
|
}
|
|
|
|
else if (Token::Match(tok, "&& not %var%"))
|
|
|
|
{
|
2009-05-31 10:42:27 +02:00
|
|
|
tok->next()->str("!");
|
2009-08-29 15:11:47 +02:00
|
|
|
}
|
|
|
|
else if (Token::Match(tok, "|| not %var%"))
|
|
|
|
{
|
2009-05-31 10:42:27 +02:00
|
|
|
tok->next()->str("!");
|
2009-08-29 15:11:47 +02:00
|
|
|
}
|
2009-08-29 15:29:19 +02:00
|
|
|
// "%var%|) and %var%|("
|
|
|
|
else if (tok->str() == "and" &&
|
|
|
|
((Token::Match(tok->previous(), "%var%") || tok->previous()->str() == ")") ||
|
2009-08-29 22:33:12 +02:00
|
|
|
(Token::Match(tok->next(), "%var%") || tok->next()->str() == "(")))
|
2009-08-29 15:29:19 +02:00
|
|
|
{
|
|
|
|
tok->str("&&");
|
|
|
|
}
|
2009-05-31 10:42:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
bool Tokenizer::simplifyKnownVariables()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
// Search for a block of code
|
|
|
|
if (! Token::Match(tok, ") const| {"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// parse the block of code..
|
|
|
|
int indentlevel = 0;
|
2009-12-08 22:12:49 +01:00
|
|
|
Token *tok2 = tok;
|
|
|
|
for (; tok2; tok2 = tok2->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-22 22:36:03 +02:00
|
|
|
else if (tok2->previous()->str() != "*" &&
|
|
|
|
(Token::Match(tok2, "%var% = %num% ;") ||
|
2009-10-17 18:21:13 +02:00
|
|
|
Token::Match(tok2, "%var% = %str% ;") ||
|
2009-11-07 08:25:39 +01:00
|
|
|
Token::Match(tok2, "%var% [ ] = %str% ;") ||
|
2009-10-17 18:21:13 +02:00
|
|
|
Token::Match(tok2, "%var% = %bool% ;") ||
|
|
|
|
Token::Match(tok2, "%var% = %var% ;")))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-11-12 22:16:24 +01:00
|
|
|
const unsigned int varid = tok2->varId();
|
2009-01-24 18:15:38 +01:00
|
|
|
if (varid == 0)
|
|
|
|
continue;
|
|
|
|
|
2009-12-30 20:15:44 +01:00
|
|
|
if (tok2->str() == tok2->strAt(2))
|
|
|
|
continue;
|
|
|
|
|
2009-10-17 18:21:13 +02:00
|
|
|
const bool pointeralias(tok2->tokAt(2)->isName());
|
|
|
|
|
2009-02-24 17:11:37 +01:00
|
|
|
std::string value(tok2->strAt(2));
|
2009-11-07 08:25:39 +01:00
|
|
|
if (value == "]")
|
|
|
|
value = tok2->strAt(4);
|
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
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok3 = tok2->next(); tok3; tok3 = tok3->next())
|
|
|
|
{
|
2009-05-25 14:21:58 +02:00
|
|
|
if (tok3->str() == "{")
|
|
|
|
{
|
|
|
|
++indentlevel3;
|
|
|
|
}
|
|
|
|
else if (tok3->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel3;
|
|
|
|
if (indentlevel3 < indentlevel)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-10-17 18:21:13 +02:00
|
|
|
if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str()))
|
|
|
|
break;
|
|
|
|
|
2009-08-22 10:23:55 +02:00
|
|
|
// Stop if something like 'while (--var)' is found
|
2009-09-28 17:15:31 +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;
|
2009-10-09 17:28:23 +02:00
|
|
|
for (const Token *tok4 = tok3; tok4 && tok4 != endpar; tok4 = tok4->next())
|
2009-08-22 10:23:55 +02:00
|
|
|
{
|
|
|
|
if (Token::Match(tok4, "++|-- %varid%", varid) ||
|
|
|
|
Token::Match(tok4, "%varid% ++|--", varid))
|
|
|
|
{
|
|
|
|
bailout = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bailout)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-03 20:57:27 +02:00
|
|
|
if (bailOutFromLoop)
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
if (tok3->varId() == varid)
|
|
|
|
{
|
|
|
|
// Continue
|
|
|
|
tok2 = bailOutFromLoop;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (tok3 == bailOutFromLoop)
|
|
|
|
{
|
|
|
|
// We have skipped the loop
|
|
|
|
bailOutFromLoop = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2009-07-27 11:24:24 +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.
|
|
|
|
bailOutFromLoop = tok3->link();
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-25 17:54:04 +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
|
|
|
|
|
|
|
// Variable is used somehow in a non-defined pattern => bail out
|
|
|
|
if (tok3->varId() == varid)
|
|
|
|
break;
|
|
|
|
|
2009-03-25 07:10:17 +01:00
|
|
|
// Using the variable in condition..
|
2009-06-03 21:02:16 +02:00
|
|
|
if (Token::Match(tok3, "(|!|==|!=|<|<=|>|>= %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..
|
2009-10-20 22:58:46 +02:00
|
|
|
if (Token::Match(tok3, "[=+-*/[] %varid% [?+-*/;]]", varid) ||
|
2009-10-06 08:14:59 +02:00
|
|
|
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);
|
2009-02-14 07:11:37 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
2009-02-24 17:11:37 +01:00
|
|
|
|
2009-11-02 23:07:15 +01:00
|
|
|
if (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));
|
2009-05-25 08:26:11 +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-01-15 22:46:47 +01:00
|
|
|
if (Token::Match(tok3->next(), "++|-- %varid%", varid) && MathLib::isInt(value) &&
|
2009-12-07 22:42:30 +01: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);
|
2009-05-25 08:31:20 +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..
|
|
|
|
if (Token::Match(tok3, "return %varid% ;", varid))
|
|
|
|
{
|
|
|
|
tok3->next()->str(value);
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-08 22:12:49 +01:00
|
|
|
|
|
|
|
if (tok2)
|
|
|
|
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
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (!Token::simpleMatch(tok, "else if"))
|
|
|
|
continue;
|
|
|
|
int indent = 0;
|
|
|
|
for (Token *tok2 = tok; indent >= 0 && tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2, "(|{"))
|
|
|
|
++indent;
|
|
|
|
else if (Token::Match(tok2, ")|}"))
|
|
|
|
--indent;
|
|
|
|
|
|
|
|
if (indent == 0 && Token::Match(tok2, "}|;"))
|
|
|
|
{
|
2009-11-23 18:59:50 +01: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;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-29 23:04:01 +02:00
|
|
|
if (tok->str() != "(")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-06-12 15:04:58 +02:00
|
|
|
while (Token::Match(tok->previous(), "[;{] ( %var% (") &&
|
|
|
|
tok->link()->previous() == tok->tokAt(2)->link())
|
|
|
|
{
|
|
|
|
// We have "( func ( *something* ))", remove the outer
|
|
|
|
// paranthesis
|
|
|
|
tok->link()->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2009-06-12 16:14:01 +02:00
|
|
|
while (Token::Match(tok->previous(), "[;{] ( delete %var% ) ;"))
|
|
|
|
{
|
|
|
|
// We have "( delete var )", remove the outer
|
|
|
|
// paranthesis
|
|
|
|
tok->tokAt(3)->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (Token::Match(tok->previous(), "[;{] ( delete [ ] %var% ) ;"))
|
|
|
|
{
|
|
|
|
// We have "( delete [] var )", remove the outer
|
|
|
|
// paranthesis
|
|
|
|
tok->tokAt(5)->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2009-10-05 13:30:05 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(tok->previous(), "[(!] ( %var% . %var% )"))
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2009-05-29 23:04:01 +02:00
|
|
|
if (Token::Match(tok, "( ( %bool% )") ||
|
|
|
|
Token::Match(tok, "( ( %num% )"))
|
|
|
|
{
|
|
|
|
tok->tokAt(2)->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
2009-01-26 17:38:08 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-04 07:24:03 +01:00
|
|
|
bool Tokenizer::simplifyCalculations()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok->next(), "* 1") || Token::simpleMatch(tok->next(), "1 *"))
|
|
|
|
{
|
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)
|
2009-12-15 20:18:32 +01:00
|
|
|
while (Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% [],);=<>+-*/]") ||
|
|
|
|
Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/]") ||
|
|
|
|
Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% <<") ||
|
|
|
|
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"
|
|
|
|
if (Token::simpleMatch(tok->next(), "/ 0"))
|
|
|
|
continue;
|
|
|
|
|
2009-12-25 14:47:15 +01:00
|
|
|
// + and - are calculated after * and /
|
2009-11-20 20:42:28 +01:00
|
|
|
if (Token::Match(tok->next(), "[+-/]"))
|
2009-08-28 12:13:46 +02:00
|
|
|
{
|
|
|
|
if (tok->previous()->str() == "*")
|
|
|
|
continue;
|
2009-12-25 14:47:15 +01:00
|
|
|
if (Token::Match(tok->tokAt(3), "[*/]"))
|
2009-08-28 12:13:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-25 14:47:15 +01:00
|
|
|
if (Token::Match(tok->previous(), "- %num% - %num%"))
|
|
|
|
tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), '+'));
|
|
|
|
else if (Token::Match(tok->previous(), "- %num% + %num%"))
|
|
|
|
tok->str(MathLib::calculate(tok->tokAt(2)->str(), tok->str(), '-'));
|
|
|
|
else
|
|
|
|
tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), *(tok->strAt(1))));
|
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
|
2009-08-02 21:11:17 +02:00
|
|
|
if (Token::Match(tok->next(), "[+-*/]"))
|
|
|
|
{
|
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..
|
|
|
|
if (!tok->isName() && Token::Match(tok->next(), "( %num% )"))
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
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
|
|
|
|
|
|
|
if (Token::simpleMatch(tok->previous(), "( 0 ||") ||
|
|
|
|
Token::simpleMatch(tok, "|| 0 )") ||
|
|
|
|
Token::simpleMatch(tok->previous(), "( 1 &&") ||
|
|
|
|
Token::simpleMatch(tok, "&& 1 )"))
|
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(tok, "%num% ==|!=|<=|>=|<|> %num%") &&
|
|
|
|
MathLib::isInt(tok->str()) &&
|
|
|
|
MathLib::isInt(tok->tokAt(2)->str()))
|
|
|
|
{
|
|
|
|
const std::string prev(tok->previous() ? tok->strAt(-1) : "");
|
|
|
|
const std::string after(tok->tokAt(3) ? tok->strAt(3) : "");
|
|
|
|
if ((prev == "(" || prev == "&&" || prev == "||") && (after == ")" || after == "&&" || after == "||"))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (cmp == "==")
|
|
|
|
result = (op1 == op2) ? "1" : "0";
|
|
|
|
else if (cmp == "!=")
|
|
|
|
result = (op1 != op2) ? "1" : "0";
|
|
|
|
else if (cmp == "<=")
|
|
|
|
result = (op1 <= op2) ? "1" : "0";
|
|
|
|
else if (cmp == ">=")
|
|
|
|
result = (op1 >= op2) ? "1" : "0";
|
|
|
|
else if (cmp == "<")
|
|
|
|
result = (op1 < op2) ? "1" : "0";
|
|
|
|
else if (cmp == ">")
|
|
|
|
result = (op1 > op2) ? "1" : "0";
|
|
|
|
|
|
|
|
tok->str(result);
|
|
|
|
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;
|
2009-08-27 18:40:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
2009-08-26 18:46:56 +02:00
|
|
|
{
|
|
|
|
if (beginfunction == 0 && indentlevel == 0 && tok->link())
|
|
|
|
tok = tok->link();
|
|
|
|
else
|
|
|
|
++indentlevel;
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel == 0)
|
|
|
|
break; // break out - it seems the code is wrong
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel == 0)
|
|
|
|
{
|
|
|
|
gotos.clear();
|
|
|
|
beginfunction = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (indentlevel == 0 && Token::Match(tok, ") const| {"))
|
|
|
|
{
|
|
|
|
gotos.clear();
|
|
|
|
beginfunction = tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "goto %var% ;"))
|
|
|
|
gotos.push_back(tok);
|
|
|
|
|
2009-08-26 18:46:56 +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;
|
2009-09-30 13:30:53 +02:00
|
|
|
int lev = 0;
|
2009-08-24 23:10:12 +02:00
|
|
|
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "}")
|
|
|
|
{
|
2009-09-30 13:30:53 +02:00
|
|
|
--lev;
|
|
|
|
if (lev < 0)
|
|
|
|
{
|
|
|
|
end = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
++lev;
|
2009-08-24 23:10:12 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 13:30:53 +02:00
|
|
|
if (Token::Match(tok2, "%var% :"))
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!end)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const std::string name(tok->str());
|
|
|
|
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
// This label is at the end of the function.. replace all matching goto statements..
|
|
|
|
for (std::list<Token *>::iterator it = gotos.begin(); it != gotos.end(); ++it)
|
|
|
|
{
|
|
|
|
Token *token = *it;
|
|
|
|
if (token->next()->str() == name)
|
|
|
|
{
|
|
|
|
// Delete the "goto name;"
|
|
|
|
token = token->previous();
|
|
|
|
token->deleteNext();
|
|
|
|
token->deleteNext();
|
|
|
|
token->deleteNext();
|
|
|
|
|
|
|
|
// Insert the statements..
|
|
|
|
bool ret = false;
|
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;
|
2009-08-24 23:10:12 +02:00
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "}")
|
2009-09-30 13:30:53 +02:00
|
|
|
{
|
|
|
|
--lev;
|
|
|
|
if (lev < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
++lev;
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "return")
|
2009-08-24 23:10:12 +02:00
|
|
|
ret = true;
|
|
|
|
token->insertToken(tok2->str().c_str());
|
|
|
|
token = token->next();
|
2009-12-02 19:47:35 +01:00
|
|
|
token->linenr(tok2->linenr());
|
2009-08-28 22:31:11 +02:00
|
|
|
if (token->str() == "(")
|
|
|
|
{
|
|
|
|
links.push_back(token);
|
|
|
|
}
|
|
|
|
else if (token->str() == ")")
|
|
|
|
{
|
|
|
|
if (links.size() == 0)
|
|
|
|
{
|
|
|
|
// This should never happen at this point
|
|
|
|
syntaxError(token, ')');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::createMutualLinks(links.back(), token);
|
|
|
|
links.pop_back();
|
|
|
|
}
|
2009-10-01 09:25:32 +02:00
|
|
|
else if (token->str() == "{")
|
|
|
|
{
|
|
|
|
links2.push_back(token);
|
|
|
|
}
|
|
|
|
else if (token->str() == "}")
|
|
|
|
{
|
|
|
|
if (links2.size() == 0)
|
|
|
|
{
|
|
|
|
// This should never happen at this point
|
|
|
|
syntaxError(token, '}');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::createMutualLinks(links2.back(), token);
|
|
|
|
links2.pop_back();
|
|
|
|
}
|
|
|
|
else if (token->str() == "[")
|
|
|
|
{
|
|
|
|
links3.push_back(token);
|
|
|
|
}
|
|
|
|
else if (token->str() == "]")
|
|
|
|
{
|
|
|
|
if (links3.size() == 0)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
2009-08-24 23:10:12 +02:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (! Token::Match(tok, "[;{}] strcat ( strcat ("))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find inner strcat call
|
|
|
|
Token *tok2 = tok->tokAt(3);
|
|
|
|
while (Token::simpleMatch(tok2, "strcat ( strcat"))
|
|
|
|
{
|
|
|
|
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-01-04 17:22:06 +01:00
|
|
|
void Tokenizer::simplifyEnum()
|
|
|
|
{
|
|
|
|
std::string className;
|
|
|
|
int classLevel = 0;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "class|namespace %any%"))
|
|
|
|
{
|
|
|
|
className = tok->next()->str();
|
|
|
|
classLevel = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
--classLevel;
|
|
|
|
if (classLevel < 0)
|
|
|
|
className = "";
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->str() == "{")
|
|
|
|
{
|
|
|
|
++classLevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok, "enum %type% {"))
|
|
|
|
{
|
|
|
|
Token * tok1;
|
|
|
|
Token * end;
|
|
|
|
|
|
|
|
if (tok->tokAt(1)->str() == "{")
|
|
|
|
tok1 = tok->tokAt(2);
|
|
|
|
else
|
|
|
|
tok1 = tok->tokAt(3);
|
|
|
|
|
|
|
|
end = tok1->tokAt(-1)->link();
|
|
|
|
|
|
|
|
long last_value = -1;
|
|
|
|
|
|
|
|
for (; tok1 && tok1 != end; tok1 = tok1->next())
|
|
|
|
{
|
|
|
|
Token * enumName = 0;
|
|
|
|
Token * enumValue = 0;
|
|
|
|
|
|
|
|
if (Token::Match(tok1, "%type% ,|}"))
|
|
|
|
{
|
|
|
|
enumName = tok1;
|
|
|
|
last_value++;
|
|
|
|
tok1->insertToken("=");
|
|
|
|
tok1 = tok1->next();
|
|
|
|
tok1->insertToken(MathLib::toString<long>(last_value).c_str());
|
|
|
|
enumValue = tok1->next();
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok1, "%type% = %num% ,|}"))
|
|
|
|
{
|
|
|
|
enumName = tok1;
|
2010-01-09 10:24:35 +01:00
|
|
|
last_value = std::atoi(tok1->strAt(2));
|
2010-01-04 17:22:06 +01:00
|
|
|
enumValue = tok1->tokAt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enumName && enumValue)
|
|
|
|
{
|
|
|
|
const std::string pattern(className.empty() ? "" : (className + " :: " + enumName->str()).c_str());
|
|
|
|
int level = 0;
|
|
|
|
bool inScope = true;
|
|
|
|
|
|
|
|
bool exitThisScope = false;
|
|
|
|
int exitScope = 0;
|
|
|
|
bool simplifyEnum = false;
|
|
|
|
for (Token *tok2 = end->next(); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
--level;
|
|
|
|
if (level < 0)
|
|
|
|
inScope = false;
|
|
|
|
|
|
|
|
if (exitThisScope)
|
|
|
|
{
|
|
|
|
if (level < exitScope)
|
|
|
|
exitThisScope = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "{")
|
|
|
|
++level;
|
|
|
|
else if (!pattern.empty() && Token::Match(tok2, pattern.c_str()))
|
|
|
|
{
|
|
|
|
simplifyEnum = true;
|
|
|
|
}
|
|
|
|
else if (inScope && !exitThisScope && tok2->str() == enumName->str())
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok2->previous(), "::"))
|
|
|
|
{
|
|
|
|
// Don't replace this enum if it's preceded by "::"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
simplifyEnum = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (simplifyEnum)
|
|
|
|
{
|
|
|
|
tok2->str(enumValue->strAt(0));
|
|
|
|
|
|
|
|
simplifyEnum = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() != "std")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::Match(tok->previous(), "[(,{};] std :: %var% (") &&
|
|
|
|
f.find(tok->strAt(2)) != f.end())
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < _functionList.size(); ++i)
|
|
|
|
{
|
|
|
|
if (_functionList[i]->str() == funcname)
|
|
|
|
{
|
|
|
|
return _functionList[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Tokenizer::fillFunctionList()
|
|
|
|
{
|
|
|
|
_functionList.clear();
|
|
|
|
|
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
{
|
2010-01-16 13:56:57 +01:00
|
|
|
tok = tok->link();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(tok, "%var% ("))
|
|
|
|
{
|
|
|
|
// Check if this is the first token of a function implementation..
|
|
|
|
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
{
|
|
|
|
tok = tok2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2, ") const| {"))
|
|
|
|
{
|
|
|
|
_functionList.push_back(tok);
|
|
|
|
tok = tok2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok = tok2;
|
2010-01-16 13:56:57 +01: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 */
|
2009-01-24 18:15:38 +01:00
|
|
|
for (unsigned int func1 = 0; func1 < _functionList.size();)
|
|
|
|
{
|
|
|
|
bool hasDuplicates = false;
|
|
|
|
for (unsigned int func2 = func1 + 1; func2 < _functionList.size();)
|
|
|
|
{
|
|
|
|
if (_functionList[func1]->str() == _functionList[func2]->str())
|
|
|
|
{
|
|
|
|
hasDuplicates = true;
|
|
|
|
_functionList.erase(_functionList.begin() + func2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++func2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! hasDuplicates)
|
|
|
|
{
|
|
|
|
++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)
|
|
|
|
{
|
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
Token *next = tok->next();
|
|
|
|
delete tok;
|
|
|
|
tok = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const char *Tokenizer::getParameterName(const Token *ftok, int par)
|
|
|
|
{
|
|
|
|
int _par = 1;
|
|
|
|
for (; ftok; ftok = ftok->next())
|
|
|
|
{
|
|
|
|
if (ftok->str() == ",")
|
|
|
|
++_par;
|
|
|
|
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-01-02 17:29:55 +01:00
|
|
|
const Token * Tokenizer::findClassFunction(const Token *tok, const char classname[], const char funcname[], int &indentlevel, bool isStruct)
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
|
|
|
if (indentlevel < 0 || tok == NULL)
|
|
|
|
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 + " (");
|
|
|
|
const std::string externalPattern(std::string(classname) + " :: " + funcname + " (");
|
2009-03-13 22:28:44 +01:00
|
|
|
|
2009-07-13 15:51:35 +02:00
|
|
|
for (; tok; tok = tok->next())
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
2010-01-02 08:33:54 +01:00
|
|
|
if (indentlevel == 0 && Token::Match(tok, classPattern.c_str()))
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
|
|
|
while (tok && tok->str() != "{")
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok)
|
|
|
|
tok = tok->next();
|
|
|
|
if (! tok)
|
|
|
|
break;
|
|
|
|
indentlevel = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->str() == "{")
|
|
|
|
{
|
|
|
|
// If indentlevel==0 don't go to indentlevel 1. Skip the block.
|
|
|
|
if (indentlevel > 0)
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2010-01-02 08:33:54 +01:00
|
|
|
// skip the block
|
|
|
|
tok = tok->link();
|
2009-03-13 22:28:44 +01:00
|
|
|
if (tok == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (indentlevel == 1)
|
|
|
|
{
|
|
|
|
// Member function implemented in the class declaration?
|
2010-01-02 08:33:54 +01: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;
|
2009-03-13 22:28:44 +01:00
|
|
|
while (tok2 && tok2->str() != "{" && tok2->str() != ";")
|
|
|
|
tok2 = tok2->next();
|
|
|
|
if (tok2 && tok2->str() == "{")
|
2010-01-02 08:33:54 +01:00
|
|
|
return tok;
|
2009-03-13 22:28:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-02 08:33:54 +01:00
|
|
|
else if (indentlevel == 0 && Token::Match(tok, externalPattern.c_str()))
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2009-07-13 19:11:31 +02:00
|
|
|
if (_settings && _settings->_debug)
|
2009-05-12 23:01:53 +02:00
|
|
|
{
|
|
|
|
_tokens->printOut();
|
|
|
|
}
|
|
|
|
|
2009-10-17 23:11:48 +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 << ")";
|
|
|
|
if (_settings && _settings->_debug)
|
|
|
|
{
|
|
|
|
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;
|
2009-10-17 23:11:48 +02:00
|
|
|
if (tok)
|
|
|
|
{
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok->linenr();
|
|
|
|
loc.file = file(tok);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
"error",
|
|
|
|
std::string("Invalid number of character (") + c + "). Can't process file.",
|
|
|
|
"syntaxError");
|
|
|
|
|
|
|
|
if (_errorLogger)
|
|
|
|
_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
|
|
|
|
{
|
|
|
|
if (!_errorLogger && tok)
|
|
|
|
{
|
|
|
|
std::ostringstream err;
|
|
|
|
err << "### Unlogged error at Tokenizer::cppcheckError";
|
|
|
|
if (_settings && _settings->_debug)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(err.str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << err.str() << std::endl;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
if (tok)
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
|
|
|
|
if (_errorLogger)
|
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-13 09:35:02 +02:00
|
|
|
void Tokenizer::simplifyMathFunctions()
|
2009-09-12 22:54:47 +02:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "atol ( %str% )"))
|
|
|
|
{
|
|
|
|
if (!MathLib::isInt(tok->tokAt(2)->strValue()))
|
|
|
|
{
|
|
|
|
// Ignore strings which we can't convert
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->previous() &&
|
|
|
|
Token::simpleMatch(tok->previous()->previous(), "std ::"))
|
|
|
|
{
|
|
|
|
// 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
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-08-29 11:48:44 +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();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
|
|
|
|
continue;
|
2009-07-28 18:40:15 +02:00
|
|
|
}
|
|
|
|
|
2009-09-12 08:12:00 +02:00
|
|
|
// Skip unhandled template specifiers..
|
|
|
|
if (Token::Match(tok, "%var% <"))
|
|
|
|
{
|
|
|
|
// Todo.. use the link instead.
|
|
|
|
unsigned int parlevel = 0;
|
|
|
|
unsigned int comparelevel = 0;
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "<")
|
|
|
|
++comparelevel;
|
|
|
|
else if (tok2->str() == ">")
|
|
|
|
{
|
|
|
|
if (comparelevel <= 1)
|
|
|
|
{
|
|
|
|
tok = tok2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++comparelevel;
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel == 0)
|
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok2, "[;{}]"))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 14:38:41 +02:00
|
|
|
// If token after the comma is a constant number, simplification is not required.
|
|
|
|
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.
|
2009-06-18 23:00:16 +02:00
|
|
|
if (Token::Match(tok->next(), "delete"))
|
|
|
|
{
|
|
|
|
// Handle "delete a, delete b;"
|
|
|
|
tok->str(";");
|
|
|
|
}
|
2009-07-05 20:29:09 +02:00
|
|
|
|
|
|
|
if (tok->previous() && tok->previous()->previous())
|
2009-06-18 23:00:16 +02:00
|
|
|
{
|
2009-07-05 20:29:09 +02:00
|
|
|
if (Token::Match(tok->previous()->previous(), "delete") &&
|
|
|
|
tok->next()->varId() != 0)
|
|
|
|
{
|
|
|
|
// Handle "delete a, b;"
|
|
|
|
tok->str(";");
|
|
|
|
tok->insertToken("delete");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous())
|
|
|
|
{
|
2009-08-05 22:02:40 +02:00
|
|
|
if (tok2->str() == "=")
|
2009-07-05 20:29:09 +02:00
|
|
|
{
|
|
|
|
// Handle "a = 0, b = 0;"
|
|
|
|
tok->str(";");
|
|
|
|
break;
|
|
|
|
}
|
2009-07-27 21:13:08 +02:00
|
|
|
else if (Token::Match(tok2, "delete %var%") ||
|
|
|
|
Token::Match(tok2, "delete [ ] %var%"))
|
|
|
|
{
|
|
|
|
// Handle "delete a, a = 0;"
|
|
|
|
tok->str(";");
|
|
|
|
break;
|
|
|
|
}
|
2009-07-05 20:29:09 +02:00
|
|
|
else if (Token::Match(tok2, "[;,{}()]"))
|
|
|
|
{
|
|
|
|
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
|
2009-07-26 22:13:08 +02:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->previous())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2, "[;{}]"))
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
break;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "return" && Token::Match(tok2->previous(), "[;{}]"))
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
inReturn = true;
|
|
|
|
startFrom = tok2->next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find token where return ends and also count commas
|
2009-07-26 22:13:08 +02:00
|
|
|
if (inReturn)
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
size_t commaCounter = 0;
|
|
|
|
size_t indentlevel = 0;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
for (Token *tok2 = startFrom; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
endAt = tok2;
|
|
|
|
break;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "(")
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
++indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
--indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "," && indentlevel == 0)
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
++commaCounter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
if (commaCounter)
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
indentlevel = 0;
|
|
|
|
|
|
|
|
// change tokens:
|
|
|
|
// "; return a ( ) , b ( ) , c ;"
|
|
|
|
// to
|
|
|
|
// "; return a ( ) ; b ( ) ; c ;"
|
2009-07-26 22:13:08 +02:00
|
|
|
for (Token *tok2 = startFrom; tok2 != endAt; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
++indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
--indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "," && indentlevel == 0)
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
tok2->str(";");
|
|
|
|
--commaCounter;
|
2009-07-26 22:13:08 +02:00
|
|
|
if (commaCounter == 0)
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
tok2->insertToken("return");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete old "return"
|
|
|
|
startFrom->previous()->deleteThis();
|
|
|
|
|
|
|
|
tok = endAt;
|
2009-12-06 09:02:13 +01:00
|
|
|
if (!tok)
|
|
|
|
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
|
|
|
|
{
|
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
tok = tok->link();
|
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
break;
|
|
|
|
|
|
|
|
else if (Token::Match(tok, ") throw ("))
|
|
|
|
{
|
|
|
|
while (tok->next() && !Token::Match(tok->next(), "[;{]"))
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "class %type%"))
|
|
|
|
{
|
|
|
|
while (tok && !Token::Match(tok, "[;{]"))
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok && tok->str() == "{")
|
|
|
|
{
|
|
|
|
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;
|
2009-09-13 15:35:37 +02:00
|
|
|
for (const Token *tok = tokens(); tok; tok = tok->next())
|
|
|
|
{
|
2010-01-06 00:32:05 +01:00
|
|
|
lastTok = tok;
|
2009-09-29 22:27:10 +02:00
|
|
|
if (Token::Match(tok, "[{([]"))
|
2009-09-13 15:35:37 +02:00
|
|
|
{
|
2009-11-10 22:10:56 +01:00
|
|
|
if (tok->link() == 0)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-29 22:27:10 +02:00
|
|
|
else if (Token::Match(tok, "[})]]"))
|
2009-09-13 15:35:37 +02:00
|
|
|
{
|
2009-11-10 22:10:56 +01:00
|
|
|
if (tok->link() == 0)
|
|
|
|
{
|
2009-11-28 22:08:43 +01:00
|
|
|
cppcheckError(tok);
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (linktok.empty() == true)
|
|
|
|
{
|
2009-11-28 22:08:43 +01:00
|
|
|
cppcheckError(tok);
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->link() != linktok.top())
|
|
|
|
{
|
2009-11-28 22:08:43 +01:00
|
|
|
cppcheckError(tok);
|
2009-11-10 22:10:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok != tok->link()->link())
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2009-11-10 22:10:56 +01:00
|
|
|
if (tok->link() != 0)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2009-11-10 22:10:56 +01:00
|
|
|
if (!linktok.empty())
|
|
|
|
{
|
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-01-06 00:32:05 +01:00
|
|
|
if (lastTok != _tokensBack)
|
|
|
|
{
|
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;
|
|
|
|
for (std::string::size_type i = 0; i + 2 < str.size(); i++)
|
|
|
|
{
|
|
|
|
if (!escaped)
|
|
|
|
{
|
|
|
|
if (str[i] == '\\')
|
|
|
|
escaped = true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str[i] == 'x')
|
|
|
|
{
|
|
|
|
// Hex value
|
|
|
|
if (str[i+1] == '0' && str[i+2] == '0')
|
|
|
|
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;
|
|
|
|
while (n < 2 && std::isxdigit(str[i+1+n]))
|
|
|
|
++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
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (MathLib::isOctalDigit(str[i]))
|
|
|
|
{
|
|
|
|
if (MathLib::isOctalDigit(str[i+1]) &&
|
|
|
|
MathLib::isOctalDigit(str[i+2]))
|
|
|
|
{
|
|
|
|
if (str[i+1] == '0' && str[i+2] == '0')
|
|
|
|
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
|
|
|
|
2009-11-04 23:58:15 +01:00
|
|
|
void Tokenizer::simplifyComparisonOrder()
|
|
|
|
{
|
|
|
|
// Use "<" comparison instead of ">"
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "[;(] %any% >|>= %any% [);]"))
|
|
|
|
{
|
2009-11-12 18:53:26 +01:00
|
|
|
if (!tok->next()->isName() && !tok->next()->isNumber())
|
|
|
|
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);
|
|
|
|
if (tok->tokAt(2)->str() == ">")
|
|
|
|
tok->tokAt(2)->str("<");
|
|
|
|
else
|
|
|
|
tok->tokAt(2)->str("<=");
|
|
|
|
}
|
2009-11-30 16:45:19 +01:00
|
|
|
else if (Token::Match(tok, "( %num% ==|!= %var% )"))
|
|
|
|
{
|
|
|
|
if (!tok->next()->isName() && !tok->next()->isNumber())
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "[;{}(,] %type% const"))
|
|
|
|
{
|
|
|
|
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, ' ');
|
|
|
|
}
|
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[])
|
|
|
|
{
|
|
|
|
for (const Token *tok = tok1; tok && tok != tok2; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, pattern))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-28 08:37:34 +01:00
|
|
|
void Tokenizer::simplifyWhile0()
|
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (!Token::simpleMatch(tok, "while ( 0 )"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok->previous(), "}"))
|
|
|
|
{
|
|
|
|
// find "do"
|
|
|
|
Token *tok2 = tok->previous()->link();
|
|
|
|
tok2 = tok2 ? tok2->previous() : 0;
|
2009-12-29 12:15:39 +01: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) { .. }"
|
|
|
|
if (Token::simpleMatch(tok->tokAt(4), "{"))
|
|
|
|
{
|
|
|
|
const Token *end = tok->tokAt(4)->link();
|
|
|
|
if (!findmatch(tok, end, "continue|break"))
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok, end ? end->next() : 0);
|
|
|
|
tok->deleteThis(); // delete "while"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-28 08:37:34 +01:00
|
|
|
}
|
|
|
|
}
|