Improving simplifyKnownVariables() to simplify bool variables used inside if() like int values are simplified.
This commit is contained in:
parent
aec3584fce
commit
b7db651ee2
18
token.cpp
18
token.cpp
|
@ -37,6 +37,7 @@ TOKEN::TOKEN()
|
||||||
_varId = 0;
|
_varId = 0;
|
||||||
_isName = false;
|
_isName = false;
|
||||||
_isNumber = false;
|
_isNumber = false;
|
||||||
|
_isBoolean = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN::~TOKEN()
|
TOKEN::~TOKEN()
|
||||||
|
@ -55,6 +56,11 @@ void TOKEN::setstr( const char s[] )
|
||||||
#endif
|
#endif
|
||||||
_isName = bool(_str[0]=='_' || isalpha(_str[0]));
|
_isName = bool(_str[0]=='_' || isalpha(_str[0]));
|
||||||
_isNumber = bool(isdigit(_str[0]) != 0);
|
_isNumber = bool(isdigit(_str[0]) != 0);
|
||||||
|
if( _str == "true" || _str == "false" )
|
||||||
|
_isBoolean = true;
|
||||||
|
else
|
||||||
|
_isBoolean = false;
|
||||||
|
|
||||||
_varId = 0;
|
_varId = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,10 +233,15 @@ bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[]
|
||||||
|
|
||||||
else if (strcmp(str,"%num%")==0)
|
else if (strcmp(str,"%num%")==0)
|
||||||
{
|
{
|
||||||
if ( ! tok->isNumber() )
|
if ( !tok->isNumber() )
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (strcmp(str,"%bool%")==0)
|
||||||
|
{
|
||||||
|
if ( !tok->isBoolean() )
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
else if (strcmp(str,"%str%")==0)
|
else if (strcmp(str,"%str%")==0)
|
||||||
{
|
{
|
||||||
|
@ -297,6 +308,11 @@ bool TOKEN::isNumber() const
|
||||||
return _isNumber;
|
return _isNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool TOKEN::isBoolean() const
|
||||||
|
{
|
||||||
|
return _isBoolean;
|
||||||
|
}
|
||||||
|
|
||||||
bool TOKEN::isStandardType() const
|
bool TOKEN::isStandardType() const
|
||||||
{
|
{
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
3
token.h
3
token.h
|
@ -68,6 +68,7 @@ public:
|
||||||
* "%any%" any token
|
* "%any%" any token
|
||||||
* "%var%" any token which is a name or type e.g. "hello" or "int"
|
* "%var%" any token which is a name or type e.g. "hello" or "int"
|
||||||
* "%num%" Any numeric token, e.g. "23"
|
* "%num%" Any numeric token, e.g. "23"
|
||||||
|
* "%bool%" true or false
|
||||||
* "%str%" Any token starting with "-character (C-string).
|
* "%str%" Any token starting with "-character (C-string).
|
||||||
* "%var1%" Match with parameter varname1
|
* "%var1%" Match with parameter varname1
|
||||||
* "%var2%" Match with parameter varname2
|
* "%var2%" Match with parameter varname2
|
||||||
|
@ -96,6 +97,7 @@ public:
|
||||||
|
|
||||||
bool isName() const;
|
bool isName() const;
|
||||||
bool isNumber() const;
|
bool isNumber() const;
|
||||||
|
bool isBoolean() const;
|
||||||
bool isStandardType() const;
|
bool isStandardType() const;
|
||||||
static const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
|
static const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
|
||||||
|
|
||||||
|
@ -172,6 +174,7 @@ private:
|
||||||
char * _cstr;
|
char * _cstr;
|
||||||
bool _isName;
|
bool _isName;
|
||||||
bool _isNumber;
|
bool _isNumber;
|
||||||
|
bool _isBoolean;
|
||||||
unsigned int _varId;
|
unsigned int _varId;
|
||||||
TOKEN *_next;
|
TOKEN *_next;
|
||||||
TOKEN *_previous;
|
TOKEN *_previous;
|
||||||
|
|
|
@ -1200,8 +1200,10 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
|
|
||||||
// parse the block of code..
|
// parse the block of code..
|
||||||
int indentlevel = 0;
|
int indentlevel = 0;
|
||||||
|
|
||||||
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
|
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
|
||||||
{
|
{
|
||||||
|
|
||||||
if ( tok2->str() == "{" )
|
if ( tok2->str() == "{" )
|
||||||
++indentlevel;
|
++indentlevel;
|
||||||
|
|
||||||
|
@ -1212,7 +1214,8 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( TOKEN::Match(tok2, "%var% = %num% ;") )
|
else if ( TOKEN::Match(tok2, "%var% = %num% ;") ||
|
||||||
|
TOKEN::Match(tok2, "%var% = %bool% ;"))
|
||||||
{
|
{
|
||||||
unsigned int varid = tok2->varId();
|
unsigned int varid = tok2->varId();
|
||||||
for ( TOKEN *tok3 = tok2->next(); tok3; tok3 = tok3->next() )
|
for ( TOKEN *tok3 = tok2->next(); tok3; tok3 = tok3->next() )
|
||||||
|
|
Loading…
Reference in New Issue