Пример #1
0
        public override bool test(CSsemi.CSemiExp semi)
        {
            Display.displayRules(actionDelegate, "rule   DetectFunction");

            if (semi[semi.count - 1] != "{")
            {
                return(false);
            }

            int index = semi.FindFirst("(");

            if (index > 0 && !isSpecialToken(semi[index - 1]))
            {
                Repository rep = Repository.getInstance();

                CSsemi.CSemiExp local = new CSsemi.CSemiExp();
                local.Add("function").Add(semi[index - 1]);
                doActions(local);

                if (!rep.isFirstPass)
                {
                    /////////////////////////////////////////////////////////////////
                    //
                    // Parse function parameters to look for class dependencies
                    //
                    /////////////////////////////////////////////////////////////////
                    CSsemi.CSemiExp parameters = TokenHelper.GetFunctionParameters(semi);
                    for (int i = 0; i < TokenHelper.GetNumberOfParameters(parameters); i++)
                    {
                        parameters = TokenHelper.RemoveNewLines(parameters);
                        parameters = TokenHelper.RemoveIndicies(parameters);
                        parameters = TokenHelper.RemoveGenerics(parameters);
                        List <string> l = TokenHelper.GetFunctionParameterAtIndex(parameters, i);

                        foreach (CClassInfo ci in rep.parsedData.classList)
                        {
                            if (semi.Contains(ci.className) != -1)
                            {
                                if (rep.stack.count < 2)
                                {
                                    break;
                                }

                                string ns = rep.stack[0].name;                   // namespace
                                string cl = rep.stack[1].name;                   // class
                                string fn = rep.stack[rep.stack.count - 1].name; // function

                                CClassInfo    currentClassInfo    = rep.parsedData.getClassInfo(cl);
                                CFunctionInfo currentFunctioninfo = rep.parsedData.getClassFunctionInfo(cl, fn);

                                currentFunctioninfo.addDependency(l[l.Count - 2]);
                            }
                        }
                    }
                }

                return(true);
            }
            return(false);
        }
Пример #2
0
        //----< get function info for a particular class >------------------------------------
        public CFunctionInfo getClassFunctionInfo(string className, string classFunction)
        {
            CClassInfo classInfo = getClassInfo(className);

            foreach (CFunctionInfo fi in classInfo.functionInfoList)
            {
                if (fi.name == classFunction)
                {
                    return(fi);
                }
            }

            // allocate a new one
            CFunctionInfo newFunctionInfo = new CFunctionInfo();

            newFunctionInfo.name = classFunction;
            classInfo.functionInfoList.Add(newFunctionInfo);

            return(newFunctionInfo);
        }
Пример #3
0
        static void Main(string[] args)
        {
            CClassList classList = new CClassList();

            CClassInfo classInfo = new CClassInfo();

            classList.classList.Add(classInfo);

            // CLASS B
            classInfo.className = "CTestClassA";
            classInfo.dependencies.Add("cdCToken");
            classInfo.dependencies.Add("cdITem");
            classInfo.dependencies.Add("cdString");

            CMemberInfo memberInfo = new CMemberInfo();

            memberInfo.type = "cmA";
            memberInfo.name = "nameA";
            classInfo.dataMembers.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmB";
            memberInfo.name = "nameB";
            classInfo.dataMembers.Add(memberInfo);

            // FUNCTION A
            CFunctionInfo functionInfo = new CFunctionInfo();

            functionInfo.name = "fnTestFunction1";

            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmA";
            memberInfo.name = "nameA";
            functionInfo.dataMemberReferences.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmB";
            memberInfo.name = "nameB";
            functionInfo.dataMemberReferences.Add(memberInfo);

            classInfo.functionInfoList.Add(functionInfo);

            // FUNCTION B
            functionInfo      = new CFunctionInfo();
            functionInfo.name = "fnTestFunction2";

            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmZ";
            memberInfo.name = "newItemA";
            functionInfo.dataMemberReferences.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmB";
            memberInfo.name = "nameB";
            functionInfo.dataMemberReferences.Add(memberInfo);

            classInfo.functionInfoList.Add(functionInfo);

            // FUNCTION C
            functionInfo      = new CFunctionInfo();
            functionInfo.name = "fnTestFunction3";

            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmB";
            memberInfo.name = "nameB";
            functionInfo.dataMemberReferences.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmJ";
            memberInfo.name = "newItemB";
            functionInfo.dataMemberReferences.Add(memberInfo);

            classInfo.functionInfoList.Add(functionInfo);

            classInfo = new CClassInfo();
            classList.classList.Add(classInfo);

            // CLASS B
            classInfo.className = "CTestClassB";
            classInfo.dependencies.Add("yellow");
            classInfo.dependencies.Add("orange");
            classInfo.dependencies.Add("green");

            memberInfo      = new CMemberInfo();
            memberInfo.type = "Monkey";
            memberInfo.name = "dave";
            classInfo.dataMembers.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "Car";
            memberInfo.name = "Toyota";
            classInfo.dataMembers.Add(memberInfo);

            // FUNCTION A
            functionInfo      = new CFunctionInfo();
            functionInfo.name = "fnTestFunction1B";

            memberInfo      = new CMemberInfo();
            memberInfo.type = "Monkey";
            memberInfo.name = "dave";
            functionInfo.dataMemberReferences.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "Snake";
            memberInfo.name = "Python";
            functionInfo.dataMemberReferences.Add(memberInfo);

            classInfo.functionInfoList.Add(functionInfo);

            // FUNCTION B
            functionInfo      = new CFunctionInfo();
            functionInfo.name = "fnTestFunction2B";

            memberInfo      = new CMemberInfo();
            memberInfo.type = "Bug";
            memberInfo.name = "Beatles";
            functionInfo.dataMemberReferences.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "Snake";
            memberInfo.name = "Python";
            functionInfo.dataMemberReferences.Add(memberInfo);

            classInfo.functionInfoList.Add(functionInfo);

            // FUNCTION C
            functionInfo      = new CFunctionInfo();
            functionInfo.name = "fnTestFunction3B";

            memberInfo      = new CMemberInfo();
            memberInfo.type = "Computer";
            memberInfo.name = "Dell";
            functionInfo.dataMemberReferences.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "Computer";
            memberInfo.name = "HP";
            functionInfo.dataMemberReferences.Add(memberInfo);

            classInfo.functionInfoList.Add(functionInfo);

            Console.WriteLine("Break here and observe ParsedData data structure.");
        }
Пример #4
0
        public override bool test(CSsemi.CSemiExp semi)
        {
            Repository rep = Repository.getInstance();

            if (rep.isFirstPass)
            {
                return(false);
            }

            if (rep.stack.count <= 2)
            {
                // we are not in a class or function scope
                return(false);
            }
            else if (rep.stack[rep.stack.count - 1].type != "function")
            {
                return(false);
            }
            else if (rep.stack.count > 3)
            {
                //Console.WriteLine("skip: unknown code block in function, unhandled");
                return(false);
            }

            Display.displayRules(actionDelegate, "rule   DetectFunctionMembers");

            int index = semi.Contains(";");

            if (index != -1)
            {
                /////////////////////////////////////////////////////////////////
                //
                // Parse function body to look for class member references
                //
                /////////////////////////////////////////////////////////////////

                if (rep.stack.count < 3)
                {
                    return(false);
                }

                string ns = rep.stack[0].name; // namespace
                string cl = rep.stack[1].name; // class
                string fn = rep.stack[2].name; // function

                CClassInfo    currentClassInfo    = rep.parsedData.getClassInfo(cl);
                CFunctionInfo currentFunctionInfo = rep.parsedData.getClassFunctionInfo(cl, fn);

                foreach (CMemberInfo classDataMember in currentClassInfo.dataMembers)
                {
                    if (semi.Contains(classDataMember.name) != -1)
                    {
                        currentFunctionInfo.addDataMemberReference(classDataMember.type, classDataMember.name);
                    }
                }

                /////////////////////////////////////////////////////////////////
                //
                // Parse function body to look for class dependencies
                //
                /////////////////////////////////////////////////////////////////
                foreach (CClassInfo ci in rep.parsedData.classList)
                {
                    if (semi.Contains(ci.className) != -1)
                    {
                        currentFunctionInfo.addDependency(ci.className);
                    }
                }

                return(true);
            }

            return(false);
        }
Пример #5
0
        static void Main(string[] args)
        {
            CClassList classList = new CClassList();

            CClassInfo classInfo = new CClassInfo();

            // CLASS
            classInfo.className = "CTestClass";
            classInfo.dependencies.Add("cdCToken");
            classInfo.dependencies.Add("cdITem");
            classInfo.dependencies.Add("cdString");

            CMemberInfo memberInfo = new CMemberInfo();

            memberInfo.type = "cmA";
            memberInfo.name = "nameA";
            classInfo.dataMembers.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmB";
            memberInfo.name = "nameB";
            classInfo.dataMembers.Add(memberInfo);

            // FUNCTION A
            CFunctionInfo functionInfo = new CFunctionInfo();

            functionInfo.name = "fnTestFunction1";

            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmA";
            memberInfo.name = "nameA";
            functionInfo.dataMemberReferences.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmB";
            memberInfo.name = "nameB";
            functionInfo.dataMemberReferences.Add(memberInfo);

            classInfo.functionInfoList.Add(functionInfo);

            // FUNCTION B
            functionInfo      = new CFunctionInfo();
            functionInfo.name = "fnTestFunction2";

            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmZ";
            memberInfo.name = "newItemA";
            functionInfo.dataMemberReferences.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmB";
            memberInfo.name = "nameB";
            functionInfo.dataMemberReferences.Add(memberInfo);

            classInfo.functionInfoList.Add(functionInfo);

            // FUNCTION C
            functionInfo      = new CFunctionInfo();
            functionInfo.name = "fnTestFunction3";

            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmB";
            memberInfo.name = "nameB";
            functionInfo.dataMemberReferences.Add(memberInfo);
            memberInfo      = new CMemberInfo();
            memberInfo.type = "cmJ";
            memberInfo.name = "newItemB";
            functionInfo.dataMemberReferences.Add(memberInfo);

            classInfo.functionInfoList.Add(functionInfo);

            int cohesion = calculateCohesion(classInfo);

            int coupling = calculateCoupling(classInfo);

            Console.WriteLine("cohesion {0}, coupling {1}", cohesion, coupling);
        }