tokenize: handle simple typedefs

This commit is contained in:
Daniel Marjamäki 2008-08-28 06:37:11 +00:00
parent 778410ccbf
commit 3daf128c0f
2 changed files with 54 additions and 102 deletions

View File

@ -36,6 +36,7 @@ public:
{ {
TEST_CASE( division1 ); TEST_CASE( division1 );
TEST_CASE( division2 ); TEST_CASE( division2 );
TEST_CASE( division3 );
} }
void division1() void division1()
@ -59,6 +60,19 @@ public:
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string("[test.cpp:5]: Warning: Division with signed and unsigned operators\n"), errout.str() ); ASSERT_EQUALS( std::string("[test.cpp:5]: Warning: Division with signed and unsigned operators\n"), errout.str() );
} }
void division3()
{
check( "typedef int s32;\n"
"typedef unsigned int u32;\n"
"void f()\n"
"{\n"
" s32 ivar = -2;\n"
" u32 uvar = 2;\n"
" return uvar / ivar;\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:7]: Warning: Division with signed and unsigned operators\n"), errout.str() );
}
}; };
REGISTER_FIXTURE( TestDivision ) REGISTER_FIXTURE( TestDivision )

View File

@ -564,6 +564,46 @@ void TokenizeCode(std::istream &code, const unsigned int FileIndex)
strcpy( tok->str, "." ); strcpy( tok->str, "." );
} }
} }
// typedef..
for ( TOKEN *tok = tokens; tok; tok = tok->next )
{
if (Match(tok, "typedef %type% %type% ;"))
{
const char *type1 = getstr(tok, 1);
const char *type2 = getstr(tok, 2);
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
{
if (tok2->str!=type2 && strcmp(tok2->str,type2)==0)
{
free(tok2->str);
tok2->str = strdup(type1);
}
}
}
else if (Match(tok, "typedef %type% %type% %type% ;"))
{
const char *type1 = getstr(tok, 1);
const char *type2 = getstr(tok, 2);
const char *type3 = getstr(tok, 3);
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
{
if (tok2->str!=type3 && strcmp(tok2->str,type3)==0)
{
free(tok2->str);
tok2->str = strdup(type1);
TOKEN *newtok = new TOKEN;
newtok->str = strdup(type2);
newtok->FileIndex = tok2->FileIndex;
newtok->linenr = tok2->linenr;
newtok->next = tok2->next;
tok2->next = newtok;
}
}
}
}
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -610,108 +650,6 @@ void SimplifyTokenList()
} }
/*
// typedefs..
TOKEN *prev = NULL;
for (TOKEN *tok = tokens; tok; tok = tok->next)
{
if (!prev)
tok = tokens;
if ( strcmp(tok->str, "typedef") == 0 )
{
TOKEN *type0 = tok->next;
int len = 0, parlevel = 0;
for ( TOKEN *tok2 = gettok(type0,1); tok2; tok2 = tok2->next)
{
if (strchr("{(", tok2->str[0]))
parlevel++;
else if (strchr("})", tok2->str[0]))
parlevel--;
else if (parlevel==0 && tok2->str[0]==';')
break;
len++;
}
const char *typestr = getstr(type0, len);
if (typestr[0] != ')') // Function pointer
{
// Replace tokens..
for ( TOKEN *tok2 = gettok(type0, len+1); tok2; tok2 = tok2->next )
{
if (strcmp(tok2->str,"typedef")==0)
{
int parlevel = 0;
while (parlevel > 0 || tok2->next->str[0] != ';')
{
if (tok2->str[0] == '{')
parlevel++;
else if (tok2->str[0] == '}')
parlevel--;
tok2 = tok2->next;
if (!tok2->next)
break;
}
continue;
}
TOKEN *next = tok2->next;
if (next && strcmp(next->str, typestr) == 0)
{
DeleteNextToken(tok2);
InsertTokens( tok2, type0, len );
tok2 = gettok(tok2, len);
}
}
}
// Delete typedef..
if (!prev)
{
int parlevel = 0;
while ( parlevel > 0 || tokens->str[0] != ';' )
{
if ( strchr( "({", tokens->str[0] ) )
parlevel++;
else if ( strchr( ")}", tokens->str[0] ) )
parlevel--;
// Delete the first element in the tokens list.
TOKEN *next = tokens->next;
free(tokens->str);
delete tokens;
tokens = next;
if (!tokens)
break;
}
tok = tokens;
prev = NULL;
continue;
}
else
{
int parlevel = 0;
while ( parlevel > 0 || prev->next->str[0] != ';' )
{
if ( strchr( "({", prev->next->str[0] ) )
parlevel++;
else if ( strchr( ")}", prev->next->str[0] ) )
parlevel--;
DeleteNextToken(prev);
if (!prev->next)
break;
}
tok = prev;
}
}
prev = tok;
}
*/
// Fill the map TypeSize.. // Fill the map TypeSize..
TypeSize.clear(); TypeSize.clear();
TypeSize["char"] = sizeof(char); TypeSize["char"] = sizeof(char);