Ejemplo n.º 1
0
        public void TestBooleanArgument()
        {
            var xml  = @"<function><type><name>boolean</name></type> <name><name>Automobile</name><op:operator>::</op:operator><name>HasEmptyGasTank</name></name><parameter_list>(<param><decl><type><name>boolean</name></type> <name>gasTank</name></decl></param>)</parameter_list> <block>{
	<return>return <expr><lit:literal type=""boolean"">false</lit:literal></expr>;</return>
}</block></function>";
            var unit = fileUnitSetup.GetFileUnitForXmlSnippet(xml, "test.cpp");

            var func = unit.Descendants(SRC.Function).First();
            var mdn  = new MethodDeclarationNode(SrcMLElement.GetNameForMethod(func).Value, ContextBuilder.BuildMethodContext(func));

            builder.ApplyRules(mdn);
        }
Ejemplo n.º 2
0
 public void AddManyFiles()
 {
     for (int i = 0; i < 100; i++)
     {
         foreach (XElement methodElement in functions)
         {
             string                funcName = SrcMLElement.GetNameForMethod(methodElement).Value;
             MethodContext         mc       = ContextBuilder.BuildMethodContext(methodElement);
             MethodDeclarationNode mdn      = new MethodDeclarationNode(funcName, mc);
             builder.ApplyRules(mdn);
         }
     }
 }
Ejemplo n.º 3
0
        public void TestNounPhraseRule_KeyFileExists()
        {
            var xml  = @"<function><type><name>bool</name></type> <name><name>COptionsPageConnectionSFTP</name><op:operator>::</op:operator><name>KeyFileExists</name></name><parameter_list>(<param><decl><type><name>const</name> <name>wxString</name><type:modifier>&amp;</type:modifier></type> <name>keyFile</name></decl></param>)</parameter_list> <block>{
	<return>return <expr><lit:literal type=""boolean"">true</lit:literal></expr>;</return>
}</block></function>";
            var unit = fileUnitSetup.GetFileUnitForXmlSnippet(xml, "test.cpp");

            var func = unit.Descendants(SRC.Function).First();
            var mdn  = new MethodDeclarationNode(SrcMLElement.GetNameForMethod(func).Value, ContextBuilder.BuildMethodContext(func));

            builder.ApplyRules(mdn);

            Assert.AreEqual(typeof(NounPhraseRule), mdn.SwumRuleUsed.GetType());
            var expected = @"get(Verb) | Key(NounModifier) File(NounModifier) Exists(Noun)
	 ++ [wx(NounModifier) String(NounIgnorable) - key(Unknown) File(Unknown)] ++ C(NounModifier) Options(NounModifier) Page(NounModifier) Connection(NounModifier) SFTP(Noun)"    ;

            Assert.AreEqual(expected, mdn.ToString());
        }
Ejemplo n.º 4
0
        public void TestModalVerb()
        {
            var xml  = @"<function><type><name>int</name></type> <name><name>ToolBarXmlHandlerEx</name><op:operator>::</op:operator><name>CanHandle</name></name><parameter_list>(<param><decl><type><name>wxXmlNode</name> <type:modifier>*</type:modifier></type><name>node</name></decl></param>)</parameter_list> <block>{
	<return>return <expr><lit:literal type=""number"">0</lit:literal></expr>;</return>
}</block></function>";
            var unit = fileUnitSetup.GetFileUnitForXmlSnippet(xml, "test.cpp");

            var func = unit.Descendants(SRC.Function).First();
            var mdn  = new MethodDeclarationNode(SrcMLElement.GetNameForMethod(func).Value, ContextBuilder.BuildMethodContext(func));

            builder.ApplyRules(mdn);

            Assert.AreEqual(typeof(CheckerRule), mdn.SwumRuleUsed.GetType());
            var expected = @"Can(VerbIgnorable) Handle(VerbIgnorable) | [wx(NounModifier) Xml(NounModifier) Node(NounIgnorable) - node(Unknown)]
	 ++ Tool(NounModifier) Bar(NounModifier) Xml(NounModifier) Handler(NounModifier) Ex(Noun) ++ int(Noun)"    ;

            Assert.AreEqual(expected, mdn.ToString());
        }
Ejemplo n.º 5
0
        public override void Execute()
        {
            Console.WriteLine("Using srcML file {0}", this.File);

            UnigramSwumBuilder builder = new UnigramSwumBuilder();

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

            if (this.SamplePercent <= 0)
            {
                this.SamplePercent = 100;
            }
            Random rand = new Random();

            SrcMLFile testFile      = new SrcMLFile(this.File);
            int       methodCount   = 0;
            var       functionTypes = new XName[] { SRC.Function, SRC.Constructor, SRC.Destructor };

            foreach (XElement file in testFile.FileUnits)
            {
                Console.WriteLine("File {0}:", file.Attribute("filename").Value);

                //print all the function names
                foreach (var func in (from func in file.Descendants()
                                      where functionTypes.Any(c => c == func.Name) && !func.Ancestors(SRC.Declaration).Any() && (rand.Next(100) < this.SamplePercent)
                                      select func))
                {
                    string funcName = SrcMLElement.GetNameForMethod(func).Value;
                    Console.WriteLine("<{0}> {1}", func.Name.LocalName, GetMethodSignature(func));

                    MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, ContextBuilder.BuildMethodContext(func));
                    builder.ApplyRules(mdn);
                    Console.WriteLine(mdn.ToString() + Environment.NewLine);
                    methodCount++;
                }
            }
            Console.WriteLine("{0} functions analyzed", methodCount);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Constructs SWUM on the given srcML method element.
        /// </summary>
        /// <param name="methodElement">The srcML element to use. This can be either a Function, Constructor or Destructor.</param>
        /// <param name="className">The class on which this method is declared.</param>
        /// <returns>A MethodDeclarationNode with SWUM rules applied to it.</returns>
        protected MethodDeclarationNode ConstructSwumFromMethodElement(XElement methodElement, string className)
        {
            if (!functionTypes.Contains(methodElement.Name))
            {
                throw new ArgumentException("Element not a valid method type.", "methodElement");
            }

            string        funcName = SrcMLElement.GetNameForMethod(methodElement).Value;
            MethodContext mc       = ContextBuilder.BuildMethodContext(methodElement);

            //set the declaring class name, if it's been passed in
            //this is necessary because the xml from the database for inline methods won't have the surrounding class xml
            if (string.IsNullOrEmpty(mc.DeclaringClass) && !string.IsNullOrEmpty(className))
            {
                mc.DeclaringClass = className;
            }

            MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, mc);

            builder.ApplyRules(mdn);
            return(mdn);
        }
Ejemplo n.º 7
0
        public void TestLeadingPrepositionRule_OnGetAccObject()
        {
            var xml  = @"<function><type><name>LRESULT</name></type> <name><name>CMenuContainer</name><op:operator>::</op:operator><name>OnGetAccObject</name></name><parameter_list>( <param><decl><type><name>UINT</name></type> <name>uMsg</name></decl></param>, <param><decl><type><name>WPARAM</name></type> <name>wParam</name></decl></param>, <param><decl><type><name>LPARAM</name></type> <name>lParam</name></decl></param>, <param><decl><type><name>BOOL</name><type:modifier>&amp;</type:modifier></type> <name>bHandled</name></decl></param> )</parameter_list> <block>{
    <if>if <condition>(<expr><op:operator>(</op:operator><name>DWORD</name><op:operator>)</op:operator><name>lParam</name><op:operator>==</op:operator><op:operator>(</op:operator><name>DWORD</name><op:operator>)</op:operator><name>OBJID_CLIENT</name> <op:operator>&amp;&amp;</op:operator> <name>m_pAccessible</name></expr>)</condition><then> <block>{
        <return>return <expr><call><name>LresultFromObject</name><argument_list>(<argument><expr><name>IID_IAccessible</name></expr></argument>,<argument><expr><name>wParam</name></expr></argument>,<argument><expr><name>m_pAccessible</name></expr></argument>)</argument_list></call></expr>;</return>
    }</block></then> <else>else <block>{
        <expr_stmt><expr><name>bHandled</name><op:operator>=</op:operator><name>FALSE</name></expr>;</expr_stmt>
        <return>return <expr><lit:literal type=""number"">0</lit:literal></expr>;</return>
    }</block></else></if>
}</block></function>";
            var unit = fileUnitSetup.GetFileUnitForXmlSnippet(xml, "test.cpp");

            var func = unit.Descendants(SRC.Function).First();
            var mdn  = new MethodDeclarationNode(SrcMLElement.GetNameForMethod(func).Value, ContextBuilder.BuildMethodContext(func));

            builder.ApplyRules(mdn);

            Assert.AreEqual(typeof(LeadingPrepositionRule), mdn.SwumRuleUsed.GetType());
            var expected = @"handle(Verb) | On(NounModifier) Get(NounModifier) Acc(NounModifier) Object(NounIgnorable)
	 ++ [UINT(Noun) - u(Unknown) Msg(Unknown)] ++ [WPARAM(Noun) - w(Unknown) Param(Unknown)] ++ [LPARAM(Noun) - l(Unknown) Param(Unknown)] ++ [BOOL(Noun) - b(Unknown) Handled(Unknown)] ++ C(NounModifier) Menu(NounModifier) Container(NounIgnorable) ++ LRESULT(Noun)"    ;

            Assert.AreEqual(expected, mdn.ToString());
        }
Ejemplo n.º 8
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();
            }
        }
Ejemplo n.º 9
0
        public override void Execute()
        {
            Console.WriteLine("Using file {0}", this.File);

            SrcMLFile        testFile      = new SrcMLFile(this.File);
            HashSet <string> sameCaseWords = new HashSet <string>();
            //ConservativeIdSplitter splitter = new ConservativeIdSplitter();

            var functionTypes = new List <XName>()
            {
                SRC.Function, SRC.Constructor, SRC.Destructor
            };

            foreach (XElement file in testFile.FileUnits)
            {
                Console.WriteLine("File {0}:", file.Attribute("filename").Value);

                //print all the function names
                //var funcs = from func in file.Descendants()
                //            where functionTypes.Any(c => c == func.Name)
                //            select func;
                foreach (var func in (from func in file.Descendants()
                                      where functionTypes.Any(c => c == func.Name)
                                      select func))
                {
                    string funcName = SrcMLElement.GetNameForMethod(func).Value;
                    if (IsSameCase(funcName))
                    {
                        sameCaseWords.Add(funcName);
                    }
                    if (PrintAll)
                    {
                        Console.WriteLine("<{0}> {1}", func.Name.LocalName, funcName);
                    }
                }

                //print all the parameter names
                if (this.Parameters)
                {
                    foreach (var param in file.Descendants(SRC.Parameter))
                    {
                        if (param.Element(SRC.Declaration) != null)
                        {
                            var paramName = param.Element(SRC.Declaration).Element(SRC.Name);
                            if (paramName != null)
                            {
                                if (IsSameCase(paramName.Value))
                                {
                                    sameCaseWords.Add(paramName.Value);
                                }
                                if (PrintAll)
                                {
                                    Console.WriteLine("<{0}> {1}", param.Name.LocalName, paramName.Value);
                                }
                            }
                        }
                    }
                }

                //print all the variable names
                if (this.Variables)
                {
                    foreach (var declStmt in file.Descendants(SRC.DeclarationStatement))
                    {
                        foreach (var decl in declStmt.Elements(SRC.Declaration))
                        {
                            var variableName = decl.Element(SRC.Name);
                            if (variableName != null)
                            {
                                //if a qualified name, grab the last name
                                var childNames = variableName.Elements(SRC.Name);
                                if (childNames.Count() > 1)
                                {
                                    variableName = childNames.Last();
                                }

                                if (IsSameCase(variableName.Value))
                                {
                                    sameCaseWords.Add(variableName.Value);
                                }
                                if (PrintAll)
                                {
                                    Console.WriteLine("<{0}> {1}", declStmt.Name.LocalName, variableName.Value);
                                }
                            }
                        }
                    }
                }
            }

            Console.WriteLine("=== sameCaseWords: ===");
            foreach (string word in sameCaseWords)
            {
                Console.WriteLine(word);
            }
        }
        /// <summary>
        /// Builds a MethodContext object based on the given method element.
        /// </summary>
        /// <param name="methodTag">An XElement representing the method to build the context for. This can be either a function, constructor or destructor element.</param>
        /// <returns>A MethodContext object based on the given method.</returns>
        /// <exception cref="System.ArgumentException">The passed XElement does not represent a function, constructor, or destructor element.</exception>
        /// <exception cref="System.ArgumentNullException">methodTag is null.</exception>
        public static MethodContext BuildMethodContext(XElement methodTag)
        {
            if (methodTag == null)
            {
                throw new ArgumentNullException("methodTag");
            }
            else if (!(methodTag.Name == SRC.Function || methodTag.Name == SRC.Constructor || methodTag.Name == SRC.Destructor))
            {
                throw new ArgumentException(string.Format("The passed XElement must represent a <function>, <constructor> or <destructor> element. Received a <{0}> element.", methodTag.Name.ToString()), "methodTag");
            }

            MethodContext mc = new MethodContext();

            //set return type
            if (methodTag.Name == SRC.Function)
            {
                bool isPrimitive;
                mc.IdType            = ConstructTypeName(methodTag.Element(SRC.Type), out isPrimitive);
                mc.IdTypeIsPrimitive = isPrimitive;
            }

            //record if constructor
            if (methodTag.Name == SRC.Constructor)
            {
                mc.IsConstructor = true;
            }
            //record if destructor
            if (methodTag.Name == SRC.Destructor)
            {
                mc.IsDestructor = true;
            }

            //record if static
            //look for the static keyword (at the function definition)
            //This is not entirely sufficient because it's possible for the static keyword to be present at only the function declaration and not the definition
            //Also, some other word may be #defined to static and used instead
            var typeElement = methodTag.Element(SRC.Type);

            if (typeElement != null)
            {
                foreach (var typeNameTag in typeElement.Elements(SRC.Name))
                {
                    if (typeNameTag.Value.Equals("static", StringComparison.InvariantCultureIgnoreCase))
                    {
                        mc.IsStatic = true;
                        break;
                    }
                }
            }

            //formal parameters
            mc.FormalParameters = new List <FormalParameterRecord>();
            XElement paramList = methodTag.Element(SRC.ParameterList);

            if (paramList != null)
            {
                foreach (var param in paramList.Elements(SRC.Parameter))
                {
                    //a param (usually) looks like: <param><decl><type><name>...</name></type> <name>...</name></decl></param>
                    string type        = string.Empty;
                    string name        = string.Empty;
                    bool   isPrimitive = false;
                    var    declElement = param.Element(SRC.Declaration);
                    if (declElement != null && declElement.Element(SRC.Type) != null)
                    {
                        type = ConstructTypeName(declElement.Element(SRC.Type), out isPrimitive);
                    }
                    if (declElement != null && declElement.Element(SRC.Name) != null)
                    {
                        name = param.Element(SRC.Declaration).Element(SRC.Name).Value;
                    }
                    //add parameter, if it's valid
                    if (!(string.IsNullOrEmpty(type) && string.IsNullOrEmpty(name)) && type != "void")
                    {
                        mc.FormalParameters.Add(new FormalParameterRecord(type, isPrimitive, name));
                    }
                }
            }

            //Determine declaring class
            XElement classNameTag = SrcMLElement.GetClassNameForMethod(methodTag);

            if (classNameTag != null)
            {
                //class name listed with method name: <ClassName>::<MethodName>
                mc.DeclaringClass = classNameTag.Value;
            }
            else if (classNameTag == null && mc.IsConstructor)
            {
                //class name not listed, but the method is a constructor
                //I'm not sure if this is actually possible
                mc.DeclaringClass = SrcMLElement.GetNameForMethod(methodTag).Value;
            }
            else if (classNameTag == null)
            {
                //no class name listed, but this might be an inline method in the class declaration
                //search for the enclosing <class> or <struct> tag
                XElement classElement = FindEnclosingClassElement(methodTag);
                if (classElement != null && classElement.Element(SRC.Name) != null)
                {
                    mc.DeclaringClass = GetNameFromNameElement(classElement.Element(SRC.Name));
                }
            }

            return(mc);
        }