diff --git a/src/tokenize.cpp b/src/tokenize.cpp index d905a7c2b..2dfda58d5 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -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; diff --git a/src/tokenize.h b/src/tokenize.h index e51d84961..9ebe2ea7f 100644 --- a/src/tokenize.h +++ b/src/tokenize.h @@ -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 diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 71dbb79ca..7171470b8 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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)