1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 10:16:03 +02:00

Fix for omitted positive expression in conditional expression, bug 212905.

This commit is contained in:
Markus Schorn 2008-04-08 11:41:44 +00:00
parent b7b91190f5
commit 3c78f0d8e1
8 changed files with 53 additions and 16 deletions

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -4436,4 +4436,14 @@ public class AST2Tests extends AST2BaseTest {
ba.assertNonProblem("foo1(b)", 4); ba.assertNonProblem("foo1(b)", 4);
ba.assertProblem("foo2(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);
}
} }

View file

@ -736,8 +736,11 @@ public class ASTSignatureUtil {
result.append(SPACE); result.append(SPACE);
result.append(Keywords.cpQUESTION); result.append(Keywords.cpQUESTION);
result.append(SPACE); result.append(SPACE);
result.append(getExpressionString(expression.getPositiveResultExpression())); final IASTExpression positiveExpression = expression.getPositiveResultExpression();
if (positiveExpression != null) {
result.append(getExpressionString(positiveExpression));
result.append(SPACE); result.append(SPACE);
}
result.append(Keywords.cpCOLON); result.append(Keywords.cpCOLON);
result.append(SPACE); result.append(SPACE);
result.append(getExpressionString(expression.getNegativeResultExpression())); result.append(getExpressionString(expression.getNegativeResultExpression()));

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005 IBM Corporation and others. * Copyright (c) 2005, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -58,9 +58,10 @@ public interface IASTConditionalExpression extends IASTExpression {
public void setLogicalConditionExpression(IASTExpression expression); public void setLogicalConditionExpression(IASTExpression expression);
/** /**
* Get the positive result expression. * Get the positive result expression, or <code>null</code> in case the positive condition was omitted (this is
* a gcc extension).
* *
* @return <code>IASTExpression</code> * @return <code>IASTExpression</code>, or <code>null</code>.
*/ */
public IASTExpression getPositiveResultExpression(); public IASTExpression getPositiveResultExpression();

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -860,7 +860,10 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
IASTExpression firstExpression = logicalOrExpression(); IASTExpression firstExpression = logicalOrExpression();
if (LT(1) == IToken.tQUESTION) { if (LT(1) == IToken.tQUESTION) {
consume(); consume();
IASTExpression secondExpression = expression(); IASTExpression secondExpression= null;
if (LT(1) != IToken.tCOLON) {
secondExpression = expression();
}
IASTExpression thirdExpression = null; IASTExpression thirdExpression = null;
if (LT(1) != IToken.tEOC) { if (LT(1) != IToken.tEOC) {

View file

@ -799,8 +799,12 @@ public class CVisitor {
return getExpressionType( ((IASTExpressionStatement)st).getExpression() ); return getExpressionType( ((IASTExpressionStatement)st).getExpression() );
} }
} else if( expression instanceof IASTConditionalExpression ){ } else if( expression instanceof IASTConditionalExpression ){
IASTConditionalExpression conditional = (IASTConditionalExpression) expression; final IASTConditionalExpression conditional = (IASTConditionalExpression) expression;
IType t2 = getExpressionType( conditional.getPositiveResultExpression() ); IASTExpression positiveExpression = conditional.getPositiveResultExpression();
if (positiveExpression == null) {
positiveExpression= conditional.getLogicalConditionExpression();
}
IType t2 = getExpressionType(positiveExpression);
IType t3 = getExpressionType( conditional.getNegativeResultExpression() ); IType t3 = getExpressionType( conditional.getNegativeResultExpression() );
if( t3 instanceof IPointerType || t2 == null ) if( t3 instanceof IPointerType || t2 == null )
return t3; return t3;

View file

@ -2008,8 +2008,12 @@ public class CPPVisitor {
return getExpressionType(((IASTExpressionStatement)st).getExpression()); return getExpressionType(((IASTExpressionStatement)st).getExpression());
} }
} else if (expression instanceof IASTConditionalExpression) { } else if (expression instanceof IASTConditionalExpression) {
IASTConditionalExpression conditional = (IASTConditionalExpression) expression; final IASTConditionalExpression conditional = (IASTConditionalExpression) expression;
IType t2 = getExpressionType(conditional.getPositiveResultExpression()); IASTExpression positiveExpression = conditional.getPositiveResultExpression();
if (positiveExpression == null) {
positiveExpression= conditional.getLogicalConditionExpression();
}
IType t2 = getExpressionType(positiveExpression);
IType t3 = getExpressionType(conditional.getNegativeResultExpression()); IType t3 = getExpressionType(conditional.getNegativeResultExpression());
if (t3 instanceof IPointerType || t2 == null) if (t3 instanceof IPointerType || t2 == null)
return t3; return t3;

View file

@ -8,6 +8,7 @@
* *
* Contributors: * 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; package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
@ -456,7 +457,14 @@ public class ExpressionWriter extends NodeWriter{
private void writeConditionalExpression(IASTConditionalExpression condExp) { private void writeConditionalExpression(IASTConditionalExpression condExp) {
condExp.getLogicalConditionExpression().accept(visitor); condExp.getLogicalConditionExpression().accept(visitor);
scribe.print(SPACE_QUESTIONMARK_SPACE); 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); scribe.print(SPACE_COLON_SPACE);
condExp.getNegativeResultExpression().accept(visitor); condExp.getNegativeResultExpression().accept(visitor);

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Anton Leherbauer (Wind River Systems) - initial API and implementation * Anton Leherbauer (Wind River Systems) - initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.formatter; package org.eclipse.cdt.internal.formatter;
@ -1748,7 +1749,10 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
if (preferences.insert_space_after_question_in_conditional) { if (preferences.insert_space_after_question_in_conditional) {
scribe.space(); 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.printTrailingComment();
scribe.alignFragment(conditionalExpressionAlignment, 1); scribe.alignFragment(conditionalExpressionAlignment, 1);
scribe.printNextToken(Token.tCOLON, preferences.insert_space_before_colon_in_conditional); scribe.printNextToken(Token.tCOLON, preferences.insert_space_before_colon_in_conditional);