1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-05 23:35:48 +02:00

Bug 348387 - fp in ReturnChecker using try/catch

This commit is contained in:
Tomasz Wesolowski 2012-11-17 01:38:45 -05:00 committed by Marc-Andre Laperle
parent e019d4965e
commit 5e369d662e
5 changed files with 71 additions and 15 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2011 Alena Laskavaia * Copyright (c) 2009, 2012 Alena Laskavaia
* 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
@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Alena Laskavaia - initial API and implementation * Alena Laskavaia - initial API and implementation
* Tomasz Wesolowski - Bug 348387
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers; package org.eclipse.cdt.codan.internal.checkers;
@ -47,6 +48,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLambdaExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLambdaExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor; import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
@ -205,8 +207,9 @@ public class ReturnChecker extends AbstractAstFunctionChecker {
*/ */
private boolean isCompoundStatement(IASTStatement last) { private boolean isCompoundStatement(IASTStatement last) {
return last instanceof IASTIfStatement || last instanceof IASTWhileStatement || return last instanceof IASTIfStatement || last instanceof IASTWhileStatement ||
last instanceof IASTDoStatement || last instanceof IASTForStatement || last instanceof IASTDoStatement || last instanceof IASTForStatement ||
last instanceof IASTSwitchStatement || last instanceof IASTCompoundStatement; last instanceof IASTSwitchStatement || last instanceof IASTCompoundStatement ||
last instanceof ICPPASTTryBlockStatement;
} }
protected boolean isFuncExitStatement(IASTStatement statement) { protected boolean isFuncExitStatement(IASTStatement statement) {

View file

@ -1,12 +1,13 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2010 Alena Laskavaia * Copyright (c) 2009, 2012 Alena Laskavaia 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
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* Alena Laskavaia - initial API and implementation * Alena Laskavaia - initial API and implementation
* Tomasz Wesolowski - Bug 348387
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.core.cxx.internal.model.cfg; package org.eclipse.cdt.codan.core.cxx.internal.model.cfg;
@ -32,6 +33,7 @@ import org.eclipse.cdt.core.dom.ast.IASTBreakStatement;
import org.eclipse.cdt.core.dom.ast.IASTCaseStatement; import org.eclipse.cdt.core.dom.ast.IASTCaseStatement;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTContinueStatement; import org.eclipse.cdt.core.dom.ast.IASTContinueStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement; import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement; import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement;
import org.eclipse.cdt.core.dom.ast.IASTDoStatement; import org.eclipse.cdt.core.dom.ast.IASTDoStatement;
@ -231,14 +233,20 @@ public class ControlFlowGraphBuilder {
addOutgoing(prev, ifNode); addOutgoing(prev, ifNode);
IConnectorNode mergeNode = factory.createConnectorNode(); IConnectorNode mergeNode = factory.createConnectorNode();
ifNode.setMergeNode(mergeNode); ifNode.setMergeNode(mergeNode);
IBranchNode thenNode = factory.createBranchNode(IBranchNode.THEN); IBranchNode tryBodyNode = factory.createBranchNode(IBranchNode.TRY_BODY);
addOutgoing(ifNode, thenNode); addOutgoing(ifNode, tryBodyNode);
IBasicBlock then = createSubGraph(thenNode, body.getTryBody()); IBasicBlock tryBody = createSubGraph(tryBodyNode, body.getTryBody());
addJump(then, mergeNode); addJump(tryBody, mergeNode);
ICPPASTCatchHandler[] catchHandlers = body.getCatchHandlers(); ICPPASTCatchHandler[] catchHandlers = body.getCatchHandlers();
for (int i = 0; i < catchHandlers.length; i++) { for (int i = 0; i < catchHandlers.length; i++) {
ICPPASTCatchHandler handler = catchHandlers[i]; ICPPASTCatchHandler handler = catchHandlers[i];
IBranchNode handlerNode = factory.createBranchNode(handler.getDeclaration()); IBranchNode handlerNode;
IASTDeclaration declaration = handler.getDeclaration();
if (declaration != null) {
handlerNode = factory.createBranchNode(declaration);
} else {
handlerNode = factory.createBranchNode(IBranchNode.CATCH_ANY);
}
addOutgoing(ifNode, handlerNode); addOutgoing(ifNode, handlerNode);
IBasicBlock els = createSubGraph(handlerNode, handler.getCatchBody()); IBasicBlock els = createSubGraph(handlerNode, handler.getCatchBody());
addJump(els, mergeNode); addJump(els, mergeNode);

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2010 Alena Laskavaia * Copyright (c) 2009, 2012 Alena Laskavaia
* 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
@ -363,7 +363,7 @@ public class ControlFlowGraphTest extends CodanFastCxxAstTestCase {
IPlainNode decl = (IPlainNode) startNode.getOutgoing(); IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing(); IDecisionNode des = (IDecisionNode) decl.getOutgoing();
//assertEquals("", data(des)); //assertEquals("", data(des));
IPlainNode bThen = (IPlainNode) branchEnd(des, IBranchNode.THEN); IPlainNode bThen = (IPlainNode) branchEnd(des, IBranchNode.TRY_BODY);
assertEquals("*p = 1;", data(bThen)); assertEquals("*p = 1;", data(bThen));
IBasicBlock bElse = null; IBasicBlock bElse = null;
IBasicBlock[] outgoingNodes = des.getOutgoingNodes(); IBasicBlock[] outgoingNodes = des.getOutgoingNodes();

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2011 Alena Laskavaia * Copyright (c) 2009, 2012 Alena Laskavaia
* 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
@ -8,6 +8,7 @@
* Contributors: * Contributors:
* Alena Laskavaia - initial API and implementation * Alena Laskavaia - initial API and implementation
* Felipe Martinez - ReturnCheckerTest implementation * Felipe Martinez - ReturnCheckerTest implementation
* Tomasz Wesolowski - Bug 348387
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.core.internal.checkers; package org.eclipse.cdt.codan.core.internal.checkers;
@ -310,4 +311,39 @@ public class ReturnCheckerTest extends CheckerTestCase {
loadCodeAndRun(getAboveComment()); loadCodeAndRun(getAboveComment());
checkNoErrors(); checkNoErrors();
} }
// int try1() {
// try {
// return 5;
// } catch (...) {
// return 5;
// }
// }
public void testTryBlock1() throws Exception {
// bug 348387
loadCodeAndRunCpp(getAboveComment());
checkNoErrors();
}
// int try2() {
// try {
// return 5;
// } catch (int) {
// }
// }
public void testTryBlock2() throws Exception {
loadCodeAndRunCpp(getAboveComment());
checkErrorLine(1);
}
// int try3() {
// try {
// } catch (int a) {
// return 5;
// }
// }
public void testTryBlock3() throws Exception {
loadCodeAndRunCpp(getAboveComment());
checkErrorLine(1);
}
} }

View file

@ -1,12 +1,13 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2010 Alena Laskavaia * Copyright (c) 2009, 2012 Alena Laskavaia 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
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* Alena Laskavaia - initial API and implementation * Alena Laskavaia - initial API and implementation
* Tomasz Wesolowski - Bug 348387
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.core.model.cfg; package org.eclipse.cdt.codan.core.model.cfg;
@ -29,6 +30,14 @@ public interface IBranchNode extends IBasicBlock, ISingleIncoming, ISingleOutgoi
* Default branch of "switch" statement * Default branch of "switch" statement
*/ */
public static String DEFAULT = "default"; //$NON-NLS-1$ public static String DEFAULT = "default"; //$NON-NLS-1$
/**
* Try branch of "try" block statement
*/
public static String TRY_BODY = "try"; //$NON-NLS-1$
/**
* Catch "..." branch of "try" block statement
*/
public static String CATCH_ANY = "..."; //$NON-NLS-1$
/** /**
* @return label of a branch * @return label of a branch