2008-11-20 20:23:05 +01:00
|
|
|
/*
|
2008-10-26 08:55:15 +01:00
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007 Daniel Marjamäki
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "tokenize.h"
|
2008-11-20 23:19:26 +01:00
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include <locale>
|
|
|
|
#include <fstream>
|
|
|
|
|
2008-11-12 23:50:40 +01:00
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
#include <string>
|
2008-11-20 20:18:55 +01:00
|
|
|
#include <cstring>
|
2008-11-22 10:44:02 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2008-11-21 22:14:24 +01:00
|
|
|
#include <list>
|
2008-11-15 23:54:39 +01:00
|
|
|
#include <algorithm>
|
2007-05-24 07:40:45 +02:00
|
|
|
#include <stdlib.h> // <- strtoul
|
2008-12-14 20:03:34 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <FileLister.h>
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2008-02-18 18:11:34 +01:00
|
|
|
#ifdef __BORLANDC__
|
2008-09-11 19:03:58 +02:00
|
|
|
#include <ctype.h>
|
2008-02-18 18:11:34 +01:00
|
|
|
#include <mem.h>
|
|
|
|
#endif
|
|
|
|
|
2008-09-11 19:03:58 +02:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
#define _strdup(str) strdup(str)
|
|
|
|
#endif
|
|
|
|
|
2008-11-20 20:18:55 +01:00
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-22 20:39:12 +01:00
|
|
|
Tokenizer::Tokenizer()
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
|
|
|
_tokens = 0;
|
2008-11-23 12:08:07 +01:00
|
|
|
_tokensBack = 0;
|
|
|
|
_dsymlist = 0;
|
2008-11-20 20:18:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Tokenizer::~Tokenizer()
|
|
|
|
{
|
2008-11-22 10:44:02 +01:00
|
|
|
DeallocateTokens();
|
2008-11-12 23:50:40 +01:00
|
|
|
}
|
2008-11-20 20:18:55 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Helper functions..
|
|
|
|
|
|
|
|
TOKEN *Tokenizer::_gettok(TOKEN *tok, int index)
|
|
|
|
{
|
|
|
|
while (tok && index>0)
|
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
tok = tok->next();
|
2008-11-20 20:18:55 +01:00
|
|
|
index--;
|
|
|
|
}
|
|
|
|
return tok;
|
2008-12-09 01:49:32 +01:00
|
|
|
}
|
|
|
|
|
2008-11-20 20:18:55 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const TOKEN *Tokenizer::tokens() const
|
|
|
|
{
|
|
|
|
return _tokens;
|
2008-11-12 23:50:40 +01:00
|
|
|
}
|
2007-05-28 08:17:18 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Defined symbols.
|
|
|
|
// "#define abc 123" will create a defined symbol "abc" with the value 123
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-16 16:18:50 +01:00
|
|
|
|
2008-11-20 20:18:55 +01:00
|
|
|
|
2008-11-22 21:00:36 +01:00
|
|
|
const std::vector<std::string> *Tokenizer::getFiles() const
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
return &_files;
|
2008-11-20 20:18:55 +01:00
|
|
|
}
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2008-11-09 08:19:53 +01:00
|
|
|
void Tokenizer::Define(const char Name[], const char Value[])
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
|
|
|
if (!(Name && Name[0]))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(Value && Value[0]))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Is 'Value' a decimal value..
|
|
|
|
bool dec = true, hex = true;
|
|
|
|
for (int i = 0; Value[i]; i++)
|
|
|
|
{
|
2008-09-11 19:03:58 +02:00
|
|
|
if ( ! isdigit(Value[i]) )
|
2007-05-24 07:40:45 +02:00
|
|
|
dec = false;
|
|
|
|
|
2008-09-11 19:03:58 +02:00
|
|
|
if ( ! isxdigit(Value[i]) && (!(i==1 && Value[i]=='x')))
|
2007-05-24 07:40:45 +02:00
|
|
|
hex = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dec && !hex)
|
|
|
|
return;
|
|
|
|
|
2008-09-11 19:03:58 +02:00
|
|
|
char *strValue = _strdup(Value);
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
if (!dec && hex)
|
|
|
|
{
|
2008-09-11 19:03:58 +02:00
|
|
|
// Convert Value from hexadecimal to decimal
|
|
|
|
unsigned long value;
|
|
|
|
std::istringstream istr(Value+2);
|
|
|
|
istr >> std::hex >> value;
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << value;
|
2007-05-24 07:40:45 +02:00
|
|
|
free(strValue);
|
2008-09-11 19:03:58 +02:00
|
|
|
strValue = _strdup(ostr.str().c_str());
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DefineSymbol *NewSym = new DefineSymbol;
|
|
|
|
memset(NewSym, 0, sizeof(DefineSymbol));
|
2008-09-11 19:03:58 +02:00
|
|
|
NewSym->name = _strdup(Name);
|
2007-05-24 07:40:45 +02:00
|
|
|
NewSym->value = strValue;
|
2008-11-23 12:08:07 +01:00
|
|
|
NewSym->next = _dsymlist;
|
|
|
|
_dsymlist = NewSym;
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
2007-05-28 08:17:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// addtoken
|
|
|
|
// add a token. Used by 'Tokenizer'
|
|
|
|
//---------------------------------------------------------------------------
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2008-11-09 08:19:53 +01:00
|
|
|
void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsigned int fileno)
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
|
|
|
if (str[0] == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Replace hexadecimal value with decimal
|
2008-09-11 19:03:58 +02:00
|
|
|
std::ostringstream str2;
|
|
|
|
if (strncmp(str,"0x",2)==0)
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-09-11 19:03:58 +02:00
|
|
|
str2 << strtoul(str+2, NULL, 16);
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
2008-09-11 19:03:58 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
str2 << str;
|
|
|
|
}
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2008-11-23 12:08:07 +01:00
|
|
|
if (_tokensBack)
|
2008-12-13 12:39:36 +01:00
|
|
|
{
|
|
|
|
_tokensBack->insertToken( str2.str().c_str() );
|
|
|
|
_tokensBack = _tokensBack->next();
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-12-13 12:39:36 +01:00
|
|
|
_tokens = new TOKEN;
|
|
|
|
_tokensBack = _tokens;
|
2008-12-09 18:31:04 +01:00
|
|
|
_tokensBack->setstr( str2.str().c_str() );
|
2008-12-13 12:39:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_tokensBack->linenr( lineno );
|
|
|
|
_tokensBack->fileIndex( fileno );
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
// Check if str is defined..
|
2008-11-23 12:08:07 +01:00
|
|
|
for (DefineSymbol *sym = _dsymlist; sym; sym = sym->next)
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
|
|
|
if (strcmp(str,sym->name)==0)
|
|
|
|
{
|
2008-12-09 18:31:04 +01:00
|
|
|
_tokensBack->setstr(sym->value);
|
2007-05-24 07:40:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// SizeOfType - gives the size of a type
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-12 23:50:40 +01:00
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
|
2008-11-22 20:47:10 +01:00
|
|
|
int Tokenizer::SizeOfType(const char type[]) const
|
2007-05-28 08:17:18 +02:00
|
|
|
{
|
|
|
|
if (!type)
|
|
|
|
return 0;
|
|
|
|
|
2008-11-23 12:08:07 +01:00
|
|
|
std::map<std::string, unsigned int>::const_iterator it = _typeSize.find(type);
|
|
|
|
if ( it == _typeSize.end() )
|
2008-11-22 20:47:10 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return it->second;
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// InsertTokens - Copy and insert tokens
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-09 08:19:53 +01:00
|
|
|
void Tokenizer::InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n)
|
2007-05-29 08:24:36 +02:00
|
|
|
{
|
|
|
|
while (n > 0)
|
2008-12-13 12:39:36 +01:00
|
|
|
{
|
|
|
|
dest->insertToken( src->aaaa() );
|
|
|
|
dest->next()->fileIndex( src->fileIndex() );
|
|
|
|
dest->next()->linenr( src->linenr() );
|
2008-12-08 22:49:05 +01:00
|
|
|
dest = dest->next();
|
|
|
|
src = src->next();
|
2007-05-29 08:24:36 +02:00
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Tokenize - tokenizes a given file.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-25 19:34:51 +01:00
|
|
|
void Tokenizer::tokenize(std::istream &code, const char FileName[])
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
|
|
|
// Has this file been tokenized already?
|
2008-11-23 12:08:07 +01:00
|
|
|
for (unsigned int i = 0; i < _files.size(); i++)
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
if ( SameFileName( _files[i].c_str(), FileName ) )
|
2007-05-24 07:40:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-23 12:08:07 +01:00
|
|
|
// The "_files" vector remembers what files have been tokenized..
|
2008-12-14 20:03:34 +01:00
|
|
|
_files.push_back( FileLister::simplifyPath( FileName ) );
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2008-02-16 16:46:32 +01:00
|
|
|
// Tokenize the file..
|
2008-11-25 19:34:51 +01:00
|
|
|
tokenizeCode( code, (unsigned int)(_files.size() - 1) );
|
2008-02-16 16:46:32 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Tokenize - tokenizes input stream
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-25 19:34:51 +01:00
|
|
|
void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex)
|
2008-02-16 16:46:32 +01:00
|
|
|
{
|
2007-10-23 08:36:29 +02:00
|
|
|
// Tokenize the file.
|
2007-05-24 07:40:45 +02:00
|
|
|
unsigned int lineno = 1;
|
2008-11-04 20:09:31 +01:00
|
|
|
std::string CurrentToken;
|
2008-10-30 20:42:34 +01:00
|
|
|
for (char ch = (char)code.get(); code.good(); ch = (char)code.get())
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-25 19:15:32 +01:00
|
|
|
// We are not handling UTF and stuff like that. Code is supposed to plain simple text.
|
2008-09-20 19:34:37 +02:00
|
|
|
if ( ch < 0 )
|
|
|
|
continue;
|
|
|
|
|
2007-10-23 08:36:29 +02:00
|
|
|
// Preprocessor stuff?
|
2008-11-04 20:09:31 +01:00
|
|
|
if (ch == '#' && CurrentToken.empty())
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-03 08:53:30 +01:00
|
|
|
std::string line("#");
|
|
|
|
{
|
|
|
|
char chPrev = '#';
|
|
|
|
while ( code.good() )
|
|
|
|
{
|
|
|
|
ch = (char)code.get();
|
|
|
|
if (chPrev!='\\' && ch=='\n')
|
|
|
|
break;
|
|
|
|
if (ch!=' ')
|
|
|
|
chPrev = ch;
|
|
|
|
if (ch!='\\' && ch!='\n')
|
|
|
|
line += ch;
|
|
|
|
if (ch=='\n')
|
|
|
|
++lineno;
|
|
|
|
}
|
|
|
|
}
|
2007-05-24 07:40:45 +02:00
|
|
|
if (strncmp(line.c_str(),"#include",8)==0 &&
|
|
|
|
line.find("\"") != std::string::npos)
|
|
|
|
{
|
|
|
|
// Extract the filename
|
|
|
|
line.erase(0, line.find("\"")+1);
|
|
|
|
line.erase(line.find("\""));
|
|
|
|
|
|
|
|
// Relative path..
|
2008-11-23 12:08:07 +01:00
|
|
|
if (_files.back().find_first_of("\\/") != std::string::npos)
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
std::string path = _files.back();
|
2008-02-16 16:46:32 +01:00
|
|
|
path.erase( 1 + path.find_last_of("\\/") );
|
2007-05-24 07:40:45 +02:00
|
|
|
line = path + line;
|
|
|
|
}
|
|
|
|
|
2008-02-16 16:46:32 +01:00
|
|
|
addtoken("#include", lineno, FileIndex);
|
|
|
|
addtoken(line.c_str(), lineno, FileIndex);
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2008-10-30 20:42:34 +01:00
|
|
|
std::ifstream fin( line.c_str() );
|
2008-11-25 19:34:51 +01:00
|
|
|
tokenize(fin, line.c_str());
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (strncmp(line.c_str(), "#define", 7) == 0)
|
|
|
|
{
|
2008-11-04 20:09:31 +01:00
|
|
|
std::string strId;
|
2007-05-24 07:40:45 +02:00
|
|
|
enum {Space1, Id, Space2, Value} State;
|
|
|
|
State = Space1;
|
|
|
|
for (unsigned int i = 8; i < line.length(); i++)
|
|
|
|
{
|
|
|
|
if (State==Space1 || State==Space2)
|
|
|
|
{
|
2008-09-11 19:03:58 +02:00
|
|
|
if (isspace(line[i]))
|
2007-05-24 07:40:45 +02:00
|
|
|
continue;
|
|
|
|
State = (State==Space1) ? Id : Value;
|
|
|
|
}
|
|
|
|
|
2008-03-18 08:45:35 +01:00
|
|
|
else if (State==Id)
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-09-11 19:03:58 +02:00
|
|
|
if ( isspace( line[i] ) )
|
2008-03-18 08:45:35 +01:00
|
|
|
{
|
2008-11-04 20:09:31 +01:00
|
|
|
strId = CurrentToken;
|
|
|
|
CurrentToken.clear();
|
2008-03-18 08:45:35 +01:00
|
|
|
State = Space2;
|
|
|
|
continue;
|
|
|
|
}
|
2008-09-11 19:03:58 +02:00
|
|
|
else if ( ! isalnum(line[i]) )
|
2008-03-18 08:45:35 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 20:09:31 +01:00
|
|
|
CurrentToken += line[i];
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (State==Value)
|
|
|
|
{
|
2008-02-16 16:46:32 +01:00
|
|
|
addtoken("def", lineno, FileIndex);
|
2008-11-04 20:09:31 +01:00
|
|
|
addtoken(strId.c_str(), lineno, FileIndex);
|
2008-02-16 16:46:32 +01:00
|
|
|
addtoken(";", lineno, FileIndex);
|
2008-11-04 20:09:31 +01:00
|
|
|
Define(strId.c_str(), CurrentToken.c_str());
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 20:09:31 +01:00
|
|
|
CurrentToken.clear();
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
|
2007-06-02 18:32:07 +02:00
|
|
|
else
|
|
|
|
{
|
2008-02-16 16:46:32 +01:00
|
|
|
addtoken("#", lineno, FileIndex);
|
|
|
|
addtoken(";", lineno, FileIndex);
|
2007-06-02 18:32:07 +02:00
|
|
|
}
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
lineno++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
// Add current token..
|
2008-11-04 20:09:31 +01:00
|
|
|
addtoken(CurrentToken.c_str(), lineno++, FileIndex);
|
|
|
|
CurrentToken.clear();
|
2007-05-24 07:40:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comments..
|
2008-10-30 20:42:34 +01:00
|
|
|
if (ch == '/' && code.good())
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-04 20:09:31 +01:00
|
|
|
bool newstatement = bool( strchr(";{}", CurrentToken.empty() ? '\0' : CurrentToken[0]) != NULL );
|
2008-04-11 20:37:15 +02:00
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
// Add current token..
|
2008-11-04 20:09:31 +01:00
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
CurrentToken.clear();
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
// Read next character..
|
2008-02-16 16:46:32 +01:00
|
|
|
ch = (char)code.get();
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
// If '//'..
|
|
|
|
if (ch == '/')
|
|
|
|
{
|
2008-04-11 20:37:15 +02:00
|
|
|
std::string comment;
|
2008-04-12 08:33:45 +02:00
|
|
|
getline( code, comment ); // Parse in the whole comment
|
|
|
|
|
|
|
|
// If the comment says something like "fred is deleted" then generate appropriate tokens for that
|
|
|
|
comment = comment + " ";
|
|
|
|
if ( newstatement && comment.find(" deleted ")!=std::string::npos )
|
2008-04-11 20:37:15 +02:00
|
|
|
{
|
2008-04-12 08:33:45 +02:00
|
|
|
// delete
|
|
|
|
addtoken( "delete", lineno, FileIndex );
|
|
|
|
|
|
|
|
// fred
|
|
|
|
std::string::size_type pos1 = comment.find_first_not_of(" \t");
|
|
|
|
std::string::size_type pos2 = comment.find(" ", pos1);
|
|
|
|
std::string firstWord = comment.substr( pos1, pos2-pos1 );
|
|
|
|
addtoken( firstWord.c_str(), lineno, FileIndex );
|
|
|
|
|
|
|
|
// ;
|
|
|
|
addtoken( ";", lineno, FileIndex );
|
2008-04-11 20:37:15 +02:00
|
|
|
}
|
2008-04-12 08:33:45 +02:00
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
lineno++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If '/*'..
|
|
|
|
if (ch == '*')
|
|
|
|
{
|
|
|
|
char chPrev;
|
|
|
|
ch = chPrev = 'A';
|
2008-10-30 20:42:34 +01:00
|
|
|
while (code.good() && (chPrev!='*' || ch!='/'))
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
|
|
|
chPrev = ch;
|
2008-02-16 16:46:32 +01:00
|
|
|
ch = (char)code.get();
|
2007-05-24 07:40:45 +02:00
|
|
|
if (ch == '\n')
|
|
|
|
lineno++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a comment.. add token..
|
2008-02-16 16:46:32 +01:00
|
|
|
addtoken("/", lineno, FileIndex);
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// char..
|
|
|
|
if (ch == '\'')
|
|
|
|
{
|
|
|
|
// Add previous token
|
2008-11-04 20:09:31 +01:00
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
CurrentToken.clear();
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
// Read this ..
|
2008-11-04 20:09:31 +01:00
|
|
|
CurrentToken += ch;
|
|
|
|
CurrentToken += (char)code.get();
|
|
|
|
CurrentToken += (char)code.get();
|
2007-05-24 07:40:45 +02:00
|
|
|
if (CurrentToken[1] == '\\')
|
2008-11-04 20:09:31 +01:00
|
|
|
CurrentToken += (char)code.get();
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
// Add token and start on next..
|
2008-11-04 20:09:31 +01:00
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
CurrentToken.clear();
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// String..
|
|
|
|
if (ch == '\"')
|
|
|
|
{
|
2008-11-04 20:09:31 +01:00
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
CurrentToken.clear();
|
2007-05-24 07:40:45 +02:00
|
|
|
bool special = false;
|
|
|
|
char c = ch;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Append token..
|
2008-12-09 01:49:32 +01:00
|
|
|
CurrentToken += c;
|
|
|
|
|
|
|
|
if ( c == '\n' )
|
2008-12-06 18:53:30 +01:00
|
|
|
++lineno;
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
// Special sequence '\.'
|
|
|
|
if (special)
|
|
|
|
special = false;
|
|
|
|
else
|
|
|
|
special = (c == '\\');
|
|
|
|
|
|
|
|
// Get next character
|
2008-02-16 16:46:32 +01:00
|
|
|
c = (char)code.get();
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
2008-10-30 20:42:34 +01:00
|
|
|
while (code.good() && (special || c != '\"'));
|
2008-11-04 20:09:31 +01:00
|
|
|
CurrentToken += '\"';
|
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
CurrentToken.clear();
|
2007-05-24 07:40:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-12-06 18:53:30 +01:00
|
|
|
if (strchr("+-*/%&|^?!=<>[](){};:,.~",ch))
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-04 20:09:31 +01:00
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
CurrentToken.clear();
|
|
|
|
CurrentToken += ch;
|
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
CurrentToken.clear();
|
2007-05-24 07:40:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-11 19:03:58 +02:00
|
|
|
if (isspace(ch) || iscntrl(ch))
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-04 20:09:31 +01:00
|
|
|
addtoken(CurrentToken.c_str(), lineno, FileIndex);
|
|
|
|
CurrentToken.clear();
|
2007-05-24 07:40:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-04 20:09:31 +01:00
|
|
|
CurrentToken += ch;
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
2008-11-04 20:09:31 +01:00
|
|
|
addtoken( CurrentToken.c_str(), lineno, FileIndex );
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
// Combine tokens..
|
2008-12-08 22:49:05 +01:00
|
|
|
for (TOKEN *tok = _tokens; tok && tok->next(); tok = tok->next())
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->combineWithNext("<", "<");
|
|
|
|
tok->combineWithNext(">", ">");
|
|
|
|
|
|
|
|
tok->combineWithNext("&", "&");
|
|
|
|
tok->combineWithNext("|", "|");
|
|
|
|
|
|
|
|
tok->combineWithNext("+", "=");
|
|
|
|
tok->combineWithNext("-", "=");
|
|
|
|
tok->combineWithNext("*", "=");
|
|
|
|
tok->combineWithNext("/", "=");
|
|
|
|
tok->combineWithNext("&", "=");
|
|
|
|
tok->combineWithNext("|", "=");
|
|
|
|
|
|
|
|
tok->combineWithNext("=", "=");
|
|
|
|
tok->combineWithNext("!", "=");
|
|
|
|
tok->combineWithNext("<", "=");
|
|
|
|
tok->combineWithNext(">", "=");
|
|
|
|
|
|
|
|
tok->combineWithNext(":", ":");
|
|
|
|
tok->combineWithNext("-", ">");
|
|
|
|
|
|
|
|
tok->combineWithNext("private", ":");
|
|
|
|
tok->combineWithNext("protected", ":");
|
|
|
|
tok->combineWithNext("public", ":");
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
2008-03-23 15:15:44 +01:00
|
|
|
|
|
|
|
// Replace "->" with "."
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
|
2008-03-23 15:15:44 +01:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
if ( tok->str() == "->" )
|
2008-03-23 15:15:44 +01:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
tok->setstr(".");
|
2008-03-23 15:15:44 +01:00
|
|
|
}
|
|
|
|
}
|
2008-08-28 08:37:11 +02:00
|
|
|
|
|
|
|
// typedef..
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
|
2008-08-28 08:37:11 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok, "typedef %type% %type% ;"))
|
2008-08-28 08:37:11 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
const char *type1 = tok->strAt( 1);
|
|
|
|
const char *type2 = tok->strAt( 2);
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
|
2008-08-28 08:37:11 +02:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
if (tok2->aaaa()!=type1 && tok2->aaaa()!=type2 && (tok2->str() == type2))
|
2008-08-28 08:37:11 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->setstr(type1);
|
2008-08-28 08:37:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok, "typedef %type% %type% %type% ;"))
|
2008-08-28 08:37:11 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
const char *type1 = tok->strAt( 1);
|
|
|
|
const char *type2 = tok->strAt( 2);
|
|
|
|
const char *type3 = tok->strAt( 3);
|
2008-10-19 08:21:01 +02:00
|
|
|
|
|
|
|
TOKEN *tok2 = tok;
|
2008-11-22 23:49:14 +01:00
|
|
|
while ( ! TOKEN::Match(tok2, ";") )
|
2008-12-08 22:49:05 +01:00
|
|
|
tok2 = tok2->next();
|
2008-10-19 08:21:01 +02:00
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( ; tok2; tok2 = tok2->next() )
|
2008-08-28 08:37:11 +02:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
if (tok2->aaaa()!=type3 && (tok2->str() == type3))
|
2008-08-28 08:37:11 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->setstr(type1);
|
2008-12-13 12:39:36 +01:00
|
|
|
tok2->insertToken( type2 );
|
|
|
|
tok2->next()->fileIndex( tok2->fileIndex() );
|
|
|
|
tok2->next()->linenr( tok2->linenr() );
|
2008-12-09 18:31:04 +01:00
|
|
|
tok2 = tok2->next();
|
2008-08-28 08:37:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-05 08:28:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Remove __asm..
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
|
2008-11-05 08:28:40 +01:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
if ( TOKEN::Match(tok->next(), "__asm {") )
|
2008-11-05 08:28:40 +01:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
while ( tok->next() )
|
2008-11-05 08:28:40 +01:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
bool last = TOKEN::Match( tok->next(), "}" );
|
2008-11-05 08:28:40 +01:00
|
|
|
|
2008-12-13 12:39:36 +01:00
|
|
|
// Unlink and delete tok->next()
|
|
|
|
tok->eraseToken();
|
2008-11-05 08:28:40 +01:00
|
|
|
|
|
|
|
// break if this was the last token to delete..
|
|
|
|
if (last)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-09 01:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove "volatile"
|
|
|
|
while ( TOKEN::Match(_tokens, "volatile") )
|
|
|
|
{
|
|
|
|
TOKEN *tok = _tokens;
|
|
|
|
_tokens = _tokens->next();
|
|
|
|
delete tok;
|
|
|
|
}
|
|
|
|
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
|
|
|
|
{
|
|
|
|
while ( TOKEN::Match(tok->next(), "volatile") )
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
2008-11-05 08:28:40 +01:00
|
|
|
}
|
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2008-12-09 01:49:32 +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() )
|
|
|
|
{
|
|
|
|
if ( ! TOKEN::Match(tok, "[;{}(] %type% %var%") )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Determine name of declared variable..
|
|
|
|
const char *varname = 0;
|
|
|
|
TOKEN *tok2 = tok->next();
|
2008-12-12 21:25:08 +01:00
|
|
|
while ( tok2 && ! TOKEN::Match( tok2, "[;[=(]" ) )
|
2008-12-09 01:49:32 +01:00
|
|
|
{
|
|
|
|
if ( tok2->isName() )
|
|
|
|
varname = tok2->strAt(0);
|
|
|
|
else if ( tok2->str() != "*" )
|
|
|
|
break;
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variable declaration found => Set variable ids
|
|
|
|
if ( TOKEN::Match(tok2, "[;[=]") && varname )
|
|
|
|
{
|
|
|
|
++_varId;
|
|
|
|
int indentlevel = 0;
|
|
|
|
int parlevel = 0;
|
|
|
|
for ( tok2 = tok->next(); tok2 && indentlevel >= 0; tok2 = tok2->next() )
|
|
|
|
{
|
|
|
|
if ( tok2->str() == varname )
|
|
|
|
tok2->varId( _varId );
|
|
|
|
else if ( tok2->str() == "{" )
|
|
|
|
++indentlevel;
|
|
|
|
else if ( tok2->str() == "}" )
|
|
|
|
--indentlevel;
|
|
|
|
else if ( tok2->str() == "(" )
|
|
|
|
++parlevel;
|
|
|
|
else if ( tok2->str() == ")" )
|
|
|
|
--parlevel;
|
|
|
|
else if ( parlevel < 0 && tok2->str() == ";" )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Simplify token list
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-25 19:34:51 +01:00
|
|
|
void Tokenizer::simplifyTokenList()
|
2007-05-29 08:24:36 +02:00
|
|
|
{
|
2008-02-20 19:20:59 +01:00
|
|
|
|
|
|
|
// Remove the keyword 'unsigned'
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
|
2008-02-20 19:20:59 +01:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
if (tok->next() && (tok->next()->str() == "unsigned"))
|
2008-12-09 01:49:32 +01:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->deleteNext();
|
2008-02-20 19:20:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
// Replace constants..
|
2008-12-08 22:49:05 +01:00
|
|
|
for (TOKEN *tok = _tokens; tok; tok = tok->next())
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok,"const %type% %var% = %num% ;"))
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
const char *sym = tok->strAt(2);
|
|
|
|
const char *num = tok->strAt(4);
|
2007-05-28 08:17:18 +02:00
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
for (TOKEN *tok2 = _gettok(tok,6); tok2; tok2 = tok2->next())
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
if (tok2->str() == sym)
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->setstr(num);
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-25 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
|
2008-11-23 12:08:07 +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);
|
2008-12-08 22:49:05 +01:00
|
|
|
for (TOKEN *tok = _tokens; tok; tok = tok->next())
|
2007-05-25 08:50:16 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok,"class %var%"))
|
2007-05-28 08:17:18 +02:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
_typeSize[tok->strAt(1)] = 11;
|
2007-05-28 08:17:18 +02:00
|
|
|
}
|
2007-05-25 08:50:16 +02:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok, "struct %var%"))
|
2007-05-25 08:50:16 +02:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
_typeSize[tok->strAt(1)] = 13;
|
2007-05-25 08:50:16 +02:00
|
|
|
}
|
2007-05-28 08:17:18 +02:00
|
|
|
}
|
|
|
|
|
2007-05-25 08:50:16 +02:00
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
// Replace 'sizeof(type)'..
|
2008-12-08 22:49:05 +01:00
|
|
|
for (TOKEN *tok = _tokens; tok; tok = tok->next())
|
2007-05-28 08:17:18 +02:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
if (tok->str() != "sizeof")
|
2007-05-28 08:17:18 +02:00
|
|
|
continue;
|
2007-05-25 08:50:16 +02:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok, "sizeof ( %type% * )"))
|
2007-05-25 08:50:16 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
std::ostringstream str;
|
2007-05-28 08:17:18 +02:00
|
|
|
// 'sizeof(type *)' has the same size as 'sizeof(char *)'
|
2008-09-11 19:03:58 +02:00
|
|
|
str << sizeof(char *);
|
2008-11-06 19:31:39 +01:00
|
|
|
tok->setstr( str.str().c_str() );
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2007-05-25 08:50:16 +02:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->deleteNext();
|
2007-05-25 08:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok, "sizeof ( %type% )"))
|
2007-05-25 08:50:16 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
const char *type = tok->strAt( 2);
|
2007-05-28 08:17:18 +02:00
|
|
|
int size = SizeOfType(type);
|
|
|
|
if (size > 0)
|
2007-05-25 08:50:16 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
std::ostringstream str;
|
|
|
|
str << size;
|
|
|
|
tok->setstr( str.str().c_str() );
|
2007-05-28 08:17:18 +02:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->deleteNext();
|
2007-05-28 08:17:18 +02:00
|
|
|
}
|
2007-05-25 08:50:16 +02:00
|
|
|
}
|
2008-11-20 20:18:55 +01:00
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok, "sizeof ( * %var% )"))
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
|
|
|
tok->setstr("100");
|
|
|
|
for ( int i = 0; i < 4; ++i )
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->deleteNext();
|
2007-05-25 08:50:16 +02:00
|
|
|
}
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
2007-05-26 08:44:28 +02:00
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
// Replace 'sizeof(var)'
|
2008-12-08 22:49:05 +01:00
|
|
|
for (TOKEN *tok = _tokens; tok; tok = tok->next())
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
// type array [ num ] ;
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( ! TOKEN::Match(tok, "%type% %var% [ %num% ] ;") )
|
2007-05-26 08:44:28 +02:00
|
|
|
continue;
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
int size = SizeOfType(tok->aaaa());
|
2007-05-26 08:44:28 +02:00
|
|
|
if (size <= 0)
|
|
|
|
continue;
|
|
|
|
|
2008-11-23 11:09:16 +01:00
|
|
|
const char *varname = tok->strAt( 1);
|
|
|
|
int total_size = size * atoi( tok->strAt( 3) );
|
2007-05-26 08:44:28 +02:00
|
|
|
|
|
|
|
// Replace 'sizeof(var)' with number
|
|
|
|
int indentlevel = 0;
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( TOKEN *tok2 = _gettok(tok,5); tok2; tok2 = tok2->next() )
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
if (tok2->str() == "{")
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
++indentlevel;
|
2007-05-26 08:44:28 +02:00
|
|
|
}
|
|
|
|
|
2008-12-05 20:04:41 +01:00
|
|
|
else if (tok2->str() == "}")
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
--indentlevel;
|
2007-05-26 08:44:28 +02:00
|
|
|
if (indentlevel < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
// Todo: TOKEN::Match varname directly
|
|
|
|
else if (TOKEN::Match(tok2, "sizeof ( %var% )"))
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
if (strcmp(tok2->strAt(2), varname) == 0)
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
std::ostringstream str;
|
2008-09-11 19:03:58 +02:00
|
|
|
str << total_size;
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->setstr(str.str().c_str());
|
2007-05-26 08:44:28 +02:00
|
|
|
// Delete the other tokens..
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
tok2->deleteNext();
|
2007-05-26 08:44:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
|
2007-05-26 08:44:28 +02:00
|
|
|
// Simple calculations..
|
2008-11-22 18:53:22 +01:00
|
|
|
for ( bool done = false; !done; done = true )
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for (TOKEN *tok = _tokens; tok; tok = tok->next())
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
if (TOKEN::Match(tok->next(), "* 1") || TOKEN::Match(tok->next(), "1 *"))
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
for (int i = 0; i < 2; i++)
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->deleteNext();
|
2007-05-26 08:44:28 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
2008-12-09 01:49:32 +01:00
|
|
|
// (1-2)
|
2008-12-05 20:17:25 +01:00
|
|
|
if (TOKEN::Match(tok, "[[,(=<>] %num% [+-*/] %num% [],);=<>]"))
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
int i1 = atoi(tok->strAt(1));
|
|
|
|
int i2 = atoi(tok->strAt(3));
|
|
|
|
if ( i2 == 0 && *(tok->strAt(2)) == '/' )
|
2008-03-19 18:09:51 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-23 11:09:16 +01:00
|
|
|
switch (*(tok->strAt(2)))
|
2007-05-26 08:44:28 +02:00
|
|
|
{
|
|
|
|
case '+': i1 += i2; break;
|
|
|
|
case '-': i1 -= i2; break;
|
|
|
|
case '*': i1 *= i2; break;
|
|
|
|
case '/': i1 /= i2; break;
|
|
|
|
}
|
2008-12-08 22:49:05 +01:00
|
|
|
tok = tok->next();
|
2008-11-06 19:31:39 +01:00
|
|
|
std::ostringstream str;
|
2008-09-11 19:03:58 +02:00
|
|
|
str << i1;
|
2008-11-06 19:31:39 +01:00
|
|
|
tok->setstr(str.str().c_str());
|
2007-05-26 08:44:28 +02:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->deleteNext();
|
2007-05-26 08:44:28 +02:00
|
|
|
}
|
2008-03-22 12:46:06 +01:00
|
|
|
|
2008-03-19 18:09:51 +01:00
|
|
|
done = false;
|
2007-05-26 08:44:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-28 12:34:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Replace "*(str + num)" => "str[num]"
|
2008-12-08 22:49:05 +01:00
|
|
|
for (TOKEN *tok = _tokens; tok; tok = tok->next())
|
2007-05-28 12:34:18 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( ! strchr(";{}(=<>", tok->aaaa0()) )
|
2007-05-28 12:34:18 +02:00
|
|
|
continue;
|
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
TOKEN *next = tok->next();
|
2007-05-28 12:34:18 +02:00
|
|
|
if ( ! next )
|
|
|
|
break;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(next, "* ( %var% + %num% )"))
|
2007-05-28 12:34:18 +02:00
|
|
|
{
|
|
|
|
const char *str[4] = {"var","[","num","]"};
|
2008-11-23 11:09:16 +01:00
|
|
|
str[0] = tok->strAt(3);
|
|
|
|
str[2] = tok->strAt(5);
|
2007-05-28 12:34:18 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
tok = tok->next();
|
2008-11-06 19:31:39 +01:00
|
|
|
tok->setstr(str[i]);
|
2007-05-28 12:34:18 +02:00
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
2007-05-28 12:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Split up variable declarations if possible..
|
2008-12-08 22:49:05 +01:00
|
|
|
for (TOKEN *tok = _tokens; tok; tok = tok->next())
|
2007-05-29 08:24:36 +02:00
|
|
|
{
|
2008-12-07 09:47:56 +01:00
|
|
|
if ( ! TOKEN::Match(tok, "[{};]") )
|
2007-05-29 08:24:36 +02:00
|
|
|
continue;
|
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
TOKEN *type0 = tok->next();
|
2008-11-22 23:49:14 +01:00
|
|
|
if (!TOKEN::Match(type0, "%type%"))
|
2008-11-02 11:33:38 +01:00
|
|
|
continue;
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(type0, "else") || TOKEN::Match(type0, "return"))
|
2007-06-02 18:32:07 +02:00
|
|
|
continue;
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
TOKEN *tok2 = NULL;
|
|
|
|
unsigned int typelen = 0;
|
|
|
|
|
2008-12-07 09:47:56 +01:00
|
|
|
if ( TOKEN::Match(type0, "%type% %var% ,|=") )
|
2008-12-09 01:49:32 +01:00
|
|
|
{
|
|
|
|
if ( type0->next()->str() != "operator" )
|
2008-12-07 09:47:56 +01:00
|
|
|
{
|
|
|
|
tok2 = _gettok(type0, 2); // The ',' or '=' token
|
2008-12-09 01:49:32 +01:00
|
|
|
typelen = 1;
|
2008-12-07 09:47:56 +01:00
|
|
|
}
|
2007-05-29 08:24:36 +02:00
|
|
|
}
|
|
|
|
|
2008-12-07 09:47:56 +01:00
|
|
|
else if ( TOKEN::Match(type0, "%type% * %var% ,|=") )
|
2008-12-09 01:49:32 +01:00
|
|
|
{
|
|
|
|
if ( type0->next()->next()->str() != "operator" )
|
2008-12-07 09:47:56 +01:00
|
|
|
{
|
|
|
|
tok2 = _gettok(type0, 3); // The ',' token
|
2008-12-09 01:49:32 +01:00
|
|
|
typelen = 1;
|
2008-12-07 09:47:56 +01:00
|
|
|
}
|
2007-05-29 08:24:36 +02:00
|
|
|
}
|
|
|
|
|
2008-12-07 09:47:56 +01:00
|
|
|
else if ( TOKEN::Match(type0, "%type% %var% [ %num% ] ,|=") )
|
2007-05-29 08:24:36 +02:00
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
tok2 = _gettok(type0, 5); // The ',' token
|
2007-05-29 08:24:36 +02:00
|
|
|
typelen = 1;
|
|
|
|
}
|
|
|
|
|
2008-12-07 09:47:56 +01:00
|
|
|
else if ( TOKEN::Match(type0, "%type% * %var% [ %num% ] ,|=") )
|
2007-05-29 08:24:36 +02:00
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
tok2 = _gettok(type0, 6); // The ',' token
|
2007-05-29 19:11:53 +02:00
|
|
|
typelen = 1;
|
|
|
|
}
|
|
|
|
|
2008-12-07 09:47:56 +01:00
|
|
|
else if ( TOKEN::Match(type0, "struct %type% %var% ,|=") )
|
2007-05-29 19:11:53 +02:00
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
tok2 = _gettok(type0, 3);
|
2007-05-29 19:11:53 +02:00
|
|
|
typelen = 2;
|
|
|
|
}
|
|
|
|
|
2008-12-07 09:47:56 +01:00
|
|
|
else if ( TOKEN::Match(type0, "struct %type% * %var% ,|=") )
|
2007-05-29 19:11:53 +02:00
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
tok2 = _gettok(type0, 4);
|
2007-05-29 08:24:36 +02:00
|
|
|
typelen = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tok2)
|
|
|
|
{
|
2008-12-05 19:25:08 +01:00
|
|
|
if (tok2->str() == ",")
|
2007-05-29 08:24:36 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->setstr(";");
|
2007-05-29 08:24:36 +02:00
|
|
|
InsertTokens(tok2, type0, typelen);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TOKEN *eq = tok2;
|
|
|
|
|
|
|
|
int parlevel = 0;
|
|
|
|
while (tok2)
|
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( strchr("{(", tok2->aaaa0()) )
|
2007-05-29 08:24:36 +02:00
|
|
|
{
|
|
|
|
parlevel++;
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( strchr("})", tok2->aaaa0()) )
|
2007-05-29 08:24:36 +02:00
|
|
|
{
|
|
|
|
if (parlevel<0)
|
|
|
|
break;
|
|
|
|
parlevel--;
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( parlevel==0 && strchr(";,",tok2->aaaa0()) )
|
2007-05-29 08:24:36 +02:00
|
|
|
{
|
|
|
|
// "type var =" => "type var; var ="
|
2008-03-22 12:46:06 +01:00
|
|
|
TOKEN *VarTok = _gettok(type0,typelen);
|
2008-11-24 20:38:08 +01:00
|
|
|
if (VarTok->aaaa0()=='*')
|
2008-12-08 22:49:05 +01:00
|
|
|
VarTok = VarTok->next();
|
2007-05-29 19:11:53 +02:00
|
|
|
InsertTokens(eq, VarTok, 2);
|
2008-11-06 19:31:39 +01:00
|
|
|
eq->setstr(";");
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
// "= x, " => "= x; type "
|
2008-12-05 20:04:41 +01:00
|
|
|
if (tok2->str() == ",")
|
2007-05-29 08:24:36 +02:00
|
|
|
{
|
2008-11-06 19:31:39 +01:00
|
|
|
tok2->setstr(";");
|
2007-05-29 08:24:36 +02:00
|
|
|
InsertTokens( tok2, type0, typelen );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
tok2 = tok2->next();
|
2007-05-29 08:24:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-20 20:18:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace NULL with 0..
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "NULL") )
|
2008-11-20 20:18:55 +01:00
|
|
|
tok->setstr("0");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace pointer casts of 0.. "(char *)0" => "0"
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
if ( TOKEN::Match(tok->next(), "( %type% * ) 0") || TOKEN::Match(tok->next(),"( %type% %type% * ) 0") )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
while (!TOKEN::Match(tok->next(),"0"))
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->deleteNext();
|
2008-11-20 20:18:55 +01:00
|
|
|
}
|
2008-11-22 20:47:10 +01:00
|
|
|
}
|
|
|
|
|
2008-12-14 18:05:21 +01:00
|
|
|
|
2008-12-13 18:57:36 +01:00
|
|
|
bool done = false;
|
|
|
|
while ( ! done )
|
2008-12-14 18:05:21 +01:00
|
|
|
{
|
2008-12-13 18:57:36 +01:00
|
|
|
done = true;
|
2008-12-14 18:05:21 +01:00
|
|
|
if( simplifyConditions() )
|
|
|
|
done = false;
|
|
|
|
|
|
|
|
if( simplifyCasts() )
|
|
|
|
done = false;
|
|
|
|
|
|
|
|
if( simplifyFunctionReturn() )
|
|
|
|
done = false;
|
2008-12-13 18:57:36 +01:00
|
|
|
}
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2008-11-22 20:47:10 +01:00
|
|
|
bool Tokenizer::simplifyConditions()
|
|
|
|
{
|
2008-12-13 19:54:48 +01:00
|
|
|
bool ret = false;
|
2008-11-22 20:47:10 +01:00
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
|
2008-11-22 20:47:10 +01:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
if (TOKEN::Match(tok, "( true &&") || TOKEN::Match(tok, "&& true &&") || TOKEN::Match(tok->next(), "&& true )"))
|
2008-11-22 20:47:10 +01:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
2008-12-13 19:54:48 +01:00
|
|
|
ret = true;
|
2008-11-22 20:47:10 +01:00
|
|
|
}
|
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
else if (TOKEN::Match(tok, "( false ||") || TOKEN::Match(tok, "|| false ||") || TOKEN::Match(tok->next(), "|| false )"))
|
2008-11-22 20:47:10 +01:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
2008-12-13 19:54:48 +01:00
|
|
|
ret = true;
|
2008-11-22 20:47:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change numeric constant in condition to "true" or "false"
|
2008-11-23 11:09:16 +01:00
|
|
|
const TOKEN *tok2 = tok->tokAt(2);
|
2008-11-22 23:49:14 +01:00
|
|
|
if ((TOKEN::Match(tok, "(") || TOKEN::Match(tok, "&&") || TOKEN::Match(tok, "||")) &&
|
2008-12-08 22:49:05 +01:00
|
|
|
TOKEN::Match(tok->next(), "%num%") &&
|
2008-11-22 23:49:14 +01:00
|
|
|
(TOKEN::Match(tok2, ")") || TOKEN::Match(tok2, "&&") || TOKEN::Match(tok2, "||")) )
|
2008-11-22 20:47:10 +01:00
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
tok->next()->setstr((tok->next()->str() != "0") ? "true" : "false");
|
2008-12-13 19:54:48 +01:00
|
|
|
ret = true;
|
2008-12-09 01:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce "(%num% == %num%)" => "(true)"/"(false)"
|
|
|
|
if ( (TOKEN::Match(tok, "&&") || TOKEN::Match(tok, "||") || TOKEN::Match(tok, "(")) &&
|
|
|
|
TOKEN::Match(tok->tokAt(1), "%num% %any% %num%") &&
|
|
|
|
(TOKEN::Match(tok->tokAt(4), "&&") || TOKEN::Match(tok->tokAt(4), "||") || TOKEN::Match(tok->tokAt(4), ")")) )
|
|
|
|
{
|
|
|
|
double op1 = (strstr(tok->strAt(1), "0x")) ? strtol(tok->strAt(1),0,16) : atof( tok->strAt(1) );
|
|
|
|
double op2 = (strstr(tok->strAt(3), "0x")) ? strtol(tok->strAt(3),0,16) : atof( tok->strAt(3) );
|
|
|
|
std::string cmp = tok->strAt(2);
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
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 = "";
|
|
|
|
|
|
|
|
if ( ! cmp.empty() )
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
|
|
|
|
tok->setstr( result ? "true" : "false" );
|
2008-12-13 19:54:48 +01:00
|
|
|
ret = true;
|
2008-12-09 01:49:32 +01:00
|
|
|
}
|
2008-11-22 20:47:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-12-14 18:05:21 +01:00
|
|
|
|
|
|
|
bool Tokenizer::simplifyCasts()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
|
|
|
|
{
|
|
|
|
if ( TOKEN::Match(tok->next(), "( %type% * )") )
|
|
|
|
{
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( TOKEN::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <" ) )
|
|
|
|
{
|
|
|
|
while ( tok->next() && tok->next()->str() != ">" )
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
TOKEN *tok2 = tok;
|
|
|
|
int parlevel = 0;
|
|
|
|
while ( tok2->next() && parlevel >= 0 )
|
|
|
|
{
|
|
|
|
tok2 = tok2->next();
|
|
|
|
if ( TOKEN::Match(tok2->next(), "(") )
|
|
|
|
++parlevel;
|
|
|
|
else if ( TOKEN::Match(tok2->next(), ")") )
|
|
|
|
--parlevel;
|
|
|
|
}
|
|
|
|
if (tok2->next())
|
|
|
|
tok2->deleteNext();
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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->setstr( tok->strAt(5) );
|
|
|
|
tok2->deleteNext();
|
|
|
|
tok2->deleteNext();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Tokenizer::simplifyKnownVariables()
|
|
|
|
{
|
|
|
|
// TODO, this function should be called from simplifyTokenList()
|
|
|
|
// after the implementation is done.
|
|
|
|
// TODO, this functions needs to be implemented.
|
|
|
|
// TODO, test
|
|
|
|
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 )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( TOKEN::Match(tok2, "%var% = %num% ;") )
|
|
|
|
{
|
|
|
|
unsigned int varid = tok2->varId();
|
|
|
|
TOKEN *tok3 = tok2;
|
|
|
|
while ( tok3 )
|
|
|
|
{
|
|
|
|
tok3 = tok3->next();
|
|
|
|
|
|
|
|
// Perhaps it's a loop => bail out
|
|
|
|
if ( TOKEN::Match(tok3, "[{}]") )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Variable is used somehow in a non-defined pattern => bail out
|
|
|
|
if ( tok3->varId() == varid )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Replace variable with numeric constant..
|
|
|
|
if ( TOKEN::Match(tok3, "if ( %varid% )", 0, 0, varid) )
|
|
|
|
{
|
|
|
|
tok3 = tok3->next()->next();
|
|
|
|
tok3->setstr( tok2->strAt(2) );
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2008-11-22 20:47:10 +01:00
|
|
|
|
2007-05-24 15:07:30 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Helper functions for handling the tokens list
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-29 19:11:53 +02:00
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
|
2008-11-20 20:18:55 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const TOKEN *Tokenizer::GetFunctionTokenByName( const char funcname[] ) const
|
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
for ( unsigned int i = 0; i < _functionList.size(); ++i )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
if ( _functionList[i]->str() == funcname )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
return _functionList[i];
|
2008-11-20 20:18:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 12:08:07 +01:00
|
|
|
void Tokenizer::fillFunctionList()
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
_functionList.clear();
|
2008-11-20 20:18:55 +01:00
|
|
|
|
|
|
|
bool staticfunc = false;
|
|
|
|
bool classfunc = false;
|
|
|
|
|
|
|
|
int indentlevel = 0;
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( const TOKEN *tok = _tokens; tok; tok = tok->next() )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
if ( tok->str() == "{" )
|
|
|
|
++indentlevel;
|
2008-11-20 20:18:55 +01:00
|
|
|
|
2008-12-05 20:04:41 +01:00
|
|
|
else if ( tok->str() == "}" )
|
|
|
|
--indentlevel;
|
2008-11-20 20:18:55 +01:00
|
|
|
|
|
|
|
if (indentlevel > 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if (strchr("};", tok->aaaa0()))
|
2008-11-20 20:18:55 +01:00
|
|
|
staticfunc = classfunc = false;
|
|
|
|
|
2008-12-05 20:04:41 +01:00
|
|
|
else if ( tok->str() == "static" )
|
2008-11-20 20:18:55 +01:00
|
|
|
staticfunc = true;
|
|
|
|
|
2008-12-05 20:04:41 +01:00
|
|
|
else if ( tok->str() == "::" )
|
2008-11-20 20:18:55 +01:00
|
|
|
classfunc = true;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok, "%var% ("))
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
|
|
|
// Check if this is the first token of a function implementation..
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
if ( tok2->str() == ";" )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
|
|
|
tok = tok2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-12-05 20:04:41 +01:00
|
|
|
else if ( tok2->str() == "{" )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-12-05 20:04:41 +01:00
|
|
|
else if ( tok2->str() == ")" )
|
2008-12-09 01:49:32 +01:00
|
|
|
{
|
|
|
|
if ( TOKEN::Match(tok2, ") const| {") )
|
|
|
|
{
|
|
|
|
_functionList.push_back( tok );
|
|
|
|
tok = tok2;
|
|
|
|
}
|
2008-11-20 20:18:55 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tok = tok2;
|
2008-12-08 22:49:05 +01:00
|
|
|
while (tok->next() && !strchr(";{", tok->next()->aaaa0()))
|
|
|
|
tok = tok->next();
|
2008-11-20 20:18:55 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-23 12:08:07 +01:00
|
|
|
// If the _functionList functions with duplicate names, remove them
|
2008-11-20 20:18:55 +01:00
|
|
|
// TODO this will need some better handling
|
2008-11-23 12:08:07 +01:00
|
|
|
for ( unsigned int func1 = 0; func1 < _functionList.size(); )
|
2008-12-09 01:49:32 +01:00
|
|
|
{
|
2008-11-20 20:18:55 +01:00
|
|
|
bool hasDuplicates = false;
|
2008-11-23 12:08:07 +01:00
|
|
|
for ( unsigned int func2 = func1 + 1; func2 < _functionList.size(); )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
2008-12-05 20:04:41 +01:00
|
|
|
if ( _functionList[func1]->str() == _functionList[func2]->str() )
|
2008-11-20 20:18:55 +01:00
|
|
|
{
|
|
|
|
hasDuplicates = true;
|
2008-11-23 12:08:07 +01:00
|
|
|
_functionList.erase( _functionList.begin() + func2 );
|
2008-11-20 20:18:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++func2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! hasDuplicates )
|
|
|
|
{
|
|
|
|
++func1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
_functionList.erase( _functionList.begin() + func1 );
|
2008-11-20 20:18:55 +01:00
|
|
|
}
|
2008-12-09 01:49:32 +01:00
|
|
|
}
|
2008-11-20 20:18:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2007-05-29 08:24:36 +02:00
|
|
|
|
2008-11-22 10:44:02 +01:00
|
|
|
// Deallocate lists..
|
|
|
|
void Tokenizer::DeallocateTokens()
|
|
|
|
{
|
|
|
|
deleteTokens( _tokens );
|
|
|
|
_tokens = 0;
|
2008-11-23 12:08:07 +01:00
|
|
|
_tokensBack = 0;
|
2008-11-22 10:44:02 +01:00
|
|
|
|
2008-11-23 12:08:07 +01:00
|
|
|
while (_dsymlist)
|
2008-11-22 10:44:02 +01:00
|
|
|
{
|
2008-11-23 12:08:07 +01:00
|
|
|
struct DefineSymbol *next = _dsymlist->next;
|
|
|
|
free(_dsymlist->name);
|
|
|
|
free(_dsymlist->value);
|
|
|
|
delete _dsymlist;
|
|
|
|
_dsymlist = next;
|
2008-11-22 10:44:02 +01:00
|
|
|
}
|
|
|
|
|
2008-11-23 12:08:07 +01:00
|
|
|
_files.clear();
|
2008-11-22 10:44:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tokenizer::deleteTokens(TOKEN *tok)
|
|
|
|
{
|
|
|
|
while (tok)
|
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
TOKEN *next = tok->next();
|
2008-11-22 10:44:02 +01:00
|
|
|
delete tok;
|
|
|
|
tok = next;
|
|
|
|
}
|
2008-12-09 01:49:32 +01:00
|
|
|
}
|
2008-11-22 10:44:02 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const char *Tokenizer::getParameterName( const TOKEN *ftok, int par )
|
|
|
|
{
|
|
|
|
int _par = 1;
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( ; ftok; ftok = ftok->next())
|
2008-11-22 10:44:02 +01:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(ftok, ",") )
|
2008-11-22 10:44:02 +01:00
|
|
|
++_par;
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( par==_par && TOKEN::Match(ftok, "%var% [,)]") )
|
2008-11-24 20:38:08 +01:00
|
|
|
return ftok->aaaa();
|
2008-11-22 10:44:02 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-22 20:47:10 +01:00
|
|
|
std::string Tokenizer::fileLine( const TOKEN *tok ) const
|
2008-11-22 10:44:02 +01:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-12-08 23:02:37 +01:00
|
|
|
ostr << "[" << _files.at(tok->fileIndex()) << ":" << tok->linenr() << "]";
|
2008-11-22 10:44:02 +01:00
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool Tokenizer::SameFileName( const char fname1[], const char fname2[] )
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
return bool( strcmp(fname1, fname2) == 0 );
|
|
|
|
#endif
|
|
|
|
#ifdef __GNUC__
|
|
|
|
return bool( strcasecmp(fname1, fname2) == 0 );
|
|
|
|
#endif
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
return bool( stricmp(fname1, fname2) == 0 );
|
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return bool( _stricmp(fname1, fname2) == 0 );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|