2008-11-24 09:59:10 +01:00
|
|
|
/*
|
2008-11-24 08:35:03 +01:00
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007-2008 Daniel Marjamäki and Reijo Tomperi
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "token.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2008-12-09 23:03:05 +01:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2008-11-24 08:35:03 +01:00
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#include <ctype.h> // isalpha, isdigit
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TOKEN::TOKEN()
|
|
|
|
{
|
2008-12-08 23:02:37 +01:00
|
|
|
_fileIndex = 0;
|
2008-12-09 01:49:32 +01:00
|
|
|
_cstr = 0;
|
2008-11-24 20:38:08 +01:00
|
|
|
_str = "";
|
2008-12-08 23:02:37 +01:00
|
|
|
_linenr = 0;
|
2008-12-09 18:31:04 +01:00
|
|
|
_next = 0;
|
|
|
|
_previous = 0;
|
2008-12-08 22:49:05 +01:00
|
|
|
_varId = 0;
|
2008-11-24 08:35:03 +01:00
|
|
|
_isName = false;
|
2008-12-09 18:31:04 +01:00
|
|
|
_isNumber = false;
|
2008-11-24 08:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TOKEN::~TOKEN()
|
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
std::free(_cstr);
|
2008-11-24 08:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::setstr( const char s[] )
|
2008-12-09 01:49:32 +01:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
_str = s;
|
|
|
|
std::free(_cstr);
|
2008-11-24 08:35:03 +01:00
|
|
|
#ifndef _MSC_VER
|
2008-11-24 20:38:08 +01:00
|
|
|
_cstr = strdup(s);
|
2008-11-24 08:35:03 +01:00
|
|
|
#else
|
2008-11-24 20:38:08 +01:00
|
|
|
_cstr = _strdup(s);
|
2008-11-24 08:35:03 +01:00
|
|
|
#endif
|
2008-11-24 20:38:08 +01:00
|
|
|
_isName = bool(_str[0]=='_' || isalpha(_str[0]));
|
2008-12-04 22:48:15 +01:00
|
|
|
_isNumber = bool(isdigit(_str[0]) != 0);
|
2008-11-24 08:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::combineWithNext(const char str1[], const char str2[])
|
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
if (!(_next))
|
2008-11-24 08:35:03 +01:00
|
|
|
return;
|
2008-12-08 22:49:05 +01:00
|
|
|
if (_str!=str1 || _next->_str!=str2)
|
2008-11-24 08:35:03 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::string newstr(std::string(str1) + std::string(str2));
|
|
|
|
setstr( newstr.c_str() );
|
|
|
|
deleteNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::deleteNext()
|
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
TOKEN *n = _next;
|
|
|
|
_next = n->next();
|
2008-11-24 08:35:03 +01:00
|
|
|
delete n;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TOKEN *TOKEN::tokAt(int index) const
|
|
|
|
{
|
|
|
|
const TOKEN *tok = this;
|
|
|
|
while (index>0 && tok)
|
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
tok = tok->next();
|
2008-11-24 08:35:03 +01:00
|
|
|
index--;
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *TOKEN::strAt(int index) const
|
|
|
|
{
|
|
|
|
const TOKEN *tok = this->tokAt(index);
|
2008-11-24 20:38:08 +01:00
|
|
|
return tok ? tok->_cstr : "";
|
2008-11-24 08:35:03 +01:00
|
|
|
}
|
2008-12-09 01:49:32 +01:00
|
|
|
|
|
|
|
int TOKEN::multiCompare( const char *needle, const char *haystack )
|
|
|
|
{
|
|
|
|
bool emptyStringFound = false;
|
|
|
|
bool findNextOr = false;
|
|
|
|
const char *haystackPointer = haystack;
|
|
|
|
for( ; *needle; ++needle )
|
|
|
|
{
|
|
|
|
if( *needle == '|' )
|
|
|
|
{
|
|
|
|
// If needle and haystack are both at the end, we have a match.
|
|
|
|
if( *haystackPointer == 0 )
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
haystackPointer = haystack;
|
|
|
|
if( findNextOr )
|
|
|
|
findNextOr = false;
|
|
|
|
else
|
|
|
|
emptyStringFound = true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( findNextOr )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If haystack and needle don't share the same character, reset
|
|
|
|
// haystackpointer and find next '|' character.
|
|
|
|
if( *haystackPointer != *needle )
|
|
|
|
{
|
|
|
|
haystackPointer = haystack;
|
|
|
|
findNextOr = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All characters in haystack and needle have matched this far
|
|
|
|
haystackPointer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If both needle and haystack are at the end, then we have a match.
|
|
|
|
if( *haystackPointer == 0 )
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// If empty string was found or if last character in needle was '|'
|
|
|
|
if( emptyStringFound || findNextOr == false )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-11-24 08:35:03 +01:00
|
|
|
|
2008-12-09 08:25:44 +01:00
|
|
|
bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[], unsigned int varid)
|
2008-11-24 08:35:03 +01:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char *p = pattern;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
// Skip spaces in pattern..
|
|
|
|
while ( *p == ' ' )
|
|
|
|
p++;
|
|
|
|
|
2008-12-09 01:49:32 +01:00
|
|
|
// Extract token from pattern..
|
2008-12-06 14:21:33 +01:00
|
|
|
// TODO: Refactor this so there can't be buffer overflows
|
|
|
|
char str[500];
|
2008-11-24 08:35:03 +01:00
|
|
|
char *s = str;
|
|
|
|
while (*p && *p!=' ')
|
|
|
|
{
|
|
|
|
*s = *p;
|
|
|
|
s++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
|
|
|
|
// No token => Success!
|
|
|
|
if (str[0] == 0)
|
|
|
|
return true;
|
|
|
|
|
2008-12-08 23:49:11 +01:00
|
|
|
bool useVar1;
|
2008-11-24 08:35:03 +01:00
|
|
|
// Any symbolname..
|
|
|
|
if (strcmp(str,"%var%")==0 || strcmp(str,"%type%")==0)
|
|
|
|
{
|
|
|
|
if (!tok->isName())
|
|
|
|
return false;
|
2008-12-09 01:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accept any token
|
|
|
|
else if (strcmp(str,"%any%")==0 )
|
|
|
|
{
|
|
|
|
|
2008-11-24 08:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Variable name..
|
2008-12-08 23:49:11 +01:00
|
|
|
else if ((useVar1 = (strcmp(str,"%var1%")==0)) || strcmp(str,"%var2%")==0)
|
2008-11-24 08:35:03 +01:00
|
|
|
{
|
2008-12-08 23:49:11 +01:00
|
|
|
const char **varname = useVar1 ? varname1 : varname2;
|
2008-11-24 08:35:03 +01:00
|
|
|
|
|
|
|
if ( ! varname )
|
|
|
|
return false;
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if (tok->_str != varname[0])
|
2008-11-24 08:35:03 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for ( int i = 1; varname[i]; i++ )
|
|
|
|
{
|
|
|
|
if ( !(tok->tokAt(2)) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( strcmp(tok->strAt( 1), ".") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( strcmp(tok->strAt( 2), varname[i]) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-09 08:25:44 +01:00
|
|
|
else if (strcmp(str,"%varid%")==0)
|
|
|
|
{
|
|
|
|
if ( tok->varId() != varid )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-24 08:35:03 +01:00
|
|
|
else if (strcmp(str,"%num%")==0)
|
|
|
|
{
|
|
|
|
if ( ! tok->isNumber() )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else if (strcmp(str,"%str%")==0)
|
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->_str[0] != '\"' )
|
2008-11-24 08:35:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [.. => search for a one-character token..
|
2008-11-24 20:38:08 +01:00
|
|
|
else if (str[0]=='[' && strchr(str, ']') && tok->_str[1] == 0)
|
2008-11-24 08:35:03 +01:00
|
|
|
{
|
|
|
|
*strrchr(str, ']') = 0;
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( strchr( str + 1, tok->_str[0] ) == 0 )
|
2008-11-24 08:35:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
2008-12-09 01:49:32 +01:00
|
|
|
|
|
|
|
// Parse multi options, such as void|int|char (accept token which is one of these 3)
|
|
|
|
else if ( strchr(str, '|') && strlen( str ) > 2 )
|
|
|
|
{
|
|
|
|
int res = multiCompare( str, tok->_cstr );
|
|
|
|
if( res == 0 )
|
|
|
|
{
|
|
|
|
// Empty alternative matches, use the same token on next round
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if( res == -1 )
|
|
|
|
{
|
|
|
|
// No match
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-11-24 08:35:03 +01:00
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
else if (str != tok->_str)
|
2008-11-24 08:35:03 +01:00
|
|
|
return false;
|
|
|
|
|
2008-12-08 22:49:05 +01:00
|
|
|
tok = tok->next();
|
2008-11-24 08:35:03 +01:00
|
|
|
if (!tok && *p)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The end of the pattern has been reached and nothing wrong has been found
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TOKEN::isName() const
|
|
|
|
{
|
|
|
|
return _isName;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TOKEN::isNumber() const
|
|
|
|
{
|
|
|
|
return _isNumber;
|
|
|
|
}
|
|
|
|
|
2008-11-26 08:22:34 +01:00
|
|
|
bool TOKEN::isStandardType() const
|
2008-12-09 01:49:32 +01:00
|
|
|
{
|
2008-11-26 08:22:34 +01:00
|
|
|
bool ret = false;
|
2008-11-24 08:35:03 +01:00
|
|
|
const char *type[] = {"bool","char","short","int","long","float","double",0};
|
|
|
|
for (int i = 0; type[i]; i++)
|
2008-11-26 08:22:34 +01:00
|
|
|
ret |= (_str == type[i]);
|
|
|
|
return ret;
|
2008-11-24 08:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const TOKEN *TOKEN::findmatch(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
|
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for ( ; tok; tok = tok->next())
|
2008-11-24 08:35:03 +01:00
|
|
|
{
|
|
|
|
if ( TOKEN::Match(tok, pattern, varname1, varname2) )
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TOKEN *TOKEN::findtoken(const TOKEN *tok1, const char *tokenstr[])
|
|
|
|
{
|
2008-12-08 22:49:05 +01:00
|
|
|
for (const TOKEN *ret = tok1; ret; ret = ret->next())
|
2008-11-24 08:35:03 +01:00
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
const TOKEN *tok = ret;
|
|
|
|
while (tokenstr[i])
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return NULL;
|
2008-11-24 20:38:08 +01:00
|
|
|
if (*(tokenstr[i]) && (tokenstr[i] != tok->_str))
|
2008-11-24 08:35:03 +01:00
|
|
|
break;
|
2008-12-08 22:49:05 +01:00
|
|
|
tok = tok->next();
|
2008-11-24 08:35:03 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (!tokenstr[i])
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-09 01:49:32 +01:00
|
|
|
|
|
|
|
unsigned int TOKEN::varId() const
|
|
|
|
{
|
|
|
|
return _varId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::varId( unsigned int id )
|
|
|
|
{
|
|
|
|
_varId = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOKEN *TOKEN::next() const
|
|
|
|
{
|
|
|
|
return _next;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::next( TOKEN *next )
|
|
|
|
{
|
|
|
|
_next = next;
|
|
|
|
}
|
2008-12-09 18:31:04 +01:00
|
|
|
|
|
|
|
TOKEN *TOKEN::previous() const
|
|
|
|
{
|
|
|
|
return _previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::previous( TOKEN *previous )
|
|
|
|
{
|
|
|
|
_previous = previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::insertToken( const char *str )
|
|
|
|
{
|
|
|
|
TOKEN *newToken = new TOKEN;
|
|
|
|
newToken->setstr( str );
|
|
|
|
if( this->next() )
|
|
|
|
{
|
|
|
|
newToken->next( this->next() );
|
|
|
|
newToken->next()->previous( newToken );
|
|
|
|
}
|
|
|
|
|
|
|
|
this->next( newToken );
|
|
|
|
newToken->previous( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::eraseTokens( TOKEN *begin, const TOKEN *end )
|
|
|
|
{
|
|
|
|
if ( ! begin )
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ( begin->next() && begin->next() != end )
|
|
|
|
{
|
|
|
|
begin->eraseToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::eraseToken()
|
|
|
|
{
|
|
|
|
TOKEN *next = this->next();
|
|
|
|
this->next( next->next() );
|
2008-12-12 01:18:57 +01:00
|
|
|
if ( next->next() )
|
|
|
|
next->next()->previous( this );
|
2008-12-09 18:31:04 +01:00
|
|
|
delete next;
|
|
|
|
}
|
2008-12-09 01:49:32 +01:00
|
|
|
|
|
|
|
unsigned int TOKEN::fileIndex() const
|
|
|
|
{
|
|
|
|
return _fileIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::fileIndex( unsigned int fileIndex )
|
|
|
|
{
|
|
|
|
_fileIndex = fileIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int TOKEN::linenr() const
|
|
|
|
{
|
|
|
|
return _linenr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::linenr( unsigned int linenr )
|
|
|
|
{
|
|
|
|
_linenr = linenr;
|
|
|
|
}
|
2008-12-09 23:03:05 +01:00
|
|
|
|
|
|
|
void TOKEN::printOut( const char *title ) const
|
|
|
|
{
|
|
|
|
std::cout << std::endl << "###";
|
2008-12-12 01:18:57 +01:00
|
|
|
if ( title )
|
2008-12-09 23:03:05 +01:00
|
|
|
std::cout << " " << title << " ";
|
|
|
|
else
|
|
|
|
std::cout << "########";
|
|
|
|
|
|
|
|
std::cout << "###" << std::endl;
|
|
|
|
for( const TOKEN *t = this; t; t = t->next() )
|
|
|
|
{
|
|
|
|
std::cout << t->linenr() << ": " << t->str();
|
2008-12-12 01:18:57 +01:00
|
|
|
if ( t->varId() )
|
2008-12-09 23:03:05 +01:00
|
|
|
std::cout << " ("<< t->varId() <<")";
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|