Fix #754 (Tokenizer: transform modifier "signed" to "int")

http://sourceforge.net/apps/trac/cppcheck/ticket/754
Also fix int unsigned -> unsigned int
This commit is contained in:
Reijo Tomperi 2009-09-28 21:25:05 +03:00
parent b9237db9a3
commit 2d4404f030
3 changed files with 96 additions and 5 deletions

View File

@ -2714,21 +2714,66 @@ void Tokenizer::unsignedint()
{
for (Token *tok = _tokens; tok; tok = tok->next())
{
// A variable declaration where the "int" is left out?
if (!Token::Match(tok, "unsigned %var% [;,=]"))
if (!Token::Match(tok, "unsigned|signed"))
continue;
// Previous token should either be a symbol or one of "{};("
if (Token::Match(tok->previous(), "%type% unsigned|signed %var% [;,=)]") &&
tok->previous()->isStandardType())
{
if (tok->str() == "signed")
{
// int signed a; -> int a;
tok = tok->previous();
tok->deleteNext();
}
else
{
// int unsigned a; -> unsigned int a;
std::string temp = tok->str();
tok->str(tok->previous()->str());
tok->previous()->str(temp);
}
continue;
}
// signed int a; -> int a;
if (Token::Match(tok, "signed %type% %var% [;,=)]"))
{
if (tok->next()->isStandardType())
{
tok->str(tok->next()->str());
tok->deleteNext();
continue;
}
}
// A variable declaration where the "int" is left out?
else if (!Token::Match(tok, "unsigned|signed %var% [;,=)]"))
continue;
// Previous token should either be a symbol or one of "{};(,"
if (tok->previous() &&
!tok->previous()->isName() &&
!Token::Match(tok->previous(), "[{};(]"))
!Token::Match(tok->previous(), "[{};(,]"))
continue;
// next token should not be a standard type?
if (tok->next()->isStandardType())
{
if (tok->str() == "signed")
{
tok->str(tok->next()->str());
tok->deleteNext();
}
continue;
}
// The "int" is missing.. add it
if (tok->str() == "signed")
tok->str("int");
else
tok->insertToken("int");
}
}

View File

@ -129,6 +129,8 @@ private:
/**
* insert an "int" after "unsigned" if needed:
* "unsigned i" => "unsigned int i"
* "signed int i" => "int i"
* "signed i" => "int i"
*/
void unsignedint();

View File

@ -154,6 +154,7 @@ private:
TEST_CASE(unsigned1);
TEST_CASE(testUpdateClassList);
TEST_CASE(createLinks);
TEST_CASE(signed1);
}
@ -2367,6 +2368,43 @@ private:
}
/**
* tokenize "signed i" => "signed int i"
*/
void signed1()
{
{
const char code[] = "void foo ( signed int , signed float ) ;";
const char code2[] = "void foo ( int , float ) ;";
ASSERT_EQUALS(code2, tokenizeAndStringify(code));
}
{
const char code1[] = "signed i ;";
const char code2[] = "int i ;";
ASSERT_EQUALS(code2, tokenizeAndStringify(code1));
}
{
const char code1[] = "signed int i ;";
const char code2[] = "int i ;";
ASSERT_EQUALS(code2, tokenizeAndStringify(code1));
}
{
const char code1[] = "int signed i ;";
const char code2[] = "int i ;";
ASSERT_EQUALS(code2, tokenizeAndStringify(code1));
}
{
const char code1[] = "for (signed i=0; i<10; i++)";
const char code2[] = "for ( int i = 0 ; i < 10 ; i ++ )";
ASSERT_EQUALS(code2, tokenizeAndStringify(code1));
}
}
/**
* tokenize "unsigned i" => "unsigned int i"
* tokenize "unsigned int" => "unsigned int"
@ -2386,6 +2424,12 @@ private:
ASSERT_EQUALS(code2, tokenizeAndStringify(code1));
}
{
const char code1[] = "int unsigned i ;";
const char code2[] = "unsigned int i ;";
ASSERT_EQUALS(code2, tokenizeAndStringify(code1));
}
// insert "int" after "unsigned"..
{
const char code1[] = "for (unsigned i=0; i<10; i++)";