Exemplo n.º 1
0
        private static bool RemoveReturnCheck(Statement stat, StructMethod mt)
        {
            Statement parent = stat.GetParent();

            if (parent != null && parent.type == Statement.Type_If && stat.type == Statement.
                Type_Basicblock && stat.GetExprents().Count == 1)
            {
                Exprent exprent = stat.GetExprents()[0];
                if (exprent.type == Exprent.Exprent_Exit)
                {
                    ExitExprent exit_exprent = (ExitExprent)exprent;
                    if (exit_exprent.GetExitType() == ExitExprent.Exit_Return)
                    {
                        Exprent exprent_value = exit_exprent.GetValue();
                        //if(exprent_value.type == Exprent.EXPRENT_VAR) {
                        //	VarExprent var_value = (VarExprent)exprent_value;
                        IfStatement ifparent     = (IfStatement)parent;
                        Exprent     if_condition = ifparent.GetHeadexprent().GetCondition();
                        if (ifparent.GetElsestat() == stat && if_condition.type == Exprent.Exprent_Function &&
                            ((FunctionExprent)if_condition).GetFuncType() == FunctionExprent.Function_Eq)
                        {
                            // TODO: reversed order possible (in theory)
                            FunctionExprent func         = (FunctionExprent)if_condition;
                            Exprent         first_param  = func.GetLstOperands()[0];
                            Exprent         second_param = func.GetLstOperands()[1];
                            StatEdge        ifedge       = ifparent.GetIfEdge();
                            StatEdge        elseedge     = ifparent.GetElseEdge();
                            Statement       ifbranch     = ifparent.GetIfstat();
                            Statement       elsebranch   = ifparent.GetElsestat();
                            if (second_param.type == Exprent.Exprent_Const && second_param.GetExprType().type
                                == ICodeConstants.Type_Null)
                            {
                                // TODO: reversed parameter order
                                //if(first_param.type == Exprent.EXPRENT_VAR && ((VarExprent)first_param).getIndex() == var_value.getIndex()) {
                                if (first_param.Equals(exprent_value))
                                {
                                    // TODO: check for absence of side effects like method invocations etc.
                                    if (ifbranch.type == Statement.Type_Basicblock && ifbranch.GetExprents().Count ==
                                        1 && ifbranch.GetExprents()[0].type == Exprent.Exprent_Exit)
                                    {
                                        // TODO: special check for IllegalStateException
                                        ifparent.GetFirst().RemoveSuccessor(ifedge);
                                        ifparent.GetFirst().RemoveSuccessor(elseedge);
                                        ifparent.GetStats().RemoveWithKey(ifbranch.id);
                                        ifparent.GetStats().RemoveWithKey(elsebranch.id);
                                        if (!(ifbranch.GetAllSuccessorEdges().Count == 0))
                                        {
                                            ifbranch.RemoveSuccessor(ifbranch.GetAllSuccessorEdges()[0]);
                                        }
                                        if (!(ifparent.GetFirst().GetExprents().Count == 0))
                                        {
                                            elsebranch.GetExprents().InsertRange(0, ifparent.GetFirst().GetExprents());
                                        }
                                        ifparent.GetParent().ReplaceStatement(ifparent, elsebranch);
                                        ifparent.GetParent().SetAllParent();
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (parent != null && parent.type == Statement.Type_Sequence && stat.type ==
                     Statement.Type_Basicblock && stat.GetExprents().Count == 1)
            {
                //}
                Exprent exprent = stat.GetExprents()[0];
                if (exprent.type == Exprent.Exprent_Exit)
                {
                    ExitExprent exit_exprent = (ExitExprent)exprent;
                    if (exit_exprent.GetExitType() == ExitExprent.Exit_Return)
                    {
                        Exprent           exprent_value = exit_exprent.GetValue();
                        SequenceStatement sequence      = (SequenceStatement)parent;
                        int sequence_stats_number       = sequence.GetStats().Count;
                        if (sequence_stats_number > 1 && sequence.GetStats().GetLast() == stat && sequence
                            .GetStats()[sequence_stats_number - 2].type == Statement.Type_If)
                        {
                            IfStatement ifstat       = (IfStatement)sequence.GetStats()[sequence_stats_number - 2];
                            Exprent     if_condition = ifstat.GetHeadexprent().GetCondition();
                            if (ifstat.iftype == IfStatement.Iftype_If && if_condition.type == Exprent.Exprent_Function &&
                                ((FunctionExprent)if_condition).GetFuncType() == FunctionExprent.Function_Eq)
                            {
                                // TODO: reversed order possible (in theory)
                                FunctionExprent func         = (FunctionExprent)if_condition;
                                Exprent         first_param  = func.GetLstOperands()[0];
                                Exprent         second_param = func.GetLstOperands()[1];
                                Statement       ifbranch     = ifstat.GetIfstat();
                                if (second_param.type == Exprent.Exprent_Const && second_param.GetExprType().type
                                    == ICodeConstants.Type_Null)
                                {
                                    // TODO: reversed parameter order
                                    if (first_param.Equals(exprent_value))
                                    {
                                        // TODO: check for absence of side effects like method invocations etc.
                                        if (ifbranch.type == Statement.Type_Basicblock && ifbranch.GetExprents().Count ==
                                            1 && ifbranch.GetExprents()[0].type == Exprent.Exprent_Exit)
                                        {
                                            // TODO: special check for IllegalStateException
                                            ifstat.RemoveSuccessor(ifstat.GetAllSuccessorEdges()[0]);
                                            // remove 'else' edge
                                            if (!(ifstat.GetFirst().GetExprents().Count == 0))
                                            {
                                                stat.GetExprents().InsertRange(0, ifstat.GetFirst().GetExprents());
                                            }
                                            foreach (StatEdge edge in ifstat.GetAllPredecessorEdges())
                                            {
                                                ifstat.RemovePredecessor(edge);
                                                edge.GetSource().ChangeEdgeNode(Statement.Direction_Forward, edge, stat);
                                                stat.AddPredecessor(edge);
                                            }
                                            sequence.GetStats().RemoveWithKey(ifstat.id);
                                            sequence.SetFirst(sequence.GetStats()[0]);
                                            return(true);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            foreach (Statement st in stat.GetStats())
            {
                if (RemoveReturnCheck(st, mt))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 2
0
 private static void RemoveEmptyStatements(SequenceStatement sequence)
 {
     if (sequence.GetStats().Count <= 1)
     {
         return;
     }
     MergeFlatStatements(sequence);
     while (true)
     {
         bool found = false;
         foreach (Statement st in sequence.GetStats())
         {
             if (st.GetExprents() != null && (st.GetExprents().Count == 0))
             {
                 if ((st.GetAllSuccessorEdges().Count == 0))
                 {
                     List <StatEdge> lstBreaks = st.GetPredecessorEdges(StatEdge.Type_Break);
                     if ((lstBreaks.Count == 0))
                     {
                         foreach (StatEdge edge in st.GetAllPredecessorEdges())
                         {
                             edge.GetSource().RemoveSuccessor(edge);
                         }
                         found = true;
                     }
                 }
                 else
                 {
                     StatEdge sucedge = st.GetAllSuccessorEdges()[0];
                     if (sucedge.GetType() != StatEdge.Type_Finallyexit)
                     {
                         st.RemoveSuccessor(sucedge);
                         foreach (StatEdge edge in st.GetAllPredecessorEdges())
                         {
                             if (sucedge.GetType() != StatEdge.Type_Regular)
                             {
                                 edge.GetSource().ChangeEdgeType(Statement.Direction_Forward, edge, sucedge.GetType
                                                                     ());
                             }
                             st.RemovePredecessor(edge);
                             edge.GetSource().ChangeEdgeNode(Statement.Direction_Forward, edge, sucedge.GetDestination
                                                                 ());
                             sucedge.GetDestination().AddPredecessor(edge);
                             if (sucedge.closure != null)
                             {
                                 sucedge.closure.AddLabeledEdge(edge);
                             }
                         }
                         found = true;
                     }
                 }
                 if (found)
                 {
                     sequence.GetStats().RemoveWithKey(st.id);
                     break;
                 }
             }
         }
         if (!found)
         {
             break;
         }
     }
     sequence.SetFirst(sequence.GetStats()[0]);
 }