Fix ticket 330 (found memory leak when __builtin_expect uses)

This commit is contained in:
Daniel Marjamäki 2009-05-27 20:49:29 +02:00
parent e89c03da92
commit ca6d927dfa
2 changed files with 41 additions and 2 deletions

View File

@ -1047,7 +1047,7 @@ void Tokenizer::simplifyTokenList()
} }
// Remove unwanted keywords // 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 (Token *tok = _tokens; tok; tok = tok->next())
{ {
for (unsigned ui = 0; ui < sizeof(unwantedWords) / sizeof(unwantedWords[0]) && tok->next(); ui++) for (unsigned ui = 0; ui < sizeof(unwantedWords) / sizeof(unwantedWords[0]) && tok->next(); ui++)
@ -1058,6 +1058,31 @@ void Tokenizer::simplifyTokenList()
break; 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 - // Convert + + into + and + - into -

View File

@ -140,6 +140,8 @@ private:
TEST_CASE(vardecl2); TEST_CASE(vardecl2);
TEST_CASE(volatile_variables); TEST_CASE(volatile_variables);
TEST_CASE(syntax_error); 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.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
if (simplify)
tokenizer.simplifyTokenList();
std::ostringstream ostr; std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
@ -1765,6 +1769,16 @@ private:
ASSERT_EQUALS(std::string(""), errout.str()); 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) REGISTER_TEST(TestTokenizer)