Beispiel #1
0
        public void TestCreateTypeDefinitions_InnerClassWithNamespace()
        {
            // package A; class B { class C { } }
            string xml = @"<package>package <name>A</name>;</package>
<class>class <name>B</name> <block>{
	<class>class <name>C</name> <block>{
	}</block></class>
}</block></class>";

            XElement xmlElement      = fileSetup.GetFileUnitForXmlSnippet(xml, "B.java");
            var      scopes          = VariableScopeIterator.Visit(codeParser.ParseFileUnit(xmlElement));
            var      typeDefinitions = from scope in scopes
                                       let definition = (scope as ITypeDefinition)
                                                        where definition != null
                                                        select definition;

            Assert.AreEqual(2, typeDefinitions.Count());

            var outer = typeDefinitions.First() as ITypeDefinition;
            var inner = typeDefinitions.Last() as ITypeDefinition;

            Assert.AreEqual("B", outer.Name);
            Assert.AreEqual("A", outer.GetFirstParent <INamespaceDefinition>().GetFullName());
            Assert.AreEqual("A.B", outer.GetFullName());

            Assert.AreEqual("C", inner.Name);
            Assert.AreEqual("A", inner.GetFirstParent <INamespaceDefinition>().GetFullName());
            Assert.AreEqual("A.B.C", inner.GetFullName());
        }
Beispiel #2
0
        public void TestCreateVariableDeclaration()
        {
            // class A { private int X; int GetX() { return X; } }
            string xml = @"<class>class <name>A</name> <block>{
	<decl_stmt><decl><type><specifier>private</specifier> <name>int</name></type> <name>X</name></decl>;</decl_stmt>
	<function><type><specifier>public</specifier> <name>int</name></type> <name>GetX</name><parameter_list>()</parameter_list> <block>{ <return>return <expr><name>X</name></expr>;</return> }</block></function>
}</block></class>";

            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(xml, "A.java");

            var declarationElement = xmlElement.Descendants(SRC.DeclarationStatement).First();

            var rootScope = codeParser.ParseFileUnit(xmlElement);
            var scope     = VariableScopeIterator.Visit(rootScope).Last();
            var useOfX    = xmlElement.Descendants(SRC.Return).First().Descendants(SRC.Name).First();

            Assert.AreEqual(3, rootScope.GetScopeForLocation(useOfX.GetXPath()).GetParentScopesAndSelf().Count());

            var matchingDeclarations = rootScope.GetDeclarationsForVariableName("X", useOfX.GetXPath());
            var declaration          = matchingDeclarations.First();

            Assert.AreEqual("int", declaration.VariableType.Name);
            Assert.AreEqual("X", declaration.Name);

            Assert.That(useOfX.GetXPath().StartsWith(declaration.ParentScope.PrimaryLocation.XPath));
        }
        public void TestCreateTypeDefinition_ClassWithMethodDeclaration()
        {
            // class A {
            // public:
            // int foo(int a); };
            string xml = @"<class>class <name>A</name> <block>{<private type=""default"">
</private><public>public:
    <function_decl><type><name>int</name></type> <name>foo</name><parameter_list>(<param><decl><type><name>int</name></type> <name>a</name></decl></param>)</parameter_list>;</function_decl>
</public>}</block>;</class>";

            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(xml, "A.h");

            var globalScope = codeParser.ParseFileUnit(xmlElement);
            var scopes      = VariableScopeIterator.Visit(globalScope);

            var typeA     = globalScope.ChildScopes.First() as ITypeDefinition;
            var methodFoo = typeA.ChildScopes.First() as IMethodDefinition;

            Assert.AreEqual(3, scopes.Count());

            Assert.AreEqual("A", typeA.Name);
            Assert.AreEqual("foo", methodFoo.Name);

            Assert.AreEqual(1, methodFoo.Parameters.Count);
        }
        public void TestCreateTypeDefinitions_InnerClassWithNamespace()
        {
            // namespace A { class B { class C { }; }; }
            string xml = @"<namespace>namespace <name>A</name> <block>{
    <class>class <name>B</name> <block>{<private type=""default"">
        <class>class <name>C</name> <block>{<private type=""default"">
        </private>}</block>;</class>
    </private>}</block>;</class>
}</block></namespace>";

            XElement xmlElement = fileSetup.GetFileUnitForXmlSnippet(xml, "A.h");
            var      scopes     = VariableScopeIterator.Visit(codeParser.ParseFileUnit(xmlElement));

            Assert.AreEqual(4, scopes.Count());

            var typeDefinitions = from scope in scopes
                                  let definition = scope as ITypeDefinition
                                                   where definition != null
                                                   select definition;

            var outer = typeDefinitions.First() as ITypeDefinition;
            var inner = typeDefinitions.Last() as ITypeDefinition;

            Assert.AreEqual("B", outer.Name);
            Assert.AreEqual("A", outer.GetFirstParent <INamespaceDefinition>().GetFullName());
            Assert.AreEqual("A.B", outer.GetFullName());

            Assert.AreEqual("C", inner.Name);
            Assert.AreEqual("A", inner.GetFirstParent <INamespaceDefinition>().GetFullName());
            Assert.AreEqual("A.B.C", inner.GetFullName());
        }
Beispiel #5
0
        private void PrintScopeReport(IScope globalScope)
        {
            Console.WriteLine("\nScope Report");
            Console.WriteLine("===============");

            Console.WriteLine("{0,10:N0} scopes", VariableScopeIterator.Visit(globalScope).Count());
            var namedScopes = from scope in VariableScopeIterator.Visit(globalScope)
                              where (scope as INamedScope) != null
                              select scope;

            Console.WriteLine("{0,10:N0} named scopes", namedScopes.Count());
            var namespaceScopes = from scope in namedScopes
                                  where (scope as INamespaceDefinition) != null
                                  select scope;
            var typeScopes = from scope in namedScopes
                             where (scope as ITypeDefinition) != null
                             select scope;
            var methodScopes = from scope in namedScopes
                               where (scope as IMethodDefinition) != null
                               select scope;

            Console.WriteLine("{0,10:N0} namespaces", namespaceScopes.Count());
            Console.WriteLine("{0,10:N0} types", typeScopes.Count());
            Console.WriteLine("{0,10:N0} methods", methodScopes.Count());
        }
Beispiel #6
0
        private static void PrintScopeReport(IScope globalScope, string sourcePath, string csvDirectory)
        {
            var csvPath = Path.Combine(csvDirectory, "scopes.csv");

            Console.WriteLine("\nScope Report");
            Console.WriteLine("===============");

            var allScopes      = VariableScopeIterator.Visit(globalScope);
            int numScopes      = allScopes.Count();
            int numNamedScopes = allScopes.OfType <INamedScope>().Count();
            int numNamespaces  = allScopes.OfType <NamespaceDefinition>().Count();
            int numTypes       = allScopes.OfType <ITypeDefinition>().Count();
            int numMethods     = allScopes.OfType <IMethodDefinition>().Count();

            Console.WriteLine("{0,10:N0} scopes", numScopes);

            Console.WriteLine("{0,10:N0} named scopes", numNamedScopes);

            Console.WriteLine("{0,10:N0} namespaces", numNamespaces);
            Console.WriteLine("{0,10:N0} types", numTypes);
            Console.WriteLine("{0,10:N0} methods", numMethods);
            if (!File.Exists(csvPath))
            {
                File.WriteAllText(csvPath, String.Format("{0}{1}", String.Join(",", "Project", "Scopes", "Named Scopes", "Namespaces", "Types", "Methods"), Environment.NewLine));
            }
            File.AppendAllText(csvPath, String.Format("{0}{1}", String.Join(",", sourcePath, numScopes, numNamedScopes, numNamespaces, numTypes, numMethods), Environment.NewLine));
        }
        public void BasicParentTest_Java()
        {
            // # A.java class A implements B { }
            string a_xml = @"<class>class <name>A</name> <super><implements>implements <name>B</name></implements></super> <block>{
}</block></class>";
            // # B.java class B { }
            string b_xml = @"<class>class <name>B</name> <block>{
}</block></class>";

            // # C.java class C { A a; }
            string c_xml = @"<class>class <name>C</name> <block>{
	<decl_stmt><decl><type><name>A</name></type> <name>a</name></decl>;</decl_stmt>
}</block></class>";

            var fileUnitA = FileUnitSetup[Language.Java].GetFileUnitForXmlSnippet(a_xml, "A.java");
            var fileUnitB = FileUnitSetup[Language.Java].GetFileUnitForXmlSnippet(b_xml, "B.java");
            var fileUnitC = FileUnitSetup[Language.Java].GetFileUnitForXmlSnippet(c_xml, "C.java");

            var globalScope = CodeParser[Language.Java].ParseFileUnit(fileUnitA) as IScope;

            globalScope = globalScope.Merge(CodeParser[Language.Java].ParseFileUnit(fileUnitB) as IScope);
            globalScope = globalScope.Merge(CodeParser[Language.Java].ParseFileUnit(fileUnitC) as IScope);

            Assert.AreEqual(3, globalScope.ChildScopes.Count());

            var scopes          = VariableScopeIterator.Visit(globalScope);
            var typeDefinitions = (from scope in scopes
                                   let typeDefinition = (scope as ITypeDefinition)
                                                        where typeDefinition != null
                                                        orderby typeDefinition.Name
                                                        select typeDefinition).ToList();

            var typeA = typeDefinitions[0];
            var typeB = typeDefinitions[1];
            var typeC = typeDefinitions[2];

            Assert.AreEqual("B", typeB.Name);

            var cDotA     = typeC.DeclaredVariables.First();
            var parentOfA = typeA.ParentTypes.First();

            Assert.That(cDotA.VariableType.FindMatches().FirstOrDefault(), Is.SameAs(typeA));
            Assert.That(parentOfA.FindMatches().FirstOrDefault(), Is.SameAs(typeB));
        }
Beispiel #8
0
        private static void PrintMethodCallReport(IScope globalScope, string sourcePath, string csvDirectory, string callLogPath)
        {
            var csvPath = Path.Combine(csvDirectory, "methodcalls.csv");

            Console.WriteLine("\nMethod Call Report");
            Console.WriteLine("===============");
            var methodCalls = from scope in VariableScopeIterator.Visit(globalScope)
                              from call in scope.MethodCalls
                              select call;

            int       numMethodCalls        = 0;
            int       numMatchedMethodCalls = 0;
            Stopwatch sw = new Stopwatch();

            using (var callLog = new StreamWriter(callLogPath)) {
                foreach (var call in methodCalls)
                {
                    sw.Start();
                    var match = call.FindMatches().FirstOrDefault();
                    sw.Stop();
                    numMethodCalls++;
                    if (null != match)
                    {
                        numMatchedMethodCalls++;
                        callLog.WriteLine("{0} ({1}:{2}) -> {3} ({4}:{5})", call.Name, call.Location.SourceFileName, call.Location.StartingLineNumber, match.Name, match.PrimaryLocation.SourceFileName, match.PrimaryLocation.StartingLineNumber);
                    }
                }
            }

            Console.WriteLine("{0,10:N0} method calls", numMethodCalls);
            Console.WriteLine("{0,10:N0} matched method calls ({1,8:P2})", numMatchedMethodCalls, ((float)numMatchedMethodCalls) / numMethodCalls);
            Console.WriteLine("{0,10:N0} matches / millisecond ({1,7:N0} ms elapsed)", ((float)numMethodCalls) / sw.ElapsedMilliseconds, sw.ElapsedMilliseconds);
            Console.WriteLine("See matched method calls in {0}", callLogPath);

            if (!File.Exists(csvPath))
            {
                File.WriteAllText(csvPath, String.Format("{0}{1}", String.Join(",", "Project", "Method Calls", "Matched Method Calls", "Time (ms)"), Environment.NewLine));
            }
            File.AppendAllText(csvPath, String.Format("{0}{1}", String.Join(",", sourcePath, numMethodCalls, numMatchedMethodCalls, sw.ElapsedMilliseconds), Environment.NewLine));
        }
Beispiel #9
0
        private void PrintMethodCallReport(IScope globalScope, string callLogPath)
        {
            Console.WriteLine("\nMethod Call Report");
            Console.WriteLine("===============");
            var methodCalls = from scope in VariableScopeIterator.Visit(globalScope)
                              from call in scope.MethodCalls
                              select call;

            int       numMethodCalls        = 0;
            int       numMatchedMethodCalls = 0;
            Stopwatch sw = new Stopwatch();

            using (var callLog = new StreamWriter(callLogPath)) {
                foreach (var call in methodCalls)
                {
                    sw.Start();
                    IMethodDefinition match = null;
                    try {
                        match = call.FindMatches().FirstOrDefault();
                    } catch (Exception e) {
                        Console.WriteLine("{0}:{1}:{2}): {3}", call.Location.SourceFileName, call.Location.StartingLineNumber, call.Location.StartingColumnNumber, e.Message);
                    }

                    sw.Stop();
                    numMethodCalls++;
                    if (null != match)
                    {
                        numMatchedMethodCalls++;
                        callLog.WriteLine("{0} ({1}:{2}) -> {3} ({4}:{5})", call.Name, call.Location.SourceFileName, call.Location.StartingLineNumber, match.Name, match.PrimaryLocation.SourceFileName, match.PrimaryLocation.StartingLineNumber);
                    }
                }
            }

            Console.WriteLine("{0,10:N0} method calls", numMethodCalls);
            Console.WriteLine("{0,10:N0} matched method calls ({1,8:P2})", numMatchedMethodCalls, ((float)numMatchedMethodCalls) / numMethodCalls);
            Console.WriteLine("{0,10:N0} matches / millisecond ({1,7:N0} ms elapsed)", ((float)numMethodCalls) / sw.ElapsedMilliseconds, sw.ElapsedMilliseconds);
            Console.WriteLine(callLogPath);
        }