tokenize negative numbers into a single token

This commit is contained in:
Daniel Marjamäki 2009-03-17 20:50:06 +01:00
parent 2a3535c04f
commit 9c057c707b
4 changed files with 26 additions and 6 deletions

View File

@ -408,9 +408,9 @@ void CheckOther::CheckUnsignedDivision()
} }
} }
else if (!Token::Match(tok, "[).]") && Token::Match(tok->next(), "%var% / - %num%")) else if (!Token::Match(tok, "[).]") && Token::Match(tok->next(), "%var% / %num%"))
{ {
if (ErrorLogger::udivError()) if (tok->strAt(3)[0] == '-' && ErrorLogger::udivError())
{ {
const char *varname1 = tok->strAt(1); const char *varname1 = tok->strAt(1);
char sign1 = varsign[varname1]; char sign1 = varsign[varname1];
@ -421,11 +421,11 @@ void CheckOther::CheckUnsignedDivision()
} }
} }
else if (Token::Match(tok, "[([=*/+-] - %num% / %var%")) else if (Token::Match(tok, "[([=*/+-,] %num% / %var%"))
{ {
if (ErrorLogger::udivError()) if (tok->strAt(1)[0] == '-' && ErrorLogger::udivError())
{ {
const char *varname2 = tok->strAt(4); const char *varname2 = tok->strAt(3);
char sign2 = varsign[varname2]; char sign2 = varsign[varname2];
if (sign2 == 'u') if (sign2 == 'u')
{ {

View File

@ -49,7 +49,7 @@ void Token::str(const char s[])
{ {
_str = s; _str = s;
_isName = bool(_str[0] == '_' || std::isalpha(_str[0])); _isName = bool(_str[0] == '_' || std::isalpha(_str[0]));
_isNumber = bool(std::isdigit(_str[0]) != 0); _isNumber = bool(std::isdigit(_str[(_str[0]=='-')?1:0]) != 0);
if (_str == "true" || _str == "false") if (_str == "true" || _str == "false")
_isBoolean = true; _isBoolean = true;
else else

View File

@ -336,6 +336,16 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[])
} }
addtoken(CurrentToken.c_str(), lineno, FileIndex); addtoken(CurrentToken.c_str(), lineno, FileIndex);
// Combine "- %num%" ..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "[(+-*/=,] - %num%") && tok->strAt(2)[0] != '-')
{
tok->next()->str((std::string("-") + tok->strAt(2)).c_str());
tok->next()->deleteNext();
}
}
// Combine tokens.. // Combine tokens..
for (Token *tok = _tokens; tok && tok->next(); tok = tok->next()) for (Token *tok = _tokens; tok && tok->next(); tok = tok->next())
{ {

View File

@ -61,6 +61,8 @@ private:
void run() void run()
{ {
TEST_CASE(minus);
TEST_CASE(longtok); TEST_CASE(longtok);
TEST_CASE(removeCast1); TEST_CASE(removeCast1);
@ -171,6 +173,14 @@ private:
} }
void minus()
{
ASSERT_EQUALS("i = -12", tokenizeAndStringify("i = -12"));
ASSERT_EQUALS("1 - 2", tokenizeAndStringify("1-2"));
ASSERT_EQUALS("foo ( -1 ) - 2", tokenizeAndStringify("foo(-1)-2"));
}
void longtok() void longtok()
{ {