Fix #153 (Unsigned divide)

The "unsigned i" variable declaration wasn't handled well. So I added an "int" token.
This commit is contained in:
Daniel Marjamäki 2009-06-06 10:40:48 +02:00
parent d0757c5797
commit dd473b074a
3 changed files with 58 additions and 0 deletions

View File

@ -473,6 +473,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[])
}
}
// replace "unsigned i" with "unsigned int i"
unsignedint();
simplifyVarDecl();
// Handle templates..
@ -2214,6 +2217,30 @@ bool Tokenizer::simplifyVarDecl()
}
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% [;,=]"))
continue;
// Previous token should either be a symbol or one of "{};"
if (tok->previous() &&
!tok->previous()->isName() &&
!Token::Match(tok->previous(), "[{};]"))
continue;
// next token should not be a standard type?
if (tok->next()->isStandardType())
continue;
// The "int" is missing.. add it
tok->insertToken("int");
}
}
bool Tokenizer::simplifyIfAssign()
{
bool ret = false;

View File

@ -100,6 +100,12 @@ public:
*/
bool simplifyVarDecl();
/**
* insert an "int" after "unsigned" if needed:
* "unsigned i" => "unsigned int i"
*/
void unsignedint();
/**
* Simplify question mark - colon operator
* Example: 0 ? (2/0) : 0 => 0

View File

@ -145,6 +145,9 @@ private:
TEST_CASE(syntax_error);
TEST_CASE(removeKeywords);
// unsigned i; => unsigned int i;
TEST_CASE(unsigned1);
}
@ -2028,6 +2031,28 @@ private:
ASSERT_EQUALS("if ( ! ! x ) { ; }", actual);
}
/**
* tokenize "unsigned i" => "unsigned int i"
* tokenize "unsigned int" => "unsigned int"
*/
void unsigned1()
{
// No changes..
{
const char code[] = "void foo ( unsigned int , unsigned float ) ;";
ASSERT_EQUALS(code, tokenizeAndStringify(code));
}
// insert "int" after "unsigned"..
{
const char code1[] = "unsigned i ;";
const char code2[] = "unsigned int i ;";
ASSERT_EQUALS(code2, tokenizeAndStringify(code1));
}
}
};
REGISTER_TEST(TestTokenizer)