Skip to content

Commit

Permalink
Fix #153 (Unsigned divide)
Browse files Browse the repository at this point in the history
The "unsigned i" variable declaration wasn't handled well. So I added an "int" token.
  • Loading branch information
Daniel Marjamäki committed Jun 6, 2009
1 parent d0757c5 commit dd473b0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/tokenize.cpp
Expand Up @@ -473,6 +473,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[])
}
}

// replace "unsigned i" with "unsigned int i"
unsignedint();

simplifyVarDecl();

// Handle templates..
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/tokenize.h
Expand Up @@ -100,6 +100,12 @@ class Tokenizer
*/
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
Expand Down
25 changes: 25 additions & 0 deletions test/testtokenize.cpp
Expand Up @@ -145,6 +145,9 @@ class TestTokenizer : public TestFixture
TEST_CASE(syntax_error);

TEST_CASE(removeKeywords);

// unsigned i; => unsigned int i;
TEST_CASE(unsigned1);
}


Expand Down Expand Up @@ -2028,6 +2031,28 @@ class TestTokenizer : public TestFixture
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)

0 comments on commit dd473b0

Please sign in to comment.