Fixed #6868 (Tokenizer: wrong simplification of the unlikely keyword)

This commit is contained in:
Daniel Marjamäki 2015-08-08 12:49:42 +02:00
parent 8f309aed12
commit 72706cd0b8
5 changed files with 20 additions and 29 deletions

View File

@ -296,4 +296,6 @@
</arg>
<leak-ignore/>
</function>
<define name="likely(X)" value="(X)"/>
<define name="unlikely(X)" value="(X)"/>
</def>

View File

@ -3456,7 +3456,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
// remove Borland stuff..
simplifyBorland();
// Remove __builtin_expect, likely and unlikely
// Remove __builtin_expect
simplifyBuiltinExpect();
if (hasEnumsWithTypedef()) {
@ -9537,31 +9537,25 @@ void Tokenizer::simplifyBitfields()
}
// Remove __builtin_expect(...), likely(...), and unlikely(...)
// Remove __builtin_expect(...)
void Tokenizer::simplifyBuiltinExpect()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::simpleMatch(tok->next(), "__builtin_expect (")) {
// Count parentheses for tok2
const Token* end = tok->linkAt(2);
for (Token *tok2 = tok->tokAt(3); tok2 != end; tok2 = tok2->next()) {
if (tok2->str() == "(") {
tok2 = tok2->link();
} else if (tok2->str() == ",") {
if (Token::Match(tok2, ", %num% )")) {
tok->deleteNext();
tok2->deleteNext();
tok2->deleteThis();
}
break;
if (!Token::simpleMatch(tok->next(), "__builtin_expect ("))
continue;
// Count parentheses for tok2
const Token* end = tok->linkAt(2);
for (Token *tok2 = tok->tokAt(3); tok2 != end; tok2 = tok2->next()) {
if (tok2->str() == "(") {
tok2 = tok2->link();
} else if (tok2->str() == ",") {
if (Token::Match(tok2, ", %num% )")) {
tok->deleteNext();
tok2->deleteNext();
tok2->deleteThis();
}
break;
}
} else if (Token::Match(tok->next(), "likely|unlikely (")) {
// remove closing ')'
tok->linkAt(2)->deleteThis();
// remove "likely|unlikely ("
tok->deleteNext(2);
}
}
}

View File

@ -650,7 +650,7 @@ private:
void simplifyBitfields();
/**
* Remove __builtin_expect(...), likely(...), and unlikely(...)
* Remove __builtin_expect(...)
*/
void simplifyBuiltinExpect();

View File

@ -856,12 +856,9 @@ private:
void ifa_ifa() {
ASSERT_EQUALS("int a ; if ( a ) { { ab } cd }", tok("int a ; if (a) { if (a) { ab } cd }", true));
ASSERT_EQUALS("int a ; if ( a ) { { ab } cd }", tok("int a ; if (unlikely(a)) { if (a) { ab } cd }", true));
}
unsigned int sizeofFromTokenizer(const char type[]) {
errout.str("");
@ -3857,8 +3854,6 @@ private:
ASSERT_EQUALS("class C { int f ( ) ; } ;", tok("class C { int f() override ; };", true));
ASSERT_EQUALS("class C { int f ( ) ; } ;", tok("class C { int f() final ; };", true));
ASSERT_EQUALS("void f ( ) { int final [ 10 ] ; }", tok("void f() { int final[10]; }", true));
ASSERT_EQUALS("if ( a ) { }", tok("if ( likely ( a ) ) { }", true));
ASSERT_EQUALS("if ( a ) { }", tok("if ( unlikely ( a ) ) { }", true));
ASSERT_EQUALS("int * p ;", tok("int * __restrict p;", "test.c"));
ASSERT_EQUALS("int * * p ;", tok("int * __restrict__ * p;", "test.c"));
ASSERT_EQUALS("void foo ( float * a , float * b ) ;", tok("void foo(float * __restrict__ a, float * __restrict__ b);", "test.c"));

View File

@ -295,7 +295,7 @@ private:
TEST_CASE(syntax_error_templates_3); // Ticket #5605, #5759, #5762, #5774
TEST_CASE(template_member_ptr); // Ticket #5786 - crash upon valid code
TEST_CASE(removeKeywords);
TEST_CASE(simplifyBuiltinExpect);
// unsigned i; => unsigned int i;
TEST_CASE(unsigned1);
@ -4355,7 +4355,7 @@ private:
"};");
}
void removeKeywords() {
void simplifyBuiltinExpect() {
const char code[] = "if (__builtin_expect(!!(x), 1));";
ASSERT_EQUALS("if ( ! ! x ) { ; }", tokenizeAndStringify(code, true));
}