tokenizer: Added functions "alwaysTrue" and "alwaysFalse" to check if a

condition is always true / false
This commit is contained in:
Daniel Marjamäki 2008-11-22 09:32:57 +00:00
parent 125692bc65
commit 9ce8918895
3 changed files with 62 additions and 2 deletions

View File

@ -41,7 +41,10 @@ private:
TEST_CASE( inlineasm ); TEST_CASE( inlineasm );
TEST_CASE( dupfuncname ); TEST_CASE( dupfuncname );
TEST_CASE( alwaysTrue );
TEST_CASE( alwaysFalse );
} }
@ -155,6 +158,47 @@ private:
ASSERT_EQUALS( 1, tokenizer.FunctionList.size() ); ASSERT_EQUALS( 1, tokenizer.FunctionList.size() );
ASSERT_EQUALS( std::string("b"), tokenizer.FunctionList[0]->str ); ASSERT_EQUALS( std::string("b"), tokenizer.FunctionList[0]->str );
}
bool alwaysTrueCheck(const char condition[])
{
// tokenize..
Tokenizer tokenizer( this );
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(condition);
tokenizer.TokenizeCode(istr, 0);
// check..
return tokenizer.alwaysTrue( tokenizer.tokens() );
}
void alwaysTrue()
{
ASSERT_EQUALS(true, alwaysTrueCheck("(1)"));
ASSERT_EQUALS(true, alwaysTrueCheck("(true)"));
ASSERT_EQUALS(true, alwaysTrueCheck("(true || false)"));
ASSERT_EQUALS(false, alwaysTrueCheck("(true && false)"));
}
bool alwaysFalseCheck(const char condition[])
{
// tokenize..
Tokenizer tokenizer( this );
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(condition);
tokenizer.TokenizeCode(istr, 0);
// check..
return tokenizer.alwaysFalse( tokenizer.tokens() );
}
void alwaysFalse()
{
ASSERT_EQUALS(true, alwaysFalseCheck("(0)"));
ASSERT_EQUALS(true, alwaysFalseCheck("(false)"));
ASSERT_EQUALS(true, alwaysFalseCheck("(false && true)"));
ASSERT_EQUALS(false, alwaysFalseCheck("(false || true)"));
} }
}; };

View File

@ -1468,7 +1468,7 @@ bool Tokenizer::Match(const TOKEN *tok, const char pattern[], const char *varnam
return false; return false;
tok = tok->next; tok = tok->next;
if (!tok) if (!tok && *p)
return false; return false;
} }
@ -1517,3 +1517,17 @@ bool Tokenizer::IsStandardType(const char str[])
Ret |= (strcmp(str,type[i])==0); Ret |= (strcmp(str,type[i])==0);
return Ret; return Ret;
} }
//---------------------------------------------------------------------------
bool Tokenizer::alwaysTrue( const TOKEN *tok )
{
return (Match(tok,"( 1 [|)]") | Match(tok,"( 1 ||") |
Match(tok,"( true [|)]") | Match(tok,"( true ||"));
}
//---------------------------------------------------------------------------
bool Tokenizer::alwaysFalse( const TOKEN *tok )
{
return (Match(tok,"( 0 [&)]") | Match(tok,"( 0 &&") |
Match(tok,"( false [&)]") | Match(tok,"( false &&"));
}
//---------------------------------------------------------------------------

View File

@ -90,6 +90,8 @@ public:
static bool IsNumber(const char str[]); static bool IsNumber(const char str[]);
static bool IsStandardType(const char str[]); static bool IsStandardType(const char str[]);
bool alwaysTrue( const TOKEN *tok );
bool alwaysFalse( const TOKEN *tok );
std::string fileLine( const TOKEN *tok ); std::string fileLine( const TOKEN *tok );