mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Fix for omitted positive expression in conditional expression, bug 212905.
This commit is contained in:
parent
b7b91190f5
commit
3c78f0d8e1
8 changed files with 53 additions and 16 deletions
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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()));
|
||||
|
|
|
@ -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 <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();
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue