Ejemplo n.º 1
0
        public void TestGetMethodSignature_InitializerList()
        {
            string   testSrcML = @"<constructor><name><name>MyClass</name><op:operator>::</op:operator><name>MyClass</name></name><parameter_list>(<param><decl><type><name>int</name></type> <name>bar</name></decl></param>)</parameter_list> <member_list>: <call><name>_capacity</name><argument_list>(<argument><expr><lit:literal type=""number"">15</lit:literal></expr></argument>)</argument_list></call>, <call><name>_len</name><argument_list>(<argument><expr><lit:literal type=""number"">0</lit:literal></expr></argument>)</argument_list></call> </member_list><block>{
    <if>if<condition>(<expr><name>bar</name> <op:operator>&gt;</op:operator> <call><name>GetNumber</name><argument_list>()</argument_list></call></expr>)</condition><then> <block>{
        <return>return <expr><lit:literal type=""string"">""Hello, world!""</lit:literal></expr>;</return>
    }</block></then> <else>else <block>{
        <return>return <expr><lit:literal type=""string"">""Goodbye cruel world!""</lit:literal></expr>;</return>
    }</block></else></if>
}</block></constructor>";
            XElement xml       = XElement.Parse(string.Format(srcMLFormat, testSrcML), LoadOptions.PreserveWhitespace);

            string actual   = SrcMLElement.GetMethodSignature(xml.Element(SRC.Constructor));
            string expected = "MyClass::MyClass(int bar)";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 2
0
        public void TestGetMethodSignature_Normal()
        {
            string   testSrcML = @"<function><type><name>char</name><type:modifier>*</type:modifier></type> <name><name>MyClass</name><op:operator>::</op:operator><name>foo</name></name><parameter_list>(<param><decl><type><name>int</name></type> <name>bar</name></decl></param>)</parameter_list> <block>{
    <if>if<condition>(<expr><name>bar</name> <op:operator>&gt;</op:operator> <call><name>GetNumber</name><argument_list>()</argument_list></call></expr>)</condition><then> <block>{
        <return>return <expr><lit:literal type=""string"">""Hello, world!""</lit:literal></expr>;</return>
    }</block></then> <else>else <block>{
        <return>return <expr><lit:literal type=""string"">""Goodbye cruel world!""</lit:literal></expr>;</return>
    }</block></else></if>
}</block></function>";
            XElement xml       = XElement.Parse(string.Format(srcMLFormat, testSrcML), LoadOptions.PreserveWhitespace);

            string actual   = SrcMLElement.GetMethodSignature(xml.Element(SRC.Function));
            string expected = "char* MyClass::foo(int bar)";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Constructs SWUMs for each of the methods defined in <paramref name="unitElement"/> and adds them to the cache.
        /// </summary>
        /// <param name="unitElement">The root element for the file unit to be processed.</param>
        /// <param name="filePath">The path for the file represented by <paramref name="unitElement"/>.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="unitElement"/> is null.</exception>
        protected void AddSwumForMethodDefinitions(XElement unitElement, string filePath)
        {
            if (unitElement == null)
            {
                throw new ArgumentNullException("unitElement");
            }
            if (unitElement.Name != SRC.Unit)
            {
                throw new ArgumentException("Must be a SRC.Unit element", "unitElement");
            }

            //iterate over each method definition in the SrcML file
            var fileAttribute = unitElement.Attribute("filename");

            if (fileAttribute != null)
            {
                filePath = fileAttribute.Value;
            }
            var functions = from func in unitElement.Descendants()
                            where functionTypes.Contains(func.Name) && !func.Ancestors(SRC.Declaration).Any()
                            select func;

            foreach (XElement func in functions)
            {
                //construct SWUM on the function (if necessary)
                string sig = SrcMLElement.GetMethodSignature(func);
                lock (signaturesToSwum) {
                    if (signaturesToSwum.ContainsKey(sig))
                    {
                        //update the SwumDataRecord with the filename of the duplicate method
                        signaturesToSwum[sig].FileNames.Add(filePath);
                    }
                    else
                    {
                        MethodDeclarationNode mdn = ConstructSwumFromMethodElement(func);
                        var swumData = ProcessSwumNode(mdn);
                        swumData.FileNames.Add(filePath);
                        signaturesToSwum[sig] = swumData;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public override void Execute()
        {
            if (Pause)
            {
                Console.WriteLine("Ready to begin (press Enter)");
                Console.ReadLine();
            }

            Console.WriteLine("Using srcML file {0}", this.File);

            var builder = new UnigramSwumBuilder();

            if (!string.IsNullOrWhiteSpace(CountFile))
            {
                Console.WriteLine("Initializing SamuraiIdSplitter using word count file {0}", this.CountFile);
                builder.Splitter = new SamuraiIdSplitter(CountFile);
            }
            Console.WriteLine("SwumBuilder initialized");

            int methodCount = 0, fieldCount = 0;

            {
                SrcMLFile testFile      = new SrcMLFile(this.File);
                var       functionTypes = new XName[] { SRC.Function, SRC.Constructor, SRC.Destructor };
                foreach (XElement file in testFile.FileUnits)
                {
                    string fileName = file.Attribute("filename").Value;
                    Console.WriteLine("File {0}:", fileName);

                    //compute SWUM on each function
                    foreach (var func in (from func in file.Descendants()
                                          where functionTypes.Contains(func.Name) && !func.Ancestors(SRC.Declaration).Any()
                                          select func))
                    {
                        var nameElement = SrcMLElement.GetNameForMethod(func);
                        if (nameElement != null)
                        {
                            string funcName      = nameElement.Value;
                            string funcSignature = SrcMLElement.GetMethodSignature(func);
                            if (PrintSwum)
                            {
                                Console.WriteLine("<{0}> {1}", func.Name.LocalName, funcSignature);
                            }

                            MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, ContextBuilder.BuildMethodContext(func));
                            builder.ApplyRules(mdn);
                            methodSwum[string.Format("{0}:{1}", fileName, funcSignature)] = mdn;
                            if (PrintSwum)
                            {
                                Console.WriteLine(mdn.ToString() + Environment.NewLine);
                            }

                            methodCount++;
                        }
                    }

                    //compute SWUM on each field
                    foreach (var fieldDecl in (from declStmt in file.Descendants(SRC.DeclarationStatement)
                                               where !declStmt.Ancestors().Any(n => functionTypes.Contains(n.Name))
                                               select declStmt.Element(SRC.Declaration)))
                    {
                        int declPos = 1;
                        foreach (var nameElement in fieldDecl.Elements(SRC.Name))
                        {
                            string fieldName = nameElement.Elements(SRC.Name).Any() ? nameElement.Elements(SRC.Name).Last().Value : nameElement.Value;
                            if (PrintSwum)
                            {
                                Console.WriteLine("Field: {0}, Name: {1}", fieldDecl.Value, fieldName);
                            }

                            FieldDeclarationNode fdn = new FieldDeclarationNode(fieldName, ContextBuilder.BuildFieldContext(fieldDecl));
                            builder.ApplyRules(fdn);
                            fieldSwum[string.Format("{0}:{1}:{2}", fileName, fieldDecl.Value, declPos)] = fdn;
                            if (PrintSwum)
                            {
                                Console.WriteLine(fdn.ToString() + Environment.NewLine);
                            }

                            fieldCount++;
                            declPos++;
                        }
                    }
                }
            }

            GC.Collect();

            Console.WriteLine("{0} functions analyzed", methodCount);
            Console.WriteLine("{0} functions in dictionary", methodSwum.Count);
            Console.WriteLine("{0} fields analyzed", fieldCount);
            Console.WriteLine("{0} fields in dictionary", fieldSwum.Count);

            if (Pause)
            {
                Console.WriteLine("Finished building SWUM (press Enter)");
                Console.ReadLine();
            }
        }