Exemple #1
0
        // End of code changes

        private void ParseProperties(List <ProgramElement> programElements, XElement elements, string fileName)
        {
            IEnumerable <XElement> props =
                from el in elements.Descendants(SRC.Declaration)
                where el.Element(SRC.Name) != null &&
                el.Element(SRC.Type) != null &&
                el.Element(SRC.Block) != null &&
                el.Elements().Count() == 3
                select el;

            foreach (XElement prop in props)
            {
                string name;
                int    definitionLineNumber;
                int    definitionColumnNumber;
                SrcMLParsingUtils.ParseNameAndLineNumber(prop, out name, out definitionLineNumber, out definitionColumnNumber);

                ClassElement classElement = SrcMLParsingUtils.RetrieveClassElement(prop, programElements);
                Guid         classId      = classElement != null ? classElement.Id : Guid.Empty;
                string       className    = classElement != null ? classElement.Name : String.Empty;

                //parse access level and type
                XElement    accessElement = prop.Element(SRC.Type);
                AccessLevel accessLevel   = SrcMLParsingUtils.RetrieveAccessLevel(accessElement);

                IEnumerable <XElement> types = prop.Element(SRC.Type).Elements(SRC.Name);

                //oops, namespaces have the same structure in srcml so need this check
                if (types.Count() == 0 || types.First().Value == "namespace")
                {
                    continue;
                }

                string propertyType = String.Empty;
                foreach (XElement type in types)
                {
                    propertyType += type.Value + " ";
                }
                propertyType = propertyType.TrimEnd();

                string body = SrcMLParsingUtils.ParseBody(prop);

                string fullFilePath = System.IO.Path.GetFullPath(fileName);
                string source       = SrcMLParsingUtils.RetrieveSource(prop);

                programElements.Add(new PropertyElement(name, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, propertyType, body, classId, className, String.Empty));
            }
        }
Exemple #2
0
        public virtual MethodElement ParseMethod(XElement method, List <ProgramElement> programElements, string fileName, Type myType, bool isConstructor = false)
        {
            string name = String.Empty;
            int    definitionLineNumber   = 0;
            int    definitionColumnNumber = 0;
            string returnType             = String.Empty;

            SrcMLParsingUtils.ParseNameAndLineNumber(method, out name, out definitionLineNumber, out definitionColumnNumber);

            AccessLevel accessLevel;
            XElement    type = method.Element(SRC.Type);

            if (type != null)
            {
                accessLevel = SrcMLParsingUtils.RetrieveAccessLevel(type);

                XElement typeName = type.Element(SRC.Name);
                if (typeName != null)
                {
                    returnType = typeName.Value;
                }
                else
                {
                    returnType = "void";
                }
            }
            else
            {
                accessLevel = SrcMLParsingUtils.RetrieveAccessLevel(method);
            }

            if (String.IsNullOrEmpty(returnType))
            {
                if (name.Equals("get"))
                {
                    try
                    {
                        var myName =
                            method.Ancestors(SRC.DeclarationStatement).Descendants(SRC.Declaration).
                            Descendants(SRC.Type).Elements(SRC.Name);
                        returnType = myName.First().Value;
                    }
                    catch (NullReferenceException nre)
                    {
                        returnType = "";
                    }
                }
                else if (name.Equals("set"))
                {
                    returnType = "void";
                }
                else if (name.Equals("add") || name.Equals("remove"))
                {
                    try
                    {
                        var myName =
                            method.Parent.Parent.Elements(SRC.Type).First().Elements(SRC.Name).First();
                        returnType = myName.Value;
                    }
                    catch (NullReferenceException nre)
                    {
                        returnType = "";
                    }
                }
            }


            //parse arguments
            XElement paramlist = method.Element(SRC.ParameterList);
            string   arguments = String.Empty;

            if (paramlist != null)
            {
                IEnumerable <XElement> argumentElements =
                    from el in paramlist.Descendants(SRC.Name)
                    select el;

                foreach (XElement elem in argumentElements)
                {
                    arguments += elem.Value + " ";
                }
            }
            arguments = arguments.TrimEnd();

            string body = SrcMLParsingUtils.ParseBody(method);

            ClassElement classElement = SrcMLParsingUtils.RetrieveClassElement(method, programElements);
            Guid         classId      = classElement != null ? classElement.Id : Guid.Empty;
            string       className    = classElement != null ? classElement.Name : String.Empty;

            string fullFilePath = System.IO.Path.GetFullPath(fileName);

            string source = SrcMLParsingUtils.RetrieveSource(method);

            return(Activator.CreateInstance(myType, name, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, arguments, returnType, body,
                                            classId, className, String.Empty, isConstructor) as MethodElement);
        }
Exemple #3
0
        public virtual MethodElement ParseCppFunction(XElement function, List <ProgramElement> programElements, string fileName,
                                                      string[] includedFiles, Type resolvedType, Type unresolvedType, bool isConstructor = false)
        {
            try
            {
                MethodElement methodElement          = null;
                string        source                 = String.Empty;
                int           definitionLineNumber   = 0;
                int           definitionColumnNumber = 0;
                string        returnType             = String.Empty;

                XElement type = function.Element(SRC.Type);
                if (type != null)
                {
                    XElement typeName = type.Element(SRC.Name);
                    returnType = typeName.Value;
                }

                XElement paramlist = function.Element(SRC.ParameterList);
                IEnumerable <XElement> argumentElements =
                    from el in paramlist.Descendants(SRC.Name)
                    select el;
                string arguments = String.Empty;
                foreach (XElement elem in argumentElements)
                {
                    arguments += elem.Value + " ";
                }
                arguments = arguments.TrimEnd();

                string body         = SrcMLParsingUtils.ParseBody(function);
                string fullFilePath = System.IO.Path.GetFullPath(fileName);


                XElement nameElement = function.Element(SRC.Name);
                string   wholeName   = nameElement.Value;
                if (wholeName.Contains("::"))
                {
                    //class function
                    string[] twonames  = wholeName.Split("::".ToCharArray());
                    string   funcName  = twonames[2];
                    string   className = twonames[0];
                    definitionLineNumber   = Int32.Parse(nameElement.Element(SRC.Name).Attribute(POS.Line).Value);
                    definitionColumnNumber = Int32.Parse(nameElement.Element(SRC.Name).Attribute(POS.Column).Value);
                    source = SrcMLParsingUtils.RetrieveSource(function);

                    return(Activator.CreateInstance(unresolvedType, funcName, definitionLineNumber, definitionColumnNumber, fullFilePath, source, arguments, returnType, body,
                                                    className, isConstructor, includedFiles) as MethodElement);
                }
                else
                {
                    //regular C-type function, or an inlined class function
                    string funcName = wholeName;
                    definitionLineNumber   = Int32.Parse(nameElement.Attribute(POS.Line).Value);
                    definitionColumnNumber = Int32.Parse(nameElement.Attribute(POS.Column).Value);
                    source = SrcMLParsingUtils.RetrieveSource(function);
                    AccessLevel accessLevel = RetrieveCppAccessLevel(function);

                    Guid          classId       = Guid.Empty;
                    string        className     = String.Empty;
                    ClassElement  classElement  = SrcMLParsingUtils.RetrieveClassElement(function, programElements);
                    StructElement structElement = RetrieveStructElement(function, programElements);
                    if (classElement != null)
                    {
                        classId   = classElement.Id;
                        className = classElement.Name;
                    }
                    else if (structElement != null)
                    {
                        classId   = structElement.Id;
                        className = structElement.Name;
                    }
                    methodElement = Activator.CreateInstance(resolvedType, funcName, definitionLineNumber, definitionColumnNumber, fullFilePath, source, accessLevel, arguments,
                                                             returnType, body,
                                                             classId, className, String.Empty, isConstructor) as MethodElement;
                }

                return(methodElement);
            }
            catch (Exception error)
            {
                FileLogger.DefaultLogger.Info("Exception in SrcMLCppParser " + error.Message + "\n" + error.StackTrace);
                return(null);
            }
        }