diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java
index 7bb3b5fea06..94796701bf3 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004, 2007 IBM Corporation and others.
+ * Copyright (c) 2004, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -4436,4 +4436,14 @@ public class AST2Tests extends AST2BaseTest {
ba.assertNonProblem("foo1(b)", 4);
ba.assertProblem("foo2(b)", 4);
}
+
+ // int a, b, c;
+ // void test() {
+ // a= b ? : c;
+ // }
+ public void testOmittedPositiveExpression_Bug212905() throws Exception {
+ final String code = getAboveComment();
+ parseAndCheckBindings(code, ParserLanguage.C);
+ parseAndCheckBindings(code, ParserLanguage.CPP);
+ }
}
\ No newline at end of file
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTSignatureUtil.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTSignatureUtil.java
index 29ef5306721..e6fa57385fa 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTSignatureUtil.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTSignatureUtil.java
@@ -736,8 +736,11 @@ public class ASTSignatureUtil {
result.append(SPACE);
result.append(Keywords.cpQUESTION);
result.append(SPACE);
- result.append(getExpressionString(expression.getPositiveResultExpression()));
- result.append(SPACE);
+ final IASTExpression positiveExpression = expression.getPositiveResultExpression();
+ if (positiveExpression != null) {
+ result.append(getExpressionString(positiveExpression));
+ result.append(SPACE);
+ }
result.append(Keywords.cpCOLON);
result.append(SPACE);
result.append(getExpressionString(expression.getNegativeResultExpression()));
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTConditionalExpression.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTConditionalExpression.java
index a518b98df13..5f157cda823 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTConditionalExpression.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTConditionalExpression.java
@@ -1,12 +1,12 @@
/*******************************************************************************
- * Copyright (c) 2005 IBM Corporation and others.
+ * Copyright (c) 2005, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
- * IBM Rational Software - Initial API and implementation
+ * IBM Rational Software - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.dom.ast;
@@ -58,9 +58,10 @@ public interface IASTConditionalExpression extends IASTExpression {
public void setLogicalConditionExpression(IASTExpression expression);
/**
- * Get the positive result expression.
+ * Get the positive result expression, or null
in case the positive condition was omitted (this is
+ * a gcc extension).
*
- * @return IASTExpression
+ * @return IASTExpression
, or null
.
*/
public IASTExpression getPositiveResultExpression();
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java
index b99ec621b60..27717a5bedd 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2007 IBM Corporation and others.
+ * Copyright (c) 2005, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -860,7 +860,10 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
IASTExpression firstExpression = logicalOrExpression();
if (LT(1) == IToken.tQUESTION) {
consume();
- IASTExpression secondExpression = expression();
+ IASTExpression secondExpression= null;
+ if (LT(1) != IToken.tCOLON) {
+ secondExpression = expression();
+ }
IASTExpression thirdExpression = null;
if (LT(1) != IToken.tEOC) {
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java
index b5c7b4e7938..c8be960dcd0 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java
@@ -799,8 +799,12 @@ public class CVisitor {
return getExpressionType( ((IASTExpressionStatement)st).getExpression() );
}
} else if( expression instanceof IASTConditionalExpression ){
- IASTConditionalExpression conditional = (IASTConditionalExpression) expression;
- IType t2 = getExpressionType( conditional.getPositiveResultExpression() );
+ final IASTConditionalExpression conditional = (IASTConditionalExpression) expression;
+ IASTExpression positiveExpression = conditional.getPositiveResultExpression();
+ if (positiveExpression == null) {
+ positiveExpression= conditional.getLogicalConditionExpression();
+ }
+ IType t2 = getExpressionType(positiveExpression);
IType t3 = getExpressionType( conditional.getNegativeResultExpression() );
if( t3 instanceof IPointerType || t2 == null )
return t3;
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java
index 2697e722d33..ec5f111c02b 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java
@@ -2008,8 +2008,12 @@ public class CPPVisitor {
return getExpressionType(((IASTExpressionStatement)st).getExpression());
}
} else if (expression instanceof IASTConditionalExpression) {
- IASTConditionalExpression conditional = (IASTConditionalExpression) expression;
- IType t2 = getExpressionType(conditional.getPositiveResultExpression());
+ final IASTConditionalExpression conditional = (IASTConditionalExpression) expression;
+ IASTExpression positiveExpression = conditional.getPositiveResultExpression();
+ if (positiveExpression == null) {
+ positiveExpression= conditional.getLogicalConditionExpression();
+ }
+ IType t2 = getExpressionType(positiveExpression);
IType t3 = getExpressionType(conditional.getNegativeResultExpression());
if (t3 instanceof IPointerType || t2 == null)
return t3;
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java
index ea8c206d909..ba99ccf56ba 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java
@@ -7,7 +7,8 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
- * Institute for Software - initial API and implementation
+ * Institute for Software - initial API and implementation
+ * Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
@@ -456,7 +457,14 @@ public class ExpressionWriter extends NodeWriter{
private void writeConditionalExpression(IASTConditionalExpression condExp) {
condExp.getLogicalConditionExpression().accept(visitor);
scribe.print(SPACE_QUESTIONMARK_SPACE);
- condExp.getPositiveResultExpression().accept(visitor);
+ final IASTExpression positiveExpression = condExp.getPositiveResultExpression();
+ // gcc extension allows to omit the positive expression.
+ if (positiveExpression == null) {
+ scribe.print(' ');
+ }
+ else {
+ positiveExpression.accept(visitor);
+ }
scribe.print(SPACE_COLON_SPACE);
condExp.getNegativeResultExpression().accept(visitor);
diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
index c1841152ccd..df62382dac4 100644
--- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
+++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Anton Leherbauer (Wind River Systems) - initial API and implementation
+ * Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.formatter;
@@ -1748,7 +1749,10 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
if (preferences.insert_space_after_question_in_conditional) {
scribe.space();
}
- node.getPositiveResultExpression().accept(this);
+ final IASTExpression positiveExpression = node.getPositiveResultExpression();
+ if (positiveExpression != null) { // gcc-extension allows to omit the positive expression.
+ positiveExpression.accept(this);
+ }
scribe.printTrailingComment();
scribe.alignFragment(conditionalExpressionAlignment, 1);
scribe.printNextToken(Token.tCOLON, preferences.insert_space_before_colon_in_conditional);