1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-19 15:05:36 +02:00

Fixed Bug 84478 [C++ Parser] does not support declarations inside while condition

This commit is contained in:
John Camelon 2005-06-21 15:12:00 +00:00
parent 58615beb77
commit 629a1ea350
9 changed files with 137 additions and 44 deletions

View file

@ -4763,4 +4763,11 @@ public class AST2CPPTests extends AST2BaseTest {
assertNoProblemBindings( col );
}
public void testBug84478_3() throws Exception {
IASTTranslationUnit tu = parse( "void foo() { switch( int x = 4 ) { case 4: break; default: break;} }", ParserLanguage.CPP ); //$NON-NLS-1$
CPPNameCollector col = new CPPNameCollector();
tu.accept( col );
assertNoProblemBindings( col );
}
}

View file

@ -42,7 +42,7 @@ public interface IASTForStatement extends IASTStatement {
public static final ASTNodeProperty BODY = new ASTNodeProperty("IASTForStatement.BODY - IASTStatement body of IASTForStatement"); //$NON-NLS-1$
/**
* <code>INITDECLARATION</code> represents the relationship between a
* <code>INITIALIZER</code> represents the relationship between a
* <code>IASTForStatement</code> and its <code>IASTDeclaration</code>
* initializer.
*/

View file

@ -18,11 +18,11 @@ package org.eclipse.cdt.core.dom.ast;
public interface IASTSwitchStatement extends IASTStatement {
/**
* <code>CONTROLLER</code> represents the relationship between an
* <code>CONTROLLER_EXP</code> represents the relationship between an
* <code>IASTSwitchStatement</code> and it's nested
* <code>IASTExpression</code>.
*/
public static final ASTNodeProperty CONTROLLER = new ASTNodeProperty(
public static final ASTNodeProperty CONTROLLER_EXP = new ASTNodeProperty(
"IASTSwitchStatement.CONTROLLER - IASTExpression (controller) for IASTSwitchExpression"); //$NON-NLS-1$
/**
@ -37,7 +37,7 @@ public interface IASTSwitchStatement extends IASTStatement {
*
* @return the controller expression
*/
public IASTExpression getController();
public IASTExpression getControllerExpression();
/**
* Set the controlling expression for the switch.
@ -45,7 +45,7 @@ public interface IASTSwitchStatement extends IASTStatement {
* @param controller
* <code>IASTExpression</code>
*/
public void setController(IASTExpression controller);
public void setControllerExpression(IASTExpression controller);
/**
* Returns the body of the switch statement.

View file

@ -0,0 +1,42 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.core.dom.ast.cpp;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
public interface ICPPASTSwitchStatement extends IASTSwitchStatement {
/**
* <code>CONTROLLER_DECLARATION</code> represents the relationship between an
* <code>IASTSwitchStatement</code> and it's nested
* <code>IASTDeclaration</code>.
*/
public static final ASTNodeProperty CONTROLLER_DECLARATION = new ASTNodeProperty(
"IASTSwitchStatement.CONTROLLER - IASTDeclaration (controller) for IASTSwitchExpression"); //$NON-NLS-1$
/**
* In C++, a switch statement can be contorller by a declaration.
*
* @return <code>IASTDeclaration</code>
*/
public IASTDeclaration getControllerDeclaration();
/**
* In C++, a switch statement can be contorller by a declaration.
*
* @param d <code>IASTDeclaration</code>
*/
public void setControllerDeclaration( IASTDeclaration d );
}

View file

@ -49,7 +49,6 @@ import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
@ -1412,11 +1411,6 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
*/
protected abstract IASTWhileStatement createWhileStatement();
/**
* @return
*/
protected abstract IASTSwitchStatement createSwitchStatement();
/**
* @return
*/
@ -1929,32 +1923,6 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
return while_statement;
}
/**
* @return
* @throws EndOfFileException
* @throws BacktrackException
*/
protected IASTStatement parseSwitchStatement() throws EndOfFileException,
BacktrackException {
int startOffset;
startOffset = consume(IToken.t_switch).getOffset();
consume(IToken.tLPAREN);
IASTExpression switch_condition = condition();
consume(IToken.tRPAREN);
IASTStatement switch_body = statement();
IASTSwitchStatement switch_statement = createSwitchStatement();
((ASTNode) switch_statement).setOffsetAndLength(startOffset,
calculateEndOffset(switch_body) - startOffset);
switch_statement.setController(switch_condition);
switch_condition.setParent(switch_statement);
switch_condition.setPropertyInParent(IASTSwitchStatement.CONTROLLER);
switch_statement.setBody(switch_body);
switch_body.setParent(switch_statement);
switch_body.setPropertyInParent(IASTSwitchStatement.BODY);
return switch_statement;
}
/**
* @param result
*/

View file

@ -28,14 +28,14 @@ public class CASTSwitchStatement extends CASTNode implements
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTSwitchStatement#getController()
*/
public IASTExpression getController() {
public IASTExpression getControllerExpression() {
return controller;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTSwitchStatement#setController(org.eclipse.cdt.core.dom.ast.IASTExpression)
*/
public void setController(IASTExpression controller) {
public void setControllerExpression(IASTExpression controller) {
this.controller = controller;
}

View file

@ -2792,5 +2792,30 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
calculateEndOffset(castExpression));
}
/**
* @return
* @throws EndOfFileException
* @throws BacktrackException
*/
protected IASTStatement parseSwitchStatement() throws EndOfFileException, BacktrackException {
int startOffset;
startOffset = consume(IToken.t_switch).getOffset();
consume(IToken.tLPAREN);
IASTExpression switch_condition = condition();
consume(IToken.tRPAREN);
IASTStatement switch_body = statement();
IASTSwitchStatement switch_statement = createSwitchStatement();
((ASTNode) switch_statement).setOffsetAndLength(startOffset,
calculateEndOffset(switch_body) - startOffset);
switch_statement.setControllerExpression(switch_condition);
switch_condition.setParent(switch_statement);
switch_condition.setPropertyInParent(IASTSwitchStatement.CONTROLLER_EXP);
switch_statement.setBody(switch_body);
switch_body.setParent(switch_statement);
switch_body.setPropertyInParent(IASTSwitchStatement.BODY);
return switch_statement;
}
}

View file

@ -11,32 +11,34 @@
package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSwitchStatement;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
/**
* @author jcamelon
*/
public class CPPASTSwitchStatement extends CPPASTNode implements
IASTSwitchStatement, IASTAmbiguityParent {
ICPPASTSwitchStatement, IASTAmbiguityParent {
private IASTExpression controller;
private IASTStatement body;
private IASTDeclaration decl;
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTSwitchStatement#getController()
*/
public IASTExpression getController() {
public IASTExpression getControllerExpression() {
return controller;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTSwitchStatement#setController(org.eclipse.cdt.core.dom.ast.IASTExpression)
*/
public void setController(IASTExpression controller) {
public void setControllerExpression(IASTExpression controller) {
this.controller = controller;
}
@ -80,7 +82,21 @@ public class CPPASTSwitchStatement extends CPPASTNode implements
other.setParent( child.getParent() );
controller = (IASTExpression) other;
}
if( child == decl )
{
other.setPropertyInParent( child.getPropertyInParent() );
other.setParent( child.getParent() );
decl = (IASTDeclaration) other;
}
}
public IASTDeclaration getControllerDeclaration() {
return decl;
}
public void setControllerDeclaration(IASTDeclaration d) {
decl = d;
}
}

View file

@ -108,6 +108,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
@ -4893,7 +4894,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
/**
* @return
*/
protected IASTSwitchStatement createSwitchStatement() {
protected ICPPASTSwitchStatement createSwitchStatement() {
return new CPPASTSwitchStatement();
}
@ -5334,4 +5335,38 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
return buildUnaryExpression(operator, castExpression, offset,
calculateEndOffset(castExpression));
}
/**
* @return
* @throws EndOfFileException
* @throws BacktrackException
*/
protected IASTStatement parseSwitchStatement() throws EndOfFileException, BacktrackException {
int startOffset;
startOffset = consume(IToken.t_switch).getOffset();
consume(IToken.tLPAREN);
IASTNode switch_condition = cppStyleCondition();
consume(IToken.tRPAREN);
IASTStatement switch_body = statement();
ICPPASTSwitchStatement switch_statement = createSwitchStatement();
((ASTNode) switch_statement).setOffsetAndLength(startOffset,
calculateEndOffset(switch_body) - startOffset);
if( switch_condition instanceof IASTExpression )
{
switch_statement.setControllerExpression((IASTExpression) switch_condition);
switch_condition.setParent(switch_statement);
switch_condition.setPropertyInParent(IASTSwitchStatement.CONTROLLER_EXP);
}
else if( switch_condition instanceof IASTDeclaration )
{
switch_statement.setControllerDeclaration((IASTDeclaration) switch_condition);
switch_condition.setParent(switch_statement);
switch_condition.setPropertyInParent(ICPPASTSwitchStatement.CONTROLLER_DECLARATION);
}
switch_statement.setBody(switch_body);
switch_body.setParent(switch_statement);
switch_body.setPropertyInParent(IASTSwitchStatement.BODY);
return switch_statement;
}
}