diff options
author | Elliott Baron <ebaron@fedoraproject.org> | 2009-10-05 20:48:19 -0400 |
---|---|---|
committer | Elliott Baron <ebaron@fedoraproject.org> | 2009-10-05 20:48:19 -0400 |
commit | 18360ea0c9cd1fce259ba7cf6824b48736334c4f (patch) | |
tree | 127e36727f77f520bc8d443595c91d639bf57dea /org.eclipse.ptp.pldt.mpi.analysis.cdt | |
parent | e35f5131df28786841f5e09d9982b912bd3469d0 (diff) | |
download | codan-18360ea0c9cd1fce259ba7cf6824b48736334c4f.tar.gz codan-18360ea0c9cd1fce259ba7cf6824b48736334c4f.tar.xz codan-18360ea0c9cd1fce259ba7cf6824b48736334c4f.zip |
Initial implementation of intra-procedural property simulation.
* org.eclipse.cdt.codan.extension/plugin.xml: Moved checker to new package.
* org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/CloseOpenedFilesChecker.java: Moved.
* org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ExecutionState.java: New file.
* org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/PropertyState.java: New file.
* org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/SymbolicState.java: New file.
* org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/checkers/FunctionNameParser.java: New file.
* org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IBlock.java: Added edge support.
* org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IControlFlowEdge.java: Store CFG edges.
* org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IControlFlowGraph.java: Added getEdges.
* org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/Block.java: Store CFG edges.
* org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/ControlFlowEdge.java: New file.
* org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/ControlFlowGraph.java: Compute CFG edges.
Diffstat (limited to 'org.eclipse.ptp.pldt.mpi.analysis.cdt')
6 files changed, 539 insertions, 411 deletions
diff --git a/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IBlock.java b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IBlock.java index f61b879..cc5ec77 100644 --- a/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IBlock.java +++ b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IBlock.java @@ -41,6 +41,15 @@ public interface IBlock { */ public List<IBlock> getPreds(); + + public IControlFlowEdge[] getInEdges(); + + public IControlFlowEdge[] getOutEdges(); + + public void addInEdge(IControlFlowEdge edge); + + public void addOutEdge(IControlFlowEdge edge); + /** * @return the next block in CFG according to the topological * order (top-down order) diff --git a/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IControlFlowEdge.java b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IControlFlowEdge.java new file mode 100644 index 0000000..076f9c9 --- /dev/null +++ b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IControlFlowEdge.java @@ -0,0 +1,28 @@ +/********************************************************************** + * Copyright (c) 2009 Elliott Baron. + * 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: + * Elliott Baron - initial API and implementation + *******************************************************************************/ +package org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs; + +/** + * An edge in the {@link IControlFlowGraph} between two {@link IBlock} objects. + */ +public interface IControlFlowEdge { + + /** + * @return the block for which this is an outgoing edge + */ + public abstract IBlock getFrom(); + + /** + * @return the block for which this is an incoming edge + */ + public abstract IBlock getTo(); + +}
\ No newline at end of file diff --git a/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IControlFlowGraph.java b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IControlFlowGraph.java index ecc4449..a817f1c 100644 --- a/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IControlFlowGraph.java +++ b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/IControlFlowGraph.java @@ -60,4 +60,14 @@ public interface IControlFlowGraph { public void buildCFG(); public void print(); + + /** + * @return all of the blocks in this {@link IControlFlowGraph} + */ + public IBlock[] getBlocks(); + + /** + * @return all of the edges in this {@link IControlFlowGraph} + */ + public IControlFlowEdge[] getEdges(); } diff --git a/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/Block.java b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/Block.java index db76c50..ccaaec2 100644 --- a/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/Block.java +++ b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/Block.java @@ -1,5 +1,5 @@ /********************************************************************** - * Copyright (c) 2007 IBM Corporation. + * Copyright (c) 2007, 2009 IBM Corporation. * 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Elliott Baron - fixed print issue *******************************************************************************/ package org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.impl; @@ -21,6 +22,7 @@ import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IBlock; +import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IControlFlowEdge; /** * @@ -30,6 +32,8 @@ import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IBlock; public class Block implements IBlock { protected List<IBlock> succs_; protected List<IBlock> preds_; + protected List<IControlFlowEdge> inEdges_; + protected List<IControlFlowEdge> outEdges_; protected IBlock topNext_ = null; protected IBlock botNext_ = null; protected List<IBlock> DOM_ = null; @@ -105,6 +109,8 @@ public class Block implements IBlock { protected void blockInit(){ succs_ = new ArrayList<IBlock>(); preds_ = new ArrayList<IBlock>(); + inEdges_ = new ArrayList<IControlFlowEdge>(); + outEdges_ = new ArrayList<IControlFlowEdge>(); DOM_ = new ArrayList<IBlock>(); PDOM_ = new ArrayList<IBlock>(); attrs_ = new Hashtable<String,Object>(); @@ -193,6 +199,22 @@ public class Block implements IBlock { return succs_; } + public IControlFlowEdge[] getInEdges() { + return inEdges_.toArray(new IControlFlowEdge[inEdges_.size()]); + } + + public void addInEdge(IControlFlowEdge edge) { + inEdges_.add(edge); + } + + public IControlFlowEdge[] getOutEdges() { + return outEdges_.toArray(new IControlFlowEdge[outEdges_.size()]); + } + + public void addOutEdge(IControlFlowEdge edge) { + outEdges_.add(edge); + } + public List<IBlock> getDOM(){ return DOM_; } diff --git a/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/ControlFlowEdge.java b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/ControlFlowEdge.java new file mode 100644 index 0000000..9364fe7 --- /dev/null +++ b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/ControlFlowEdge.java @@ -0,0 +1,39 @@ +/********************************************************************** + * Copyright (c) 2009 Elliott Baron. + * 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: + * Elliott Baron - initial API and implementation + *******************************************************************************/ +package org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.impl; + +import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IBlock; +import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IControlFlowEdge; + +public class ControlFlowEdge implements IControlFlowEdge { + private IBlock from; + private IBlock to; + + public ControlFlowEdge(IBlock from, IBlock to) { + this.from = from; + this.to = to; + } + + /* (non-Javadoc) + * @see org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IControlFlowEdge#getFrom() + */ + public IBlock getFrom() { + return from; + } + + /* (non-Javadoc) + * @see org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IControlFlowEdge#getTo() + */ + public IBlock getTo() { + return to; + } + +} diff --git a/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/ControlFlowGraph.java b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/ControlFlowGraph.java index 3ee0072..3fd7aae 100644 --- a/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/ControlFlowGraph.java +++ b/org.eclipse.ptp.pldt.mpi.analysis.cdt/src/org/eclipse/ptp/pldt/mpi/analysis/cdt/graphs/impl/ControlFlowGraph.java @@ -1,5 +1,5 @@ /********************************************************************** - * Copyright (c) 2007,2008 IBM Corporation. + * Copyright (c) 2007, 2009 IBM Corporation. * 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Elliott Baron - added use of ControlFlowEdge type *******************************************************************************/ package org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.impl; @@ -40,35 +41,38 @@ import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement; import org.eclipse.cdt.core.dom.ast.IASTWhileStatement; import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IBlock; +import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IControlFlowEdge; import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IControlFlowGraph; /** * Control Flow Graph implementation * * @author Yuan Zhang - * + * */ public class ControlFlowGraph implements IControlFlowGraph { protected IASTStatement prog_; protected List<IBlock> BBs_; + protected List<IControlFlowEdge> edges_; protected IBlock entry_; protected IBlock exit_; private static final boolean traceOn = false; - - public ControlFlowGraph(IASTStatement prog){ + + public ControlFlowGraph(IASTStatement prog) { this.prog_ = prog; BBs_ = new ArrayList<IBlock>(); + edges_ = new ArrayList<IControlFlowEdge>(); } - /** Constructing a CFG consists of several steps: - * Step 1: collecting all blocks in a function - * Step 2: calculating control flows among blocks - * Step 3: calculating dominator and post-dominator relations - * Step 4: Sort all blocks according to the topological order - * Step 5: Other additional operations + /** + * Constructing a CFG consists of several steps: Step 1: collecting all + * blocks in a function Step 2: calculating control flows among blocks Step + * 3: calculating dominator and post-dominator relations Step 4: Sort all + * blocks according to the topological order Step 5: Other additional + * operations */ - public void buildCFG(){ + public void buildCFG() { collectBlocks(); setBlockFlow(); buildDOM(); @@ -76,614 +80,617 @@ public class ControlFlowGraph implements IControlFlowGraph { sort(); otherOPs(); } - + /** Collecting all blocks in a function */ - protected void collectBlocks(){ + protected void collectBlocks() { entry_ = new Block(); exit_ = new Block(); BasicBlockCollector bc = new BasicBlockCollector(); bc.run(); } - + /** Calculating control flows among blocks */ - protected void setBlockFlow(){ + protected void setBlockFlow() { FlowBuilder flowBuilder = new FlowBuilder(); flowBuilder.run(); } - - class BasicBlockCollector extends ASTVisitor{ - public void run(){ + class BasicBlockCollector extends ASTVisitor { + + public void run() { this.shouldVisitStatements = true; this.shouldVisitDeclarations = true; prog_.accept(this); } - - public int visit(IASTStatement stmt) - { + + public int visit(IASTStatement stmt) { IBlock block; - if(stmt instanceof IASTBreakStatement){ + if (stmt instanceof IASTBreakStatement) { block = new Block(stmt); addBlock(block); - } - else if(stmt instanceof IASTCaseStatement){ + } else if (stmt instanceof IASTCaseStatement) { block = new Block(stmt); addBlock(block); - } - else if(stmt instanceof IASTCompoundStatement){ - } - else if(stmt instanceof IASTContinueStatement){ + } else if (stmt instanceof IASTCompoundStatement) { + } else if (stmt instanceof IASTContinueStatement) { block = new Block(stmt); addBlock(block); - } - else if(stmt instanceof IASTDeclarationStatement){ + } else if (stmt instanceof IASTDeclarationStatement) { block = new Block(stmt); addBlock(block); - } - else if(stmt instanceof IASTDefaultStatement){ + } else if (stmt instanceof IASTDefaultStatement) { block = new Block(stmt); addBlock(block); - } - else if(stmt instanceof IASTDoStatement){ - IASTDoStatement doStmt = (IASTDoStatement)stmt; + } else if (stmt instanceof IASTDoStatement) { + IASTDoStatement doStmt = (IASTDoStatement) stmt; block = new Block(doStmt.getCondition(), stmt); addBlock(block); IBlock exitjoin = new Block(null, stmt, Block.exit_join_type); addBlock(exitjoin); - IBlock continuejoin = new Block(null, stmt, Block.continue_join_type); + IBlock continuejoin = new Block(null, stmt, + Block.continue_join_type); addBlock(continuejoin); - } - else if(stmt instanceof IASTExpressionStatement){ + } else if (stmt instanceof IASTExpressionStatement) { block = new Block(stmt); addBlock(block); - } - else if(stmt instanceof IASTForStatement){ - IASTForStatement forStmt = (IASTForStatement)stmt; + } else if (stmt instanceof IASTForStatement) { + IASTForStatement forStmt = (IASTForStatement) stmt; /* The initialization is a statement, and will be added later */ block = new Block(forStmt.getConditionExpression(), stmt); addBlock(block); - if(forStmt.getIterationExpression() != null){ + if (forStmt.getIterationExpression() != null) { block = new Block(forStmt.getIterationExpression(), stmt); addBlock(block); } - IBlock continuejoin = new Block(null, stmt, Block.continue_join_type); + IBlock continuejoin = new Block(null, stmt, + Block.continue_join_type); addBlock(continuejoin); IBlock exitjoin = new Block(null, stmt, Block.exit_join_type); addBlock(exitjoin); - } - else if(stmt instanceof IASTGotoStatement){ + } else if (stmt instanceof IASTGotoStatement) { block = new Block(stmt); addBlock(block); - } - else if(stmt instanceof IASTIfStatement){ - IASTIfStatement ifStmt = (IASTIfStatement)stmt; + } else if (stmt instanceof IASTIfStatement) { + IASTIfStatement ifStmt = (IASTIfStatement) stmt; block = new Block(ifStmt.getConditionExpression(), stmt); addBlock(block); IBlock join = new Block(null, stmt, Block.exit_join_type); addBlock(join); - } - else if(stmt instanceof IASTLabelStatement){ - IASTLabelStatement labelS = (IASTLabelStatement)stmt; + } else if (stmt instanceof IASTLabelStatement) { + IASTLabelStatement labelS = (IASTLabelStatement) stmt; block = new Block(labelS.getName(), stmt, Block.label_type); addBlock(block); - } - else if(stmt instanceof IASTNullStatement){ + } else if (stmt instanceof IASTNullStatement) { block = new Block(stmt); addBlock(block); - } - else if(stmt instanceof IASTProblemStatement){ + } else if (stmt instanceof IASTProblemStatement) { block = new Block(stmt); addBlock(block); - } - else if(stmt instanceof IASTReturnStatement){ - IASTReturnStatement rtStmt = (IASTReturnStatement)stmt; + } else if (stmt instanceof IASTReturnStatement) { + IASTReturnStatement rtStmt = (IASTReturnStatement) stmt; block = new Block(rtStmt.getReturnValue(), stmt); addBlock(block); - } - else if(stmt instanceof IASTSwitchStatement){ - IASTSwitchStatement swStmt = (IASTSwitchStatement)stmt; + } else if (stmt instanceof IASTSwitchStatement) { + IASTSwitchStatement swStmt = (IASTSwitchStatement) stmt; block = new Block(swStmt.getControllerExpression(), stmt); addBlock(block); IBlock join = new Block(null, stmt, Block.exit_join_type); addBlock(join); - } - else if(stmt instanceof IASTWhileStatement){ - IASTWhileStatement whStmt = (IASTWhileStatement)stmt; + } else if (stmt instanceof IASTWhileStatement) { + IASTWhileStatement whStmt = (IASTWhileStatement) stmt; block = new Block(whStmt.getCondition(), stmt); addBlock(block); IBlock join = new Block(null, stmt, Block.continue_join_type); addBlock(join); IBlock exitjoin = new Block(null, stmt, Block.exit_join_type); addBlock(exitjoin); - } + } return PROCESS_CONTINUE; } } - - public class FlowBuilder extends ASTVisitor{ + + public class FlowBuilder extends ASTVisitor { private boolean exitBlock = false; - - public void run(){ + + public void run() { this.shouldVisitStatements = true; this.shouldVisitDeclarations = true; this.shouldVisitTranslationUnit = true; this.shouldVisitExpressions = true; IBlock first = firstBlock(prog_); - ControlFlowEdge(entry_, first); + addControlFlowEdge(entry_, first); prog_.accept(this); } - - public int visit(IASTStatement stmt){ - if(stmt instanceof IASTBreakStatement){ + + public int visit(IASTStatement stmt) { + if (stmt instanceof IASTBreakStatement) { /** A break statement flows to a block with exit_join_type */ - if(traceOn) System.out.println("BreakStatement"); + if (traceOn) + System.out.println("BreakStatement"); IBlock block = getBlock(stmt); IASTNode parent = stmt.getParent(); - while(true){ - if(parent instanceof IASTDoStatement || - parent instanceof IASTForStatement || - parent instanceof IASTWhileStatement || - parent instanceof IASTSwitchStatement){ - IBlock exitjoin = getBlock(null, (IASTStatement)parent, - Block.exit_join_type); - ControlFlowEdge(block, exitjoin); + while (true) { + if (parent instanceof IASTDoStatement + || parent instanceof IASTForStatement + || parent instanceof IASTWhileStatement + || parent instanceof IASTSwitchStatement) { + IBlock exitjoin = getBlock(null, + (IASTStatement) parent, Block.exit_join_type); + addControlFlowEdge(block, exitjoin); break; - } - else{ + } else { parent = parent.getParent(); } } - } - else if(stmt instanceof IASTCaseStatement || - stmt instanceof IASTDefaultStatement){ - /** A case(default) statement flows from the switch - * condition block or the previous case statement, and - * flows to the first statement in the case(default) body. + } else if (stmt instanceof IASTCaseStatement + || stmt instanceof IASTDefaultStatement) { + /** + * A case(default) statement flows from the switch condition + * block or the previous case statement, and flows to the first + * statement in the case(default) body. */ - if(traceOn) System.out.println("CaseStatement / DefaultStatement"); + if (traceOn) + System.out.println("CaseStatement / DefaultStatement"); IASTNode parent = stmt.getParent(); - while(true){ - if(parent instanceof IASTSwitchStatement) break; - else parent = parent.getParent(); + while (true) { + if (parent instanceof IASTSwitchStatement) + break; + else + parent = parent.getParent(); } - IASTSwitchStatement swStmt = (IASTSwitchStatement)parent; - IBlock swblock = getBlock(swStmt.getControllerExpression(), - (IASTStatement)parent); + IASTSwitchStatement swStmt = (IASTSwitchStatement) parent; + IBlock swblock = getBlock(swStmt.getControllerExpression(), + (IASTStatement) parent); IBlock caseblock = getBlock(stmt); - ControlFlowEdge(swblock, caseblock); + addControlFlowEdge(swblock, caseblock); IBlock next = nextBlock(stmt); - ControlFlowEdge(caseblock, next); - } - else if(stmt instanceof IASTCompoundStatement){ - } - else if(stmt instanceof IASTContinueStatement){ + addControlFlowEdge(caseblock, next); + } else if (stmt instanceof IASTCompoundStatement) { + } else if (stmt instanceof IASTContinueStatement) { /** A continue statement flows to the continue_join_type block */ - if(traceOn) System.out.println("ContinueStatement"); + if (traceOn) + System.out.println("ContinueStatement"); IBlock block = getBlock(stmt); IASTNode parent = stmt.getParent(); - while(true){ - if(parent instanceof IASTDoStatement || - parent instanceof IASTForStatement || - parent instanceof IASTWhileStatement){ - IBlock continuejoin = getBlock(null, (IASTStatement)parent, + while (true) { + if (parent instanceof IASTDoStatement + || parent instanceof IASTForStatement + || parent instanceof IASTWhileStatement) { + IBlock continuejoin = getBlock(null, + (IASTStatement) parent, Block.continue_join_type); - ControlFlowEdge(block, continuejoin); + addControlFlowEdge(block, continuejoin); break; - } - else{ + } else { parent = parent.getParent(); } } - } - else if(stmt instanceof IASTDeclarationStatement){ - /** Except the regular variable declaration statements, the - * initializer of a for statement could also be a - * declaration statement. In the latter case, the flow - * relation is already calculated and therefore nothing - * is done here. */ - if(traceOn) System.out.println("DeclarationStatement"); + } else if (stmt instanceof IASTDeclarationStatement) { + /** + * Except the regular variable declaration statements, the + * initializer of a for statement could also be a declaration + * statement. In the latter case, the flow relation is already + * calculated and therefore nothing is done here. + */ + if (traceOn) + System.out.println("DeclarationStatement"); IASTNode parent = stmt.getParent(); - if(parent instanceof IASTForStatement){ - IASTForStatement forStmt = (IASTForStatement)parent; - if(forStmt.getInitializerStatement() == stmt) + if (parent instanceof IASTForStatement) { + IASTForStatement forStmt = (IASTForStatement) parent; + if (forStmt.getInitializerStatement() == stmt) return PROCESS_CONTINUE; } IBlock block = getBlock(stmt); IBlock next = nextBlock(stmt); - ControlFlowEdge(block, next); - } - else if(stmt instanceof IASTDoStatement){ - /** We calculate four types of control flows here: - * (1) the condition block flows to the first block of the loop body - * (2) the condition block flows to the exit_join_type block - * (3) the continue_join_type block flows to the condition block - * (4) the exit_join_type block flows to the next statement after - * this do loop + addControlFlowEdge(block, next); + } else if (stmt instanceof IASTDoStatement) { + /** + * We calculate four types of control flows here: (1) the + * condition block flows to the first block of the loop body (2) + * the condition block flows to the exit_join_type block (3) the + * continue_join_type block flows to the condition block (4) the + * exit_join_type block flows to the next statement after this + * do loop */ - if(traceOn) System.out.println("DoStatement"); - IASTDoStatement doStmt = (IASTDoStatement)stmt; + if (traceOn) + System.out.println("DoStatement"); + IASTDoStatement doStmt = (IASTDoStatement) stmt; IBlock cond = getBlock(doStmt.getCondition(), stmt); - if(doStmt.getBody() == null){ - ControlFlowEdge(cond, cond); + if (doStmt.getBody() == null) { + addControlFlowEdge(cond, cond); } else { IBlock first = firstBlock(doStmt.getBody()); - ControlFlowEdge(cond, first); - IBlock continuejoin = getBlock(null, stmt, Block.continue_join_type); - ControlFlowEdge(continuejoin, cond); + addControlFlowEdge(cond, first); + IBlock continuejoin = getBlock(null, stmt, + Block.continue_join_type); + addControlFlowEdge(continuejoin, cond); } IBlock exitjoin = getBlock(null, stmt, Block.exit_join_type); - ControlFlowEdge(cond, exitjoin); + addControlFlowEdge(cond, exitjoin); IBlock next = nextBlock(stmt); - ControlFlowEdge(exitjoin, next); - } - else if(stmt instanceof IASTExpressionStatement){ - /** If the expression statement is the initializer of a for - * loop, do nothing here. If this statement contains an - * "exit" function call, it flows to the exit block of the - * current function */ - if(traceOn) System.out.println("ExpressionSatement"); + addControlFlowEdge(exitjoin, next); + } else if (stmt instanceof IASTExpressionStatement) { + /** + * If the expression statement is the initializer of a for loop, + * do nothing here. If this statement contains an "exit" + * function call, it flows to the exit block of the current + * function + */ + if (traceOn) + System.out.println("ExpressionSatement"); IASTNode parent = stmt.getParent(); - if(parent instanceof IASTForStatement){ - IASTForStatement forStmt = (IASTForStatement)parent; - if(forStmt.getInitializerStatement() == stmt) + if (parent instanceof IASTForStatement) { + IASTForStatement forStmt = (IASTForStatement) parent; + if (forStmt.getInitializerStatement() == stmt) return PROCESS_CONTINUE; } IBlock block = getBlock(stmt); - IASTExpressionStatement exprStmt = (IASTExpressionStatement)stmt; + IASTExpressionStatement exprStmt = (IASTExpressionStatement) stmt; exitBlock = false; exprStmt.getExpression().accept(this); - if(exitBlock){ - ControlFlowEdge(block, exit_); - } - else { + if (exitBlock) { + addControlFlowEdge(block, exit_); + } else { IBlock next = nextBlock(stmt); - ControlFlowEdge(block, next); + addControlFlowEdge(block, next); } - } - else if(stmt instanceof IASTForStatement){ - /** In a for loop, the initializer flows to the loop condition, + } else if (stmt instanceof IASTForStatement) { + /** + * In a for loop, the initializer flows to the loop condition, * the loop condition flows to the first statement in the loop - * body and the block with exit_join_type. The continue_join_type - * block flows to the iterator, and the iterator flows to the - * condition block */ - if(traceOn) System.out.println("ForStatement"); - IASTForStatement forStmt = (IASTForStatement)stmt; + * body and the block with exit_join_type. The + * continue_join_type block flows to the iterator, and the + * iterator flows to the condition block + */ + if (traceOn) + System.out.println("ForStatement"); + IASTForStatement forStmt = (IASTForStatement) stmt; IASTStatement initStmt = forStmt.getInitializerStatement(); IBlock init = getBlock(initStmt); IBlock cond = getBlock(forStmt.getConditionExpression(), stmt); IBlock iter = null; - IBlock continuejoin = getBlock(null, stmt, Block.continue_join_type); + IBlock continuejoin = getBlock(null, stmt, + Block.continue_join_type); IBlock exitjoin = getBlock(null, stmt, Block.exit_join_type); - - if(forStmt.getIterationExpression() != null) + + if (forStmt.getIterationExpression() != null) iter = getBlock(forStmt.getIterationExpression(), stmt); - + /* The empty initialization will be a Null statement */ - ControlFlowEdge(init, cond); - - if(forStmt.getBody() != null){ + addControlFlowEdge(init, cond); + + if (forStmt.getBody() != null) { IBlock first = firstBlock(forStmt.getBody()); - ControlFlowEdge(cond, first); - if(iter != null){ - ControlFlowEdge(continuejoin, iter); - ControlFlowEdge(iter, cond); + addControlFlowEdge(cond, first); + if (iter != null) { + addControlFlowEdge(continuejoin, iter); + addControlFlowEdge(iter, cond); } else - ControlFlowEdge(continuejoin, cond); + addControlFlowEdge(continuejoin, cond); } else { - if(iter != null){ - ControlFlowEdge(cond, iter); - ControlFlowEdge(iter, cond); + if (iter != null) { + addControlFlowEdge(cond, iter); + addControlFlowEdge(iter, cond); } else { - ControlFlowEdge(cond, cond); + addControlFlowEdge(cond, cond); } - } - ControlFlowEdge(cond, exitjoin); + } + addControlFlowEdge(cond, exitjoin); IBlock next = nextBlock(stmt); - ControlFlowEdge(exitjoin, next); - } - else if(stmt instanceof IASTGotoStatement){ + addControlFlowEdge(exitjoin, next); + } else if (stmt instanceof IASTGotoStatement) { /** Goto statement flows to the corresponding label statement */ - if(traceOn) System.out.println("GotoStatement"); - IASTGotoStatement gotoStmt = (IASTGotoStatement)stmt; - IBlock label = getBlock(gotoStmt.getName(), gotoStmt, Block.label_type); - if(label == null){ + if (traceOn) + System.out.println("GotoStatement"); + IASTGotoStatement gotoStmt = (IASTGotoStatement) stmt; + IBlock label = getBlock(gotoStmt.getName(), gotoStmt, + Block.label_type); + if (label == null) { System.out.println("Null Label Error"); } IBlock block = getBlock(gotoStmt); - ControlFlowEdge(block, label); - } - else if(stmt instanceof IASTIfStatement){ - /** The if condition block flows to the first block of the + addControlFlowEdge(block, label); + } else if (stmt instanceof IASTIfStatement) { + /** + * The if condition block flows to the first block of the * then(else) clause if it is not empty, the exit_join_type - * block otherwise */ - if(traceOn) System.out.println("IfStatement"); - IASTIfStatement ifStmt = (IASTIfStatement)stmt; + * block otherwise + */ + if (traceOn) + System.out.println("IfStatement"); + IASTIfStatement ifStmt = (IASTIfStatement) stmt; IBlock condb = getBlock(ifStmt.getConditionExpression(), stmt); IBlock join = getBlock(null, stmt, Block.exit_join_type); - if(ifStmt.getThenClause() != null){ + if (ifStmt.getThenClause() != null) { IBlock thenb = firstBlock(ifStmt.getThenClause()); - ControlFlowEdge(condb, thenb); + addControlFlowEdge(condb, thenb); } else { - ControlFlowEdge(condb, join); + addControlFlowEdge(condb, join); } - - if(ifStmt.getElseClause() != null){ + + if (ifStmt.getElseClause() != null) { IBlock elseb = firstBlock(ifStmt.getElseClause()); - ControlFlowEdge(condb, elseb); + addControlFlowEdge(condb, elseb); } else { - ControlFlowEdge(condb, join); + addControlFlowEdge(condb, join); } IBlock next = nextBlock(stmt); - ControlFlowEdge(join, next); - } - else if(stmt instanceof IASTLabelStatement){ - /** If there is a nested statement, the label statement flows - * to the first block of the nested statement + addControlFlowEdge(join, next); + } else if (stmt instanceof IASTLabelStatement) { + /** + * If there is a nested statement, the label statement flows to + * the first block of the nested statement */ - if(traceOn) System.out.println("LabelStatement"); - IASTLabelStatement label = (IASTLabelStatement)stmt; + if (traceOn) + System.out.println("LabelStatement"); + IASTLabelStatement label = (IASTLabelStatement) stmt; IBlock block = getBlock(label.getName(), stmt, Block.label_type); - if(label.getNestedStatement() == null){ + if (label.getNestedStatement() == null) { IBlock next = nextBlock(stmt); - ControlFlowEdge(block, next); + addControlFlowEdge(block, next); } else { IBlock next = firstBlock(label.getNestedStatement()); - ControlFlowEdge(block, next); + addControlFlowEdge(block, next); } - } - else if(stmt instanceof IASTNullStatement){ - if(traceOn) System.out.println("NullStatement"); + } else if (stmt instanceof IASTNullStatement) { + if (traceOn) + System.out.println("NullStatement"); IASTNode parent = stmt.getParent(); - if(parent instanceof IASTForStatement){ - IASTForStatement forStmt = (IASTForStatement)parent; - if(forStmt.getInitializerStatement() == stmt) + if (parent instanceof IASTForStatement) { + IASTForStatement forStmt = (IASTForStatement) parent; + if (forStmt.getInitializerStatement() == stmt) return PROCESS_CONTINUE; } IBlock block = getBlock(stmt); IBlock next = nextBlock(stmt); - ControlFlowEdge(block, next); - } - else if(stmt instanceof IASTProblemStatement){ - if(traceOn) System.out.println("ProblemStatement"); + addControlFlowEdge(block, next); + } else if (stmt instanceof IASTProblemStatement) { + if (traceOn) + System.out.println("ProblemStatement"); IBlock block = getBlock(stmt); IBlock next = nextBlock(stmt); - ControlFlowEdge(block, next); - } - else if(stmt instanceof IASTReturnStatement){ + addControlFlowEdge(block, next); + } else if (stmt instanceof IASTReturnStatement) { /* The return statement flows to the exit block */ - if(traceOn) System.out.println("ReturnStatement"); - IASTReturnStatement rtStmt = (IASTReturnStatement)stmt; + if (traceOn) + System.out.println("ReturnStatement"); + IASTReturnStatement rtStmt = (IASTReturnStatement) stmt; IBlock rv = getBlock(rtStmt.getReturnValue(), stmt); - ControlFlowEdge(rv, exit_); - } - else if(stmt instanceof IASTSwitchStatement){ - /** The exit_join block of a switch statement flows to the - * first block of the next statement + addControlFlowEdge(rv, exit_); + } else if (stmt instanceof IASTSwitchStatement) { + /** + * The exit_join block of a switch statement flows to the first + * block of the next statement */ - if(traceOn) System.out.println("SwitchStatement"); + if (traceOn) + System.out.println("SwitchStatement"); IBlock join = getBlock(null, stmt, Block.exit_join_type); IBlock next = nextBlock(stmt); - ControlFlowEdge(join, next); - } - else if(stmt instanceof IASTWhileStatement){ - /** The condition block of a while loop flows to the first - * statement in the loop body and the exit_join block. - * The condition_join block flows to the condition block. + addControlFlowEdge(join, next); + } else if (stmt instanceof IASTWhileStatement) { + /** + * The condition block of a while loop flows to the first + * statement in the loop body and the exit_join block. The + * condition_join block flows to the condition block. */ - if(traceOn) System.out.println("WhileStatement"); - IASTWhileStatement whStmt = (IASTWhileStatement)stmt; + if (traceOn) + System.out.println("WhileStatement"); + IASTWhileStatement whStmt = (IASTWhileStatement) stmt; IBlock cond = getBlock(whStmt.getCondition(), stmt); - IBlock continuejoin = getBlock(null, stmt, Block.continue_join_type); + IBlock continuejoin = getBlock(null, stmt, + Block.continue_join_type); IBlock exitjoin = getBlock(null, stmt, Block.exit_join_type); - if(whStmt.getBody() == null){ - ControlFlowEdge(cond, cond); + if (whStmt.getBody() == null) { + addControlFlowEdge(cond, cond); } else { IBlock first = firstBlock(whStmt.getBody()); - ControlFlowEdge(cond, first); - ControlFlowEdge(continuejoin, cond); + addControlFlowEdge(cond, first); + addControlFlowEdge(continuejoin, cond); } IBlock next = nextBlock(stmt); - ControlFlowEdge(cond, exitjoin); - ControlFlowEdge(exitjoin, next); + addControlFlowEdge(cond, exitjoin); + addControlFlowEdge(exitjoin, next); } return PROCESS_CONTINUE; } - + /* * @return the first block of stmt */ - public IBlock firstBlock(IASTStatement stmt){ - - if(stmt instanceof IASTBreakStatement){ + public IBlock firstBlock(IASTStatement stmt) { + + if (stmt instanceof IASTBreakStatement) { return getBlock(stmt); - } - else if(stmt instanceof IASTCaseStatement){ + } else if (stmt instanceof IASTCaseStatement) { return getBlock(stmt); - } - else if(stmt instanceof IASTCompoundStatement){ - IASTCompoundStatement cmpStmt = (IASTCompoundStatement)stmt; + } else if (stmt instanceof IASTCompoundStatement) { + IASTCompoundStatement cmpStmt = (IASTCompoundStatement) stmt; IASTStatement[] stmts = cmpStmt.getStatements(); - for(int i=0; i<stmts.length; i++){ - if(stmts[i] != null) return firstBlock(stmts[i]); + for (int i = 0; i < stmts.length; i++) { + if (stmts[i] != null) + return firstBlock(stmts[i]); } - /* The compound statement is empty. Return the first block - * of the next Statement. + /* + * The compound statement is empty. Return the first block of + * the next Statement. */ return nextBlock(stmt); - } - else if(stmt instanceof IASTContinueStatement){ + } else if (stmt instanceof IASTContinueStatement) { return getBlock(stmt); - } - else if(stmt instanceof IASTDeclarationStatement){ + } else if (stmt instanceof IASTDeclarationStatement) { return getBlock(stmt); - } - else if(stmt instanceof IASTDefaultStatement){ + } else if (stmt instanceof IASTDefaultStatement) { return getBlock(stmt); - } - else if(stmt instanceof IASTDoStatement){ - IASTDoStatement doStmt = (IASTDoStatement)stmt; - if(doStmt.getBody() != null) + } else if (stmt instanceof IASTDoStatement) { + IASTDoStatement doStmt = (IASTDoStatement) stmt; + if (doStmt.getBody() != null) return firstBlock(doStmt.getBody()); else return getBlock(doStmt.getCondition(), stmt); - } - else if(stmt instanceof IASTExpressionStatement){ + } else if (stmt instanceof IASTExpressionStatement) { return getBlock(stmt); - } - else if(stmt instanceof IASTForStatement){ - IASTForStatement forStmt = (IASTForStatement)stmt; + } else if (stmt instanceof IASTForStatement) { + IASTForStatement forStmt = (IASTForStatement) stmt; IASTStatement initS = forStmt.getInitializerStatement(); - if(initS != null) + if (initS != null) return getBlock(initS); else return getBlock(forStmt.getConditionExpression(), stmt); - } - else if(stmt instanceof IASTGotoStatement){ + } else if (stmt instanceof IASTGotoStatement) { return getBlock(stmt); - } - else if(stmt instanceof IASTIfStatement){ - IASTIfStatement ifStmt = (IASTIfStatement)stmt; + } else if (stmt instanceof IASTIfStatement) { + IASTIfStatement ifStmt = (IASTIfStatement) stmt; return getBlock(ifStmt.getConditionExpression(), stmt); - } - else if(stmt instanceof IASTLabelStatement){ - IASTLabelStatement label = (IASTLabelStatement)stmt; + } else if (stmt instanceof IASTLabelStatement) { + IASTLabelStatement label = (IASTLabelStatement) stmt; return getBlock(label.getName(), stmt, Block.label_type); - } - else if(stmt instanceof IASTNullStatement){ + } else if (stmt instanceof IASTNullStatement) { return getBlock(stmt); - } - else if(stmt instanceof IASTProblemStatement){ + } else if (stmt instanceof IASTProblemStatement) { return getBlock(stmt); - } - else if(stmt instanceof IASTReturnStatement){ - IASTReturnStatement rtStmt = (IASTReturnStatement)stmt; + } else if (stmt instanceof IASTReturnStatement) { + IASTReturnStatement rtStmt = (IASTReturnStatement) stmt; return getBlock(rtStmt.getReturnValue(), stmt); - } - else if(stmt instanceof IASTSwitchStatement){ - IASTSwitchStatement swStmt = (IASTSwitchStatement)stmt; + } else if (stmt instanceof IASTSwitchStatement) { + IASTSwitchStatement swStmt = (IASTSwitchStatement) stmt; return getBlock(swStmt.getControllerExpression(), stmt); - } - else if(stmt instanceof IASTWhileStatement){ - IASTWhileStatement whStmt = (IASTWhileStatement)stmt; + } else if (stmt instanceof IASTWhileStatement) { + IASTWhileStatement whStmt = (IASTWhileStatement) stmt; return getBlock(whStmt.getCondition(), stmt); } return null; } - /* * @return */ - public IBlock nextBlock(IASTStatement stmt){ - + public IBlock nextBlock(IASTStatement stmt) { + IASTNode node = stmt.getParent(); - if(!(node instanceof IASTStatement)){ + if (!(node instanceof IASTStatement)) { /* empty return statement for void-typed functions */ return exit_; } - - IASTStatement parent = (IASTStatement)node; - if(parent instanceof IASTCompoundStatement){ - IASTCompoundStatement cmpStmt = (IASTCompoundStatement)parent; + + IASTStatement parent = (IASTStatement) node; + if (parent instanceof IASTCompoundStatement) { + IASTCompoundStatement cmpStmt = (IASTCompoundStatement) parent; IASTStatement[] stmts = cmpStmt.getStatements(); int i; - for(i=0; i<stmts.length; i++){ - if(stmts[i] == stmt) break; + for (i = 0; i < stmts.length; i++) { + if (stmts[i] == stmt) + break; } - for(i=i+1; i<stmts.length; i++){ - if(stmts[i] != null) return firstBlock(stmts[i]); + for (i = i + 1; i < stmts.length; i++) { + if (stmts[i] != null) + return firstBlock(stmts[i]); } - /*stmt is the last one in compound statement */ + /* stmt is the last one in compound statement */ return nextBlock(parent); - } - else if(parent instanceof IASTDeclarationStatement){ + } else if (parent instanceof IASTDeclarationStatement) { return nextBlock(parent); - } - else if(parent instanceof IASTDoStatement){ - /* "stmt" is the body of Do statement, control flows to - * the continue join block + } else if (parent instanceof IASTDoStatement) { + /* + * "stmt" is the body of Do statement, control flows to the + * continue join block */ return getBlock(null, parent, Block.continue_join_type); - } - else if(parent instanceof IASTForStatement){ + } else if (parent instanceof IASTForStatement) { /* control flows to the for loop continue join block */ return getBlock(null, parent, Block.continue_join_type); - } - else if(parent instanceof IASTIfStatement){ + } else if (parent instanceof IASTIfStatement) { return getBlock(null, parent, Block.exit_join_type); - } - else if(parent instanceof IASTLabelStatement){ + } else if (parent instanceof IASTLabelStatement) { return nextBlock(parent); - } - else if(parent instanceof IASTSwitchStatement){ + } else if (parent instanceof IASTSwitchStatement) { return getBlock(null, parent, Block.exit_join_type); - } - else if(parent instanceof IASTWhileStatement){ + } else if (parent instanceof IASTWhileStatement) { return getBlock(null, parent, Block.continue_join_type); - } - else + } else return nextBlock(parent); } - - private void ControlFlowEdge(IBlock from, IBlock to){ - if(!from.getSuccs().contains(to)) + + private void addControlFlowEdge(IBlock from, IBlock to) { + boolean newEdge = false; + if (!from.getSuccs().contains(to)) { from.getSuccs().add(to); - if(!to.getPreds().contains(from)) + newEdge = true; + } + if (!to.getPreds().contains(from)) { to.getPreds().add(from); + newEdge = true; + } + if (newEdge) { + ControlFlowEdge edge = new ControlFlowEdge(from, to); + edges_.add(edge); + from.addOutEdge(edge); + to.addInEdge(edge); + } } - - public int visit(IASTExpression expr){ + + public int visit(IASTExpression expr) { if (expr instanceof IASTFunctionCallExpression) { - IASTFunctionCallExpression funcExpr = (IASTFunctionCallExpression)expr; + IASTFunctionCallExpression funcExpr = (IASTFunctionCallExpression) expr; IASTExpression funcname = funcExpr.getFunctionNameExpression(); String signature = funcname.getRawSignature(); - if(signature.equals("exit")){ + if (signature.equals("exit")) { exitBlock = true; } return PROCESS_SKIP; - } + } return PROCESS_CONTINUE; } } - - protected void addBasicBlock(IBlock block){ - if(!BBs_.contains(block)) + protected void addBasicBlock(IBlock block) { + if (!BBs_.contains(block)) BBs_.add(block); } - + public IBlock getBlock(IASTExpression expr, IASTStatement parent) { - for(Iterator<IBlock> i = BBs_.iterator(); i.hasNext();){ + for (Iterator<IBlock> i = BBs_.iterator(); i.hasNext();) { IBlock bb = i.next(); - if(bb.search(expr, parent)) return bb; + if (bb.search(expr, parent)) + return bb; } return null; } - public IBlock getBlock(IASTStatement stmt){ - for(Iterator<IBlock> i = BBs_.iterator(); i.hasNext();){ + public IBlock getBlock(IASTStatement stmt) { + for (Iterator<IBlock> i = BBs_.iterator(); i.hasNext();) { IBlock bb = i.next(); - if(bb.search(stmt)) return bb; + if (bb.search(stmt)) + return bb; } return null; } - - public IBlock getBlock(IASTName label){ - for(Iterator<IBlock> i = BBs_.iterator(); i.hasNext();){ + + public IBlock getBlock(IASTName label) { + for (Iterator<IBlock> i = BBs_.iterator(); i.hasNext();) { IBlock bb = i.next(); - if(bb.search(label)) return bb; + if (bb.search(label)) + return bb; } return null; } - - public IBlock getBlock(IASTNode content, IASTStatement parent, int type){ - for(Iterator<IBlock> i = BBs_.iterator(); i.hasNext();){ - Block bb = (Block)i.next(); - if(bb.search(content, parent, type)) return bb; + + public IBlock getBlock(IASTNode content, IASTStatement parent, int type) { + for (Iterator<IBlock> i = BBs_.iterator(); i.hasNext();) { + Block bb = (Block) i.next(); + if (bb.search(content, parent, type)) + return bb; } return null; } - + + public IBlock[] getBlocks() { + return BBs_.toArray(new IBlock[BBs_.size()]); + } + + public IControlFlowEdge[] getEdges() { + return edges_.toArray(new IControlFlowEdge[edges_.size()]); + } + public IBlock getEntry() { return entry_; } @@ -691,129 +698,142 @@ public class ControlFlowGraph implements IControlFlowGraph { public IBlock getExit() { return exit_; } - - public void addBlock(IBlock bb){ - if(!BBs_.contains(bb)) + + public void addBlock(IBlock bb) { + if (!BBs_.contains(bb)) BBs_.add(bb); } @SuppressWarnings("unchecked") - public void buildDOM(){ + public void buildDOM() { List<IBlock> all = new ArrayList<IBlock>(); all.add(entry_); all.addAll(BBs_); all.add(exit_); - + entry_.getDOM().add(entry_); - + Iterator<IBlock> i; - for(i = BBs_.iterator(); i.hasNext();){ + for (i = BBs_.iterator(); i.hasNext();) { i.next().setDOM(all); } exit_.setDOM(all); - + boolean change = true; - while(change){ + while (change) { change = false; - for(i = all.iterator(); i.hasNext();){ + for (i = all.iterator(); i.hasNext();) { IBlock block = i.next(); - if(block == entry_) continue; + if (block == entry_) + continue; List<IBlock> temp = new ArrayList<IBlock>(all); - for(Iterator<IBlock> ii = block.getPreds().iterator(); ii.hasNext();){ + for (Iterator<IBlock> ii = block.getPreds().iterator(); ii + .hasNext();) { IBlock pred = ii.next(); temp = intersect(temp, pred.getDOM()); } List<IBlock> D = new ArrayList<IBlock>(temp); - if(!D.contains(block)) D.add(block); - if(!equals(D, block.getDOM())){ + if (!D.contains(block)) + D.add(block); + if (!equals(D, block.getDOM())) { change = true; block.setDOM(D); } } } } - - public void buildPDOM(){ + + public void buildPDOM() { /* TODO */ } - + @SuppressWarnings("unchecked") - public List intersect(List A, List B){ - if(A == null || B == null) return null; + public List intersect(List A, List B) { + if (A == null || B == null) + return null; List list = new ArrayList(); - for(Iterator i = A.iterator(); i.hasNext();){ + for (Iterator i = A.iterator(); i.hasNext();) { Object o = i.next(); - if(B.contains(o)) list.add(o); + if (B.contains(o)) + list.add(o); } return list; } + @SuppressWarnings("unchecked") - public boolean equals(List A, List B){ - if(A == null && B == null) return true; - if(A == null && B != null) return false; - if(A != null && B == null) return false; - if(A.size() != B.size()) return false; - for(Iterator i = A.iterator(); i.hasNext();){ - if(!B.contains(i.next())) return false; + public boolean equals(List A, List B) { + if (A == null && B == null) + return true; + if (A == null && B != null) + return false; + if (A != null && B == null) + return false; + if (A.size() != B.size()) + return false; + for (Iterator i = A.iterator(); i.hasNext();) { + if (!B.contains(i.next())) + return false; } return true; } - + /** * Sort Blocks in topological order */ private Stack<IBlock> order; - protected void sort(){ + + protected void sort() { List<IBlock> all = new ArrayList<IBlock>(); all.add(entry_); all.addAll(BBs_); all.add(exit_); - for(Iterator<IBlock> i = all.iterator(); i.hasNext();){ + for (Iterator<IBlock> i = all.iterator(); i.hasNext();) { IBlock block = i.next(); block.setAttr("color", new Integer(0)); } order = new Stack<IBlock>(); - for(Iterator<IBlock> i = all.iterator(); i.hasNext();){ - IBlock block = (IBlock)i.next(); - int color = ((Integer)block.getAttr("color")).intValue(); - if(color == 0) + for (Iterator<IBlock> i = all.iterator(); i.hasNext();) { + IBlock block = (IBlock) i.next(); + int color = ((Integer) block.getAttr("color")).intValue(); + if (color == 0) DFSVisit(block); } IBlock b1 = order.pop(); IBlock b2 = null; - while(!order.empty()){ + while (!order.empty()) { b2 = order.pop(); b1.setTopNext(b2); b1 = b2; } } - - protected void DFSVisit(IBlock block){ - block.setAttr("color", new Integer(1)); //gray - for(Iterator<IBlock> i = block.getSuccs().iterator(); i.hasNext();){ + + protected void DFSVisit(IBlock block) { + block.setAttr("color", new Integer(1)); // gray + for (Iterator<IBlock> i = block.getSuccs().iterator(); i.hasNext();) { IBlock succ = i.next(); - if(isBackEdgeSucc(block, succ)) continue; - int color = ((Integer)succ.getAttr("color")).intValue(); - if(color == 0) //white + if (isBackEdgeSucc(block, succ)) + continue; + int color = ((Integer) succ.getAttr("color")).intValue(); + if (color == 0) // white DFSVisit(succ); } order.push(block); } - + /** - * @return "true" if edge (from, to) is a back edge. + * @return "true" if edge (from, to) is a back edge. */ - protected boolean isBackEdgeSucc(IBlock from, IBlock to){ + protected boolean isBackEdgeSucc(IBlock from, IBlock to) { return from.getSuccs().contains(to) && from.getDOM().contains(to); } - - protected void otherOPs(){ + + protected void otherOPs() { /* Empty method for future extensions */ } - - public void print(){ - for(IBlock b = entry_; b != null; b = b.topNext()) + + public void print() { + for (IBlock b = entry_; b != null; b = b.topNext()) b.print(); } |