2009-01-24 18:15:38 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
|
2009-01-24 18:15:38 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-24 18:15:38 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "tokenize.h"
|
2009-07-13 19:11:31 +02:00
|
|
|
#include "token.h"
|
2009-01-24 18:15:38 +01:00
|
|
|
#include "filelister.h"
|
2009-04-06 19:23:30 +02:00
|
|
|
#include "mathlib.h"
|
2009-07-13 19:11:31 +02:00
|
|
|
#include "settings.h"
|
|
|
|
#include "errorlogger.h"
|
2009-10-17 23:11:48 +02:00
|
|
|
#include "check.h"
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
#include <locale>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <list>
|
2009-09-13 15:00:48 +02:00
|
|
|
#include <cassert>
|
2009-01-24 18:15:38 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
2009-08-27 18:33:42 +02:00
|
|
|
#include <stack>
|
2009-09-01 19:30:54 +02:00
|
|
|
#include <stdexcept> // for std::runtime_error
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Tokenizer::Tokenizer()
|
2009-07-16 07:20:29 +02:00
|
|
|
: _settings(0), _errorLogger(0)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
_tokens = 0;
|
|
|
|
_tokensBack = 0;
|
|
|
|
}
|
|
|
|
|
2009-07-13 19:11:31 +02:00
|
|
|
Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger)
|
2009-07-16 07:20:29 +02:00
|
|
|
: _settings(settings), _errorLogger(errorLogger)
|
2009-03-16 22:31:52 +01:00
|
|
|
{
|
|
|
|
_tokens = 0;
|
|
|
|
_tokensBack = 0;
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
Tokenizer::~Tokenizer()
|
|
|
|
{
|
2009-07-05 22:16:43 +02:00
|
|
|
deallocateTokens();
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Helper functions..
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const Token *Tokenizer::tokens() const
|
|
|
|
{
|
|
|
|
return _tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::vector<std::string> *Tokenizer::getFiles() const
|
|
|
|
{
|
|
|
|
return &_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// addtoken
|
|
|
|
// add a token. Used by 'Tokenizer'
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsigned int fileno)
|
|
|
|
{
|
|
|
|
if (str[0] == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Replace hexadecimal value with decimal
|
|
|
|
std::ostringstream str2;
|
|
|
|
if (strncmp(str, "0x", 2) == 0)
|
|
|
|
{
|
|
|
|
str2 << std::strtoul(str + 2, NULL, 16);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str2 << str;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_tokensBack)
|
|
|
|
{
|
|
|
|
_tokensBack->insertToken(str2.str().c_str());
|
|
|
|
_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-10-10 21:54:58 +02:00
|
|
|
unsigned int Tokenizer::sizeOfType(const Token *type) const
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-10-07 09:54:34 +02:00
|
|
|
if (!type || !type->strAt(0))
|
2009-01-24 18:15:38 +01:00
|
|
|
return 0;
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
if (type->str()[0] == '"')
|
2009-10-11 16:40:50 +02:00
|
|
|
return static_cast<unsigned int>(Token::getStrLength(type) + 1);
|
2009-10-07 09:54:34 +02:00
|
|
|
|
|
|
|
std::map<std::string, unsigned int>::const_iterator it = _typeSize.find(type->strAt(0));
|
2009-01-24 18:15:38 +01:00
|
|
|
if (it == _typeSize.end())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// InsertTokens - Copy and insert tokens
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-09-09 23:25:58 +02:00
|
|
|
void Tokenizer::insertTokens(Token *dest, const Token *src, unsigned int n)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
while (n > 0)
|
|
|
|
{
|
2009-05-03 20:10:59 +02:00
|
|
|
dest->insertToken(src->str().c_str());
|
2009-01-24 18:15:38 +01:00
|
|
|
dest = dest->next();
|
|
|
|
dest->fileIndex(src->fileIndex());
|
|
|
|
dest->linenr(src->linenr());
|
|
|
|
dest->varId(src->varId());
|
|
|
|
src = src->next();
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Tokenize - tokenizes a given file.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-05-09 21:32:29 +02:00
|
|
|
void Tokenizer::createTokens(std::istream &code)
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// line number in parsed code
|
|
|
|
unsigned int lineno = 1;
|
|
|
|
|
|
|
|
// The current token being parsed
|
|
|
|
std::string CurrentToken;
|
|
|
|
|
|
|
|
// lineNumbers holds line numbers for files in fileIndexes
|
|
|
|
// every time an include file is complitely parsed, last item in the vector
|
|
|
|
// is removed and lineno is set to point to that value.
|
|
|
|
std::vector<unsigned int> lineNumbers;
|
|
|
|
|
|
|
|
// fileIndexes holds index for _files vector about currently parsed files
|
|
|
|
// every time an include file is complitely parsed, last item in the vector
|
|
|
|
// is removed and FileIndex is set to point to that value.
|
|
|
|
std::vector<unsigned int> fileIndexes;
|
|
|
|
|
|
|
|
// FileIndex. What file in the _files vector is read now?
|
|
|
|
unsigned int FileIndex = 0;
|
|
|
|
|
|
|
|
// Read one byte at a time from code and create tokens
|
|
|
|
for (char ch = (char)code.get(); code.good(); ch = (char)code.get())
|
|
|
|
{
|
|
|
|
// We are not handling UTF and stuff like that. Code is supposed to plain simple text.
|
|
|
|
if (ch < 0)
|
|
|
|
continue;
|
|
|
|
|
2009-03-04 18:02:45 +01:00
|
|
|
// char/string..
|
|
|
|
if (ch == '\'' || ch == '\"')
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-03-15 22:09:27 +01:00
|
|
|
std::string line;
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-03-04 18:02:45 +01:00
|
|
|
// read char
|
2009-01-24 18:15:38 +01:00
|
|
|
bool special = false;
|
|
|
|
char c = ch;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Append token..
|
2009-03-15 22:09:27 +01:00
|
|
|
line += c;
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
++lineno;
|
|
|
|
|
|
|
|
// Special sequence '\.'
|
|
|
|
if (special)
|
|
|
|
special = false;
|
|
|
|
else
|
|
|
|
special = (c == '\\');
|
|
|
|
|
|
|
|
// Get next character
|
|
|
|
c = (char)code.get();
|
|
|
|
}
|
2009-03-04 18:02:45 +01:00
|
|
|
while (code.good() && (special || c != ch));
|
2009-03-15 22:09:27 +01:00
|
|
|
line += ch;
|
2009-03-04 18:02:45 +01:00
|
|
|
|
2009-03-15 22:09:27 +01:00
|
|
|
// Handle #file "file.h"
|
|
|
|
if (CurrentToken == "#file")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Extract the filename
|
2009-03-15 22:09:27 +01:00
|
|
|
line = line.substr(1, line.length() - 2);
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Has this file been tokenized already?
|
|
|
|
++lineno;
|
|
|
|
bool foundOurfile = false;
|
|
|
|
fileIndexes.push_back(FileIndex);
|
|
|
|
for (unsigned int i = 0; i < _files.size(); i++)
|
|
|
|
{
|
2009-07-05 22:16:43 +02:00
|
|
|
if (FileLister::sameFileName(_files[i].c_str(), line.c_str()))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Use this index
|
|
|
|
foundOurfile = true;
|
|
|
|
FileIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundOurfile)
|
|
|
|
{
|
|
|
|
// The "_files" vector remembers what files have been tokenized..
|
|
|
|
_files.push_back(FileLister::simplifyPath(line.c_str()));
|
2009-02-07 21:55:25 +01:00
|
|
|
FileIndex = static_cast<unsigned int>(_files.size() - 1);
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lineNumbers.push_back(lineno);
|
2009-03-15 22:09:27 +01:00
|
|
|
lineno = 0;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-15 22:09:27 +01:00
|
|
|
// Add previous token
|
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
|
|
|
|
// Add content of the string
|
2009-01-24 18:15:38 +01:00
|
|
|
addtoken(line.c_str(), lineno, FileIndex);
|
|
|
|
}
|
2009-03-15 22:09:27 +01:00
|
|
|
|
|
|
|
CurrentToken.clear();
|
|
|
|
|
|
|
|
continue;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-03-15 22:09:27 +01:00
|
|
|
if (strchr("+-*/%&|^?!=<>[](){};:,.~\n ", ch))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-02-08 11:56:20 +01:00
|
|
|
if (ch == '.' &&
|
|
|
|
CurrentToken.length() > 0 &&
|
|
|
|
std::isdigit(CurrentToken[0]))
|
2009-02-08 10:51:45 +01:00
|
|
|
{
|
2009-02-08 11:56:20 +01:00
|
|
|
// Don't separate doubles "5.4"
|
|
|
|
}
|
|
|
|
else if (strchr("+-", ch) &&
|
|
|
|
CurrentToken.length() > 0 &&
|
|
|
|
std::isdigit(CurrentToken[0]) &&
|
|
|
|
CurrentToken[CurrentToken.length()-1] == 'e')
|
|
|
|
{
|
|
|
|
// Don't separate doubles "4.2e+10"
|
2009-02-08 10:51:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-15 22:09:27 +01:00
|
|
|
if (CurrentToken == "#file")
|
|
|
|
{
|
|
|
|
// Handle this where strings are handled
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (CurrentToken == "#endfile")
|
|
|
|
{
|
|
|
|
if (lineNumbers.empty() || fileIndexes.empty())
|
|
|
|
{
|
|
|
|
std::cerr << "####### Preprocessor bug! #######\n";
|
|
|
|
std::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
lineno = lineNumbers.back();
|
|
|
|
lineNumbers.pop_back();
|
|
|
|
FileIndex = fileIndexes.back();
|
|
|
|
fileIndexes.pop_back();
|
|
|
|
CurrentToken.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If token contains # characters, split it up
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-09-30 13:35:00 +02:00
|
|
|
void Tokenizer::simplifyTypedef()
|
|
|
|
{
|
|
|
|
std::string className;
|
|
|
|
int classLevel = 0;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "class %any%"))
|
|
|
|
{
|
|
|
|
className = tok->next()->str();
|
|
|
|
classLevel = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
--classLevel;
|
|
|
|
if (classLevel < 0)
|
|
|
|
className = "";
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->str() == "{")
|
|
|
|
{
|
|
|
|
++classLevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->str() != "typedef")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *type1 = 0;
|
|
|
|
const char *type2 = 0;
|
|
|
|
const char *typeName = 0;
|
2009-10-03 17:02:23 +02:00
|
|
|
bool pointer = false;
|
2009-09-30 13:35:00 +02:00
|
|
|
|
|
|
|
if (Token::Match(tok->next(), "%type% %type% ;") ||
|
2009-10-03 17:02:23 +02:00
|
|
|
Token::Match(tok->next(), "%type% %type% *| %type% ;"))
|
2009-09-30 13:35:00 +02:00
|
|
|
{
|
|
|
|
if (tok->tokAt(3)->str() == ";")
|
|
|
|
{
|
|
|
|
type1 = tok->strAt(1);
|
|
|
|
type2 = 0;
|
|
|
|
typeName = tok->strAt(2);
|
|
|
|
tok = tok->tokAt(3);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-03 17:02:23 +02:00
|
|
|
pointer = (tok->tokAt(3)->str() == "*");
|
|
|
|
|
2009-09-30 13:35:00 +02:00
|
|
|
type1 = tok->strAt(1);
|
|
|
|
type2 = tok->strAt(2);
|
2009-10-03 17:02:23 +02:00
|
|
|
|
|
|
|
if (pointer)
|
|
|
|
{
|
|
|
|
typeName = tok->strAt(4);
|
|
|
|
tok = tok->tokAt(5);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
typeName = tok->strAt(3);
|
|
|
|
tok = tok->tokAt(4);
|
|
|
|
|
|
|
|
}
|
2009-09-30 13:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string pattern = className + " :: " + typeName;
|
|
|
|
int level = 0;
|
|
|
|
bool inScope = true;
|
|
|
|
|
|
|
|
bool exitThisScope = false;
|
|
|
|
int exitScope = 0;
|
|
|
|
bool simplifyType = false;
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
--level;
|
|
|
|
if (level < 0)
|
|
|
|
inScope = false;
|
|
|
|
|
|
|
|
if (exitThisScope)
|
|
|
|
{
|
|
|
|
if (level < exitScope)
|
|
|
|
exitThisScope = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "{")
|
|
|
|
++level;
|
|
|
|
else if (Token::Match(tok2, pattern.c_str()))
|
|
|
|
{
|
|
|
|
tok2->deleteNext();
|
|
|
|
tok2->deleteNext();
|
|
|
|
simplifyType = true;
|
|
|
|
}
|
|
|
|
else if (inScope && !exitThisScope && tok2->str() == typeName)
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2->tokAt(-2), "!!typedef") &&
|
|
|
|
Token::Match(tok2->tokAt(-3), "!!typedef"))
|
|
|
|
{
|
|
|
|
simplifyType = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Typedef with the same name.
|
|
|
|
exitThisScope = true;
|
|
|
|
exitScope = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (simplifyType)
|
|
|
|
{
|
|
|
|
tok2->str(type1);
|
|
|
|
if (type2)
|
|
|
|
{
|
|
|
|
tok2->insertToken(type2);
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2009-10-03 17:02:23 +02:00
|
|
|
if (pointer)
|
|
|
|
{
|
|
|
|
tok2->insertToken("*");
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
2009-09-30 13:35:00 +02:00
|
|
|
|
|
|
|
simplifyType = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-09 21:32:29 +02:00
|
|
|
bool Tokenizer::tokenize(std::istream &code, const char FileName[])
|
|
|
|
{
|
|
|
|
// The "_files" vector remembers what files have been tokenized..
|
|
|
|
_files.push_back(FileLister::simplifyPath(FileName));
|
|
|
|
|
|
|
|
createTokens(code);
|
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
if (!createLinks())
|
|
|
|
{
|
|
|
|
// Source has syntax errors, can't proceed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-30 20:42:14 +02:00
|
|
|
simplifyDoWhileAddBraces();
|
|
|
|
simplifyIfAddBraces();
|
|
|
|
|
2009-03-17 20:50:06 +01:00
|
|
|
// Combine "- %num%" ..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
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:" },
|
2009-08-31 19:40:49 +02:00
|
|
|
{ "public", ":", "public:" },
|
|
|
|
{ "__published", ":", "__published:" }
|
2009-01-24 18:15:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
for (unsigned ui = 0; ui < sizeof(combineWithNext) / sizeof(combineWithNext[0]); ui++)
|
|
|
|
{
|
|
|
|
if (tok->str() == combineWithNext[ui][0] && tok->next()->str() == combineWithNext[ui][1])
|
|
|
|
{
|
|
|
|
tok->str(combineWithNext[ui][2]);
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// typedef..
|
2009-09-30 13:35:00 +02:00
|
|
|
simplifyTypedef();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// 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-10-04 07:51:12 +02:00
|
|
|
// Use "<" comparison instead of ">"
|
2009-11-04 23:58:15 +01:00
|
|
|
simplifyComparisonOrder();
|
2009-10-04 07:51:12 +02:00
|
|
|
|
2009-07-14 15:30:23 +02:00
|
|
|
/**
|
|
|
|
* @todo simplify "for"
|
|
|
|
* - move out start-statement "for (a;b;c);" => "{ a; for(;b;c); }"
|
|
|
|
* - try to change "for" loop to a "while" loop instead
|
|
|
|
*/
|
|
|
|
|
2009-11-12 22:49:39 +01:00
|
|
|
simplifyConst();
|
|
|
|
|
2009-06-10 19:36:00 +02:00
|
|
|
// Split up variable declarations.
|
2009-03-18 20:32:05 +01:00
|
|
|
simplifyVarDecl();
|
|
|
|
|
2009-03-11 18:50:24 +01:00
|
|
|
// Handle templates..
|
2009-05-03 21:23:47 +02:00
|
|
|
simplifyTemplates();
|
2009-05-08 16:19:22 +02:00
|
|
|
|
2009-07-14 12:06:38 +02:00
|
|
|
// Simplify the operator "?:"
|
|
|
|
simplifyConditionOperator();
|
|
|
|
|
2009-10-01 19:45:48 +02:00
|
|
|
// remove exception specifications..
|
|
|
|
removeExceptionSpecifications(_tokens);
|
|
|
|
|
2009-09-19 10:54:10 +02:00
|
|
|
setVarId();
|
2009-11-10 22:10:56 +01:00
|
|
|
if (!validate())
|
|
|
|
{
|
|
|
|
std::cerr << "### A bug in the Cppcheck itself, while checking: " << FileName << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-19 10:54:10 +02:00
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
return true;
|
2009-05-03 21:23:47 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-11-01 15:56:00 +01:00
|
|
|
/**
|
|
|
|
* is the token pointing at a template parameters block..
|
|
|
|
* < int , 3 > => yes
|
|
|
|
* \param tok start token that must point at "<"
|
|
|
|
* \return true if the tokens look like template parameters
|
|
|
|
*/
|
|
|
|
static bool templateParameters(const Token *tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (tok->str() != "<")
|
|
|
|
return false;
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
// num/type ..
|
|
|
|
if (!tok->isNumber() && !tok->isName())
|
|
|
|
return false;
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// optional "*"
|
|
|
|
if (tok->str() == "*")
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
// ,/>
|
|
|
|
if (tok->str() == ">")
|
|
|
|
return true;
|
|
|
|
if (tok->str() != ",")
|
|
|
|
break;
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
/**
|
|
|
|
* Remove "template < ..." they can cause false positives because they are not expanded
|
|
|
|
*/
|
|
|
|
static void removeTemplates(Token *tok)
|
|
|
|
{
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (! Token::simpleMatch(tok, "template <"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
tok2 = tok2->link();
|
|
|
|
tok2 = tok2 ? tok2->next() : 0;
|
|
|
|
Token::eraseTokens(tok, tok2);
|
|
|
|
tok->str(";");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
{
|
|
|
|
tok2 = tok2->link();
|
|
|
|
if (!tok2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
{
|
2009-11-01 20:49:43 +01:00
|
|
|
Token::eraseTokens(tok, tok2->next());
|
2009-11-01 19:03:52 +01:00
|
|
|
tok->str(";");
|
2009-11-01 20:49:43 +01:00
|
|
|
break;
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-03 21:23:47 +02:00
|
|
|
void Tokenizer::simplifyTemplates()
|
|
|
|
{
|
2009-09-03 22:19:44 +02:00
|
|
|
// Remove "typename" unless used in template arguments..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "typename")
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, "template <"))
|
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
while (tok && tok->str() != ">")
|
2009-09-03 22:19:44 +02:00
|
|
|
tok = tok->next();
|
2009-09-06 08:22:45 +02:00
|
|
|
if (!tok)
|
|
|
|
break;
|
2009-09-03 22:19:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-01 15:56:00 +01:00
|
|
|
std::set<std::string> expandedtemplates;
|
|
|
|
|
2009-10-31 12:34:52 +01:00
|
|
|
// Locate specialized templates..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() != "template")
|
|
|
|
continue;
|
|
|
|
if (!Token::simpleMatch(tok->next(), "< >"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// what kind of template is this?
|
|
|
|
Token *tok2 = tok->tokAt(3);
|
|
|
|
while (tok2 && (tok2->isName() || tok2->str() == "*"))
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
2009-11-01 15:56:00 +01:00
|
|
|
if (!templateParameters(tok2))
|
|
|
|
continue;
|
|
|
|
|
2009-10-31 12:34:52 +01:00
|
|
|
// unknown template.. bail out
|
2009-11-01 15:56:00 +01:00
|
|
|
if (!tok2->previous()->isName())
|
2009-10-31 12:34:52 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
tok2 = tok2->previous();
|
2009-11-01 19:03:52 +01:00
|
|
|
std::string s;
|
2009-10-31 12:34:52 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
std::ostringstream ostr;
|
|
|
|
const Token *tok3 = tok2;
|
|
|
|
for (tok3 = tok2; tok3 && tok3->str() != ">"; tok3 = tok3->next())
|
2009-10-31 12:34:52 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
if (tok3 != tok2)
|
|
|
|
ostr << " ";
|
|
|
|
ostr << tok3->str();
|
2009-10-31 12:34:52 +01:00
|
|
|
}
|
|
|
|
if (!Token::Match(tok3, "> ("))
|
|
|
|
continue;
|
2009-11-01 19:03:52 +01:00
|
|
|
s = ostr.str();
|
2009-10-31 12:34:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// save search pattern..
|
|
|
|
const std::string pattern(s + " > (");
|
|
|
|
|
|
|
|
// remove spaces to create new name
|
|
|
|
while (s.find(" ") != std::string::npos)
|
|
|
|
s.erase(s.find(" "), 1);
|
|
|
|
const std::string name(s + ">");
|
2009-11-01 15:56:00 +01:00
|
|
|
expandedtemplates.insert(name);
|
2009-10-31 12:34:52 +01:00
|
|
|
|
|
|
|
// Rename template..
|
|
|
|
Token::eraseTokens(tok2, Token::findmatch(tok2, "("));
|
|
|
|
tok2->str(name);
|
|
|
|
|
|
|
|
// delete the "template < >"
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
// Use this special template in the code..
|
|
|
|
while (0 != (tok2 = const_cast<Token *>(Token::findmatch(tok2, pattern.c_str()))))
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok2, Token::findmatch(tok2, "("));
|
|
|
|
tok2->str(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-05 20:16:57 +02:00
|
|
|
// Locate templates..
|
|
|
|
std::list<Token *> templates;
|
2009-03-11 18:50:24 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-05 20:16:57 +02:00
|
|
|
if (Token::simpleMatch(tok, "template <"))
|
2009-05-07 16:05:07 +02:00
|
|
|
{
|
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
// Just a declaration => ignore this
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Implementation => add to "templates"
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
templates.push_back(tok);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-05 20:16:57 +02:00
|
|
|
}
|
|
|
|
if (templates.empty())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
removeTemplates(_tokens);
|
2009-05-05 20:16:57 +02:00
|
|
|
return;
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
2009-05-05 20:16:57 +02:00
|
|
|
|
|
|
|
// Locate possible instantiations of templates..
|
|
|
|
std::list<Token *> used;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-06 21:03:11 +02:00
|
|
|
// template definition.. skip it
|
|
|
|
if (Token::simpleMatch(tok, "template <"))
|
|
|
|
{
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
if (tok->str() == "{" || tok->str() == "(")
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
tok = tok->link();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
if (tok->str() == "}")
|
2009-05-06 21:03:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
else if (tok->str() == ";")
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
else if (Token::Match(tok->previous(), "[{};=] %var% <") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "[,:] private|protected|public %var% <"))
|
2009-05-06 21:03:11 +02:00
|
|
|
{
|
2009-11-01 15:56:00 +01:00
|
|
|
if (templateParameters(tok->next()))
|
|
|
|
used.push_back(tok);
|
2009-05-06 21:03:11 +02:00
|
|
|
}
|
2009-05-05 20:16:57 +02:00
|
|
|
}
|
|
|
|
if (used.empty())
|
2009-11-01 19:03:52 +01:00
|
|
|
{
|
|
|
|
removeTemplates(_tokens);
|
2009-05-05 20:16:57 +02:00
|
|
|
return;
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
|
|
|
|
2009-05-05 20:16:57 +02:00
|
|
|
|
2009-09-03 21:46:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Template arguments with default values
|
|
|
|
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
|
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
std::list<Token *> eq;
|
|
|
|
unsigned int templatepar = 1;
|
|
|
|
std::string classname;
|
2009-09-03 21:46:07 +02:00
|
|
|
for (Token *tok = *iter1; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == ">")
|
2009-09-06 08:22:45 +02:00
|
|
|
{
|
|
|
|
if (Token::Match(tok, "> class %var%"))
|
|
|
|
classname = tok->strAt(2);
|
2009-09-03 21:46:07 +02:00
|
|
|
break;
|
2009-09-06 08:22:45 +02:00
|
|
|
}
|
2009-09-03 21:46:07 +02:00
|
|
|
|
|
|
|
if (tok->str() == ",")
|
2009-09-06 08:22:45 +02:00
|
|
|
++templatepar;
|
|
|
|
|
|
|
|
else if (tok->str() == "=")
|
|
|
|
eq.push_back(tok);
|
|
|
|
}
|
|
|
|
if (eq.empty() || classname.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
|
|
|
|
{
|
|
|
|
Token *tok = *iter2;
|
|
|
|
|
|
|
|
if (!Token::Match(tok, (classname + " < %any%").c_str()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// count the parameters..
|
|
|
|
unsigned int usedpar = 1;
|
|
|
|
for (tok = tok->tokAt(3); tok; tok = tok->tokAt(2))
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
if (tok->str() == ">")
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (tok->str() == ",")
|
|
|
|
++usedpar;
|
|
|
|
|
2009-09-03 21:46:07 +02:00
|
|
|
else
|
2009-09-06 08:22:45 +02:00
|
|
|
break;
|
2009-09-03 21:46:07 +02:00
|
|
|
}
|
2009-09-06 08:22:45 +02:00
|
|
|
if (tok && tok->str() == ">")
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
tok = tok->previous();
|
|
|
|
std::list<Token *>::const_iterator it = eq.begin();
|
|
|
|
for (unsigned int i = (templatepar - eq.size()); it != eq.end() && i < usedpar; ++i)
|
|
|
|
++it;
|
|
|
|
while (it != eq.end())
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
tok->insertToken(",");
|
|
|
|
tok = tok->next();
|
|
|
|
tok->insertToken((*it)->strAt(1));
|
|
|
|
tok = tok->next();
|
|
|
|
++it;
|
2009-09-03 21:46:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-06 08:22:45 +02:00
|
|
|
for (std::list<Token *>::iterator it = eq.begin(); it != eq.end(); ++it)
|
2009-09-03 21:46:07 +02:00
|
|
|
{
|
2009-09-06 08:22:45 +02:00
|
|
|
(*it)->deleteThis();
|
|
|
|
(*it)->deleteThis();
|
2009-09-03 21:46:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-05 20:16:57 +02:00
|
|
|
// expand templates
|
2009-11-01 19:03:52 +01:00
|
|
|
bool done = false;
|
|
|
|
while (!done)
|
2009-05-05 20:16:57 +02:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
done = true;
|
|
|
|
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
|
2009-03-14 21:26:32 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
Token *tok = *iter1;
|
2009-03-11 18:50:24 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
std::vector<std::string> type;
|
|
|
|
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
if (Token::Match(tok, "%var% ,|>"))
|
|
|
|
type.push_back(tok->str());
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// bail out if the end of the file was reached
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// if this is a template function, get the position of the function name
|
|
|
|
unsigned int pos = 0;
|
|
|
|
if (Token::Match(tok, "> %type% *| %var% ("))
|
|
|
|
pos = 2;
|
|
|
|
else if (Token::Match(tok, "> %type% %type% *| %var% ("))
|
|
|
|
pos = 3;
|
|
|
|
if (pos > 0 && tok->tokAt(pos)->str() == "*")
|
|
|
|
++pos;
|
2009-05-05 20:16:57 +02:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// name of template function/class..
|
|
|
|
const std::string name(tok->strAt(pos > 0 ? pos : 2));
|
2009-04-14 22:21:52 +02:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
const bool isfunc(pos > 0);
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// locate template usage..
|
|
|
|
|
|
|
|
std::string s(name + " <");
|
|
|
|
for (unsigned int i = 0; i < type.size(); ++i)
|
2009-03-14 21:26:32 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
if (i > 0)
|
|
|
|
s += ",";
|
|
|
|
s += " %any% ";
|
2009-03-14 21:26:32 +01:00
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
const std::string pattern(s + "> ");
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
std::string::size_type sz1 = used.size();
|
|
|
|
unsigned int recursiveCount = 0;
|
2009-03-11 18:50:24 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
|
2009-03-11 19:14:45 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
// If the size of "used" has changed, simplify calculations
|
|
|
|
if (sz1 != used.size())
|
2009-03-11 18:50:24 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
sz1 = used.size();
|
|
|
|
simplifyCalculations();
|
|
|
|
recursiveCount++;
|
|
|
|
if (recursiveCount > 100)
|
2009-05-09 08:02:59 +02:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
// bail out..
|
|
|
|
break;
|
2009-05-09 08:02:59 +02:00
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
2009-05-09 08:02:59 +02:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
Token *tok2 = *iter2;
|
2009-03-17 18:55:28 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
if (tok2->str() != name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::Match(tok2->previous(), "[;{}=]") &&
|
|
|
|
!Token::Match(tok2, (pattern + (isfunc ? "(" : "%var%")).c_str()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// New type..
|
|
|
|
std::vector<std::string> types2;
|
|
|
|
s = "";
|
|
|
|
for (const Token *tok3 = tok2->tokAt(2); tok3->str() != ">"; tok3 = tok3->next())
|
|
|
|
{
|
|
|
|
if (tok3->str() != ",")
|
|
|
|
types2.push_back(tok3->str());
|
|
|
|
s += tok3->str();
|
|
|
|
}
|
|
|
|
const std::string type2(s);
|
2009-03-17 18:55:28 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// New classname/funcname..
|
|
|
|
const std::string name2(name + "<" + type2 + ">");
|
2009-03-17 18:55:28 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
if (expandedtemplates.find(name2) == expandedtemplates.end())
|
|
|
|
{
|
|
|
|
expandedtemplates.insert(name2);
|
|
|
|
// Copy template..
|
|
|
|
int _indentlevel = 0;
|
|
|
|
int _parlevel = 0;
|
|
|
|
for (const Token *tok3 = _tokens; tok3; tok3 = tok3->next())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
|
|
|
if (tok3->str() == "{")
|
2009-11-01 19:03:52 +01:00
|
|
|
++_indentlevel;
|
2009-11-01 15:56:00 +01:00
|
|
|
else if (tok3->str() == "}")
|
2009-11-01 19:03:52 +01:00
|
|
|
--_indentlevel;
|
|
|
|
else if (tok3->str() == "(")
|
|
|
|
++_parlevel;
|
|
|
|
else if (tok3->str() == ")")
|
|
|
|
--_parlevel;
|
|
|
|
|
|
|
|
// Start of template..
|
|
|
|
if (tok3 == tok)
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
tok3 = tok3->next();
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// member function implemented outside class definition
|
|
|
|
else if (_indentlevel == 0 && _parlevel == 0 && Token::Match(tok3, (pattern + " :: %var% (").c_str()))
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
while (tok3->str() != "::")
|
|
|
|
tok3 = tok3->next();
|
|
|
|
}
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// not part of template.. go on to next token
|
|
|
|
else
|
|
|
|
continue;
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
int indentlevel = 0;
|
|
|
|
std::stack<Token *> braces; // holds "{" tokens
|
|
|
|
std::stack<Token *> brackets; // holds "(" tokens
|
2009-11-11 19:16:05 +01:00
|
|
|
std::stack<Token *> brackets2; // holds "[" tokens
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
for (; tok3; tok3 = tok3->next())
|
|
|
|
{
|
|
|
|
if (tok3->str() == "{")
|
|
|
|
++indentlevel;
|
2009-11-01 15:56:00 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
else if (tok3->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
// there is a bug if indentlevel is 0
|
|
|
|
// the "}" token should only be added if indentlevel is 1 but I add it always intentionally
|
|
|
|
// if indentlevel ever becomes 0, cppcheck will write:
|
|
|
|
// ### Error: Invalid number of character {
|
|
|
|
addtoken("}", tok3->linenr(), tok3->fileIndex());
|
2009-11-01 15:56:00 +01:00
|
|
|
Token::createMutualLinks(braces.top(), _tokensBack);
|
|
|
|
braces.pop();
|
2009-11-01 19:03:52 +01:00
|
|
|
break;
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tok3->isName())
|
|
|
|
{
|
|
|
|
// search for this token in the type vector
|
|
|
|
unsigned int itype = 0;
|
|
|
|
while (itype < type.size() && type[itype] != tok3->str())
|
|
|
|
++itype;
|
|
|
|
|
|
|
|
// replace type with given type..
|
|
|
|
if (itype < type.size())
|
2009-11-01 15:56:00 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
addtoken(types2[itype].c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
continue;
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
2009-08-27 18:33:42 +02:00
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
|
|
|
|
// replace name..
|
|
|
|
if (Token::Match(tok3, (name + " !!<").c_str()))
|
|
|
|
{
|
|
|
|
addtoken(name2.c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy
|
|
|
|
addtoken(tok3->str().c_str(), tok3->linenr(), tok3->fileIndex());
|
|
|
|
if (Token::Match(tok3, "%type% <"))
|
|
|
|
{
|
|
|
|
if (!Token::Match(tok3, (name + " <").c_str()))
|
|
|
|
done = false;
|
|
|
|
used.push_back(_tokensBack);
|
|
|
|
}
|
|
|
|
|
|
|
|
// link() newly tokens manually
|
|
|
|
if (tok3->str() == "{")
|
|
|
|
{
|
|
|
|
braces.push(_tokensBack);
|
|
|
|
}
|
|
|
|
else if (tok3->str() == "}")
|
|
|
|
{
|
|
|
|
assert(braces.empty() == false);
|
|
|
|
Token::createMutualLinks(braces.top(), _tokensBack);
|
|
|
|
braces.pop();
|
|
|
|
}
|
|
|
|
else if (tok3->str() == "(")
|
|
|
|
{
|
|
|
|
brackets.push(_tokensBack);
|
|
|
|
}
|
2009-11-11 19:16:05 +01:00
|
|
|
else if (tok3->str() == "[")
|
|
|
|
{
|
|
|
|
brackets2.push(_tokensBack);
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
else if (tok3->str() == ")")
|
|
|
|
{
|
|
|
|
assert(brackets.empty() == false);
|
|
|
|
Token::createMutualLinks(brackets.top(), _tokensBack);
|
|
|
|
brackets.pop();
|
|
|
|
}
|
2009-11-11 19:16:05 +01:00
|
|
|
else if (tok3->str() == "]")
|
|
|
|
{
|
|
|
|
assert(brackets2.empty() == false);
|
|
|
|
Token::createMutualLinks(brackets2.top(), _tokensBack);
|
|
|
|
brackets2.pop();
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
|
2009-08-27 18:33:42 +02:00
|
|
|
}
|
2009-09-13 15:00:48 +02:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
assert(braces.empty());
|
|
|
|
assert(brackets.empty());
|
|
|
|
}
|
2009-11-01 15:56:00 +01:00
|
|
|
}
|
2009-03-11 19:14:45 +01:00
|
|
|
|
2009-11-01 19:03:52 +01:00
|
|
|
// Replace all these template usages..
|
|
|
|
s = name + " < " + type2 + " >";
|
|
|
|
for (std::string::size_type pos = s.find(","); pos != std::string::npos; pos = s.find(",", pos + 2))
|
|
|
|
{
|
|
|
|
s.insert(pos + 1, " ");
|
|
|
|
s.insert(pos, " ");
|
|
|
|
}
|
|
|
|
for (Token *tok4 = tok2; tok4; tok4 = tok4->next())
|
2009-03-11 18:50:24 +01:00
|
|
|
{
|
2009-11-01 19:03:52 +01:00
|
|
|
if (Token::simpleMatch(tok4, s.c_str()))
|
|
|
|
{
|
|
|
|
tok4->str(name2);
|
|
|
|
while (tok4->next()->str() != ">")
|
|
|
|
tok4->deleteNext();
|
2009-03-14 21:26:32 +01:00
|
|
|
tok4->deleteNext();
|
2009-11-01 19:03:52 +01:00
|
|
|
}
|
2009-03-11 18:50:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-01 19:03:52 +01:00
|
|
|
|
|
|
|
removeTemplates(_tokens);
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-08-12 22:50:03 +02:00
|
|
|
void Tokenizer::updateClassList()
|
|
|
|
{
|
|
|
|
const char pattern_class[] = "class %var% [{:]";
|
|
|
|
_classInfoList.clear();
|
|
|
|
|
|
|
|
// Locate class
|
|
|
|
const Token *tok1 = tokens();
|
2009-09-13 09:03:48 +02:00
|
|
|
while ((tok1 = Token::findmatch(tok1, pattern_class)) != 0)
|
2009-08-12 22:50:03 +02:00
|
|
|
{
|
|
|
|
const char *className;
|
|
|
|
className = tok1->strAt(1);
|
|
|
|
tok1 = tok1->next();
|
|
|
|
|
|
|
|
ClassInfo::MemberType memberType = ClassInfo::PRIVATE;
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const Token *tok = tok1; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
// Indentation
|
|
|
|
if (tok->str() == "{")
|
|
|
|
{
|
|
|
|
++indentlevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse class contents (indentlevel == 1)..
|
|
|
|
if (indentlevel == 1)
|
|
|
|
{
|
|
|
|
if (tok->str() == "private:")
|
|
|
|
memberType = ClassInfo::PRIVATE;
|
|
|
|
else if (tok->str() == "protected:")
|
|
|
|
memberType = ClassInfo::PROTECTED;
|
|
|
|
else if (tok->str() == "public:")
|
|
|
|
memberType = ClassInfo::PUBLIC;
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "typedef %type% ("))
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "[:,] %var% ("))
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "%var% ("))
|
|
|
|
{
|
|
|
|
// member function
|
|
|
|
ClassInfo::MemberFunctionInfo func;
|
|
|
|
func._declaration = tok;
|
|
|
|
func._name = tok->str();
|
|
|
|
func._type = memberType;
|
|
|
|
|
|
|
|
_classInfoList[className]._memberFunctions.push_back(func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
void Tokenizer::setVarId()
|
|
|
|
{
|
|
|
|
// Clear all variable ids
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
tok->varId(0);
|
|
|
|
|
|
|
|
// Set variable ids..
|
|
|
|
unsigned int _varId = 0;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-03-21 21:58:39 +01:00
|
|
|
if (tok != _tokens && !Token::Match(tok, "[,;{}(] %type%"))
|
2009-02-02 18:35:46 +01:00
|
|
|
continue;
|
2009-02-02 19:59:32 +01:00
|
|
|
|
2009-03-21 21:58:39 +01:00
|
|
|
if (Token::Match(tok, "[,;{}(] %type%"))
|
2009-02-02 20:19:36 +01:00
|
|
|
tok = tok->next();
|
|
|
|
|
2009-09-22 17:49:13 +02:00
|
|
|
if (Token::Match(tok, "class|struct %type% :|{|;"))
|
|
|
|
continue;
|
|
|
|
|
2009-10-18 18:06:32 +02:00
|
|
|
if (Token::Match(tok, "else|return|typedef|delete|sizeof"))
|
2009-02-28 21:21:48 +01:00
|
|
|
continue;
|
|
|
|
|
2009-08-09 10:41:31 +02:00
|
|
|
if (Token::Match(tok, "const|static|extern|public:|private:|protected:"))
|
2009-06-18 18:30:04 +02:00
|
|
|
tok = tok->next();
|
|
|
|
|
2009-07-30 19:40:41 +02:00
|
|
|
while (Token::Match(tok, "%var% ::"))
|
2009-03-25 18:10:03 +01:00
|
|
|
tok = tok->tokAt(2);
|
|
|
|
|
2009-04-25 16:55:00 +02:00
|
|
|
// Skip template arguments..
|
|
|
|
if (Token::Match(tok, "%type% <"))
|
|
|
|
{
|
|
|
|
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;
|
2009-09-30 13:40:10 +02:00
|
|
|
bool funcDeclaration = false;
|
2009-01-24 18:15:38 +01:00
|
|
|
for (tok2 = tok->next(); tok2; tok2 = tok2->next())
|
|
|
|
{
|
2009-05-31 18:46:32 +02:00
|
|
|
if (!dot && tok2->str() == varname && !Token::Match(tok2->previous(), "struct|union"))
|
2009-01-24 18:15:38 +01:00
|
|
|
tok2->varId(_varId);
|
|
|
|
else if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel < 0)
|
|
|
|
break;
|
2009-09-30 13:40:10 +02:00
|
|
|
|
|
|
|
// We have reached the end of a loop: "for( int i;;) { }"
|
|
|
|
if (funcDeclaration && indentlevel <= 0)
|
|
|
|
break;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok2->str() == ")")
|
2009-02-16 18:41:33 +01:00
|
|
|
{
|
|
|
|
// Is this a function parameter or a variable declared in for example a for loop?
|
2009-02-17 20:18:26 +01:00
|
|
|
if (parlevel == 0 && indentlevel == 0 && Token::Match(tok2, ") const| {"))
|
2009-09-30 13:40:10 +02:00
|
|
|
funcDeclaration = true;
|
2009-02-16 18:41:33 +01:00
|
|
|
else
|
|
|
|
--parlevel;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
else if (parlevel < 0 && tok2->str() == ";")
|
|
|
|
break;
|
|
|
|
dot = bool(tok2->str() == ".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Struct/Class members
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-27 22:34:08 +02:00
|
|
|
// str.clear is a variable
|
|
|
|
// str.clear() is a member function
|
2009-01-24 18:15:38 +01:00
|
|
|
if (tok->varId() != 0 &&
|
2009-08-29 22:12:08 +02:00
|
|
|
Token::Match(tok->next(), ". %var% !!(") &&
|
2009-01-24 18:15:38 +01:00
|
|
|
tok->tokAt(2)->varId() == 0)
|
|
|
|
{
|
|
|
|
++_varId;
|
|
|
|
|
2009-08-29 22:25:14 +02:00
|
|
|
const std::string pattern(std::string("%varid% . ") + tok->strAt(2));
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
2009-08-29 22:25:14 +02:00
|
|
|
if (Token::Match(tok2, pattern.c_str(), tok->varId()))
|
2009-06-14 08:20:51 +02:00
|
|
|
tok2->tokAt(2)->varId(_varId);
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-02 22:57:18 +02:00
|
|
|
|
2009-08-07 23:49:37 +02:00
|
|
|
// Member functions in this source
|
|
|
|
std::list<Token *> allMemberFunctions;
|
|
|
|
{
|
|
|
|
const std::string funcpattern("%var% :: %var% (");
|
|
|
|
for (Token *tok2 = _tokens; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
// Found a class function..
|
|
|
|
if (Token::Match(tok2, funcpattern.c_str()))
|
|
|
|
{
|
|
|
|
allMemberFunctions.push_back(tok2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-02 22:57:18 +02:00
|
|
|
// class members..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "class %var% {"))
|
|
|
|
{
|
|
|
|
const std::string &classname(tok->next()->str());
|
|
|
|
|
|
|
|
|
|
|
|
// What member variables are there in this class?
|
2009-08-07 23:49:37 +02:00
|
|
|
std::map<std::string, unsigned int> varlist;
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
2009-05-03 14:31:54 +02:00
|
|
|
unsigned int indentlevel = 0;
|
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
2009-05-03 14:31:54 +02:00
|
|
|
// Indentation..
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a member variable..
|
|
|
|
else if (indentlevel == 1 && tok2->varId() > 0)
|
2009-08-07 23:49:37 +02:00
|
|
|
varlist[tok2->str()] = tok2->varId();
|
2009-05-02 22:57:18 +02:00
|
|
|
}
|
2009-05-03 14:31:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Are there any member variables in this class?
|
|
|
|
if (varlist.empty())
|
|
|
|
continue;
|
2009-05-02 22:57:18 +02:00
|
|
|
|
2009-05-03 14:31:54 +02:00
|
|
|
|
|
|
|
// Member functions for this class..
|
|
|
|
std::list<Token *> funclist;
|
|
|
|
{
|
|
|
|
const std::string funcpattern(classname + " :: %var% (");
|
2009-08-07 23:49:37 +02:00
|
|
|
for (std::list<Token *>::iterator func = allMemberFunctions.begin(); func != allMemberFunctions.end(); ++func)
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
2009-08-07 23:49:37 +02:00
|
|
|
Token *tok2 = *func;
|
|
|
|
|
2009-05-03 14:31:54 +02:00
|
|
|
// Found a class function..
|
|
|
|
if (Token::Match(tok2, funcpattern.c_str()))
|
|
|
|
{
|
|
|
|
// Goto the end paranthesis..
|
2009-08-29 22:17:01 +02:00
|
|
|
tok2 = tok2->tokAt(3)->link();
|
2009-05-03 14:31:54 +02:00
|
|
|
if (!tok2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If this is a function implementation.. add it to funclist
|
|
|
|
if (Token::Match(tok2, ") const|volatile| {"))
|
|
|
|
funclist.push_back(tok2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are there any member functions for this class?
|
|
|
|
if (funclist.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Update the variable ids..
|
2009-08-07 23:49:37 +02:00
|
|
|
// Parse each function..
|
|
|
|
for (std::list<Token *>::iterator func = funclist.begin(); func != funclist.end(); ++func)
|
2009-05-03 14:31:54 +02:00
|
|
|
{
|
2009-08-07 23:49:37 +02:00
|
|
|
unsigned int indentlevel = 0;
|
|
|
|
for (Token *tok2 = *func; tok2; tok2 = tok2->next())
|
2009-05-03 14:31:54 +02:00
|
|
|
{
|
2009-08-07 23:49:37 +02:00
|
|
|
if (tok2->str() == "{")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == "}")
|
2009-05-02 22:57:18 +02:00
|
|
|
{
|
2009-08-07 23:49:37 +02:00
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
else if (indentlevel > 0 &&
|
|
|
|
tok2->varId() == 0 &&
|
|
|
|
varlist.find(tok2->str()) != varlist.end())
|
|
|
|
{
|
|
|
|
tok2->varId(varlist[tok2->str()]);
|
2009-05-02 22:57:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-07 23:49:37 +02:00
|
|
|
|
2009-05-02 22:57:18 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Simplify token list
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2009-03-13 00:07:05 +01:00
|
|
|
void Tokenizer::simplifyNamespaces()
|
|
|
|
{
|
|
|
|
for (Token *token = _tokens; token; token = token->next())
|
|
|
|
{
|
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-09-20 22:13:06 +02:00
|
|
|
std::list<Token*> links3;
|
2009-03-13 22:25:56 +01:00
|
|
|
for (Token *token = _tokens; token; token = token->next())
|
|
|
|
{
|
2009-03-15 00:39:45 +01:00
|
|
|
if (token->link())
|
|
|
|
{
|
|
|
|
token->link(0);
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:25:56 +01:00
|
|
|
if (token->str() == "{")
|
|
|
|
{
|
|
|
|
links.push_back(token);
|
|
|
|
}
|
|
|
|
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-09-20 22:13:06 +02:00
|
|
|
else if (token->str() == "[")
|
|
|
|
{
|
|
|
|
links3.push_back(token);
|
|
|
|
}
|
|
|
|
else if (token->str() == "]")
|
|
|
|
{
|
|
|
|
if (links3.size() == 0)
|
|
|
|
{
|
2009-09-22 18:36:12 +02:00
|
|
|
// Error, [ and ] don't match.
|
2009-09-20 22:13:06 +02:00
|
|
|
syntaxError(token, '[');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::createMutualLinks(links3.back(), token);
|
|
|
|
links3.pop_back();
|
|
|
|
}
|
2009-03-13 22:25:56 +01:00
|
|
|
}
|
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
if (links.size() > 0)
|
|
|
|
{
|
|
|
|
// Error, { and } don't match.
|
2009-11-03 21:21:28 +01:00
|
|
|
syntaxError(links.back(), '{');
|
2009-05-07 22:17:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (links2.size() > 0)
|
2009-03-13 22:25:56 +01:00
|
|
|
{
|
2009-06-12 15:47:15 +02:00
|
|
|
// Error, ( and ) don't match.
|
2009-11-03 21:21:28 +01:00
|
|
|
syntaxError(links2.back(), '(');
|
2009-03-15 00:39:45 +01:00
|
|
|
return false;
|
2009-03-13 22:25:56 +01:00
|
|
|
}
|
|
|
|
|
2009-09-20 22:13:06 +02:00
|
|
|
if (links3.size() > 0)
|
|
|
|
{
|
|
|
|
// Error, [ and ] don't match.
|
2009-11-03 21:21:28 +01:00
|
|
|
syntaxError(links3.back(), '[');
|
2009-09-20 22:13:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-15 00:39:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
void Tokenizer::simplifySizeof()
|
2009-03-15 00:39:45 +01:00
|
|
|
{
|
2009-01-24 18:15:38 +01:00
|
|
|
// Fill the map _typeSize..
|
|
|
|
_typeSize.clear();
|
|
|
|
_typeSize["char"] = sizeof(char);
|
|
|
|
_typeSize["short"] = sizeof(short);
|
|
|
|
_typeSize["int"] = sizeof(int);
|
|
|
|
_typeSize["long"] = sizeof(long);
|
|
|
|
_typeSize["float"] = sizeof(float);
|
|
|
|
_typeSize["double"] = sizeof(double);
|
2009-11-12 22:31:13 +01:00
|
|
|
_typeSize["size_t"] = sizeof(size_t);
|
2009-05-31 21:33:44 +02:00
|
|
|
_typeSize["*"] = sizeof(void *);
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-02-24 17:30:57 +01:00
|
|
|
if (Token::Match(tok, "class|struct %var%"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-02-24 17:30:57 +01:00
|
|
|
_typeSize[tok->strAt(1)] = 100;
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
// Locate variable declarations and calculate the size
|
|
|
|
std::map<unsigned int, std::string> sizeOfVar;
|
2009-02-28 09:34:02 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-10-07 09:54:34 +02:00
|
|
|
if (tok->varId() != 0 && sizeOfVar.find(tok->varId()) == sizeOfVar.end())
|
2009-06-01 11:27:04 +02:00
|
|
|
{
|
2009-10-07 09:54:34 +02:00
|
|
|
const unsigned int varId = tok->varId();
|
2009-10-07 11:05:44 +02:00
|
|
|
if (Token::Match(tok->tokAt(-3), "[;{}(,] %type% * %var% [;,)]") ||
|
|
|
|
Token::Match(tok->tokAt(-4), "[;{}(,] const %type% * %var% [;),]") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "[;{}(,] %type% %var% [;),]") ||
|
|
|
|
Token::Match(tok->tokAt(-3), "[;{}(,] const %type% %var% [;),]"))
|
2009-02-28 09:34:02 +01:00
|
|
|
{
|
2009-10-10 21:54:58 +02:00
|
|
|
const unsigned int size = sizeOfType(tok->previous());
|
|
|
|
if (size == 0)
|
2009-10-10 14:26:03 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sizeOfVar[varId] = MathLib::toString<long>(size);
|
2009-06-01 11:27:04 +02:00
|
|
|
}
|
2009-10-07 09:54:34 +02:00
|
|
|
|
|
|
|
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ %num% ] [;=]") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [;=]"))
|
2009-06-01 11:27:04 +02:00
|
|
|
{
|
2009-10-10 21:54:58 +02:00
|
|
|
unsigned int size = sizeOfType(tok->tokAt(-1));
|
|
|
|
if (size == 0)
|
2009-10-07 09:54:34 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
sizeOfVar[varId] = MathLib::toString<long>(size * MathLib::toLongNumber(tok->strAt(2)));
|
|
|
|
}
|
|
|
|
|
2009-10-07 11:05:44 +02:00
|
|
|
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ %num% ] [,)]") ||
|
|
|
|
Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [,)]"))
|
|
|
|
{
|
|
|
|
Token tempTok;
|
|
|
|
tempTok.str("*");
|
|
|
|
sizeOfVar[varId] = MathLib::toString<long>(sizeOfType(&tempTok));
|
|
|
|
}
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ ] = %str% ;"))
|
|
|
|
{
|
2009-10-10 21:54:58 +02:00
|
|
|
unsigned int size = sizeOfType(tok->tokAt(4));
|
|
|
|
if (size == 0)
|
2009-10-07 09:54:34 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
sizeOfVar[varId] = MathLib::toString<long>(size);
|
2009-02-28 09:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() != "sizeof")
|
|
|
|
continue;
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
// sizeof "text"
|
|
|
|
if (Token::Match(tok->next(), "%str%"))
|
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << (Token::getStrLength(tok) + 1);
|
|
|
|
tok->str(ostr.str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sizeof ("text")
|
|
|
|
if (Token::Match(tok->next(), "( %str% )"))
|
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << (Token::getStrLength(tok) + 1);
|
|
|
|
tok->str(ostr.str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sizeof int -> sizeof( int )
|
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-09-20 22:38:32 +02:00
|
|
|
while (tempToken->next()->str() == "[")
|
|
|
|
{
|
|
|
|
tempToken = tempToken->next()->link();
|
|
|
|
}
|
|
|
|
|
2009-07-27 11:24:24 +02:00
|
|
|
if (tempToken->next()->str() == ".")
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
|
|
|
// We are checking a class or struct, search next varname
|
|
|
|
tempToken = tempToken->tokAt(1);
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-21 17:55:52 +02:00
|
|
|
else if (Token::simpleMatch(tempToken->next(), "- >"))
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
|
|
|
// We are checking a class or struct, search next varname
|
|
|
|
tempToken = tempToken->tokAt(2);
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-22 17:03:42 +02:00
|
|
|
else if (Token::Match(tempToken->next(), "++|--"))
|
2009-01-27 21:47:00 +01:00
|
|
|
{
|
|
|
|
// We have variable++ or variable--, there should be
|
|
|
|
// nothing after this
|
|
|
|
tempToken = tempToken->tokAt(2);
|
|
|
|
}
|
2009-11-13 20:07:44 +01:00
|
|
|
else if (Token::simpleMatch(tempToken->next(), ") ."))
|
|
|
|
{
|
|
|
|
tempToken = tempToken->tokAt(2);
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-27 21:47:00 +01:00
|
|
|
|
|
|
|
// Ok, we should be clean. Add ) after tempToken
|
|
|
|
tok->insertToken("(");
|
|
|
|
tempToken->insertToken(")");
|
2009-08-22 16:22:50 +02:00
|
|
|
Token::createMutualLinks(tok->next(), tempToken->next());
|
2009-01-27 21:47:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 12:01:45 +02:00
|
|
|
// sizeof(type *) => sizeof(*)
|
2009-10-07 09:54:34 +02:00
|
|
|
if (Token::Match(tok->next(), "( %type% *)"))
|
2009-06-01 12:01:45 +02:00
|
|
|
{
|
|
|
|
tok->next()->deleteNext();
|
2009-10-07 09:54:34 +02:00
|
|
|
continue;
|
2009-06-01 12:01:45 +02:00
|
|
|
}
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
if (Token::Match(tok->next(), "( * )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-10-07 09:54:34 +02:00
|
|
|
tok->str(MathLib::toString<long>(sizeOfType(tok->tokAt(2))));
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(4));
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-10-07 09:54:34 +02:00
|
|
|
// sizeof( a )
|
|
|
|
else if (Token::Match(tok->next(), "( %var% )") && tok->tokAt(2)->varId() != 0)
|
2009-06-05 04:50:06 +02:00
|
|
|
{
|
2009-10-07 09:54:34 +02:00
|
|
|
if (sizeOfVar.find(tok->tokAt(2)->varId()) != sizeOfVar.end())
|
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->str(sizeOfVar[tok->varId()]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// don't try to replace size of variable if variable has
|
|
|
|
// similar name with type (#329)
|
|
|
|
}
|
2009-06-05 04:50:06 +02:00
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
else if (Token::Match(tok, "sizeof ( %type% )"))
|
|
|
|
{
|
2009-10-10 21:54:58 +02:00
|
|
|
unsigned int size = sizeOfType(tok->tokAt(2));
|
2009-01-24 18:15:38 +01:00
|
|
|
if (size > 0)
|
|
|
|
{
|
2009-08-22 13:44:12 +02:00
|
|
|
tok->str(MathLib::toString<long>(size));
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(4));
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-02 19:59:32 +01:00
|
|
|
else if (Token::Match(tok, "sizeof ( * %var% )") || Token::Match(tok, "sizeof ( %var% [ %num% ] )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-01-28 18:38:32 +01:00
|
|
|
// Some default value..
|
2009-10-21 21:08:00 +02:00
|
|
|
unsigned int sz = 0;
|
2009-01-28 18:38:32 +01:00
|
|
|
|
2009-02-02 19:59:32 +01:00
|
|
|
unsigned int varid = tok->tokAt((tok->tokAt(2)->str() == "*") ? 3 : 2)->varId();
|
2009-02-03 22:42:50 +01:00
|
|
|
if (varid != 0)
|
2009-01-28 18:38:32 +01:00
|
|
|
{
|
2009-02-03 22:42:50 +01:00
|
|
|
// Try to locate variable declaration..
|
2009-10-21 21:08:00 +02:00
|
|
|
const Token *decltok = Token::findmatch(_tokens, "%varid%", varid);
|
2009-10-22 23:00:27 +02:00
|
|
|
if (Token::Match(decltok->previous(), "%type% %var% ["))
|
2009-02-03 22:42:50 +01:00
|
|
|
{
|
2009-10-21 21:08:00 +02:00
|
|
|
sz = sizeOfType(decltok->previous());
|
|
|
|
}
|
2009-10-22 23:00:27 +02:00
|
|
|
else if (Token::Match(decltok->previous(), "* %var% ["))
|
2009-10-21 21:08:00 +02:00
|
|
|
{
|
|
|
|
sz = sizeOfType(decltok->previous());
|
|
|
|
}
|
2009-10-22 23:00:27 +02:00
|
|
|
else if (Token::Match(decltok->tokAt(-2), "%type% * %var%"))
|
2009-10-21 21:08:00 +02:00
|
|
|
{
|
|
|
|
sz = sizeOfType(decltok->tokAt(-2));
|
2009-02-03 22:42:50 +01:00
|
|
|
}
|
2009-01-28 18:38:32 +01:00
|
|
|
}
|
|
|
|
|
2009-03-29 16:36:34 +02:00
|
|
|
if (sz > 0)
|
|
|
|
{
|
2009-08-22 13:44:12 +02:00
|
|
|
tok->str(MathLib::toString<long>(sz));
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
2009-03-29 16:36:34 +02:00
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
}
|
|
|
|
|
2009-11-10 22:10:56 +01:00
|
|
|
bool Tokenizer::simplifyTokenList()
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
2009-11-05 21:30:05 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::simpleMatch(tok, "* const"))
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
simplifyNamespaces();
|
|
|
|
|
|
|
|
simplifyGoto();
|
|
|
|
|
|
|
|
// Combine wide strings
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
while (tok->str() == "L" && tok->next() && tok->next()->str()[0] == '"')
|
|
|
|
{
|
|
|
|
// Combine 'L "string"'
|
|
|
|
tok->str(tok->next()->str());
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine strings
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-09-27 16:04:10 +02:00
|
|
|
if (tok->str()[0] != '"')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tok->str(simplifyString(tok->str()));
|
|
|
|
while (tok->next() && tok->next()->str()[0] == '"')
|
2009-09-06 13:22:01 +02:00
|
|
|
{
|
2009-09-27 16:04:10 +02:00
|
|
|
tok->next()->str(simplifyString(tok->next()->str()));
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
// Two strings after each other, combine them
|
|
|
|
tok->concatStr(tok->next()->str());
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-12 22:54:47 +02:00
|
|
|
// Convert e.g. atol("0") into 0
|
|
|
|
simplifyMathFunctions();
|
|
|
|
|
2009-11-14 11:59:05 +01:00
|
|
|
// simplify casts before removing "unsigned" keywords (to fix #961)
|
|
|
|
simplifyCasts();
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
// Remove unwanted keywords
|
|
|
|
static const char * const unwantedWords[] = { "unsigned", "unlikely", "likely" };
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
for (unsigned ui = 0; ui < sizeof(unwantedWords) / sizeof(unwantedWords[0]) && tok->next(); ui++)
|
|
|
|
{
|
|
|
|
if (tok->next()->str() == unwantedWords[ui])
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Token::simpleMatch(tok->next(), "__builtin_expect ("))
|
|
|
|
{
|
|
|
|
unsigned int parlevel = 0;
|
|
|
|
for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel <= 1)
|
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
if (parlevel == 1 && tok2->str() == ",")
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2, ", %num% )"))
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
Token::eraseTokens(tok2->previous(), tok2->tokAt(2));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert + + into + and + - into -
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
while (tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "+")
|
|
|
|
{
|
|
|
|
if (tok->next()->str() == "+")
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->next()->str() == "-")
|
|
|
|
{
|
|
|
|
tok->str("-");
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tok->str() == "-")
|
|
|
|
{
|
|
|
|
if (tok->next()->str() == "-")
|
|
|
|
{
|
|
|
|
tok->str("+");
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tok->next()->str() == "+")
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-04 22:33:41 +02:00
|
|
|
// 0[a] -> a[0]
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "%num% [ %var% ]"))
|
|
|
|
{
|
|
|
|
const std::string temp = tok->str();
|
|
|
|
tok->str(tok->tokAt(2)->str());
|
|
|
|
tok->tokAt(2)->str(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-06 13:22:01 +02:00
|
|
|
simplifySizeof();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-11-07 08:25:39 +01:00
|
|
|
// replace strlen(str)
|
|
|
|
simplifyKnownVariables();
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "strlen ( %str% )"))
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << Token::getStrLength(tok->tokAt(2));
|
|
|
|
tok->str(ostr.str());
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-07 10:08:53 +02:00
|
|
|
// change array to pointer..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "%type% %var% [ ] [,;=]"))
|
|
|
|
{
|
|
|
|
Token::eraseTokens(tok->next(), tok->tokAt(4));
|
|
|
|
tok->insertToken("*");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-14 22:33:28 +01:00
|
|
|
// Replace constants..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "const %type% %var% = %num% ;"))
|
|
|
|
{
|
2009-06-19 18:03:09 +02:00
|
|
|
unsigned int varId = tok->tokAt(2)->varId();
|
2009-08-01 15:57:54 +02:00
|
|
|
if (varId == 0)
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(5);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-02-14 22:33:28 +01:00
|
|
|
const char *num = tok->strAt(4);
|
|
|
|
int indent = 1;
|
|
|
|
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
++indent;
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "}")
|
|
|
|
{
|
|
|
|
--indent;
|
|
|
|
if (indent == 0)
|
|
|
|
break;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-02-14 22:33:28 +01:00
|
|
|
// Compare constants, but don't touch members of other structures
|
2009-06-19 18:03:09 +02:00
|
|
|
else if (tok2->varId() == varId)
|
2009-02-14 22:33:28 +01:00
|
|
|
{
|
|
|
|
tok2->str(num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-08-29 15:29:19 +02:00
|
|
|
simplifyLogicalOperators();
|
2009-03-05 20:32:02 +01:00
|
|
|
|
2009-03-04 07:24:03 +01:00
|
|
|
// Simplify simple calculations..
|
|
|
|
while (simplifyCalculations())
|
|
|
|
;
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Replace "*(str + num)" => "str[num]"
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-03 20:10:59 +02:00
|
|
|
if (! strchr(";{}(=<>", tok->str()[0]))
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Token *next = tok->next();
|
|
|
|
if (! next)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (Token::Match(next, "* ( %var% + %num% )"))
|
|
|
|
{
|
|
|
|
const char *str[4] = {"var", "[", "num", "]"};
|
|
|
|
str[0] = tok->strAt(3);
|
|
|
|
str[2] = tok->strAt(5);
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
tok->str(str[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
2009-11-11 19:16:05 +01:00
|
|
|
|
|
|
|
Token::createMutualLinks(next->tokAt(1), next->tokAt(3));
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-18 20:32:05 +01:00
|
|
|
// Simplify variable declarations
|
|
|
|
simplifyVarDecl();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Replace NULL with 0..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-09-23 21:44:52 +02:00
|
|
|
if (tok->str() == "NULL" || tok->str() == "'\\0'")
|
|
|
|
{
|
2009-01-24 18:15:38 +01:00
|
|
|
tok->str("0");
|
2009-09-23 21:44:52 +02:00
|
|
|
}
|
|
|
|
else if (tok->isNumber() &&
|
|
|
|
MathLib::isInt(tok->str()) &&
|
|
|
|
MathLib::toLongNumber(tok->str()) == 0)
|
|
|
|
{
|
|
|
|
tok->str("0");
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace pointer casts of 0.. "(char *)0" => "0"
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
if (Token::Match(tok->next(), "( %type% * ) 0") ||
|
|
|
|
Token::Match(tok->next(), "( %type% %type% * ) 0"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
simplifyFunctionParameters();
|
2009-02-17 21:03:08 +01:00
|
|
|
elseif();
|
2009-11-04 23:58:15 +01:00
|
|
|
simplifyIfAssign();
|
2009-10-05 13:10:09 +02:00
|
|
|
simplifyRedundantParanthesis();
|
2009-03-24 18:23:21 +01:00
|
|
|
simplifyIfNot();
|
2009-07-25 13:11:29 +02:00
|
|
|
simplifyIfNotNull();
|
2009-11-04 23:58:15 +01:00
|
|
|
simplifyComparisonOrder();
|
2009-09-05 18:46:27 +02:00
|
|
|
simplifyNestedStrcat();
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "case %any% : %var%"))
|
2009-06-14 08:20:51 +02:00
|
|
|
tok->tokAt(2)->insertToken(";");
|
2009-01-24 18:15:38 +01:00
|
|
|
if (Token::Match(tok, "default : %var%"))
|
|
|
|
tok->next()->insertToken(";");
|
|
|
|
}
|
|
|
|
|
2009-03-23 18:20:56 +01:00
|
|
|
// In case variable declarations have been updated...
|
|
|
|
setVarId();
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
bool modified = true;
|
|
|
|
while (modified)
|
|
|
|
{
|
|
|
|
modified = false;
|
|
|
|
modified |= simplifyConditions();
|
|
|
|
modified |= simplifyFunctionReturn();
|
|
|
|
modified |= simplifyKnownVariables();
|
|
|
|
modified |= removeReduntantConditions();
|
2009-01-26 17:38:08 +01:00
|
|
|
modified |= simplifyRedundantParanthesis();
|
2009-06-05 23:33:13 +02:00
|
|
|
modified |= simplifyQuestionMark();
|
2009-10-05 16:34:38 +02:00
|
|
|
modified |= simplifyCalculations();
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
2009-03-15 00:39:45 +01:00
|
|
|
|
2009-07-24 21:55:35 +02:00
|
|
|
// Remove redundant parantheses in return..
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
while (Token::simpleMatch(tok, "return ("))
|
|
|
|
{
|
2009-08-30 10:27:26 +02:00
|
|
|
Token *tok2 = tok->next()->link();
|
|
|
|
if (Token::simpleMatch(tok2, ") ;"))
|
2009-07-24 21:55:35 +02:00
|
|
|
{
|
2009-08-30 10:27:26 +02:00
|
|
|
tok->deleteNext();
|
|
|
|
tok2->deleteThis();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
2009-07-24 21:55:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-05 20:29:09 +02:00
|
|
|
simplifyComma();
|
2009-07-13 19:11:31 +02:00
|
|
|
if (_settings && _settings->_debug)
|
2009-03-16 22:31:52 +01:00
|
|
|
{
|
|
|
|
_tokens->printOut();
|
|
|
|
}
|
2009-11-10 22:10:56 +01:00
|
|
|
|
|
|
|
return validate();
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool Tokenizer::removeReduntantConditions()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-07-27 11:24:24 +02:00
|
|
|
if (tok->str() != "if")
|
2009-01-24 18:15:38 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Token::Match(tok->tokAt(1), "( %bool% ) {"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Find matching else
|
|
|
|
const Token *elseTag = 0;
|
|
|
|
|
|
|
|
// Find the closing "}"
|
2009-10-05 14:06:50 +02:00
|
|
|
elseTag = tok->tokAt(4)->link()->next();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
bool boolValue = false;
|
|
|
|
if (tok->tokAt(2)->str() == "true")
|
|
|
|
boolValue = true;
|
|
|
|
|
|
|
|
// Handle if with else
|
|
|
|
if (elseTag && elseTag->str() == "else")
|
|
|
|
{
|
2009-10-05 14:06:50 +02:00
|
|
|
if (Token::simpleMatch(elseTag->next(), "if ("))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Handle "else if"
|
|
|
|
if (boolValue == false)
|
|
|
|
{
|
|
|
|
// Convert "if( false ) {aaa;} else if() {bbb;}" => "if() {bbb;}"
|
|
|
|
Token::eraseTokens(tok, elseTag->tokAt(2));
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Keep first if, remove every else if and else after it
|
|
|
|
const Token *lastTagInIf = elseTag->tokAt(2);
|
|
|
|
while (lastTagInIf)
|
|
|
|
{
|
|
|
|
if (lastTagInIf->str() == "(")
|
|
|
|
{
|
2009-10-05 14:06:50 +02:00
|
|
|
lastTagInIf = lastTagInIf->link()->next();
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-10-05 14:06:50 +02:00
|
|
|
lastTagInIf = lastTagInIf->link()->next();
|
|
|
|
if (!Token::simpleMatch(lastTagInIf, "else"))
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
lastTagInIf = lastTagInIf->next();
|
2009-07-27 11:24:24 +02:00
|
|
|
if (lastTagInIf->str() == "if")
|
2009-01-24 18:15:38 +01:00
|
|
|
lastTagInIf = lastTagInIf->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::eraseTokens(elseTag->previous(), lastTagInIf);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Handle else
|
|
|
|
if (boolValue == false)
|
|
|
|
{
|
|
|
|
// Convert "if( false ) {aaa;} else {bbb;}" => "{bbb;}" or ";{bbb;}"
|
|
|
|
if (tok->previous())
|
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, elseTag->tokAt(1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-27 11:24:24 +02:00
|
|
|
if (elseTag->tokAt(1)->str() == "{")
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
// Convert "if( true ) {aaa;} else {bbb;}" => "{aaa;}"
|
2009-10-05 14:06:50 +02:00
|
|
|
const Token *end = elseTag->tokAt(1)->link();
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Remove the "else { aaa; }"
|
|
|
|
Token::eraseTokens(elseTag->previous(), end->tokAt(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove "if( true )"
|
|
|
|
if (tok->previous())
|
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle if without else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (boolValue == false)
|
|
|
|
{
|
|
|
|
// Remove if and its content
|
|
|
|
if (tok->previous())
|
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, elseTag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// convert "if( true ) {aaa;}" => "{aaa;}"
|
|
|
|
if (tok->previous())
|
|
|
|
tok = tok->previous();
|
|
|
|
else
|
|
|
|
tok->str(";");
|
|
|
|
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-08-29 21:25:15 +02:00
|
|
|
void Tokenizer::simplifyIfAddBraces()
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok ? tok->next() : NULL)
|
|
|
|
{
|
|
|
|
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-08-29 21:30:13 +02:00
|
|
|
void Tokenizer::simplifyConditionOperator()
|
2009-07-14 12:06:38 +02:00
|
|
|
{
|
|
|
|
int parlevel = 0;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok->str() == ")")
|
|
|
|
--parlevel;
|
|
|
|
else if (parlevel == 0 && Token::Match(tok, "; %var% = %var% ? %var% : %var% ;"))
|
|
|
|
{
|
|
|
|
const std::string var(tok->strAt(1));
|
|
|
|
const std::string condition(tok->strAt(3));
|
|
|
|
const std::string value1(tok->strAt(5));
|
|
|
|
const std::string value2(tok->strAt(7));
|
|
|
|
|
2009-09-29 22:27:10 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(9));
|
2009-07-14 12:06:38 +02:00
|
|
|
|
2009-09-29 22:27:10 +02:00
|
|
|
std::string str("if ( " + condition + " ) { " + var + " = " + value1 + " ; } else { " + var + " = " + value2 + " ; }");
|
2009-07-14 12:06:38 +02:00
|
|
|
std::string::size_type pos1 = 0;
|
|
|
|
while (pos1 != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string::size_type pos2 = str.find(" ", pos1);
|
|
|
|
if (pos2 == std::string::npos)
|
|
|
|
{
|
|
|
|
tok->insertToken(str.substr(pos1).c_str());
|
|
|
|
pos1 = pos2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-16 07:20:29 +02:00
|
|
|
tok->insertToken(str.substr(pos1, pos2 - pos1).c_str());
|
2009-07-14 12:06:38 +02:00
|
|
|
pos1 = pos2 + 1;
|
|
|
|
}
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
2009-08-23 02:38:00 +02:00
|
|
|
|
2009-09-29 22:27:10 +02:00
|
|
|
Token::createMutualLinks(tok->tokAt(-15), tok->tokAt(-13));
|
|
|
|
Token::createMutualLinks(tok->tokAt(-12), tok->tokAt(-7));
|
|
|
|
Token::createMutualLinks(tok->tokAt(-5), tok);
|
2009-07-14 12:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
bool Tokenizer::simplifyConditions()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-06-03 21:02:16 +02:00
|
|
|
if (Token::Match(tok, "! %num%") || Token::Match(tok, "! %bool%"))
|
|
|
|
{
|
|
|
|
if (tok->next()->str() == "0" || tok->next()->str() == "false")
|
|
|
|
tok->str("true");
|
|
|
|
else
|
|
|
|
tok->str("false");
|
|
|
|
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2009-08-22 16:04:58 +02:00
|
|
|
if (Token::simpleMatch(tok, "( true &&") ||
|
|
|
|
Token::simpleMatch(tok, "&& true &&") ||
|
|
|
|
Token::simpleMatch(tok->next(), "&& true )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-01-24 18:15:38 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2009-08-22 16:04:58 +02:00
|
|
|
else if (Token::simpleMatch(tok, "( false ||") ||
|
|
|
|
Token::simpleMatch(tok, "|| false ||") ||
|
|
|
|
Token::simpleMatch(tok->next(), "|| false )"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-01-24 18:15:38 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change numeric constant in condition to "true" or "false"
|
2009-02-07 11:54:39 +01:00
|
|
|
if (Token::Match(tok, "if|while ( %num%") &&
|
|
|
|
(tok->tokAt(3)->str() == ")" || tok->tokAt(3)->str() == "||" || tok->tokAt(3)->str() == "&&"))
|
|
|
|
{
|
2009-06-14 08:20:51 +02:00
|
|
|
tok->tokAt(2)->str((tok->tokAt(2)->str() != "0") ? "true" : "false");
|
2009-02-07 11:54:39 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
Token *tok2 = tok->tokAt(2);
|
|
|
|
if (tok2 &&
|
|
|
|
(tok->str() == "&&" || tok->str() == "||") &&
|
|
|
|
Token::Match(tok->next(), "%num%") &&
|
2009-01-24 18:15:38 +01:00
|
|
|
(tok2->str() == ")" || tok2->str() == "&&" || tok2->str() == "||"))
|
|
|
|
{
|
2009-02-07 20:15:10 +01:00
|
|
|
tok->next()->str((tok->next()->str() != "0") ? "true" : "false");
|
2009-01-24 18:15:38 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "(%num% == %num%)" => "(true)"/"(false)"
|
|
|
|
const Token *tok4 = tok->tokAt(4);
|
|
|
|
if (! tok4)
|
|
|
|
break;
|
|
|
|
if ((tok->str() == "&&" || tok->str() == "||" || tok->str() == "(") &&
|
2009-05-29 23:04:01 +02:00
|
|
|
(Token::Match(tok->tokAt(1), "%num% %any% %num%") ||
|
|
|
|
Token::Match(tok->tokAt(1), "%bool% %any% %bool%")) &&
|
2009-10-06 11:44:53 +02:00
|
|
|
(tok4->str() == "&&" || tok4->str() == "||" || tok4->str() == ")" || tok4->str() == "?"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
std::string cmp = tok->strAt(2);
|
|
|
|
bool result = false;
|
2009-05-29 23:04:01 +02:00
|
|
|
if (Token::Match(tok->tokAt(1), "%num%"))
|
|
|
|
{
|
|
|
|
// Compare numbers
|
|
|
|
double op1 = (strstr(tok->strAt(1), "0x")) ? std::strtol(tok->strAt(1), 0, 16) : std::atof(tok->strAt(1));
|
|
|
|
double op2 = (strstr(tok->strAt(3), "0x")) ? std::strtol(tok->strAt(3), 0, 16) : std::atof(tok->strAt(3));
|
|
|
|
|
|
|
|
if (cmp == "==")
|
|
|
|
result = (op1 == op2);
|
|
|
|
else if (cmp == "!=")
|
|
|
|
result = (op1 != op2);
|
|
|
|
else if (cmp == ">=")
|
|
|
|
result = (op1 >= op2);
|
|
|
|
else if (cmp == ">")
|
|
|
|
result = (op1 > op2);
|
|
|
|
else if (cmp == "<=")
|
|
|
|
result = (op1 <= op2);
|
|
|
|
else if (cmp == "<")
|
|
|
|
result = (op1 < op2);
|
|
|
|
else
|
|
|
|
cmp = "";
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
else
|
2009-05-29 23:04:01 +02:00
|
|
|
{
|
|
|
|
// Compare boolean
|
|
|
|
bool op1 = (tok->strAt(1) == std::string("true"));
|
|
|
|
bool op2 = (tok->strAt(3) == std::string("true"));
|
|
|
|
|
|
|
|
if (cmp == "==")
|
|
|
|
result = (op1 == op2);
|
|
|
|
else if (cmp == "!=")
|
|
|
|
result = (op1 != op2);
|
|
|
|
else if (cmp == ">=")
|
|
|
|
result = (op1 >= op2);
|
|
|
|
else if (cmp == ">")
|
|
|
|
result = (op1 > op2);
|
|
|
|
else if (cmp == "<=")
|
|
|
|
result = (op1 <= op2);
|
|
|
|
else if (cmp == "<")
|
|
|
|
result = (op1 < op2);
|
|
|
|
else
|
|
|
|
cmp = "";
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
if (! cmp.empty())
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
|
|
|
|
tok->str(result ? "true" : "false");
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-06-05 23:33:13 +02:00
|
|
|
bool Tokenizer::simplifyQuestionMark()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() != "?")
|
|
|
|
continue;
|
|
|
|
|
2009-10-05 16:34:38 +02:00
|
|
|
if (!tok->tokAt(-2))
|
2009-06-05 23:33:13 +02:00
|
|
|
continue;
|
|
|
|
|
2009-06-14 08:26:20 +02:00
|
|
|
if (!Token::Match(tok->tokAt(-2), "[=,(]"))
|
2009-06-05 23:33:13 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Token::Match(tok->previous(), "%bool%") &&
|
|
|
|
!Token::Match(tok->previous(), "%num%"))
|
|
|
|
continue;
|
|
|
|
|
2009-07-23 10:12:02 +02:00
|
|
|
// Find the ":" token..
|
|
|
|
Token *semicolon = 0;
|
2009-06-05 23:33:13 +02:00
|
|
|
{
|
2009-07-22 13:29:42 +02:00
|
|
|
unsigned int parlevel = 0;
|
2009-07-23 10:12:02 +02:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
2009-07-22 13:29:42 +02:00
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel == 0)
|
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
else if (parlevel == 0 && tok2->str() == ":")
|
|
|
|
{
|
2009-07-23 10:12:02 +02:00
|
|
|
semicolon = tok2;
|
2009-07-22 13:29:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-23 10:12:02 +02:00
|
|
|
}
|
|
|
|
if (!semicolon || !semicolon->next())
|
|
|
|
continue;
|
2009-07-22 13:29:42 +02:00
|
|
|
|
2009-07-23 10:12:02 +02:00
|
|
|
if (tok->previous()->str() == "false" ||
|
|
|
|
tok->previous()->str() == "0")
|
|
|
|
{
|
|
|
|
// Use code after semicolon, remove code before it.
|
|
|
|
semicolon = semicolon->next();
|
2009-06-14 08:26:20 +02:00
|
|
|
tok = tok->tokAt(-2);
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, semicolon);
|
2009-06-05 23:33:13 +02:00
|
|
|
|
|
|
|
tok = tok->next();
|
2009-06-12 23:17:06 +02:00
|
|
|
ret = true;
|
2009-06-05 23:33:13 +02:00
|
|
|
}
|
2009-07-23 10:12:02 +02:00
|
|
|
|
|
|
|
// The condition is true. Delete the operator after the ":"..
|
2009-10-05 16:34:38 +02:00
|
|
|
else
|
2009-06-05 23:33:13 +02:00
|
|
|
{
|
2009-07-23 10:12:02 +02:00
|
|
|
// delete the condition token and the "?"
|
|
|
|
tok = tok->tokAt(-2);
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-10-05 16:34:38 +02:00
|
|
|
int ind = 0;
|
|
|
|
for (const Token *end = semicolon; end; end = end->next())
|
|
|
|
{
|
|
|
|
if (end->str() == ";")
|
|
|
|
{
|
|
|
|
Token::eraseTokens(semicolon->previous(), end->next());
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-23 10:12:02 +02:00
|
|
|
|
2009-10-05 16:34:38 +02:00
|
|
|
else if (end->str() == "(")
|
|
|
|
{
|
|
|
|
++ind;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (end->str() == ")")
|
|
|
|
{
|
|
|
|
--ind;
|
|
|
|
if (ind < 0)
|
|
|
|
{
|
|
|
|
Token::eraseTokens(semicolon->previous(), end);
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-05 23:33:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
2009-08-29 21:06:14 +02:00
|
|
|
void Tokenizer::simplifyCasts()
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-10-04 23:12:12 +02:00
|
|
|
while (Token::Match(tok->next(), "( %type% *| ) *|&| %var%") ||
|
|
|
|
Token::Match(tok->next(), "( %type% %type% *| ) *|&| %var%"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-03-09 19:47:21 +01:00
|
|
|
if (tok->isName() && tok->str() != "return")
|
2009-10-04 23:12:12 +02:00
|
|
|
break;
|
2009-03-10 21:26:08 +01:00
|
|
|
|
2009-10-29 20:54:51 +01:00
|
|
|
if (Token::simpleMatch(tok->previous(), "operator"))
|
|
|
|
break;
|
|
|
|
|
2009-03-10 21:26:08 +01:00
|
|
|
// Remove cast..
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->next()->link()->next());
|
2009-10-04 23:12:12 +02:00
|
|
|
|
|
|
|
if (tok->str() == ")" && tok->link()->previous())
|
|
|
|
{
|
|
|
|
// If there was another cast before this, go back
|
|
|
|
// there to check it also. e.g. "(int)(char)x"
|
|
|
|
tok = tok->link()->previous();
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
|
2009-10-04 23:12:12 +02:00
|
|
|
if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <"))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-06-19 19:25:56 +02:00
|
|
|
Token *tok2 = tok->next();
|
2009-05-27 20:06:19 +02:00
|
|
|
unsigned int level = 0;
|
2009-06-19 19:25:56 +02:00
|
|
|
while (tok2)
|
2009-05-27 20:06:19 +02:00
|
|
|
{
|
2009-06-19 19:25:56 +02:00
|
|
|
if (tok2->str() == "<")
|
2009-05-27 20:06:19 +02:00
|
|
|
++level;
|
2009-06-19 19:25:56 +02:00
|
|
|
else if (tok2->str() == ">")
|
2009-05-27 20:06:19 +02:00
|
|
|
{
|
|
|
|
--level;
|
|
|
|
if (level == 0)
|
|
|
|
break;
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
2009-06-19 19:25:56 +02:00
|
|
|
if (Token::simpleMatch(tok2, "> ("))
|
|
|
|
{
|
2009-09-13 12:31:32 +02:00
|
|
|
Token *closeBracket = tok2->next()->link();
|
|
|
|
if (closeBracket)
|
2009-06-19 19:25:56 +02:00
|
|
|
{
|
2009-09-13 12:31:32 +02:00
|
|
|
Token::eraseTokens(tok, tok2->tokAt(2));
|
|
|
|
closeBracket->deleteThis();
|
2009-06-19 19:25:56 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-29 21:37:15 +02:00
|
|
|
void Tokenizer::simplifyFunctionParameters()
|
2009-01-26 23:26:50 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-05 21:21:25 +02:00
|
|
|
if (argumentNames.find(tok->str()) != argumentNames.end())
|
|
|
|
{
|
|
|
|
// Invalid code, two arguments with the same name.
|
|
|
|
// TODO, print error perhaps?
|
|
|
|
bailOut = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
argumentNames[tok->str()] = tok;
|
|
|
|
if (tok->next()->str() == ")")
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bailOut)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *start = tok;
|
|
|
|
while (tok && tok->str() != "{")
|
|
|
|
{
|
|
|
|
if (tok->str() == ";")
|
|
|
|
{
|
|
|
|
tok = tok->previous();
|
|
|
|
// Move tokens from start to tok into the place of
|
|
|
|
// argumentNames[tok->str()] and remove the ";"
|
|
|
|
|
|
|
|
if (argumentNames.find(tok->str()) == argumentNames.end())
|
|
|
|
{
|
|
|
|
bailOut = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the following ";"
|
|
|
|
Token *temp = tok->tokAt(2);
|
|
|
|
tok->deleteNext();
|
|
|
|
|
|
|
|
// Replace "x" with "int x" or similar
|
|
|
|
Token::replace(argumentNames[tok->str()], start, tok);
|
2009-09-05 22:41:39 +02:00
|
|
|
argumentNames.erase(tok->str());
|
2009-01-26 23:26:50 +01:00
|
|
|
tok = temp;
|
|
|
|
start = tok;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-27 08:39:11 +01:00
|
|
|
if (tok == NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-26 23:26:50 +01:00
|
|
|
if (bailOut)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++indentlevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-03 12:03:54 +02:00
|
|
|
if (Token::Match(type0, "else|return"))
|
2009-03-18 20:32:05 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
bool isconst = false;
|
2009-10-03 12:03:54 +02:00
|
|
|
bool isstatic = false;
|
2009-03-18 20:32:05 +01:00
|
|
|
Token *tok2 = type0;
|
|
|
|
unsigned int typelen = 1;
|
|
|
|
|
2009-08-06 23:31:39 +02:00
|
|
|
while (Token::Match(tok2, "%type% %type% *| *| %var%"))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
if (tok2->str() == "const")
|
|
|
|
isconst = true;
|
|
|
|
|
2009-10-03 12:03:54 +02:00
|
|
|
else if (tok2->str() == "static")
|
|
|
|
isstatic = true;
|
|
|
|
|
2009-03-18 20:32:05 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
++typelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't split up const declaration..
|
|
|
|
if (isconst && Token::Match(tok2, "%type% %var% ="))
|
|
|
|
continue;
|
|
|
|
|
2009-10-03 12:11:52 +02:00
|
|
|
if (Token::Match(tok2, "%type% *| %var% ,|="))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
2009-10-03 12:11:52 +02:00
|
|
|
const bool isPointer = (tok2->next()->str() == "*");
|
|
|
|
const Token *varName = tok2->tokAt((isPointer ? 2 : 1));
|
|
|
|
Token *endDeclaration = varName->next();
|
2009-03-18 20:32:05 +01:00
|
|
|
|
2009-10-03 12:11:52 +02:00
|
|
|
if (varName->str() != "operator")
|
2009-10-03 12:03:54 +02:00
|
|
|
{
|
2009-10-03 12:11:52 +02:00
|
|
|
tok2 = endDeclaration; // The ',' or '=' token
|
2009-10-03 12:03:54 +02:00
|
|
|
|
|
|
|
if (isstatic && tok2->str() == "=")
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2->next(), "%num% ,"))
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
else
|
|
|
|
tok2 = NULL;
|
|
|
|
}
|
|
|
|
}
|
2009-03-18 20:32:05 +01:00
|
|
|
else
|
|
|
|
tok2 = NULL;
|
2009-08-06 22:11:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok2, "%type% * * %var% ,|="))
|
|
|
|
{
|
|
|
|
if (tok2->tokAt(3)->str() != "operator")
|
|
|
|
tok2 = tok2->tokAt(4); // The ',' token
|
|
|
|
else
|
|
|
|
tok2 = NULL;
|
2009-03-18 20:32:05 +01:00
|
|
|
}
|
|
|
|
|
2009-06-14 19:32:34 +02:00
|
|
|
else if (Token::Match(tok2, "%type% * const %var% ,|="))
|
|
|
|
{
|
|
|
|
if (tok2->tokAt(3)->str() != "operator")
|
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(4); // The ',' token
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok2 = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-20 12:28:15 +02:00
|
|
|
else if (Token::Match(tok2, "%type% %var% [ %num% ] ,|=") ||
|
|
|
|
Token::Match(tok2, "%type% %var% [ %var% ] ,|="))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(5); // The ',' token
|
2009-09-20 12:28:15 +02:00
|
|
|
|
|
|
|
if (tok2->str() == "=")
|
|
|
|
{
|
|
|
|
while (tok2 && tok2->str() != ",")
|
|
|
|
{
|
2009-09-20 13:28:56 +02:00
|
|
|
if (tok2->str() == "{")
|
|
|
|
tok2 = tok2->link();
|
2009-09-26 12:02:13 +02:00
|
|
|
|
2009-09-20 12:28:15 +02:00
|
|
|
tok2 = tok2->next();
|
2009-09-26 12:02:13 +02:00
|
|
|
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
tok2 = NULL;
|
2009-09-20 12:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-18 20:32:05 +01:00
|
|
|
}
|
|
|
|
|
2009-08-09 10:16:37 +02:00
|
|
|
else if (Token::Match(tok2, "%type% * %var% [ %num% ] ,") ||
|
|
|
|
Token::Match(tok2, "%type% * %var% [ %var% ] ,"))
|
2009-03-18 20:32:05 +01:00
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(6); // The ',' token
|
|
|
|
}
|
|
|
|
|
2009-07-28 21:46:33 +02:00
|
|
|
else if (Token::Match(tok2, "std :: %type% <") || Token::Match(tok2, "%type% <"))
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Deal with templates and standart types
|
|
|
|
//
|
|
|
|
if (Token::simpleMatch(tok2, "std ::"))
|
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
2009-09-28 20:25:05 +02:00
|
|
|
if (!Token::Match(tok, "unsigned|signed"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::Match(tok->previous(), "%type% unsigned|signed %var% [;,=)]") &&
|
|
|
|
tok->previous()->isStandardType())
|
|
|
|
{
|
|
|
|
if (tok->str() == "signed")
|
|
|
|
{
|
|
|
|
// int signed a; -> int a;
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// int unsigned a; -> unsigned int a;
|
|
|
|
std::string temp = tok->str();
|
|
|
|
tok->str(tok->previous()->str());
|
|
|
|
tok->previous()->str(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// signed int a; -> int a;
|
|
|
|
if (Token::Match(tok, "signed %type% %var% [;,=)]"))
|
|
|
|
{
|
|
|
|
if (tok->next()->isStandardType())
|
|
|
|
{
|
|
|
|
tok->str(tok->next()->str());
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-06 10:40:48 +02:00
|
|
|
// A variable declaration where the "int" is left out?
|
2009-09-28 20:25:05 +02:00
|
|
|
else if (!Token::Match(tok, "unsigned|signed %var% [;,=)]"))
|
2009-06-06 10:40:48 +02:00
|
|
|
continue;
|
|
|
|
|
2009-09-28 20:25:05 +02:00
|
|
|
// Previous token should either be a symbol or one of "{};(,"
|
2009-06-06 10:40:48 +02:00
|
|
|
if (tok->previous() &&
|
|
|
|
!tok->previous()->isName() &&
|
2009-09-28 20:25:05 +02:00
|
|
|
!Token::Match(tok->previous(), "[{};(,]"))
|
2009-06-06 10:40:48 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// next token should not be a standard type?
|
|
|
|
if (tok->next()->isStandardType())
|
2009-09-28 20:25:05 +02:00
|
|
|
{
|
|
|
|
if (tok->str() == "signed")
|
|
|
|
{
|
|
|
|
tok->str(tok->next()->str());
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
|
2009-06-06 10:40:48 +02:00
|
|
|
continue;
|
2009-09-28 20:25:05 +02:00
|
|
|
}
|
2009-06-06 10:40:48 +02:00
|
|
|
|
|
|
|
// The "int" is missing.. add it
|
2009-09-28 20:25:05 +02:00
|
|
|
if (tok->str() == "signed")
|
|
|
|
tok->str("int");
|
|
|
|
else
|
|
|
|
tok->insertToken("int");
|
2009-06-06 10:40:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-29 21:17:17 +02:00
|
|
|
void Tokenizer::simplifyIfAssign()
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
if (!Token::Match(tok->next(), "if|while ( !| (| %var% =") &&
|
|
|
|
!Token::Match(tok->next(), "if|while ( !| (| %var% . %var% ="))
|
|
|
|
continue;
|
2009-06-20 13:58:30 +02:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
// simplifying a "while" condition ?
|
|
|
|
const bool iswhile(tok->next()->str() == "while");
|
2009-06-20 13:58:30 +02:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
// delete the "if"
|
|
|
|
tok->deleteNext();
|
|
|
|
|
|
|
|
// Remember if there is a "!" or not. And delete it if there are.
|
|
|
|
const bool isNot(tok->tokAt(2)->str() == "!");
|
|
|
|
if (isNot)
|
|
|
|
tok->next()->deleteNext();
|
|
|
|
|
2009-08-29 11:08:11 +02:00
|
|
|
// Delete paranthesis.. and remember how many there are with
|
|
|
|
// their links.
|
|
|
|
std::list<Token *> braces;
|
2009-08-05 19:45:05 +02:00
|
|
|
while (tok->next()->str() == "(")
|
|
|
|
{
|
2009-08-29 11:08:11 +02:00
|
|
|
braces.push_back(tok->next()->link());
|
2009-03-23 18:20:56 +01:00
|
|
|
tok->deleteNext();
|
2009-08-05 19:45:05 +02:00
|
|
|
}
|
2009-03-23 18:20:56 +01:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
// Skip the "%var% = ..."
|
|
|
|
Token *tok2;
|
|
|
|
unsigned int indentlevel = 0;
|
2009-10-06 20:03:15 +02:00
|
|
|
for (tok2 = tok->next(); tok2; tok2 = tok2->next())
|
2009-08-05 19:45:05 +02:00
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
++indentlevel;
|
|
|
|
else if (tok2->str() == ")")
|
2009-03-24 18:23:21 +01:00
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
if (indentlevel <= 0)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
2009-03-24 18:23:21 +01:00
|
|
|
}
|
2009-08-05 19:45:05 +02:00
|
|
|
}
|
2009-03-24 18:23:21 +01:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
// Insert "; if|while ( .."
|
|
|
|
tok2 = tok2->previous();
|
|
|
|
if (Token::simpleMatch(tok->tokAt(2), "."))
|
|
|
|
{
|
|
|
|
tok2->insertToken(tok->strAt(3));
|
|
|
|
tok2->insertToken(tok->strAt(2));
|
|
|
|
}
|
|
|
|
tok2->insertToken(tok->strAt(1));
|
2009-11-04 23:58:15 +01:00
|
|
|
tok2->next()->varId(tok->tokAt(1)->varId());
|
2009-08-29 11:08:11 +02:00
|
|
|
|
|
|
|
while (! braces.empty())
|
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
tok2->insertToken("(");
|
2009-08-29 11:08:11 +02:00
|
|
|
Token::createMutualLinks(tok2->next(), braces.back());
|
|
|
|
braces.pop_back();
|
|
|
|
}
|
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
if (isNot)
|
|
|
|
tok2->next()->insertToken("!");
|
|
|
|
tok2->insertToken(iswhile ? "while" : "if");
|
|
|
|
tok2->insertToken(";");
|
2009-03-23 18:20:56 +01:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
// If it's a while loop.. insert the assignment in the loop
|
|
|
|
if (iswhile)
|
|
|
|
{
|
|
|
|
indentlevel = 0;
|
|
|
|
Token *tok3 = tok2;
|
|
|
|
for (tok3 = tok2; tok3; tok3 = tok3->next())
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
if (tok3->str() == "{")
|
2009-03-23 18:20:56 +01:00
|
|
|
++indentlevel;
|
2009-08-05 19:45:05 +02:00
|
|
|
else if (tok3->str() == "}")
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
if (indentlevel <= 1)
|
2009-03-23 18:20:56 +01:00
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
}
|
2009-03-24 18:23:21 +01:00
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
if (tok3 && indentlevel == 1)
|
2009-03-23 18:20:56 +01:00
|
|
|
{
|
2009-08-05 19:45:05 +02:00
|
|
|
tok3 = tok3->previous();
|
2009-11-12 19:41:35 +01:00
|
|
|
std::list<Token *> braces2;
|
|
|
|
|
2009-08-05 19:45:05 +02:00
|
|
|
for (tok2 = tok2->next(); tok2 && tok2 != tok; tok2 = tok2->previous())
|
2009-06-20 13:58:30 +02:00
|
|
|
{
|
2009-08-29 13:09:24 +02:00
|
|
|
tok3->insertToken(tok2->strAt(0));
|
|
|
|
|
|
|
|
Token *newTok = tok3->next();
|
|
|
|
newTok->fileIndex(tok2->fileIndex());
|
|
|
|
newTok->linenr(tok2->linenr());
|
|
|
|
|
|
|
|
// link() newly tokens manually
|
|
|
|
if (newTok->str() == ")")
|
|
|
|
{
|
|
|
|
braces.push_back(newTok);
|
|
|
|
}
|
2009-11-12 19:41:35 +01:00
|
|
|
else if (newTok->str() == "]")
|
|
|
|
{
|
|
|
|
braces2.push_back(newTok);
|
|
|
|
}
|
2009-08-29 13:09:24 +02:00
|
|
|
else if (newTok->str() == "(")
|
|
|
|
{
|
|
|
|
Token::createMutualLinks(newTok, braces.back());
|
|
|
|
braces.pop_back();
|
|
|
|
}
|
2009-11-12 19:41:35 +01:00
|
|
|
else if (newTok->str() == "[")
|
|
|
|
{
|
|
|
|
Token::createMutualLinks(newTok, braces2.back());
|
|
|
|
braces2.pop_back();
|
|
|
|
}
|
2009-06-20 13:58:30 +02:00
|
|
|
}
|
2009-03-23 18:20:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-24 18:23:21 +01:00
|
|
|
|
2009-08-29 21:19:45 +02:00
|
|
|
void Tokenizer::simplifyIfNot()
|
2009-03-24 18:23:21 +01:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-07-18 10:18:46 +02:00
|
|
|
if (tok->str() == "(" || tok->str() == "||" || tok->str() == "&&")
|
2009-03-24 18:23:21 +01:00
|
|
|
{
|
2009-07-18 10:18:46 +02:00
|
|
|
tok = tok->next();
|
2009-07-29 23:37:01 +02:00
|
|
|
while (tok && tok->str() == "(")
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
|
2009-07-18 10:18:46 +02:00
|
|
|
if (Token::simpleMatch(tok, "0 == (") ||
|
|
|
|
Token::Match(tok, "0 == %var%"))
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->str("!");
|
|
|
|
}
|
2009-05-01 12:07:10 +02:00
|
|
|
|
2009-07-18 10:18:46 +02:00
|
|
|
else if (Token::Match(tok, "%var% == 0"))
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->next()->str(tok->str());
|
|
|
|
tok->str("!");
|
|
|
|
}
|
|
|
|
|
2009-07-25 13:11:29 +02:00
|
|
|
else if (Token::Match(tok, "%var% .|:: %var% == 0"))
|
2009-07-18 10:18:46 +02:00
|
|
|
{
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->insertToken("!");
|
|
|
|
tok = tok->tokAt(4);
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-07-18 10:18:46 +02:00
|
|
|
}
|
2009-10-05 13:10:09 +02:00
|
|
|
|
|
|
|
else if (Token::Match(tok, "* %var% == 0"))
|
|
|
|
{
|
|
|
|
tok = tok->previous();
|
|
|
|
tok->insertToken("!");
|
|
|
|
tok = tok->tokAt(3);
|
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
|
|
|
}
|
2009-05-01 12:07:10 +02:00
|
|
|
}
|
|
|
|
|
2009-07-18 10:18:46 +02:00
|
|
|
else if (tok->link() && Token::simpleMatch(tok, ") == 0"))
|
2009-05-01 12:07:10 +02:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok, tok->tokAt(3));
|
2009-08-20 21:37:05 +02:00
|
|
|
if (Token::Match(tok->link()->previous(), "%var%"))
|
|
|
|
{
|
|
|
|
// if( foo(x) == 0 )
|
|
|
|
tok->link()->previous()->insertToken(tok->link()->previous()->str().c_str());
|
|
|
|
tok->link()->previous()->previous()->str("!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if( (x) == 0 )
|
|
|
|
tok->link()->insertToken("(");
|
|
|
|
tok->link()->str("!");
|
2009-10-05 13:10:09 +02:00
|
|
|
Token *temp = tok->link();
|
2009-08-29 11:08:11 +02:00
|
|
|
Token::createMutualLinks(tok->link()->next(), tok);
|
2009-10-05 13:10:09 +02:00
|
|
|
temp->link(0);
|
2009-08-20 21:37:05 +02:00
|
|
|
}
|
2009-03-24 18:23:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-31 10:42:27 +02:00
|
|
|
|
2009-08-29 21:21:06 +02:00
|
|
|
void Tokenizer::simplifyIfNotNull()
|
2009-07-25 13:11:29 +02:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
Token *deleteFrom = NULL;
|
|
|
|
|
|
|
|
if (tok->str() == "(" || tok->str() == "||" || tok->str() == "&&")
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, "0 != (") ||
|
|
|
|
Token::Match(tok, "0 != %var%"))
|
|
|
|
{
|
|
|
|
deleteFrom = tok->previous();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "%var% != 0"))
|
|
|
|
{
|
|
|
|
deleteFrom = tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "%var% .|:: %var% != 0"))
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
deleteFrom = tok;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok->link() && Token::simpleMatch(tok, ") != 0"))
|
|
|
|
{
|
|
|
|
deleteFrom = tok;
|
|
|
|
}
|
|
|
|
|
2009-07-25 16:27:18 +02:00
|
|
|
if (deleteFrom)
|
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(deleteFrom, deleteFrom->tokAt(3));
|
2009-07-31 18:06:16 +02:00
|
|
|
tok = deleteFrom;
|
2009-07-25 13:11:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-29 21:03:37 +02:00
|
|
|
void Tokenizer::simplifyLogicalOperators()
|
2009-05-31 10:42:27 +02:00
|
|
|
{
|
|
|
|
// "if (not p)" => "if (!p)"
|
2009-08-29 15:29:19 +02:00
|
|
|
// "if (p and q)" => "if (p and q)"
|
2009-05-31 10:42:27 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "if|while ( not %var%"))
|
2009-08-29 15:11:47 +02:00
|
|
|
{
|
2009-06-14 08:20:51 +02:00
|
|
|
tok->tokAt(2)->str("!");
|
2009-08-29 15:11:47 +02:00
|
|
|
}
|
|
|
|
else if (Token::Match(tok, "&& not %var%"))
|
|
|
|
{
|
2009-05-31 10:42:27 +02:00
|
|
|
tok->next()->str("!");
|
2009-08-29 15:11:47 +02:00
|
|
|
}
|
|
|
|
else if (Token::Match(tok, "|| not %var%"))
|
|
|
|
{
|
2009-05-31 10:42:27 +02:00
|
|
|
tok->next()->str("!");
|
2009-08-29 15:11:47 +02:00
|
|
|
}
|
2009-08-29 15:29:19 +02:00
|
|
|
// "%var%|) and %var%|("
|
|
|
|
else if (tok->str() == "and" &&
|
|
|
|
((Token::Match(tok->previous(), "%var%") || tok->previous()->str() == ")") ||
|
2009-08-29 22:33:12 +02:00
|
|
|
(Token::Match(tok->next(), "%var%") || tok->next()->str() == "(")))
|
2009-08-29 15:29:19 +02:00
|
|
|
{
|
|
|
|
tok->str("&&");
|
|
|
|
}
|
2009-05-31 10:42:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-24 18:15:38 +01:00
|
|
|
bool Tokenizer::simplifyKnownVariables()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
// Search for a block of code
|
|
|
|
if (! Token::Match(tok, ") const| {"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// parse the block of code..
|
|
|
|
int indentlevel = 0;
|
|
|
|
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% ;") ||
|
2009-10-17 18:21:13 +02:00
|
|
|
Token::Match(tok2, "%var% = %str% ;") ||
|
2009-11-07 08:25:39 +01:00
|
|
|
Token::Match(tok2, "%var% [ ] = %str% ;") ||
|
2009-10-17 18:21:13 +02:00
|
|
|
Token::Match(tok2, "%var% = %bool% ;") ||
|
|
|
|
Token::Match(tok2, "%var% = %var% ;")))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-11-12 22:16:24 +01:00
|
|
|
const unsigned int varid = tok2->varId();
|
2009-01-24 18:15:38 +01:00
|
|
|
if (varid == 0)
|
|
|
|
continue;
|
|
|
|
|
2009-10-17 18:21:13 +02:00
|
|
|
const bool pointeralias(tok2->tokAt(2)->isName());
|
|
|
|
|
2009-02-24 17:11:37 +01:00
|
|
|
std::string value(tok2->strAt(2));
|
2009-11-07 08:25:39 +01:00
|
|
|
if (value == "]")
|
|
|
|
value = tok2->strAt(4);
|
2009-05-03 20:57:27 +02:00
|
|
|
Token* bailOutFromLoop = 0;
|
2009-05-25 14:21:58 +02:00
|
|
|
int indentlevel3 = indentlevel; // indentlevel for tok3
|
2009-01-24 18:15:38 +01:00
|
|
|
for (Token *tok3 = tok2->next(); tok3; tok3 = tok3->next())
|
|
|
|
{
|
2009-05-25 14:21:58 +02:00
|
|
|
if (tok3->str() == "{")
|
|
|
|
{
|
|
|
|
++indentlevel3;
|
|
|
|
}
|
|
|
|
else if (tok3->str() == "}")
|
|
|
|
{
|
|
|
|
--indentlevel3;
|
|
|
|
if (indentlevel3 < indentlevel)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-10-17 18:21:13 +02:00
|
|
|
if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str()))
|
|
|
|
break;
|
|
|
|
|
2009-08-22 10:23:55 +02:00
|
|
|
// Stop if something like 'while (--var)' is found
|
2009-09-28 17:15:31 +02:00
|
|
|
if (tok3->str() == "while" || tok3->str() == "do")
|
2009-08-22 10:23:55 +02:00
|
|
|
{
|
|
|
|
const Token *endpar = tok3->next()->link();
|
|
|
|
bool bailout = false;
|
2009-10-09 17:28:23 +02:00
|
|
|
for (const Token *tok4 = tok3; tok4 && tok4 != endpar; tok4 = tok4->next())
|
2009-08-22 10:23:55 +02:00
|
|
|
{
|
|
|
|
if (Token::Match(tok4, "++|-- %varid%", varid) ||
|
|
|
|
Token::Match(tok4, "%varid% ++|--", varid))
|
|
|
|
{
|
|
|
|
bailout = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bailout)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-03 20:57:27 +02:00
|
|
|
if (bailOutFromLoop)
|
|
|
|
{
|
|
|
|
// This could be a loop, skip it, but only if it doesn't contain
|
|
|
|
// the variable we are checking for. If it contains the variable
|
|
|
|
// we will bail out.
|
|
|
|
if (tok3->varId() == varid)
|
|
|
|
{
|
|
|
|
// Continue
|
|
|
|
tok2 = bailOutFromLoop;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (tok3 == bailOutFromLoop)
|
|
|
|
{
|
|
|
|
// We have skipped the loop
|
|
|
|
bailOutFromLoop = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2009-07-27 11:24:24 +02:00
|
|
|
else if (tok3->str() == "{" && tok3->previous()->str() == ")")
|
2009-05-03 20:57:27 +02:00
|
|
|
{
|
|
|
|
// There is a possible loop after the assignment. Try to skip it.
|
|
|
|
bailOutFromLoop = tok3->link();
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-25 17:54:04 +02:00
|
|
|
else if (tok3->str() == "}" && tok3->link() && tok3->link()->previous()->str() == ")")
|
2009-05-03 20:57:27 +02:00
|
|
|
{
|
|
|
|
// Assignment was in the middle of possible loop, bail out.
|
2009-01-24 18:15:38 +01:00
|
|
|
break;
|
2009-05-03 20:57:27 +02:00
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
|
|
|
|
// Variable is used somehow in a non-defined pattern => bail out
|
|
|
|
if (tok3->varId() == varid)
|
|
|
|
break;
|
|
|
|
|
2009-03-25 07:10:17 +01:00
|
|
|
// Using the variable in condition..
|
2009-06-03 21:02:16 +02:00
|
|
|
if (Token::Match(tok3, "(|!|==|!=|<|<=|>|>= %varid% )|==|!=|<|<=|>|>=", varid))
|
2009-01-24 18:15:38 +01:00
|
|
|
{
|
2009-03-25 07:10:17 +01:00
|
|
|
tok3 = tok3->next();
|
2009-08-02 10:54:46 +02:00
|
|
|
tok3->str(value);
|
2009-01-24 18:15:38 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
2009-02-14 07:11:37 +01:00
|
|
|
|
|
|
|
// Variable is used in calculation..
|
2009-10-20 22:58:46 +02:00
|
|
|
if (Token::Match(tok3, "[=+-*/[] %varid% [?+-*/;]]", varid) ||
|
2009-10-06 08:14:59 +02:00
|
|
|
Token::Match(tok3, "[=+-*/[] %varid% <<", varid) ||
|
|
|
|
Token::Match(tok3, "<< %varid% [+-*/;]]", varid))
|
2009-02-14 07:11:37 +01:00
|
|
|
{
|
|
|
|
tok3 = tok3->next();
|
2009-08-02 10:54:46 +02:00
|
|
|
tok3->str(value);
|
2009-02-14 07:11:37 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
2009-02-24 17:11:37 +01:00
|
|
|
|
2009-11-02 23:07:15 +01:00
|
|
|
if (Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value))
|
2009-02-24 17:11:37 +01:00
|
|
|
{
|
2009-03-04 07:03:51 +01:00
|
|
|
const std::string op(tok3->strAt(2));
|
2009-05-25 08:26:11 +02:00
|
|
|
if (Token::Match(tok3, "[{};] %any% %any% ;"))
|
2009-03-04 07:03:51 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok3, tok3->tokAt(3));
|
2009-03-04 07:03:51 +01:00
|
|
|
}
|
|
|
|
else
|
2009-02-27 19:25:47 +01:00
|
|
|
{
|
2009-03-04 07:03:51 +01:00
|
|
|
tok3 = tok3->next();
|
2009-08-02 10:54:46 +02:00
|
|
|
tok3->str(value);
|
2009-02-27 19:25:47 +01:00
|
|
|
tok3->deleteNext();
|
|
|
|
}
|
|
|
|
incdec(value, op);
|
2009-08-02 10:54:46 +02:00
|
|
|
tok2->tokAt(2)->str(value);
|
2009-03-04 07:24:03 +01:00
|
|
|
ret = true;
|
2009-02-24 17:11:37 +01:00
|
|
|
}
|
|
|
|
|
2009-11-12 22:16:24 +01:00
|
|
|
if (Token::Match(tok3->next(), "++|-- %varid% !!.", varid))
|
2009-02-24 17:11:37 +01:00
|
|
|
{
|
|
|
|
incdec(value, tok3->strAt(1));
|
2009-08-02 10:54:46 +02:00
|
|
|
tok2->tokAt(2)->str(value);
|
2009-05-25 08:31:20 +02:00
|
|
|
if (Token::Match(tok3, "[;{}] %any% %any% ;"))
|
2009-03-04 07:03:51 +01:00
|
|
|
{
|
2009-08-22 16:04:58 +02:00
|
|
|
Token::eraseTokens(tok3, tok3->tokAt(3));
|
2009-03-04 07:03:51 +01:00
|
|
|
}
|
|
|
|
else
|
2009-02-27 19:25:47 +01:00
|
|
|
{
|
|
|
|
tok3->deleteNext();
|
2009-08-02 10:54:46 +02:00
|
|
|
tok3->next()->str(value);
|
2009-02-27 19:25:47 +01:00
|
|
|
}
|
2009-02-24 17:11:37 +01:00
|
|
|
tok3 = tok3->next();
|
2009-03-04 07:24:03 +01:00
|
|
|
ret = true;
|
2009-02-24 17:11:37 +01:00
|
|
|
}
|
2009-01-24 18:15:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-17 20:18:26 +01:00
|
|
|
|
2009-08-29 21:28:39 +02:00
|
|
|
void Tokenizer::elseif()
|
2009-02-17 20:18:26 +01:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (!Token::simpleMatch(tok, "else if"))
|
|
|
|
continue;
|
|
|
|
int indent = 0;
|
|
|
|
for (Token *tok2 = tok; indent >= 0 && tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2, "(|{"))
|
|
|
|
++indent;
|
|
|
|
else if (Token::Match(tok2, ")|}"))
|
|
|
|
--indent;
|
|
|
|
|
|
|
|
if (indent == 0 && Token::Match(tok2, "}|;"))
|
|
|
|
{
|
2009-07-27 11:24:24 +02:00
|
|
|
if (tok2->next()->str() != "else")
|
2009-02-17 20:18:26 +01:00
|
|
|
{
|
|
|
|
tok->insertToken("{");
|
|
|
|
tok2->insertToken("}");
|
2009-08-23 02:38:00 +02:00
|
|
|
Token::createMutualLinks(tok->next(), tok2->next());
|
2009-02-17 20:18:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
bool Tokenizer::simplifyRedundantParanthesis()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-05-29 23:04:01 +02:00
|
|
|
if (tok->str() != "(")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while (Token::simpleMatch(tok, "( (") &&
|
|
|
|
tok->link()->previous() == tok->next()->link())
|
2009-01-26 17:38:08 +01:00
|
|
|
{
|
2009-05-29 23:04:01 +02:00
|
|
|
// We have "(( *something* ))", remove the inner
|
|
|
|
// paranthesis
|
|
|
|
tok->deleteNext();
|
2009-06-14 08:26:20 +02:00
|
|
|
tok->link()->tokAt(-2)->deleteNext();
|
2009-05-29 23:04:01 +02:00
|
|
|
ret = true;
|
|
|
|
}
|
2009-05-28 22:03:36 +02:00
|
|
|
|
2009-06-12 15:04:58 +02:00
|
|
|
while (Token::Match(tok->previous(), "[;{] ( %var% (") &&
|
|
|
|
tok->link()->previous() == tok->tokAt(2)->link())
|
|
|
|
{
|
|
|
|
// We have "( func ( *something* ))", remove the outer
|
|
|
|
// paranthesis
|
|
|
|
tok->link()->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2009-06-12 16:14:01 +02:00
|
|
|
while (Token::Match(tok->previous(), "[;{] ( delete %var% ) ;"))
|
|
|
|
{
|
|
|
|
// We have "( delete var )", remove the outer
|
|
|
|
// paranthesis
|
|
|
|
tok->tokAt(3)->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (Token::Match(tok->previous(), "[;{] ( delete [ ] %var% ) ;"))
|
|
|
|
{
|
|
|
|
// We have "( delete [] var )", remove the outer
|
|
|
|
// paranthesis
|
|
|
|
tok->tokAt(5)->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2009-10-05 13:30:05 +02:00
|
|
|
if (Token::Match(tok->previous(), "[(!*;}] ( %var% )") && tok->next()->varId() != 0)
|
2009-07-03 20:57:44 +02:00
|
|
|
{
|
|
|
|
// We have "( var )", remove the paranthesis
|
|
|
|
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-10-06 08:14:59 +02:00
|
|
|
if (Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% [],);=<>+-*/]") ||
|
|
|
|
Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/]") ||
|
|
|
|
Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% <<") ||
|
|
|
|
Token::Match(tok, "<< %num% [+-*/] %num% <<"))
|
2009-03-04 07:24:03 +01:00
|
|
|
{
|
2009-04-06 19:23:30 +02:00
|
|
|
tok = tok->next();
|
2009-03-04 07:24:03 +01:00
|
|
|
|
2009-04-06 19:43:54 +02:00
|
|
|
// Don't simplify "%num% / 0"
|
|
|
|
if (Token::simpleMatch(tok->next(), "/ 0"))
|
|
|
|
continue;
|
|
|
|
|
2009-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);
|
2009-10-29 20:54:51 +01:00
|
|
|
// keep parantheses here: A operator * (int);
|
|
|
|
if (!tok->isName() && tok->str() != ">" && Token::Match(tok->next(), "( %var% ) [;),+-*/><]]") && !Token::simpleMatch(tok->previous(), "operator"))
|
2009-03-04 07:24:03 +01:00
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok = tok->next();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-26 17:38:08 +01:00
|
|
|
|
2009-08-24 23:10:12 +02:00
|
|
|
void Tokenizer::simplifyGoto()
|
|
|
|
{
|
|
|
|
std::list<Token *> gotos;
|
|
|
|
unsigned int indentlevel = 0;
|
|
|
|
Token *beginfunction = 0;
|
2009-08-27 18:40:58 +02:00
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
2009-08-26 18:46:56 +02:00
|
|
|
{
|
|
|
|
if (beginfunction == 0 && indentlevel == 0 && tok->link())
|
|
|
|
tok = tok->link();
|
|
|
|
else
|
|
|
|
++indentlevel;
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
{
|
|
|
|
if (indentlevel == 0)
|
|
|
|
break; // break out - it seems the code is wrong
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel == 0)
|
|
|
|
{
|
|
|
|
gotos.clear();
|
|
|
|
beginfunction = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (indentlevel == 0 && Token::Match(tok, ") const| {"))
|
|
|
|
{
|
|
|
|
gotos.clear();
|
|
|
|
beginfunction = tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "goto %var% ;"))
|
|
|
|
gotos.push_back(tok);
|
|
|
|
|
2009-08-26 18:46:56 +02:00
|
|
|
else if (indentlevel == 1 && Token::Match(tok->previous(), "[};] %var% :"))
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
// Is this label at the end..
|
|
|
|
bool end = false;
|
2009-09-30 13:30:53 +02:00
|
|
|
int lev = 0;
|
2009-08-24 23:10:12 +02:00
|
|
|
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "}")
|
|
|
|
{
|
2009-09-30 13:30:53 +02:00
|
|
|
--lev;
|
|
|
|
if (lev < 0)
|
|
|
|
{
|
|
|
|
end = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
++lev;
|
2009-08-24 23:10:12 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 13:30:53 +02:00
|
|
|
if (Token::Match(tok2, "%var% :"))
|
2009-08-24 23:10:12 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!end)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const std::string name(tok->str());
|
|
|
|
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
// This label is at the end of the function.. replace all matching goto statements..
|
|
|
|
for (std::list<Token *>::iterator it = gotos.begin(); it != gotos.end(); ++it)
|
|
|
|
{
|
|
|
|
Token *token = *it;
|
|
|
|
if (token->next()->str() == name)
|
|
|
|
{
|
|
|
|
// Delete the "goto name;"
|
|
|
|
token = token->previous();
|
|
|
|
token->deleteNext();
|
|
|
|
token->deleteNext();
|
|
|
|
token->deleteNext();
|
|
|
|
|
|
|
|
// Insert the statements..
|
|
|
|
bool ret = false;
|
2009-08-28 22:31:11 +02:00
|
|
|
std::list<Token*> links;
|
2009-10-01 09:25:32 +02:00
|
|
|
std::list<Token*> links2;
|
|
|
|
std::list<Token*> links3;
|
2009-09-30 13:30:53 +02:00
|
|
|
int lev = 0;
|
2009-08-24 23:10:12 +02:00
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "}")
|
2009-09-30 13:30:53 +02:00
|
|
|
{
|
|
|
|
--lev;
|
|
|
|
if (lev < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
{
|
|
|
|
++lev;
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "return")
|
2009-08-24 23:10:12 +02:00
|
|
|
ret = true;
|
|
|
|
token->insertToken(tok2->str().c_str());
|
|
|
|
token = token->next();
|
2009-08-28 22:31:11 +02:00
|
|
|
if (token->str() == "(")
|
|
|
|
{
|
|
|
|
links.push_back(token);
|
|
|
|
}
|
|
|
|
else if (token->str() == ")")
|
|
|
|
{
|
|
|
|
if (links.size() == 0)
|
|
|
|
{
|
|
|
|
// This should never happen at this point
|
|
|
|
syntaxError(token, ')');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::createMutualLinks(links.back(), token);
|
|
|
|
links.pop_back();
|
|
|
|
}
|
2009-10-01 09:25:32 +02:00
|
|
|
else if (token->str() == "{")
|
|
|
|
{
|
|
|
|
links2.push_back(token);
|
|
|
|
}
|
|
|
|
else if (token->str() == "}")
|
|
|
|
{
|
|
|
|
if (links2.size() == 0)
|
|
|
|
{
|
|
|
|
// This should never happen at this point
|
|
|
|
syntaxError(token, '}');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::createMutualLinks(links2.back(), token);
|
|
|
|
links2.pop_back();
|
|
|
|
}
|
|
|
|
else if (token->str() == "[")
|
|
|
|
{
|
|
|
|
links3.push_back(token);
|
|
|
|
}
|
|
|
|
else if (token->str() == "]")
|
|
|
|
{
|
|
|
|
if (links3.size() == 0)
|
|
|
|
{
|
|
|
|
// This should never happen at this point
|
|
|
|
syntaxError(token, ']');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token::createMutualLinks(links3.back(), token);
|
|
|
|
links3.pop_back();
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
}
|
2009-08-28 22:31:11 +02:00
|
|
|
|
2009-08-24 23:10:12 +02:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
token->insertToken("return");
|
|
|
|
token = token->next();
|
|
|
|
token->insertToken(";");
|
|
|
|
token = token->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gotos.clear();
|
|
|
|
tok = beginfunction;
|
|
|
|
indentlevel = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-05 18:46:27 +02:00
|
|
|
void Tokenizer::simplifyNestedStrcat()
|
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (! Token::Match(tok, "[;{}] strcat ( strcat ("))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert extracted function calls before first strcat call
|
|
|
|
Token *insertPos = tok;
|
|
|
|
|
|
|
|
// find inner strcat call
|
|
|
|
Token *tok2 = tok->tokAt(3);
|
|
|
|
while (Token::simpleMatch(tok2, "strcat ( strcat"))
|
|
|
|
{
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *end = tok2->next()->link()->next();
|
|
|
|
Token *endOfFirstArg = NULL;
|
|
|
|
std::stack<Token *> brackets;
|
2009-09-06 12:33:55 +02:00
|
|
|
unsigned int lineno = tok->next()->linenr();
|
2009-09-05 18:46:27 +02:00
|
|
|
|
|
|
|
// copy tokens to new place
|
|
|
|
for (Token *cur = tok2; cur != end; cur = cur->next())
|
|
|
|
{
|
|
|
|
insertPos->insertToken(cur->strAt(0));
|
|
|
|
insertPos = insertPos->next();
|
|
|
|
|
|
|
|
if (cur->str() == "," && endOfFirstArg == NULL)
|
|
|
|
{
|
|
|
|
endOfFirstArg = cur;
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
|
2009-09-05 18:46:27 +02:00
|
|
|
// preserve varId
|
|
|
|
if (cur->varId())
|
|
|
|
{
|
|
|
|
insertPos->varId(cur->varId());
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
|
2009-09-06 12:33:55 +02:00
|
|
|
// use line number of first strcat token for all new
|
|
|
|
// tokens
|
|
|
|
insertPos->linenr(lineno);
|
|
|
|
|
2009-09-05 18:46:27 +02:00
|
|
|
// linkify braces
|
|
|
|
if (insertPos->str() == "(")
|
|
|
|
{
|
|
|
|
brackets.push(insertPos);
|
|
|
|
}
|
|
|
|
else if (insertPos->str() == ")")
|
|
|
|
{
|
|
|
|
Token::createMutualLinks(brackets.top(), insertPos);
|
|
|
|
brackets.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
insertPos->insertToken(";");
|
|
|
|
|
|
|
|
// remove tokens at old place, but don't remove token with
|
|
|
|
// variable name (1st argument)
|
|
|
|
Token::eraseTokens(tok2->previous(), tok2->tokAt(2));
|
|
|
|
Token::eraseTokens(endOfFirstArg->previous(), end);
|
|
|
|
|
|
|
|
// skip just inserted tokens
|
|
|
|
tok = insertPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-08-24 23:10:12 +02:00
|
|
|
|
|
|
|
|
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-10-17 23:11:48 +02:00
|
|
|
if (!_errorLogger && tok)
|
2009-05-11 21:52:04 +02:00
|
|
|
{
|
2009-09-01 19:30:54 +02:00
|
|
|
std::ostringstream err;
|
|
|
|
err << "### Unlogged error at Tokenizer::syntaxError: Invalid number of character (" << c << ")";
|
|
|
|
if (_settings && _settings->_debug)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(err.str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << err.str() << std::endl;
|
|
|
|
}
|
2009-05-11 21:52:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
2009-10-17 23:11:48 +02:00
|
|
|
if (tok)
|
|
|
|
{
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok->linenr();
|
|
|
|
loc.file = file(tok);
|
|
|
|
locationList.push_back(loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
"error",
|
|
|
|
std::string("Invalid number of character (") + c + "). Can't process file.",
|
|
|
|
"syntaxError");
|
|
|
|
|
|
|
|
if (_errorLogger)
|
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
2009-05-11 21:52:04 +02:00
|
|
|
|
2009-05-07 22:17:29 +02:00
|
|
|
}
|
2009-03-13 22:28:44 +01:00
|
|
|
|
2009-09-13 09:35:02 +02:00
|
|
|
void Tokenizer::simplifyMathFunctions()
|
2009-09-12 22:54:47 +02:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "atol ( %str% )"))
|
|
|
|
{
|
|
|
|
if (!MathLib::isInt(tok->tokAt(2)->strValue()))
|
|
|
|
{
|
|
|
|
// Ignore strings which we can't convert
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->previous() &&
|
|
|
|
Token::simpleMatch(tok->previous()->previous(), "std ::"))
|
|
|
|
{
|
|
|
|
// Delete "std ::"
|
|
|
|
tok = tok->previous()->previous();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete atol(
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteThis();
|
|
|
|
|
|
|
|
// Convert string into a number
|
|
|
|
tok->str(MathLib::toString(MathLib::toLongNumber(tok->strValue())));
|
|
|
|
|
|
|
|
// Delete remaining )
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-29 21:23:39 +02:00
|
|
|
void Tokenizer::simplifyComma()
|
2009-06-10 23:12:26 +02:00
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
2009-08-29 11:48:44 +02:00
|
|
|
if (Token::simpleMatch(tok, "for (") ||
|
|
|
|
Token::Match(tok, "=|enum {"))
|
2009-07-28 18:40:15 +02:00
|
|
|
{
|
2009-08-05 22:02:40 +02:00
|
|
|
tok = tok->next()->link();
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
|
|
|
|
continue;
|
2009-07-28 18:40:15 +02:00
|
|
|
}
|
|
|
|
|
2009-09-12 08:12:00 +02:00
|
|
|
// Skip unhandled template specifiers..
|
|
|
|
if (Token::Match(tok, "%var% <"))
|
|
|
|
{
|
|
|
|
// Todo.. use the link instead.
|
|
|
|
unsigned int parlevel = 0;
|
|
|
|
unsigned int comparelevel = 0;
|
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "<")
|
|
|
|
++comparelevel;
|
|
|
|
else if (tok2->str() == ">")
|
|
|
|
{
|
|
|
|
if (comparelevel <= 1)
|
|
|
|
{
|
|
|
|
tok = tok2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++comparelevel;
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "(")
|
|
|
|
++parlevel;
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
|
|
|
if (parlevel == 0)
|
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok2, "[;{}]"))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 14:38:41 +02:00
|
|
|
// If token after the comma is a constant number, simplification is not required.
|
|
|
|
if (tok->str() != "," || Token::Match(tok->next(), "%num%"))
|
2009-06-10 23:12:26 +02:00
|
|
|
continue;
|
2009-03-13 22:28:44 +01:00
|
|
|
|
2009-06-10 23:12:26 +02:00
|
|
|
// We must not accept just any keyword, e.g. accepting int
|
|
|
|
// would cause function parameters to corrupt.
|
2009-06-18 23:00:16 +02:00
|
|
|
if (Token::Match(tok->next(), "delete"))
|
|
|
|
{
|
|
|
|
// Handle "delete a, delete b;"
|
|
|
|
tok->str(";");
|
|
|
|
}
|
2009-07-05 20:29:09 +02:00
|
|
|
|
|
|
|
if (tok->previous() && tok->previous()->previous())
|
2009-06-18 23:00:16 +02:00
|
|
|
{
|
2009-07-05 20:29:09 +02:00
|
|
|
if (Token::Match(tok->previous()->previous(), "delete") &&
|
|
|
|
tok->next()->varId() != 0)
|
|
|
|
{
|
|
|
|
// Handle "delete a, b;"
|
|
|
|
tok->str(";");
|
|
|
|
tok->insertToken("delete");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous())
|
|
|
|
{
|
2009-08-05 22:02:40 +02:00
|
|
|
if (tok2->str() == "=")
|
2009-07-05 20:29:09 +02:00
|
|
|
{
|
|
|
|
// Handle "a = 0, b = 0;"
|
|
|
|
tok->str(";");
|
|
|
|
break;
|
|
|
|
}
|
2009-07-27 21:13:08 +02:00
|
|
|
else if (Token::Match(tok2, "delete %var%") ||
|
|
|
|
Token::Match(tok2, "delete [ ] %var%"))
|
|
|
|
{
|
|
|
|
// Handle "delete a, a = 0;"
|
|
|
|
tok->str(";");
|
|
|
|
break;
|
|
|
|
}
|
2009-07-05 20:29:09 +02:00
|
|
|
else if (Token::Match(tok2, "[;,{}()]"))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-18 23:00:16 +02:00
|
|
|
}
|
2009-07-26 13:00:11 +02:00
|
|
|
|
|
|
|
bool inReturn = false;
|
|
|
|
Token *startFrom = NULL; // next tokean after "; return"
|
|
|
|
Token *endAt = NULL; // first ";" token after "; return"
|
|
|
|
|
|
|
|
// find "; return" pattern before comma
|
2009-07-26 22:13:08 +02:00
|
|
|
for (Token *tok2 = tok; tok2; tok2 = tok2->previous())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok2, "[;{}]"))
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
break;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "return" && Token::Match(tok2->previous(), "[;{}]"))
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
inReturn = true;
|
|
|
|
startFrom = tok2->next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find token where return ends and also count commas
|
2009-07-26 22:13:08 +02:00
|
|
|
if (inReturn)
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
size_t commaCounter = 0;
|
|
|
|
size_t indentlevel = 0;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
for (Token *tok2 = startFrom; tok2; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == ";")
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
endAt = tok2;
|
|
|
|
break;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "(")
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
++indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
--indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "," && indentlevel == 0)
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
++commaCounter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
if (commaCounter)
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
indentlevel = 0;
|
|
|
|
|
|
|
|
// change tokens:
|
|
|
|
// "; return a ( ) , b ( ) , c ;"
|
|
|
|
// to
|
|
|
|
// "; return a ( ) ; b ( ) ; c ;"
|
2009-07-26 22:13:08 +02:00
|
|
|
for (Token *tok2 = startFrom; tok2 != endAt; tok2 = tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->str() == "(")
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
++indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == ")")
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
--indentlevel;
|
|
|
|
|
2009-07-26 22:13:08 +02:00
|
|
|
}
|
|
|
|
else if (tok2->str() == "," && indentlevel == 0)
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
tok2->str(";");
|
|
|
|
--commaCounter;
|
2009-07-26 22:13:08 +02:00
|
|
|
if (commaCounter == 0)
|
|
|
|
{
|
2009-07-26 13:00:11 +02:00
|
|
|
tok2->insertToken("return");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete old "return"
|
|
|
|
startFrom->previous()->deleteThis();
|
|
|
|
|
|
|
|
tok = endAt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
2009-09-13 15:35:37 +02:00
|
|
|
|
|
|
|
|
2009-10-01 19:45:48 +02:00
|
|
|
void Tokenizer::removeExceptionSpecifications(Token *tok) const
|
|
|
|
{
|
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
tok = tok->link();
|
|
|
|
|
|
|
|
else if (tok->str() == "}")
|
|
|
|
break;
|
|
|
|
|
|
|
|
else if (Token::Match(tok, ") throw ("))
|
|
|
|
{
|
|
|
|
while (tok->next() && !Token::Match(tok->next(), "[;{]"))
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(tok, "class %type%"))
|
|
|
|
{
|
|
|
|
while (tok && !Token::Match(tok, "[;{]"))
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok && tok->str() == "{")
|
|
|
|
{
|
|
|
|
removeExceptionSpecifications(tok->next());
|
|
|
|
tok = tok->link();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = tok ? tok->next() : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-13 15:35:37 +02:00
|
|
|
bool Tokenizer::validate() const
|
|
|
|
{
|
|
|
|
std::stack<const Token *> linktok;
|
|
|
|
|
|
|
|
for (const Token *tok = tokens(); tok; tok = tok->next())
|
|
|
|
{
|
2009-09-29 22:27:10 +02:00
|
|
|
if (Token::Match(tok, "[{([]"))
|
2009-09-13 15:35:37 +02:00
|
|
|
{
|
2009-11-10 22:10:56 +01:00
|
|
|
if (tok->link() == 0)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-13 15:35:37 +02:00
|
|
|
linktok.push(tok);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-29 22:27:10 +02:00
|
|
|
else if (Token::Match(tok, "[})]]"))
|
2009-09-13 15:35:37 +02:00
|
|
|
{
|
2009-11-10 22:10:56 +01:00
|
|
|
if (tok->link() == 0)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (linktok.empty() == true)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->link() != linktok.top())
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok != tok->link()->link())
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-13 15:35:37 +02:00
|
|
|
linktok.pop();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-11-10 22:10:56 +01:00
|
|
|
if (tok->link() != 0)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-13 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2009-11-10 22:10:56 +01:00
|
|
|
if (!linktok.empty())
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-13 15:35:37 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-27 16:04:10 +02:00
|
|
|
|
|
|
|
std::string Tokenizer::simplifyString(const std::string &source)
|
|
|
|
{
|
|
|
|
std::string str = source;
|
|
|
|
bool escaped = false;
|
|
|
|
for (std::string::size_type i = 0; i + 2 < str.size(); i++)
|
|
|
|
{
|
|
|
|
if (!escaped)
|
|
|
|
{
|
|
|
|
if (str[i] == '\\')
|
|
|
|
escaped = true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str[i] == 'x')
|
|
|
|
{
|
|
|
|
// Hex value
|
|
|
|
if (str[i+1] == '0' && str[i+2] == '0')
|
|
|
|
str.replace(i, 3, "0");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We will replace all other character as 'a'
|
|
|
|
// If that causes problems in the future, this can
|
|
|
|
// be improved. But for now, this should be OK.
|
2009-10-09 21:11:29 +02:00
|
|
|
int n = 1;
|
|
|
|
while (n < 2 && std::isxdigit(str[i+1+n]))
|
|
|
|
++n;
|
2009-09-27 16:04:10 +02:00
|
|
|
--i;
|
2009-10-09 21:11:29 +02:00
|
|
|
str.replace(i, 2 + n, "a");
|
2009-09-27 16:04:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (MathLib::isOctalDigit(str[i]))
|
|
|
|
{
|
|
|
|
if (MathLib::isOctalDigit(str[i+1]) &&
|
|
|
|
MathLib::isOctalDigit(str[i+2]))
|
|
|
|
{
|
|
|
|
if (str[i+1] == '0' && str[i+2] == '0')
|
|
|
|
str.replace(i, 3, "0");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We will replace all other character as 'a'
|
|
|
|
// If that causes problems in the future, this can
|
|
|
|
// be improved. But for now, this should be OK.
|
|
|
|
--i;
|
|
|
|
str.replace(i, 4, "a");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
escaped = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2009-10-17 23:11:48 +02:00
|
|
|
|
2009-11-04 23:58:15 +01:00
|
|
|
void Tokenizer::simplifyComparisonOrder()
|
|
|
|
{
|
|
|
|
// Use "<" comparison instead of ">"
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "[;(] %any% >|>= %any% [);]"))
|
|
|
|
{
|
2009-11-12 18:53:26 +01:00
|
|
|
if (!tok->next()->isName() && !tok->next()->isNumber())
|
|
|
|
continue;
|
2009-11-04 23:58:15 +01:00
|
|
|
const std::string op1(tok->strAt(1));
|
|
|
|
tok->next()->str(tok->strAt(3));
|
|
|
|
tok->tokAt(3)->str(op1);
|
|
|
|
if (tok->tokAt(2)->str() == ">")
|
|
|
|
tok->tokAt(2)->str("<");
|
|
|
|
else
|
|
|
|
tok->tokAt(2)->str("<=");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-12 22:49:39 +01:00
|
|
|
void Tokenizer::simplifyConst()
|
|
|
|
{
|
|
|
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "[;{}(,] %type% const"))
|
|
|
|
{
|
|
|
|
tok->tokAt(2)->str(tok->tokAt(1)->str());
|
|
|
|
tok->tokAt(1)->str("const");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 23:11:48 +02:00
|
|
|
void Tokenizer::getErrorMessages()
|
|
|
|
{
|
|
|
|
syntaxError(0, ' ');
|
|
|
|
}
|