diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 3315835a5..1bc7ed551 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1047,7 +1047,7 @@ void Tokenizer::simplifyTokenList() } // Remove unwanted keywords - static const char* unwantedWords[] = { "unsigned", "unlikely" }; + static const char* unwantedWords[] = { "unsigned", "unlikely", "likely" }; for (Token *tok = _tokens; tok; tok = tok->next()) { for (unsigned ui = 0; ui < sizeof(unwantedWords) / sizeof(unwantedWords[0]) && tok->next(); ui++) @@ -1058,6 +1058,31 @@ void Tokenizer::simplifyTokenList() break; } } + if (Token::simpleMatch(tok->next(), "__builtin_expect (")) + { + unsigned int parlevel = 0; + for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) + { + if (tok2->str() == "(") + ++parlevel; + else if (tok2->str() == ")") + { + if (parlevel <= 1) + break; + --parlevel; + } + if (parlevel == 1 && tok2->str() == ",") + { + if (Token::Match(tok2, ", %num% )")) + { + tok->deleteNext(); + tok2->deleteThis(); + tok2->deleteThis(); + } + break; + } + } + } } // Convert + + into + and + - into - diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index bea96f1a3..96e01ad7c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -140,6 +140,8 @@ private: TEST_CASE(vardecl2); TEST_CASE(volatile_variables); TEST_CASE(syntax_error); + + TEST_CASE(removeKeywords); } @@ -155,12 +157,14 @@ private: } - std::string tokenizeAndStringify(const char code[]) + std::string tokenizeAndStringify(const char code[], bool simplify=false) { // tokenize.. Tokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); + if (simplify) + tokenizer.simplifyTokenList(); std::ostringstream ostr; for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) @@ -1765,6 +1769,16 @@ private: ASSERT_EQUALS(std::string(""), errout.str()); } } + + void removeKeywords() + { + const char code[] = "if (__builtin_expect(!!(x), 1));"; + + const std::string actual(tokenizeAndStringify(code, true)); + + ASSERT_EQUALS("if ( ! ! x ) { ; }", actual); + } + }; REGISTER_TEST(TestTokenizer)