From 119ab519a4403099cff027bfb75a7f88d7bd95fa Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Thu, 8 Nov 2012 17:49:43 +0100 Subject: [PATCH] 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. --- lib/tokenize.cpp | 4 ++++ test/testsimplifytokens.cpp | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 399c4ab2d..5627488d2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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) { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 4ffc41c39..aea306c88 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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"