public void TestAnalyzeClass() { string code1 = @"class TestClass { public void TestMethod( int test = 0; test = 1;){}}"; SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code1); ClassDeclarationSyntax classNode = tree1.GetRootAsync().Result.DescendantNodes().OfType <ClassDeclarationSyntax>().First(); List <SyntaxTree> trees1 = new List <SyntaxTree> { tree1 }; Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1); ScriptAnalyzer analyzer = this.createAnalyzer(comp1); TaggedSyntaxLibrary lib = analyzer.AnalyzeNode(classNode); Assert.IsNotNull(lib, "Library defined"); Assert.AreEqual(1, lib.TaggedSyntaxTrees.Count(), "Has one tree"); TaggedSyntaxTree tree = lib.TaggedSyntaxTrees.First(); CollectionAssert.Contains(tree.TaggedNodes, classNode, "Tree contains class node"); IEnumerable <SyntaxNode> nodes = classNode.DescendantNodes(); foreach (SyntaxNode node in nodes) { CollectionAssert.DoesNotContain(tree.TaggedNodes, node, "SubNode is not added to tree"); } }
public void TestAnalyzeForEach() { string code1 = @"class MainClass {public int MainMethod(){ TestClass[] testClasses = new TestClass[] { new TestClass(1), new TestClass(2) };int count = 0; foreach (TestClass testclass in testClasses){count += testclass.GetInt()} return count;}};}"; string code2 = @"class TestClass{ int counter; public TestClass(int param}{this.counter = param} public int GetInt(){return this.counter}"; SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code1); SyntaxTree tree2 = CSharpSyntaxTree.ParseText(code2); ForEachStatementSyntax forEachNode = tree1.GetRootAsync().Result.DescendantNodes().OfType <ForEachStatementSyntax>().First(); List <SyntaxTree> trees1 = new List <SyntaxTree> { tree1, tree2 }; Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1); ScriptAnalyzer analyzer = this.createAnalyzer(comp1); TaggedSyntaxLibrary lib = analyzer.AnalyzeNode(forEachNode); //Assert.IsNotNull(lib, "Library defined"); //Assert.AreEqual(1, lib.TaggedSyntaxTrees.Count(), "Has one tree"); //TaggedSyntaxTree tree = lib.TaggedSyntaxTrees.First(); //CollectionAssert.Contains(tree.TaggedNodes, varDeclartionNode, "Tree contains variable declaration node"); //IEnumerable<SyntaxNode> nodes = varDeclartionNode.DescendantNodes(); //foreach (SyntaxNode node in nodes) //{ // CollectionAssert.Contains(tree.TaggedNodes, node, "SubNode is added to tree: " + node.ToString()); //} }
public List <SyntaxNode> GetSyntaxNodes(SyntaxNode node, TaggedSyntaxLibrary lib = null) { List <SyntaxNode> nodes = new List <SyntaxNode>(); Type type = node.GetType(); if (type == typeof(ClassDeclarationSyntax)) { nodes = this.GetSyntaxNodesFromClass(node as ClassDeclarationSyntax, lib); } else if (type == typeof(ConstructorInitializerSyntax)) { nodes = this.GetSyntaxNodesFromConstructorInitializer(node as ConstructorInitializerSyntax); } else if (type == typeof(MethodDeclarationSyntax)) { nodes = this.GetSyntaxNodesFromMethod(node as MethodDeclarationSyntax, lib); } else if (type == typeof(InvocationExpressionSyntax)) { nodes = this.GetSyntaxNodesFromInvocation(node as InvocationExpressionSyntax); } else if (type == typeof(ObjectCreationExpressionSyntax)) { nodes = this.GetSyntaxNodesFromObjectCreation(node as ObjectCreationExpressionSyntax); } else if (type == typeof(IdentifierNameSyntax)) { nodes = this.GetSyntaxNodesFromIdentifier(node as IdentifierNameSyntax); } return(nodes); }
public TaggedSyntaxLibrary AnalyzeNode(SyntaxNode node, TaggedSyntaxLibrary lib = null) { if (lib == null) { lib = new TaggedSyntaxLibrary(); } if (!lib.IsTagged(node)) { Type type = node.GetType(); if (type == typeof(NamespaceDeclarationSyntax)) { this.AnalyzeNameSpace(node as NamespaceDeclarationSyntax, lib); } else if (type == typeof(ClassDeclarationSyntax)) { this.AnalyzeClass(node as ClassDeclarationSyntax, lib); } else if (type == typeof(VariableDeclaratorSyntax)) { this.AnalyzeVariableDeclarator(node as VariableDeclaratorSyntax, lib); } else { this.DefaultAnalyze(node, lib); } this.ResourceAnalyze(node, lib); } return(lib); }
public void TestAnalyzeOverrideMethodFromVirtual() { string code1 = @"class TestClass { public void MainMethod(){ SubClass test = new SubClass(); test.TestMethod();}}"; string code2 = @"class SuperClass { public void TestMethod(){this.TestMethod2();} public virtual void TestMethod2(){}}"; string code3 = @"class SubClass:SuperClass { public override void TestMethod2(){}}"; SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code1); SyntaxTree tree2 = CSharpSyntaxTree.ParseText(code2); SyntaxTree tree3 = CSharpSyntaxTree.ParseText(code3); MethodDeclarationSyntax mainNode = tree1.GetRootAsync().Result.DescendantNodes().OfType <MethodDeclarationSyntax>().First(); MethodDeclarationSyntax superNode1 = tree2.GetRootAsync().Result.DescendantNodes().OfType <MethodDeclarationSyntax>().First(); MethodDeclarationSyntax superNode2 = tree2.GetRootAsync().Result.DescendantNodes().OfType <MethodDeclarationSyntax>().Last(); MethodDeclarationSyntax subNode = tree3.GetRootAsync().Result.DescendantNodes().OfType <MethodDeclarationSyntax>().First(); List <SyntaxTree> trees1 = new List <SyntaxTree> { tree1, tree2, tree3 }; Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1); ScriptAnalyzer analyzer = this.createAnalyzer(comp1); TaggedSyntaxLibrary lib = analyzer.AnalyzeNode(mainNode); Assert.IsNotNull(lib, "Library defined"); Assert.AreEqual(3, lib.TaggedSyntaxTrees.Count(), "Has three trees"); TaggedSyntaxTree rTree1 = lib.TaggedSyntaxTrees.First(); TaggedSyntaxTree rTree2 = lib.TaggedSyntaxTrees.ElementAt(1); TaggedSyntaxTree rTree3 = lib.TaggedSyntaxTrees.ElementAt(2); CollectionAssert.Contains(rTree1.TaggedNodes, mainNode, "Main tree contains main method"); CollectionAssert.Contains(rTree3.TaggedNodes, superNode1, "Super tree contains first method"); CollectionAssert.Contains(rTree3.TaggedNodes, superNode2, "Super tree contains first method"); CollectionAssert.Contains(rTree2.TaggedNodes, subNode, "Sub tree contains sub method"); }
public void TestConstructor() { TaggedSyntaxLibrary lib = new TaggedSyntaxLibrary(); Assert.IsNotNull(lib.TaggedSyntaxTrees, "Has created list of tagged syntax trees"); Assert.AreEqual(0, lib.TaggedSyntaxTrees.Count(), "List is empty"); }
public void TestIsTagged() { TaggedSyntaxLibrary lib = new TaggedSyntaxLibrary(); ClassDeclarationSyntax node = SyntaxFactory.ClassDeclaration("test"); ClassDeclarationSyntax unknown = SyntaxFactory.ClassDeclaration("unknown"); bool result = lib.TagNode(node); Assert.IsTrue(lib.IsTagged(node), "Expect result to be true"); Assert.IsFalse(lib.IsTagged(unknown), "Expect result to be false"); }
public TaggedSyntaxLibrary DefaultAnalyze(SyntaxNode node, TaggedSyntaxLibrary lib) { lib.TagNode(node); IEnumerable <SyntaxNode> subnodes = node.DescendantNodes().Where(n => n.Parent == node); if (subnodes != null && subnodes.Count() > 0) { foreach (SyntaxNode subnode in subnodes) { this.AnalyzeNode(subnode, lib); } } return(lib); }
public TaggedSyntaxLibrary ResourceAnalyze(SyntaxNode node, TaggedSyntaxLibrary lib) { // analyze resources if any List <SyntaxNode> resources = this.resourceHelper.GetSyntaxNodes(node, lib); if (resources.Count() > 0) { foreach (SyntaxNode resource in resources) { this.AnalyzeNode(resource, lib); } } return(lib); }
public void TestTagNode() { TaggedSyntaxLibrary lib = new TaggedSyntaxLibrary(); ClassDeclarationSyntax node = SyntaxFactory.ClassDeclaration("test"); bool result = lib.TagNode(node); Assert.IsTrue(result, "Expect result to be true"); Assert.IsNotNull(lib.TaggedSyntaxTrees, "Has created list of tagged syntax trees"); Assert.AreEqual(1, lib.TaggedSyntaxTrees.Count(), "List has one tagged tree"); TaggedSyntaxTree tree = lib.TaggedSyntaxTrees.First(); Assert.AreEqual(node.SyntaxTree, tree.OriginalTree, "Tagged Syntax tree has the correct syntaxtree"); Assert.IsTrue(tree.IsTagged(node), "The node is tagged on the tree"); }
public TaggedSyntaxLibrary analyze(Project project = null) { TaggedSyntaxLibrary lib = new TaggedSyntaxLibrary(); if (project == null) { project = this.project; } if (project != null) { SyntaxTree program = this.findMainTree(project); if (program != null) { List <SyntaxNode> entryNodes = this.getEntryNodes(program); ScriptAnalyzer analyzer = this.createScriptAnalyzer(project); if (entryNodes != null && entryNodes.Count > 0) { foreach (SyntaxNode entryNode in entryNodes) { analyzer.AnalyzeNode(entryNode, lib); } if (lib.TaggedSyntaxTrees.Count() > 0) { TaggedSyntaxTree mainDTree = lib.GetTreeFromNode(entryNodes.First()); IEnumerable <TaggedSyntaxTree> dTrees = lib.TaggedSyntaxTrees.Where(t => t != mainDTree); if (mainDTree != null) { this.mainTree = mainDTree.GetSyntaxTree(); } if (dTrees != null && dTrees.Count() > 0) { this.dependencies = this.parseTaggedSyntaxTrees(dTrees); } } } } } // analyze three methods return(lib); }
public List <SyntaxNode> GetSyntaxNodesFromClass(ClassDeclarationSyntax node, TaggedSyntaxLibrary lib) { List <SyntaxNode> nodes = new List <SyntaxNode>(); INamedTypeSymbol classSymbol = this.symbolHelper.GetSymbol(node) as INamedTypeSymbol; if (classSymbol != null) { List <ISymbol> symbols = new List <ISymbol>(); INamedTypeSymbol baseType = classSymbol.BaseType; if (baseType != null) { symbols.Add(baseType); } ImmutableArray <INamedTypeSymbol> interfaces = classSymbol.Interfaces; if (interfaces != null && interfaces.Count() > 0) { foreach (INamedTypeSymbol inter in interfaces) { if (inter != null) { symbols.Add(inter); } } } // check for override methods List <IMethodSymbol> oMethods = this.getOverrideMethodSymbolsFromClass(node); if (oMethods != null && oMethods.Count() > 0) { foreach (IMethodSymbol oMethod in oMethods) { IMethodSymbol overridden = oMethod.OverriddenMethod; if (overridden != null) { List <SyntaxNode> oNodes = ScriptAnalyzerResourceHelper.GetSyntaxNodesFromSymbol(overridden); if (oNodes != null && oNodes.Count() > 0) { MethodDeclarationSyntax oNode = oNodes.First() as MethodDeclarationSyntax; if (oNode != null) { TaggedSyntaxTree tTree = lib.GetTreeFromNode(oNode); if (tTree != null && tTree.IsTagged(oNode)) { symbols.Add(oMethod); } } } } } } nodes = ScriptAnalyzerResourceHelper.GetSyntaxNodesFromSymbols(symbols); } return(nodes); }
public TaggedSyntaxLibrary AnalyzeNameSpace(NamespaceDeclarationSyntax node, TaggedSyntaxLibrary lib) { lib.TagNode(node); return(lib); }
private List <IMethodSymbol> GetSymbolsFromVirtualFunction(IMethodSymbol symbol, TaggedSyntaxLibrary lib) { List <IMethodSymbol> symbols = new List <IMethodSymbol>(); if (lib.TaggedSyntaxTrees != null && lib.TaggedSyntaxTrees.Count() > 0) { foreach (TaggedSyntaxTree tTree in lib.TaggedSyntaxTrees) { List <ClassDeclarationSyntax> classes = tTree.TaggedNodes.OfType <ClassDeclarationSyntax>().ToList(); if (classes != null && classes.Count() > 0) { foreach (ClassDeclarationSyntax classSyntax in classes) { List <IMethodSymbol> mSymbols = this.getOverrideMethodSymbolsFromClass(classSyntax); if (mSymbols != null && mSymbols.Count() > 0) { foreach (IMethodSymbol mSymbol in mSymbols) { if (mSymbol.OverriddenMethod == symbol) { symbols.Add(mSymbol); } } } } } } } return(symbols); }
public List <SyntaxNode> GetSyntaxNodesFromMethod(MethodDeclarationSyntax node, TaggedSyntaxLibrary lib) { List <SyntaxNode> nodes = new List <SyntaxNode>(); IMethodSymbol symbol = this.symbolHelper.GetSymbol(node) as IMethodSymbol; if (symbol != null) { List <ISymbol> symbols = new List <ISymbol>(); if (symbol.IsOverride) { symbols.Add(symbol.OverriddenMethod); } if (symbol.IsVirtual) { List <ISymbol> mSymbols = this.GetSymbolsFromVirtualFunction(symbol, lib).Select(s => s as ISymbol).ToList(); symbols.AddRange(mSymbols); } // return type ITypeSymbol returnType = symbol.ReturnType; if (returnType != null) { symbols.Add(returnType); } // parameters ImmutableArray <IParameterSymbol> parameterList = symbol.Parameters; if (parameterList != null && parameterList.Count() > 0) { foreach (IParameterSymbol param in parameterList) { if (param != null && param.Type != null) { symbols.Add(param.Type); } } } nodes = ScriptAnalyzerResourceHelper.GetSyntaxNodesFromSymbols(symbols); } return(nodes); }
public TaggedSyntaxLibrary AnalyzeClass(ClassDeclarationSyntax node, TaggedSyntaxLibrary lib) { lib.TagNode(node); return(lib); }
public TaggedSyntaxLibrary AnalyzeVariableDeclarator(VariableDeclaratorSyntax node, TaggedSyntaxLibrary lib) { lib.TagNode(node); if (node.Parent != null) { this.AnalyzeNode(node.Parent, lib); } this.DefaultAnalyze(node, lib); return(lib); }