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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#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-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
#include <locale>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <list>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
2009-08-27 18:33:42 +02:00
|
|
|
#include <stack>
|
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());
|
|
|
|
_tokensBack = _tokensBack->next();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_tokens = new Token;
|
|
|
|
_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-07-05 22:16:43 +02:00
|
|
|
int Tokenizer::sizeOfType(const char type[]) const
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
if (!type)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
std::map<std::string, unsigned int>::const_iterator it = _typeSize.find(type);
|
|
|
|
if (it == _typeSize.end())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// InsertTokens - Copy and insert tokens
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-07-05 22:16:43 +02:00
|
|
|
void Tokenizer::insertTokens(Token *dest, 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
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
|
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-03-17 20:50:06 +01:00
|
|
|
// Combine "- %num%" ..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "[(+-*/=,] - %num%") && tok->strAt(2)[0] != '-')
|
|
|
|
{
|
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
|
|
|
|
|
|
|
if (Token::Match(tok, "return - %num%") && tok->strAt(2)[0] != '-')
|
|
|
|
{
|
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:" },
|
|
|
|
{ "public", ":", "public:" }
|
|
|
|
};
|
|
|
|
|
|
|
|
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-05-03 21:32:22 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-05-03 21:32:22 +02:00
|
|
|
if (tok->str() != "typedef")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::Match(tok->next(), "%type% %type% ;"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
const char *type1 = tok->strAt(1);
|
|
|
|
const char *type2 = tok->strAt(2);
|
2009-05-03 21:32:22 +02:00
|
|
|
tok = const_cast<Token*>(tok->tokAt(3));
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == type2)
|
|
|
|
tok2->str(type1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-03 21:32:22 +02:00
|
|
|
else if (Token::Match(tok->next(), "%type% %type% %type% ;"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
const char *type1 = tok->strAt(1);
|
|
|
|
const char *type2 = tok->strAt(2);
|
|
|
|
const char *type3 = tok->strAt(3);
|
2009-05-03 21:32:22 +02:00
|
|
|
tok = const_cast<Token*>(tok->tokAt(4));
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == type3)
|
|
|
|
{
|
|
|
|
tok2->str(type1);
|
|
|
|
tok2->insertToken(type2);
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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-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-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-06-10 19:36:00 +02:00
|
|
|
// change array to pointer..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "%type% %var% [ ] [,;=]"))
|
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok->next(), tok->tokAt(4));
|
2009-06-10 19:36:00 +02:00
|
|
|
tok->insertToken("*");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
return true;
|
2009-05-03 21:23:47 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void Tokenizer::simplifyTemplates()
|
|
|
|
{
|
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())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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 <"))
|
|
|
|
{
|
|
|
|
unsigned int indentlevel = 0;
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
{
|
|
|
|
++indentlevel;
|
|
|
|
}
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
else if (indentlevel == 0 && tok->str() == ";")
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
}
|
2009-06-14 14:57:47 +02:00
|
|
|
else if (Token::Match(tok->previous(), "[{};=] %var% <"))
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
2009-05-05 20:16:57 +02:00
|
|
|
used.push_back(tok);
|
2009-05-06 21:03:11 +02:00
|
|
|
}
|
2009-05-05 20:16:57 +02:00
|
|
|
}
|
|
|
|
if (used.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// expand templates
|
|
|
|
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
|
|
|
|
{
|
|
|
|
Token *tok = *iter1;
|
2009-03-11 18:50:24 +01:00
|
|
|
|
2009-03-14 21:26:32 +01:00
|
|
|
std::vector<std::string> type;
|
|
|
|
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "%var% ,|>"))
|
|
|
|
type.push_back(tok->str());
|
|
|
|
}
|
|
|
|
|
2009-06-14 11:28:25 +02:00
|
|
|
// bail out if the end of the file was reached
|
2009-03-14 21:26:32 +01:00
|
|
|
if (!tok)
|
|
|
|
break;
|
2009-03-12 22:17:42 +01:00
|
|
|
|
2009-06-14 11:28:25 +02:00
|
|
|
// 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-03-14 21:26:32 +01:00
|
|
|
// name of template function/class..
|
2009-06-14 11:28:25 +02:00
|
|
|
const std::string name(tok->strAt(pos > 0 ? pos : 2));
|
2009-03-14 21:26:32 +01:00
|
|
|
|
2009-06-14 11:28:25 +02:00
|
|
|
const bool isfunc(pos > 0);
|
2009-03-11 18:50:24 +01:00
|
|
|
|
|
|
|
// locate template usage..
|
2009-05-05 20:16:57 +02:00
|
|
|
|
2009-03-14 21:26:32 +01:00
|
|
|
std::string s(name + " <");
|
|
|
|
for (unsigned int i = 0; i < type.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
s += ",";
|
|
|
|
s += " %any% ";
|
|
|
|
}
|
2009-03-15 20:03:29 +01:00
|
|
|
const std::string pattern(s + "> ");
|
2009-05-05 20:16:57 +02:00
|
|
|
|
|
|
|
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
|
2009-03-11 18:50:24 +01:00
|
|
|
{
|
2009-05-05 20:16:57 +02:00
|
|
|
Token *tok2 = *iter2;
|
|
|
|
|
2009-04-14 22:21:52 +02:00
|
|
|
if (tok2->str() != name)
|
|
|
|
continue;
|
|
|
|
|
2009-03-15 20:03:29 +01:00
|
|
|
if (!Token::Match(tok2, (pattern + (isfunc ? "(" : "%var%")).c_str()))
|
2009-03-11 18:50:24 +01:00
|
|
|
continue;
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-03-11 18:50:24 +01:00
|
|
|
// New type..
|
2009-03-14 21:26:32 +01:00
|
|
|
std::vector<std::string> types2;
|
|
|
|
s = "";
|
2009-03-15 00:39:45 +01:00
|
|
|
for (const Token *tok3 = tok2->tokAt(2); tok3->str() != ">"; tok3 = tok3->next())
|
2009-03-14 21:26:32 +01:00
|
|
|
{
|
|
|
|
if (tok3->str() != ",")
|
|
|
|
types2.push_back(tok3->str());
|
|
|
|
s += tok3->str();
|
|
|
|
}
|
|
|
|
const std::string type2(s);
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-03-12 22:17:42 +01:00
|
|
|
// New classname/funcname..
|
|
|
|
const std::string name2(name + "<" + type2 + ">");
|
2009-03-11 18:50:24 +01:00
|
|
|
|
2009-03-15 20:03:29 +01:00
|
|
|
// Copy template..
|
2009-05-09 08:02:59 +02:00
|
|
|
int _indentlevel = 0;
|
2009-03-15 20:03:29 +01:00
|
|
|
for (const Token *tok3 = _tokens; tok3; tok3 = tok3->next())
|
2009-03-11 19:14:45 +01:00
|
|
|
{
|
2009-05-09 08:02:59 +02:00
|
|
|
if (tok3->str() == "{")
|
|
|
|
++_indentlevel;
|
|
|
|
else if (tok3->str() == "}")
|
|
|
|
--_indentlevel;
|
|
|
|
|
2009-03-15 20:03:29 +01:00
|
|
|
// Start of template..
|
|
|
|
if (tok3 == tok)
|
2009-03-14 21:26:32 +01:00
|
|
|
{
|
2009-03-15 20:03:29 +01:00
|
|
|
tok3 = tok3->next();
|
2009-03-14 21:26:32 +01:00
|
|
|
}
|
2009-03-11 18:50:24 +01:00
|
|
|
|
2009-03-15 20:03:29 +01:00
|
|
|
// member function implemented outside class definition
|
2009-05-09 08:02:59 +02:00
|
|
|
else if (_indentlevel == 0 && Token::Match(tok3, (pattern + " :: %var% (").c_str()))
|
2009-03-15 20:03:29 +01:00
|
|
|
{
|
|
|
|
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
while (tok3->str() != "::")
|
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-03-15 20:03:29 +01:00
|
|
|
// not part of template.. go on to next token
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int indentlevel = 0;
|
2009-08-27 18:33:42 +02:00
|
|
|
std::stack<Token *> braces; // holds "{" tokens
|
|
|
|
std::stack<Token *> brackets; // holds "(" tokens
|
|
|
|
|
2009-03-15 20:03:29 +01:00
|
|
|
for (; tok3; tok3 = tok3->next())
|
2009-03-11 18:50:24 +01:00
|
|
|
{
|
2009-05-09 08:02:59 +02:00
|
|
|
if (tok3->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else if (tok3->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
|
|
|
{
|
|
|
|
// 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-08-27 18:33:42 +02:00
|
|
|
Token::createMutualLinks(braces.top(), _tokensBack);
|
|
|
|
braces.pop();
|
2009-05-09 08:02:59 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:03:29 +01:00
|
|
|
{
|
2009-03-17 18:55:28 +01:00
|
|
|
// 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())
|
|
|
|
addtoken(types2[itype].c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
|
|
|
|
// replace name..
|
|
|
|
else if (tok3->str() == name)
|
|
|
|
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
|
|
|
|
// copy
|
|
|
|
else
|
2009-08-27 18:33:42 +02:00
|
|
|
{
|
2009-03-17 18:55:28 +01:00
|
|
|
addtoken(tok3->str().c_str(), tok3->linenr(), tok3->fileIndex());
|
2009-08-27 18:33:42 +02:00
|
|
|
|
|
|
|
// link() newly tokens manually
|
|
|
|
if (tok3->str() == "{")
|
|
|
|
{
|
|
|
|
braces.push(_tokensBack);
|
|
|
|
}
|
|
|
|
else if (tok3->str() == "}")
|
|
|
|
{
|
|
|
|
Token::createMutualLinks(braces.top(), _tokensBack);
|
|
|
|
braces.pop();
|
|
|
|
}
|
|
|
|
else if (tok3->str() == "(")
|
|
|
|
{
|
|
|
|
brackets.push(_tokensBack);
|
|
|
|
}
|
|
|
|
else if (tok3->str() == ")")
|
|
|
|
{
|
|
|
|
Token::createMutualLinks(brackets.top(), _tokensBack);
|
|
|
|
brackets.pop();
|
|
|
|
}
|
|
|
|
}
|
2009-03-15 20:03:29 +01:00
|
|
|
}
|
2009-03-11 18:50:24 +01:00
|
|
|
}
|
|
|
|
}
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-03-11 18:50:24 +01:00
|
|
|
// Replace all these template usages..
|
2009-03-14 21:26:32 +01:00
|
|
|
s = name + " < " + type2 + " >";
|
|
|
|
for (std::string::size_type pos = s.find(","); pos != std::string::npos; pos = s.find(",", pos + 2))
|
|
|
|
{
|
2009-03-15 00:39:45 +01:00
|
|
|
s.insert(pos + 1, " ");
|
2009-03-14 21:26:32 +01:00
|
|
|
s.insert(pos, " ");
|
|
|
|
}
|
2009-03-11 18:50:24 +01:00
|
|
|
for (Token *tok4 = tok2; tok4; tok4 = tok4->next())
|
|
|
|
{
|
2009-03-14 21:26:32 +01:00
|
|
|
if (Token::simpleMatch(tok4, s.c_str()))
|
2009-03-11 18:50:24 +01:00
|
|
|
{
|
2009-08-02 10:54:46 +02:00
|
|
|
tok4->str(name2);
|
2009-03-14 21:26:32 +01:00
|
|
|
while (tok4->next()->str() != ">")
|
|
|
|
tok4->deleteNext();
|
2009-03-11 18:50:24 +01:00
|
|
|
tok4->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
while ((tok1 = Token::findmatch(tok1, pattern_class)))
|
|
|
|
{
|
|
|
|
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-08-11 20:58:49 +02:00
|
|
|
if (Token::Match(tok, "else|return|typedef|delete|class"))
|
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% <"))
|
|
|
|
{
|
|
|
|
Token *tok2 = tok->tokAt(2);
|
2009-07-28 19:54:13 +02:00
|
|
|
|
2009-07-30 19:40:41 +02:00
|
|
|
while (Token::Match(tok2, "%var% ::"))
|
2009-07-28 19:54:13 +02:00
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
|
2009-08-01 18:17:35 +02:00
|
|
|
while (tok2 && (tok2->isName() || tok2->isNumber() || tok2->str() == "*" || tok2->str() == ","))
|
2009-04-25 16:55:00 +02:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
if (Token::Match(tok2, "> %var%"))
|
|
|
|
tok = tok2;
|
2009-07-18 12:37:51 +02:00
|
|
|
else if (Token::Match(tok2, "> ::|*|& %var%"))
|
2009-04-25 16:55:00 +02:00
|
|
|
tok = tok2->next();
|
|
|
|
else
|
|
|
|
continue; // Not code that I understand / not a variable declaration
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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-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 &&
|
|
|
|
Token::Match(tok->next(), ". %var%") &&
|
2009-05-27 22:34:08 +02:00
|
|
|
!Token::Match(tok->tokAt(3), "(") &&
|
2009-01-24 18:15:38 +01:00
|
|
|
tok->tokAt(2)->varId() == 0)
|
|
|
|
{
|
|
|
|
++_varId;
|
|
|
|
|
|
|
|
const std::string pattern(std::string(". ") + tok->strAt(2));
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->varId() == tok->varId() && Token::simpleMatch(tok2->next(), pattern.c_str()))
|
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..
|
|
|
|
while (tok2 && tok2->str() != ")")
|
|
|
|
tok2 = tok2->next();
|
|
|
|
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())
|
|
|
|
{
|
2009-03-30 23:59:33 +02:00
|
|
|
while (token && token->str() == "namespace" &&
|
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-03-13 22:25:56 +01:00
|
|
|
std::list<Token*> links;
|
2009-05-01 12:07:10 +02:00
|
|
|
std::list<Token*> links2;
|
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);
|
|
|
|
}
|
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
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-08-22 16:22:50 +02:00
|
|
|
Token::createMutualLinks(links2.back(), token);
|
2009-05-01 12:07:10 +02:00
|
|
|
links2.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.
|
|
|
|
syntaxError(_tokens, '{');
|
|
|
|
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-05-07 22:17:29 +02:00
|
|
|
syntaxError(_tokens, '(');
|
2009-03-15 00:39:45 +01:00
|
|
|
return false;
|
2009-03-13 22:25:56 +01:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:39:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tokenizer::simplifyTokenList()
|
|
|
|
{
|
2009-03-13 00:07:05 +01:00
|
|
|
simplifyNamespaces();
|
|
|
|
|
2009-08-24 23:10:12 +02:00
|
|
|
simplifyGoto();
|
|
|
|
|
2009-04-05 21:21:38 +02:00
|
|
|
// Combine wide strings
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
while (tok->str() == "L" && tok->next() && tok->next()->str()[0] == '"')
|
|
|
|
{
|
|
|
|
// Combine 'L "string"'
|
2009-08-02 10:54:46 +02:00
|
|
|
tok->str(tok->next()->str());
|
2009-04-05 21:21:38 +02:00
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-08 09:52:03 +01:00
|
|
|
// Combine strings
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-02-08 11:25:33 +01:00
|
|
|
while (tok->str()[0] == '"' && tok->next() && tok->next()->str()[0] == '"')
|
2009-02-08 09:52:03 +01:00
|
|
|
{
|
|
|
|
// Two strings after each other, combine them
|
2009-03-28 20:33:55 +01:00
|
|
|
tok->concatStr(tok->next()->str());
|
2009-02-08 09:52:03 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Remove unwanted keywords
|
2009-05-31 21:04:29 +02:00
|
|
|
static const char * const unwantedWords[] = { "unsigned", "unlikely", "likely" };
|
2009-01-24 18:15:38 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2009-05-27 20:49:29 +02:00
|
|
|
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();
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok2->previous(), tok2->tokAt(2));
|
2009-05-27 20:49:29 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-02-14 21:56:08 +01:00
|
|
|
// 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-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-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-06-01 11:27:04 +02:00
|
|
|
// Replace 'sizeof(var)' with 'sizeof(type)'
|
2009-02-28 09:34:02 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-06-01 12:01:45 +02:00
|
|
|
if (! Token::Match(tok, "[;{}] %type% *| %var% ;"))
|
2009-06-01 11:27:04 +02:00
|
|
|
continue;
|
2009-02-28 09:34:02 +01:00
|
|
|
|
2009-06-01 12:01:45 +02:00
|
|
|
const int type_tok = (tok->tokAt(2)->str() == "*" ? 2 : 1);
|
|
|
|
const int varname_tok = type_tok + 1;
|
|
|
|
const unsigned int varid = tok->tokAt(varname_tok)->varId();
|
2009-06-01 11:27:04 +02:00
|
|
|
|
2009-06-01 12:01:45 +02:00
|
|
|
if (varid <= 0)
|
|
|
|
continue;
|
2009-06-01 11:27:04 +02:00
|
|
|
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}")
|
2009-02-28 09:34:02 +01:00
|
|
|
{
|
2009-06-01 11:27:04 +02:00
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok2, "sizeof ( %varid% )", varid))
|
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(2);
|
2009-06-01 12:01:45 +02:00
|
|
|
tok2->str(tok->strAt(type_tok));
|
2009-02-28 09:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
// Replace 'sizeof(type)'..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() != "sizeof")
|
|
|
|
continue;
|
|
|
|
|
2009-01-27 21:47:00 +01:00
|
|
|
if (tok->strAt(1) != std::string("("))
|
|
|
|
{
|
|
|
|
// Add parenthesis around the sizeof
|
|
|
|
for (Token *tempToken = tok->next(); tempToken; tempToken = tempToken->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tempToken, "%var%"))
|
|
|
|
{
|
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-07-27 11:24:24 +02:00
|
|
|
else if (tempToken->next()->str() == "[")
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
2009-07-14 08:17:12 +02:00
|
|
|
/** @todo We need to find closing ], then check for
|
|
|
|
* dots and arrows "var[some[0]]->other" */
|
2009-01-27 21:47:00 +01:00
|
|
|
|
|
|
|
// But for now, just bail out
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(*)
|
|
|
|
if (Token::Match(tok, "sizeof ( %type% *)"))
|
|
|
|
{
|
|
|
|
tok->next()->deleteNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(tok, "sizeof ( * )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 13:44:12 +02:00
|
|
|
tok->str(MathLib::toString<long>(sizeOfType(tok->strAt(2))));
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(4));
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-06-05 04:50:06 +02:00
|
|
|
else if (Token::Match(tok, "sizeof ( %var% )") && tok->tokAt(2)->varId() > 0)
|
|
|
|
{
|
|
|
|
// don't try to replace size of variable if variable has
|
|
|
|
// similar name with type (#329)
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
else if (Token::Match(tok, "sizeof ( %type% )"))
|
|
|
|
{
|
|
|
|
const char *type = tok->strAt(2);
|
2009-07-05 22:16:43 +02:00
|
|
|
int size = sizeOfType(type);
|
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..
|
|
|
|
int sz = 100;
|
|
|
|
|
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..
|
|
|
|
const Token *decltok = Token::findmatch(_tokens, "%type% %varid% [", varid);
|
|
|
|
if (decltok)
|
|
|
|
{
|
2009-07-05 22:16:43 +02:00
|
|
|
sz = sizeOfType(decltok->strAt(0));
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace 'sizeof(var)'
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-31 21:33:44 +02:00
|
|
|
if (! Token::Match(tok, "%type% *| %var% [ %num% ] ;"))
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
2009-05-31 21:33:44 +02:00
|
|
|
const int type_tok = ((tok->next()->str() == "*") ? 1 : 0);
|
|
|
|
|
2009-07-05 22:16:43 +02:00
|
|
|
int size = sizeOfType(tok->tokAt(type_tok)->str().c_str());
|
2009-01-24 18:15:38 +01:00
|
|
|
if (size <= 0)
|
|
|
|
continue;
|
|
|
|
|
2009-05-31 21:59:38 +02:00
|
|
|
const int varname_tok = type_tok + 1;
|
2009-05-31 21:33:44 +02:00
|
|
|
const unsigned int varid = tok->tokAt(varname_tok)->varId();
|
2009-02-02 18:27:34 +01:00
|
|
|
if (varid == 0)
|
|
|
|
continue;
|
|
|
|
|
2009-05-31 21:59:38 +02:00
|
|
|
const int num_tok = varname_tok + 2;
|
2009-05-31 21:33:44 +02:00
|
|
|
int total_size = size * MathLib::toLongNumber(tok->strAt(num_tok));
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Replace 'sizeof(var)' with number
|
|
|
|
int indentlevel = 0;
|
2009-05-31 21:59:38 +02:00
|
|
|
const int next_tok = num_tok + 3;
|
|
|
|
for (Token *tok2 = tok->tokAt(next_tok); 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-02-02 18:27:34 +01:00
|
|
|
else if (Token::Match(tok2, "sizeof ( %varid% )", varid))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 13:44:12 +02:00
|
|
|
tok2->str(MathLib::toString<long>(total_size));
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok2, tok2->tokAt(4));
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-05 20:32:02 +01:00
|
|
|
simplifyCasts();
|
|
|
|
|
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-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())
|
|
|
|
{
|
|
|
|
if (tok->str() == "NULL")
|
|
|
|
tok->str("0");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-22 12:42:19 +02:00
|
|
|
simplifyDoWhileAddBraces();
|
2009-01-24 18:15:38 +01:00
|
|
|
simplifyIfAddBraces();
|
2009-01-26 23:26:50 +01:00
|
|
|
simplifyFunctionParameters();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-02-17 21:03:08 +01:00
|
|
|
elseif();
|
2009-03-24 18:23:21 +01:00
|
|
|
simplifyIfNot();
|
2009-07-25 13:11:29 +02:00
|
|
|
simplifyIfNotNull();
|
2009-03-23 18:20:56 +01:00
|
|
|
simplifyIfAssign();
|
2009-02-17 21:03:08 +01: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-03-04 07:24:03 +01:00
|
|
|
modified |= simplifyCalculations();
|
2009-06-05 23:33:13 +02:00
|
|
|
modified |= simplifyQuestionMark();
|
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 ("))
|
|
|
|
{
|
|
|
|
unsigned int parlevel = 0;
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel <= 1)
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok2, ") ;"))
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok2->deleteThis();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
_tokens->printOut();
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const Token *Tokenizer::findClosing(const Token *tok, const char *start, const char *end)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Find the closing "}"
|
|
|
|
int indentLevel = 0;
|
|
|
|
for (const Token *closing = tok->next(); closing; closing = closing->next())
|
|
|
|
{
|
|
|
|
if (closing->str() == start)
|
|
|
|
{
|
|
|
|
++indentLevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closing->str() == end)
|
|
|
|
--indentLevel;
|
|
|
|
|
|
|
|
if (indentLevel >= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Closing } is found.
|
|
|
|
return closing;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 "}"
|
|
|
|
elseTag = Tokenizer::findClosing(tok->tokAt(4), "{", "}");
|
|
|
|
if (elseTag)
|
|
|
|
elseTag = elseTag->next();
|
|
|
|
|
|
|
|
bool boolValue = false;
|
|
|
|
if (tok->tokAt(2)->str() == "true")
|
|
|
|
boolValue = true;
|
|
|
|
|
|
|
|
// Handle if with else
|
|
|
|
if (elseTag && elseTag->str() == "else")
|
|
|
|
{
|
2009-07-27 11:24:24 +02:00
|
|
|
if (elseTag->next()->str() == "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() == "(")
|
|
|
|
{
|
|
|
|
lastTagInIf = Tokenizer::findClosing(lastTagInIf, "(", ")");
|
|
|
|
lastTagInIf = lastTagInIf->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
lastTagInIf = Tokenizer::findClosing(lastTagInIf, "{", "}");
|
|
|
|
lastTagInIf = lastTagInIf->next();
|
2009-07-27 11:24:24 +02:00
|
|
|
if (lastTagInIf->str() != "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;}"
|
|
|
|
const Token *end = Tokenizer::findClosing(elseTag->tokAt(1), "{", "}");
|
|
|
|
if (!end)
|
|
|
|
{
|
|
|
|
// Possibly syntax error in code
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
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-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-08-29 21:26:56 +02:00
|
|
|
void Tokenizer::simplifyDoWhileAddBraces()
|
2009-08-22 12:42:19 +02:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = (tok ? tok->next() : NULL))
|
|
|
|
{
|
|
|
|
if (! Token::Match(tok, "do !!{"))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *tok1 = tok; // token with "do"
|
|
|
|
Token *tok2 = NULL; // token with "while"
|
|
|
|
Token *tok3 = tok;
|
|
|
|
|
|
|
|
// skip loop body
|
|
|
|
while (tok3)
|
|
|
|
{
|
|
|
|
if (tok3->str() == "while")
|
|
|
|
{
|
|
|
|
tok2 = tok3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok2)
|
|
|
|
{
|
|
|
|
// insert "{" after "do"
|
|
|
|
tok1->insertToken("{");
|
|
|
|
|
|
|
|
// insert "}" before "while"
|
|
|
|
tok2->previous()->insertToken("}");
|
|
|
|
|
2009-08-22 16:22:50 +02:00
|
|
|
Token::createMutualLinks(tok1->next(), tok2->previous());
|
2009-08-22 12:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-14 12:06:38 +02:00
|
|
|
bool Tokenizer::simplifyConditionOperator()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
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));
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(8));
|
|
|
|
|
|
|
|
std::string str("if ( " + condition + " ) " + var + " = " + value1 + " ; else " + var + " = " + value2);
|
|
|
|
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
|
|
|
|
|
|
|
Token::createMutualLinks(tok->tokAt(-10), tok->tokAt(-8));
|
2009-07-14 12:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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-01-24 18:15:38 +01:00
|
|
|
(tok4->str() == "&&" || tok4->str() == "||" || tok4->str() == ")"))
|
|
|
|
{
|
|
|
|
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-06-14 08:26:20 +02:00
|
|
|
if (!tok->previous() || !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 ":"..
|
|
|
|
else if (Token::Match(semicolon, ": %num%") || Token::Match(semicolon, ": %var% [);]"))
|
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-07-23 10:12:02 +02:00
|
|
|
|
|
|
|
// delete the ":" token and the token after it..
|
|
|
|
semicolon->deleteThis();
|
|
|
|
semicolon->deleteThis();
|
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-03-10 21:26:08 +01:00
|
|
|
if (Token::Match(tok->next(), "( %type% *| )") || Token::Match(tok->next(), "( %type% %type% *| )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-03-09 19:47:21 +01:00
|
|
|
if (tok->isName() && tok->str() != "return")
|
|
|
|
continue;
|
2009-03-10 21:26:08 +01:00
|
|
|
|
|
|
|
// Is it a cast of some variable?
|
|
|
|
const Token *tok2 = tok->tokAt(3);
|
|
|
|
while (tok2 && tok2->str() != ")")
|
|
|
|
tok2 = tok2->next();
|
|
|
|
if (!Token::Match(tok2, ") %var%"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Remove cast..
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <"))
|
|
|
|
{
|
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, "> ("))
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok, tok2->tokAt(2));
|
|
|
|
tok2 = tok;
|
|
|
|
int parlevel = 0;
|
|
|
|
while (tok2->next() && parlevel >= 0)
|
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
2009-07-27 11:24:24 +02:00
|
|
|
if (tok2->next()->str() == "(")
|
2009-06-19 19:25:56 +02:00
|
|
|
++parlevel;
|
2009-07-27 11:24:24 +02:00
|
|
|
else if (tok2->next()->str() == ")")
|
2009-06-19 19:25:56 +02:00
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
if (tok2->next())
|
|
|
|
tok2->deleteNext();
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
bool Tokenizer::simplifyFunctionParameters()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
--indentlevel;
|
|
|
|
|
|
|
|
// Find the function e.g. foo( x ) or foo( x, y )
|
|
|
|
else if (indentlevel == 0 && Token::Match(tok, "%var% ( %var% [,)]"))
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
ret = true;
|
|
|
|
tok = temp;
|
|
|
|
start = tok;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-27 08:39:11 +01:00
|
|
|
if (tok == NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
if (bailOut)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++indentlevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
if (tok->previous() && !Token::Match(tok->previous(), "[{};)]"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Token *type0 = tok;
|
|
|
|
if (!Token::Match(type0, "%type%"))
|
|
|
|
continue;
|
2009-07-06 11:45:14 +02:00
|
|
|
if (Token::Match(type0, "else|return|static"))
|
2009-03-18 20:32:05 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
bool isconst = false;
|
|
|
|
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;
|
|
|
|
|
|
|
|
tok2 = tok2->next();
|
|
|
|
++typelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't split up const declaration..
|
|
|
|
if (isconst && Token::Match(tok2, "%type% %var% ="))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::Match(tok2, "%type% %var% ,|="))
|
|
|
|
{
|
|
|
|
if (tok2->next()->str() != "operator")
|
|
|
|
tok2 = tok2->tokAt(2); // The ',' or '=' token
|
|
|
|
else
|
|
|
|
tok2 = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok2, "%type% * %var% ,|="))
|
|
|
|
{
|
2009-06-14 08:20:51 +02:00
|
|
|
if (tok2->tokAt(2)->str() != "operator")
|
2009-03-18 20:32:05 +01:00
|
|
|
tok2 = tok2->tokAt(3); // The ',' token
|
|
|
|
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-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(5); // The ',' token
|
|
|
|
}
|
|
|
|
|
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 ::"))
|
|
|
|
{
|
|
|
|
typelen += 1;
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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-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())
|
|
|
|
{
|
|
|
|
// A variable declaration where the "int" is left out?
|
|
|
|
if (!Token::Match(tok, "unsigned %var% [;,=]"))
|
|
|
|
continue;
|
|
|
|
|
2009-08-05 20:15:48 +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-08-05 20:15:48 +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())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// The "int" is missing.. add it
|
|
|
|
tok->insertToken("int");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
for (tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
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-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();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else if (newTok->str() == "(")
|
|
|
|
{
|
|
|
|
Token::createMutualLinks(newTok, braces.back());
|
|
|
|
braces.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-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-08-29 11:08:11 +02:00
|
|
|
Token::createMutualLinks(tok->link()->next(), tok);
|
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() == ")") ||
|
|
|
|
(Token::Match(tok->next(), "%var%") || tok->next()->str() == "(")))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
|
|
|
|
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% ;") ||
|
|
|
|
Token::Match(tok2, "%var% = %bool% ;")))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
unsigned int varid = tok2->varId();
|
|
|
|
if (varid == 0)
|
|
|
|
continue;
|
|
|
|
|
2009-02-24 17:11:37 +01:00
|
|
|
std::string value(tok2->strAt(2));
|
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-08-22 10:23:55 +02:00
|
|
|
// Stop if something like 'while (--var)' is found
|
|
|
|
if (tok3->str() == "while")
|
|
|
|
{
|
|
|
|
const Token *endpar = tok3->next()->link();
|
|
|
|
bool bailout = false;
|
|
|
|
for (const Token *tok4 = tok3; tok4 != endpar; tok4 = tok4->next())
|
|
|
|
{
|
|
|
|
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-02-20 18:27:57 +01:00
|
|
|
if (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
|
|
|
|
|
|
|
if (Token::Match(tok3->next(), "%varid% ++|--", varid))
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::Match(tok3->next(), "++|-- %varid%", varid))
|
|
|
|
{
|
|
|
|
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-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:18:26 +01:00
|
|
|
|
|
|
|
bool Tokenizer::elseif()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
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-07-27 11:24:24 +02:00
|
|
|
if (tok2->next()->str() != "else")
|
2009-02-17 20:18:26 +01:00
|
|
|
{
|
|
|
|
tok->insertToken("{");
|
|
|
|
tok2->insertToken("}");
|
|
|
|
ret = true;
|
2009-08-23 02:38:00 +02:00
|
|
|
Token::createMutualLinks(tok->next(), tok2->next());
|
2009-02-17 20:18:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-07-03 20:57:44 +02:00
|
|
|
if (Token::Match(tok->previous(), "( ( %var% )") && tok->next()->varId() != 0)
|
|
|
|
{
|
|
|
|
// We have "( var )", remove the paranthesis
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok = tok->next();
|
|
|
|
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-08-02 10:45:33 +02:00
|
|
|
if (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-08-28 12:13:46 +02:00
|
|
|
// + and - are calculated after *
|
2009-08-28 22:29:08 +02:00
|
|
|
if (Token::Match(tok->next(), "[+-]"))
|
2009-08-28 12:13:46 +02:00
|
|
|
{
|
|
|
|
if (tok->previous()->str() == "*")
|
|
|
|
continue;
|
|
|
|
if (Token::simpleMatch(tok->tokAt(3), "*"))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-08-02 14:22:39 +02:00
|
|
|
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-08-02 10:45:33 +02:00
|
|
|
tok = tok->tokAt(-2);
|
|
|
|
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);
|
|
|
|
if (!tok->isName() && tok->str() != ">" && Token::Match(tok->next(), "( %var% ) [;),+-*/><]]"))
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok = tok->next();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
end = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok2->str() == "{" || Token::Match(tok2, "%var% :"))
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2009-08-25 20:58:50 +02:00
|
|
|
Token *openBrace = NULL;
|
|
|
|
|
2009-08-24 23:10:12 +02:00
|
|
|
// Delete the "goto name;"
|
|
|
|
token = token->previous();
|
|
|
|
token->deleteNext();
|
|
|
|
token->deleteNext();
|
|
|
|
token->deleteNext();
|
|
|
|
|
|
|
|
const bool endpar(token->str() == ")");
|
|
|
|
if (endpar)
|
|
|
|
{
|
|
|
|
token->insertToken("{");
|
|
|
|
token = token->next();
|
2009-08-25 20:58:50 +02:00
|
|
|
openBrace = token;
|
2009-08-24 23:10:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the statements..
|
|
|
|
bool ret = false;
|
2009-08-28 22:31:11 +02:00
|
|
|
|
|
|
|
std::list<Token*> links;
|
2009-08-24 23:10:12 +02:00
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "}")
|
|
|
|
break;
|
|
|
|
if (tok2->str() == "return")
|
|
|
|
ret = true;
|
|
|
|
token->insertToken(tok2->str().c_str());
|
|
|
|
token = token->next();
|
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-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();
|
|
|
|
}
|
|
|
|
if (endpar)
|
|
|
|
{
|
|
|
|
token->insertToken("}");
|
|
|
|
token = token->next();
|
2009-08-25 20:58:50 +02:00
|
|
|
Token::createMutualLinks(openBrace, token);
|
2009-08-24 23:10:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gotos.clear();
|
|
|
|
tok = beginfunction;
|
|
|
|
indentlevel = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
--indentlevel;
|
|
|
|
|
|
|
|
if (indentlevel > 0)
|
|
|
|
{
|
|
|
|
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;
|
2009-05-03 20:10:59 +02:00
|
|
|
while (tok->next() && !strchr(";{", tok->strAt(1)[0]))
|
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
|
|
|
|
2009-07-05 22:16:43 +02:00
|
|
|
const Token * Tokenizer::findClassFunction(const Token *tok, const char classname[], const char funcname[], int &indentlevel)
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
|
|
|
if (indentlevel < 0 || tok == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
std::ostringstream classPattern;
|
|
|
|
classPattern << "class " << classname << " :|{";
|
|
|
|
|
|
|
|
std::ostringstream internalPattern;
|
|
|
|
internalPattern << funcname << " (";
|
|
|
|
|
|
|
|
std::ostringstream externalPattern;
|
|
|
|
externalPattern << classname << " :: " << funcname << " (";
|
|
|
|
|
2009-07-13 15:51:35 +02:00
|
|
|
for (; tok; tok = tok->next())
|
2009-03-13 22:28:44 +01:00
|
|
|
{
|
|
|
|
if (indentlevel == 0 && Token::Match(tok, classPattern.str().c_str()))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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?
|
|
|
|
if (tok->str() != "~" && Token::Match(tok->next(), internalPattern.str().c_str()))
|
|
|
|
{
|
|
|
|
const Token *tok2 = tok->next();
|
|
|
|
while (tok2 && tok2->str() != "{" && tok2->str() != ";")
|
|
|
|
tok2 = tok2->next();
|
|
|
|
if (tok2 && tok2->str() == "{")
|
|
|
|
return tok->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (indentlevel == 0 && Token::Match(tok, externalPattern.str().c_str()))
|
|
|
|
{
|
|
|
|
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-05-11 21:52:04 +02:00
|
|
|
if (!_errorLogger)
|
|
|
|
{
|
|
|
|
std::cout << "### Unlogged error at Tokenizer::syntaxError" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok->linenr();
|
2009-05-11 21:52:04 +02:00
|
|
|
loc.file = file(tok);
|
2009-05-07 22:17:29 +02:00
|
|
|
locationList.push_back(loc);
|
|
|
|
_errorLogger->reportErr(
|
|
|
|
ErrorLogger::ErrorMessage(locationList,
|
|
|
|
"error",
|
2009-05-11 21:52:04 +02:00
|
|
|
std::string("Invalid number of character (") + c + "). Can't process file.",
|
2009-05-07 22:17:29 +02:00
|
|
|
"syntaxError"));
|
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-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-06-10 23:12:26 +02:00
|
|
|
if (tok->str() != ",")
|
|
|
|
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-06-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
}
|