From 72706cd0b8696a9e4fef13a0566aeb6bdd3391fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 8 Aug 2015 12:49:42 +0200 Subject: [PATCH] Fixed #6868 (Tokenizer: wrong simplification of the unlikely keyword) --- cfg/gnu.cfg | 2 ++ lib/tokenize.cpp | 36 +++++++++++++++--------------------- lib/tokenize.h | 2 +- test/testsimplifytokens.cpp | 5 ----- test/testtokenize.cpp | 4 ++-- 5 files changed, 20 insertions(+), 29 deletions(-) diff --git a/cfg/gnu.cfg b/cfg/gnu.cfg index ed5c4dfda..9bebeaf24 100644 --- a/cfg/gnu.cfg +++ b/cfg/gnu.cfg @@ -296,4 +296,6 @@ + + diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 31fd5601e..9adc5214d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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); } } } diff --git a/lib/tokenize.h b/lib/tokenize.h index 76faf44ec..c24749819 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -650,7 +650,7 @@ private: void simplifyBitfields(); /** - * Remove __builtin_expect(...), likely(...), and unlikely(...) + * Remove __builtin_expect(...) */ void simplifyBuiltinExpect(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 932bc6b56..ba4ef9753 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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")); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index a64158731..57d3519df 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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)); }