From 9c057c707bebd7b7df73fd4c13d67b47a9cabe65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 17 Mar 2009 20:50:06 +0100 Subject: [PATCH] tokenize negative numbers into a single token --- src/checkother.cpp | 10 +++++----- src/token.cpp | 2 +- src/tokenize.cpp | 10 ++++++++++ test/testtokenize.cpp | 10 ++++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/checkother.cpp b/src/checkother.cpp index ef805c6e6..3c334e30e 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -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); 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]; if (sign2 == 'u') { diff --git a/src/token.cpp b/src/token.cpp index 833f06ede..2d9017064 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -49,7 +49,7 @@ void Token::str(const char s[]) { _str = s; _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") _isBoolean = true; else diff --git a/src/tokenize.cpp b/src/tokenize.cpp index d7b212f6b..14d32c644 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -336,6 +336,16 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[]) } 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.. for (Token *tok = _tokens; tok && tok->next(); tok = tok->next()) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index c49dd4c4c..d55697ea7 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -61,6 +61,8 @@ private: void run() { + TEST_CASE(minus); + TEST_CASE(longtok); 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() {