Variable Id : First simple implementation

This commit is contained in:
Daniel Marjamäki 2008-12-08 17:28:44 +00:00
parent 29afcaebcd
commit 3619552e38
4 changed files with 95 additions and 2 deletions

View File

@ -50,7 +50,9 @@ private:
TEST_CASE( multi_compare );
TEST_CASE( match1 );
TEST_CASE( match1 );
TEST_CASE( varid1 );
}
@ -259,6 +261,39 @@ private:
// Match..
ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "%var% || %var%") );
}
}
void varid1()
{
const std::string code(";static int i = 1;\n"
"void f()\n"
"{\n"
" int i = 2;\n"
" for (int i = 0; i < 10; ++i)\n"
" i = 3;\n"
" i = 4;\n"
"}\n" );
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next )
{
if ( tok->str() != "i" )
ASSERT_EQUALS( 0, tok->varId );
else if ( TOKEN::Match(tok, "i = 1") )
ASSERT_EQUALS( 1, tok->varId );
else if ( TOKEN::Match(tok, "i = 2") )
ASSERT_EQUALS( 2, tok->varId );
else if ( TOKEN::Match(tok, "i = 3") )
ASSERT_EQUALS( 3, tok->varId );
else if ( TOKEN::Match(tok, "i = 4") )
ASSERT_EQUALS( 2, tok->varId );
}
}
};

View File

@ -113,6 +113,9 @@ public:
unsigned int FileIndex;
unsigned int linenr;
unsigned int varId;
TOKEN *next;
private:

View File

@ -618,6 +618,58 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex)
}
//---------------------------------------------------------------------------
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;
while ( ! TOKEN::Match( tok2, "[;[=(]" ) )
{
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;
}
}
}
}
//---------------------------------------------------------------------------
// Simplify token list

View File

@ -49,7 +49,10 @@ public:
* @param code input stream for code
* @param FileName The filename
*/
void tokenize(std::istream &code, const char FileName[]);
void tokenize(std::istream &code, const char FileName[]);
/** Set variable id */
void setVarId();
/** Simplify tokenlist */
void simplifyTokenList();