Skip to main content
summaryrefslogtreecommitdiffstats
path: root/codan
diff options
context:
space:
mode:
authorAlena Laskavaia2010-05-24 22:34:50 -0400
committerAlena Laskavaia2010-05-24 22:34:50 -0400
commitb632454589c68c97ae71cb1c57ba394b0ba31f4b (patch)
tree5f3ce9fcf7d368edee03aeec1d048ccf8e374b31 /codan
parent3ec7ac60faf78569c1550ca5cb85b1deb29278fd (diff)
downloadorg.eclipse.cdt-b632454589c68c97ae71cb1c57ba394b0ba31f4b.tar.gz
org.eclipse.cdt-b632454589c68c97ae71cb1c57ba394b0ba31f4b.tar.xz
org.eclipse.cdt-b632454589c68c97ae71cb1c57ba394b0ba31f4b.zip
Added try block support in control flow graph
Diffstat (limited to 'codan')
-rw-r--r--codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/model/cfg/ControlFlowGraphBuilder.java31
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/cfg/ControlFlowGraphTest.java27
2 files changed, 58 insertions, 0 deletions
diff --git a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/model/cfg/ControlFlowGraphBuilder.java b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/model/cfg/ControlFlowGraphBuilder.java
index c91f3c1c0f..da18aa9c48 100644
--- a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/model/cfg/ControlFlowGraphBuilder.java
+++ b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/model/cfg/ControlFlowGraphBuilder.java
@@ -48,6 +48,8 @@ import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
/**
* This class creates C control flow graph
@@ -163,6 +165,8 @@ public class ControlFlowGraphBuilder {
return node;
} else if (body == null) {
// skip - sometimes body is empty such as no else
+ } else if (body instanceof ICPPASTTryBlockStatement) {
+ return createTry(prev, (ICPPASTTryBlockStatement) body);
} else {
System.err.println("unknown statement for cfg: " + body); //$NON-NLS-1$
}
@@ -170,6 +174,33 @@ public class ControlFlowGraphBuilder {
}
/**
+ * @param prev
+ * @param body
+ * @return
+ */
+ private IBasicBlock createTry(IBasicBlock prev,
+ ICPPASTTryBlockStatement body) {
+ DecisionNode ifNode = factory.createDecisionNode(body);
+ addOutgoing(prev, ifNode);
+ IConnectorNode mergeNode = factory.createConnectorNode();
+ ifNode.setMergeNode(mergeNode);
+ IBranchNode thenNode = factory.createBranchNode(IBranchNode.THEN);
+ addOutgoing(ifNode, thenNode);
+ IBasicBlock then = createSubGraph(thenNode, body.getTryBody());
+ addJump(then, mergeNode);
+ ICPPASTCatchHandler[] catchHandlers = body.getCatchHandlers();
+ for (int i = 0; i < catchHandlers.length; i++) {
+ ICPPASTCatchHandler handler = catchHandlers[i];
+ IBranchNode handlerNode = factory
+ .createBranchNode(handler.getDeclaration());
+ addOutgoing(ifNode, handlerNode);
+ IBasicBlock els = createSubGraph(handlerNode, handler.getCatchBody());
+ addJump(els, mergeNode);
+ }
+ return mergeNode;
+ }
+
+ /**
* @param body
* @return
*/
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/cfg/ControlFlowGraphTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/cfg/ControlFlowGraphTest.java
index e399ee3eb4..f2c0a09d99 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/cfg/ControlFlowGraphTest.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/cfg/ControlFlowGraphTest.java
@@ -333,4 +333,31 @@ public class ControlFlowGraphTest extends CodanFastCxxAstTestCase {
public IChecker getChecker() {
return null;
}
+
+ // int foo() {
+ // void * p;
+ // try {
+ // *p = 1;
+ // } catch (int e) {
+ // };
+ // }
+ public void test_try() {
+ buildAndCheck_cpp(getAboveComment());
+ IStartNode startNode = graph.getStartNode();
+ IPlainNode decl = (IPlainNode) startNode.getOutgoing();
+ IDecisionNode des = (IDecisionNode) decl.getOutgoing();
+ //assertEquals("", data(des));
+ IPlainNode bThen = (IPlainNode) branchEnd(des, IBranchNode.THEN);
+ assertEquals("*p = 1;", data(bThen));
+ IBasicBlock bElse = null;
+ IBasicBlock[] outgoingNodes = des.getOutgoingNodes();
+ for (int i = 1; i < outgoingNodes.length; i++) {
+ IBasicBlock iBasicBlock = outgoingNodes[i];
+ IBranchNode bn = (IBranchNode) iBasicBlock;
+ bElse = bn;
+ }
+ IBasicBlock m2 = jumpEnd(bThen);
+ IBasicBlock m1 = jumpEnd(bElse);
+ assertSame(m1, m2);
+ }
}

Back to the top