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

            node.reftype     = item["type"].ToString();
            node.structfield = ParseNode(((Dictionary <string, object>)item["field"]));
            node.structname  = ParseNode(((Dictionary <string, object>)item["name"]));
            return(node);
        }
Ejemplo n.º 2
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);
        }