Support GNU extension of '?:' operator:

"x ? : y" is equivalent to "x ? x : y". I found a lot of code with this extension, probably we should warn a user to not use this.
This commit is contained in:
Edoardo Prezioso 2012-11-08 17:49:43 +01:00
parent 9556634ee7
commit 119ab519a4
2 changed files with 11 additions and 1 deletions

View File

@ -4399,6 +4399,10 @@ bool Tokenizer::simplifyConstTernaryOp()
if (!semicolon || semicolon->previous()->str() != ":" || !semicolon->next())
continue;
//handle the GNU extension: "x ? : y" <-> "x ? x : y"
if (semicolon->previous() == tok->next())
tok->insertToken(tok->strAt(-offset));
// go back before the condition, if possible
tok = tok->tokAt(-2);
if (offset == 2) {

View File

@ -2122,7 +2122,7 @@ private:
const char actual[] = "template < int n > struct B { int a [ n ] ; } ; "
"bitset<1> z ; "
"class bitset<1> : B < ( ) > { }";
"class bitset<1> : B < 4 > { }";
const char expected[] = "bitset<1> z ; "
"class bitset<1> : B<4> { } "
@ -2814,6 +2814,12 @@ private:
ASSERT_EQUALS("= 0 ;", tok(code));
}
//GNU extension: "x ?: y" <-> "x ? x : y"
{
const char code[] = "; a = 1 ? : x; b = 0 ? : 2;";
ASSERT_EQUALS("; a = 1 ; b = 2 ;", tok(code));
}
{
const char code[] = "int f(int b, int d)\n"
"{\n"