Ejemplo n.º 1
0
        FlowGraphNode ParseReturn(Dictionary <string, object> item)
        {
            ReturnNode node = new ReturnNode(Id++);

            if (item["expr"] != null)
            {
                node.expr = ParseNode(((Dictionary <string, object>)item["expr"]));
            }
            return(node);
        }
Ejemplo n.º 2
0
        public List <FlowGraphNode> CreateFlowControlGraph(int id)
        {
            if (graph != null)
            {
                graph.Clear();
            }
            Function func = new Function();

            func.name = ((Dictionary <string, object>)(this.functions[id]["decl"]))["name"].ToString();


            Dictionary <string, object> type = ((Dictionary <string, object>)(this.functions[id]["decl"]));

            func.returntype = (DeclNode)ParseDeclaration(type);
            ArrayList functionflow = (ArrayList)((Dictionary <string, object>)(this.functions[id]["body"]))["block_items"];

            foreach (Dictionary <string, object> item in functionflow)
            {
                func.nodes.Add(ParseNode(item));
            }
            if (NodeType.E_RETURN != func.nodes.Last().getNodeType())
            {
                ReturnNode node = new ReturnNode(Id++);
                func.nodes.Add(node);
            }
            Dictionary <string, object> tmp = (((Dictionary <string, object>)(((Dictionary <string, object>)(((Dictionary <string, object>)(this.functions[id]["decl"]))["type"]))["args"])));

            if (tmp != null)
            {
                ArrayList prameters = (ArrayList)tmp["params"];
                foreach (Dictionary <string, object> paremeter in prameters)
                {
                    func.AddParam((DeclNode)ParseDeclaration(paremeter));
                }
            }
            ParsedFunctions.Add(func);
            return(func.nodes);
        }
Ejemplo n.º 3
0
        public List <TestCase> BuildTestCases(Function f, List <FlowGraphNode> GlobalScope, List <Function> functions)
        {
            List <TestCase> testcases           = new List <TestCase>();
            FlowGraphWalker graphwalker         = new FlowGraphWalker();
            List <List <FlowGraphNode> > pathes = graphwalker.CalculateAllPathes(f.nodes);

            foreach (List <FlowGraphNode> path in pathes)
            {
                TestCase NewCase = new TestCase();
                NewCase.id = TestCaseBuilder.id++;
                List <Variable> args = new List <Variable>();
                foreach (DeclNode node in f.Getparams())
                {
                    args.Add(DeclNodeToVar(node, f));
                }

                NewCase.Arguments      = args.AsEnumerable();
                NewCase.function_name  = f.name;
                NewCase.function_type  = DeclNodeToVar(f.returntype, f);
                NewCase.function_nodes = f.nodes;
                NewCase.path           = path;
                List <CheckReturn>     AssigmentChecks = new List <CheckReturn>();
                List <FuncCallChecker> FuncCalls       = new List <FuncCallChecker>();
                foreach (FlowGraphNode testnode in path)
                {
                    List <DeclNode> insideVars = new List <DeclNode>();
                    switch (testnode.getNodeType())
                    {
                    case NodeType.E_DECL:
                    {
                        DeclNode insidevar = (DeclNode)testnode;
                        insideVars.Add(insidevar);
                        break;
                    }

                    case NodeType.E_IF:
                    {
                        break;
                    }

                    case NodeType.E_WHILE:
                    {
                        break;
                    }

                    case NodeType.E_FOR:
                    {
                        break;
                    }

                    case NodeType.E_FUNC_CALL:
                    {
                        FuncCallNode node    = (FuncCallNode)testnode;
                        Boolean      IsLocal = false;
                        foreach (Function f1 in functions)
                        {
                            if (f1.name == node.FunctionName)
                            {
                                IsLocal = true;
                            }
                        }
                        if (!IsLocal)
                        {
                            FuncCallChecker checker = new FuncCallChecker();
                            checker.Name = node.FunctionName;
                            FuncCalls.Add(checker);
                        }
                        break;
                    }

                    case NodeType.E_ASSIGMENT:
                    {
                        OperationNode node          = (OperationNode)testnode;
                        bool          InGlobalScope = false;
                        CheckReturn   checker       = new CheckReturn();
                        Variable      toCheck       = new Variable();
                        Variable      CheckValue    = new Variable();
                        foreach (FlowGraphNode g in GlobalScope)
                        {
                            if (g.getNodeType() == NodeType.E_DECL)
                            {
                                if (node.left.getNodeType() == NodeType.E_STRUCTREF)
                                {
                                    StructRef tmp = (StructRef)node.left;
                                    while (tmp.structname.getNodeType() != NodeType.E_ID)
                                    {
                                        if (tmp.structname.getNodeType() == NodeType.E_UN_OP)
                                        {
                                            UnaryOp pp = (UnaryOp)tmp.structname;
                                            while (pp.left.getNodeType() == NodeType.E_UN_OP)
                                            {
                                                pp = (UnaryOp)pp.left;
                                            }
                                            if (pp.left.getNodeType() == NodeType.E_STRUCTREF)
                                            {
                                                tmp = (StructRef)pp.left;
                                            }
                                            if (pp.left.getNodeType() == NodeType.E_ID)
                                            {
                                                tmp.structname = pp.left;
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            tmp = (StructRef)tmp.structname;
                                        }
                                    }
                                    ID name = (ID)tmp.structname;
                                    if (name.Name == ((DeclNode)g).DeclName)
                                    {
                                        DeclNode r = resovleType(new List <FlowGraphNode>()
                                            {
                                                resolveTypedef(GlobalScope, ((DeclNode)g).DeclType)
                                            }, ((StructRef)(node.left)).structfield.ToString());
                                        CheckValue      = DeclNodeToVar(r, null);
                                        CheckValue.name = r.DeclName + "Checker";
                                        CheckValue.type = r.DeclType;
                                        InGlobalScope   = true;
                                    }
                                }
                                if (node.left.getNodeType() == NodeType.E_ID)
                                {
                                    ID tmp = (ID)node.left;
                                    if (tmp.Name == ((DeclNode)g).DeclName)
                                    {
                                        DeclNode r = resovleTypeID(GlobalScope, tmp.Name);
                                        CheckValue      = DeclNodeToVar(r, null);
                                        CheckValue.name = r.DeclName + "Checker";
                                        CheckValue.type = r.DeclType;
                                        InGlobalScope   = true;
                                    }
                                }
                                if (node.left.getNodeType() == NodeType.E_ARRAY_REF)
                                {
                                }
                            }
                        }
                        if (InGlobalScope)
                        {
                            if (node.left.getNodeType() == NodeType.E_STRUCTREF)
                            {
                                StructRef Sref = (StructRef)node.left;
                                toCheck.name = Sref.structname.ToString() + Sref.reftype.ToString() + Sref.structfield;
                                if (node.right.getNodeType() == NodeType.E_COSNT)
                                {
                                    ConstantNode tmp = (ConstantNode)node.right;
                                    toCheck.value = tmp.Value;
                                }
                                checker.memcheck = true;
                                checker.Cheker   = CheckValue;
                                checker.ToCheck  = toCheck;
                                AssigmentChecks.Add(checker);
                            }
                            if (node.left.getNodeType() == NodeType.E_ID)
                            {
                                ID Identifier = (ID)node.left;
                                toCheck.name = Identifier.Name;
                                if (node.right.getNodeType() == NodeType.E_COSNT)
                                {
                                    ConstantNode tmp = (ConstantNode)node.right;
                                    toCheck.value = tmp.Value;
                                }
                                checker.Cheker  = CheckValue;
                                checker.ToCheck = toCheck;
                                AssigmentChecks.Add(checker);
                                checker.memcheck = true;
                            }
                            if (node.left.getNodeType() == NodeType.E_ARRAY_REF)
                            {
                            }
                            if (node.right.getNodeType() == NodeType.E_COSNT)
                            {
                                checker.Cheker.value = node.right.ToString();
                            }
                        }


                        break;
                    }

                    case NodeType.E_RETURN:
                    {
                        ReturnNode ret = (ReturnNode)testnode;
                        if (NewCase.function_type.type == "void")
                        {
                            break;
                        }
                        NewCase.returnchecker = new CheckReturn()
                        {
                            ToCheck = new Variable {
                                type = NewCase.function_type.type, value = "{0}"
                            }
                        };
                        //if(ret.expr.getNodeType() == NodeType.E_COSNT)
                        {
                            NewCase.returnchecker.ToCheck.value = ret.expr.ToString();
                        }
                        if (NewCase.function_type.ispointer != "" ||
                            NewCase.function_type.isarray != "")
                        {
                            NewCase.returnchecker.memcheck = true;
                        }

                        if (NewCase.function_type.type.Contains("int"))
                        {
                            NewCase.returnchecker.ToCheck.type = "int";
                            NewCase.returnchecker.intcheck     = true;
                        }
                        if (NewCase.function_type.type.Contains("char"))
                        {
                            NewCase.returnchecker.ToCheck.type = "char";
                            NewCase.returnchecker.charcheck    = true;
                        }
                        else
                        {
                            string type = GetTypeOfTypeDef(f.returntype.DeclType, GlobalScope);
                            if (type == "E_ENUM")
                            {
                                NewCase.returnchecker.ToCheck.type = f.returntype.DeclType;
                                NewCase.returnchecker.intcheck     = true;
                            }
                        }
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }
                NewCase.FuncCalls        = FuncCalls;
                NewCase.AssigmentChecker = AssigmentChecks;
                testcases.Add(NewCase);
            }
            return(testcases);
        }