protected virtual ImmutableArray <TSyntax> FindUnusedSyntax( TNode node, TListSyntax list, SeparatedSyntaxList <TSyntax> separatedList, SemanticModel semanticModel, CancellationToken cancellationToken) { int count = separatedList.Count(f => !f.IsMissing); if (count == 1) { TSyntax syntax = separatedList.First(f => !f.IsMissing); if (IsFixable(node, list, syntax, semanticModel, cancellationToken) && !syntax.SpanContainsDirectives()) { return(ImmutableArray.Create(syntax)); } } else if (count > 1) { return(FindFixableSyntaxes(node, list, separatedList, count, semanticModel, cancellationToken) .Where(f => !f.SpanContainsDirectives()) .ToImmutableArray()); } return(ImmutableArray <TSyntax> .Empty); }
private static SyntaxNode SimplifyMethodAndConstructorInvocation(ref int numbering, ref SyntaxList <StatementSyntax> preList, SyntaxNode original, SyntaxNode origWithReplacedDesc) { SeparatedSyntaxList <ArgumentSyntax> slst = new SeparatedSyntaxList <ArgumentSyntax>(); SeparatedSyntaxList <ArgumentSyntax> myArgs; InvocationExpressionSyntax ies = null; ObjectCreationExpressionSyntax oces = null; // es necesario manejarlos por separado, porque pese a que ambos tienen como propiedad Arguments // de tipo SeparatedSyntaxList<ArgumentSyntax>, no estan en la misma linea de jerarquia // de clases... entonces: if (origWithReplacedDesc.IsKind(SyntaxKind.InvocationExpression)) { ies = (InvocationExpressionSyntax)origWithReplacedDesc; myArgs = ies.ArgumentList.Arguments; } else { oces = (ObjectCreationExpressionSyntax)origWithReplacedDesc; myArgs = oces.ArgumentList.Arguments; } foreach (var arg in myArgs) { if (!(arg.Expression is LiteralExpressionSyntax || arg.Expression is IdentifierNameSyntax)) { numbering++; preList = preList.Add(SyntaxFactory.ParseStatement("var __a" + numbering + " = " + arg + ";")); slst = slst.Add((SyntaxFactory.Argument(SyntaxFactory.ParseExpression("__a" + numbering)))); } } if (slst.Count() > 0) { var argumentList = SyntaxFactory.ArgumentList(slst); if (origWithReplacedDesc.IsKind(SyntaxKind.InvocationExpression)) { return(ies.WithArgumentList(argumentList)); } else { return(oces.WithArgumentList(argumentList)); } } else { return(original); } }
private SyntaxTree findMainTree(Project project) { SyntaxTree result = null; if (project != null && project.Documents != null && project.Documents.Count() > 0) { foreach (Document doc in project.Documents) { SyntaxTree tree = doc.GetSyntaxTreeAsync().Result; if (tree != null) { ClassDeclarationSyntax mainClass = ProjectAnalyzerHelper.GetMainClass(tree); if (mainClass != null) { BaseListSyntax baseList = mainClass.BaseList; if (baseList != null) { SeparatedSyntaxList <BaseTypeSyntax> types = baseList.Types; if (types != null && types.Count() > 0) { BaseTypeSyntax baseType = types.First(); if (baseType != null) { if (baseType.ToString() == ProjectAnalyzer.DefaultProgramBaseType) { result = doc.GetSyntaxTreeAsync().Result; Console.Out.WriteLine("Found program file: " + doc.FilePath); break; } } } } } } } } return(result); }
private static SyntaxNode SimplifyMethodAndConstructorInvocation(ref int numbering, ref SyntaxList<StatementSyntax> preList, SyntaxNode original, SyntaxNode origWithReplacedDesc) { SeparatedSyntaxList<ArgumentSyntax> slst = new SeparatedSyntaxList<ArgumentSyntax>(); SeparatedSyntaxList<ArgumentSyntax> myArgs; InvocationExpressionSyntax ies = null; ObjectCreationExpressionSyntax oces = null; // es necesario manejarlos por separado, porque pese a que ambos tienen como propiedad Arguments // de tipo SeparatedSyntaxList<ArgumentSyntax>, no estan en la misma linea de jerarquia // de clases... entonces: if (origWithReplacedDesc.IsKind(SyntaxKind.InvocationExpression)) { ies = (InvocationExpressionSyntax)origWithReplacedDesc; myArgs = ies.ArgumentList.Arguments; } else { oces = (ObjectCreationExpressionSyntax)origWithReplacedDesc; myArgs = oces.ArgumentList.Arguments; } foreach (var arg in myArgs) { if (!(arg.Expression is LiteralExpressionSyntax || arg.Expression is IdentifierNameSyntax)) { numbering++; preList = preList.Add(SyntaxFactory.ParseStatement("var __a" + numbering + " = " + arg + ";")); slst = slst.Add((SyntaxFactory.Argument(SyntaxFactory.ParseExpression("__a" + numbering)))); } } if (slst.Count() > 0) { var argumentList = SyntaxFactory.ArgumentList(slst); if (origWithReplacedDesc.IsKind(SyntaxKind.InvocationExpression)) { return ies.WithArgumentList(argumentList); } else { return oces.WithArgumentList(argumentList); } } else return original; }
private void VisitMethods(SyntaxNodeAnalysisContext ctx) { ClassDeclarationSyntax node = ctx.Node as ClassDeclarationSyntax; if (node == null) { return; } // Ensures that the analyzed class has a dependency to Controller if (node.DescendantNodesAndSelf() .OfType <BaseListSyntax>() .Where(childrenNode => childrenNode.ToString().Contains("Controller")) .Count() .Equals(0)) { return; } IEnumerable <MethodDeclarationSyntax> methodsWithParameters = node.DescendantNodesAndSelf() .OfType <MethodDeclarationSyntax>() .Where(method => !method.ParameterList.Parameters.Count().Equals(0)) .Where(method => method.Modifiers.ToString().Equals("public")) .Where(method => method.ReturnType.ToString().Equals("string")); foreach (MethodDeclarationSyntax method in methodsWithParameters) { SyntaxList <StatementSyntax> methodStatements = method.Body.Statements; IEnumerable <InvocationExpressionSyntax> methodInvocations = method.DescendantNodes().OfType <InvocationExpressionSyntax>(); if (!methodStatements.Count().Equals(0)) { DataFlowAnalysis flow = ctx.SemanticModel.AnalyzeDataFlow(methodStatements.First(), methodStatements.Last()); // Returns from the Data Flow Analysis of sensible data // Sensible data is: Data passed as a parameter that is also returned as is by the method IEnumerable <ISymbol> sensibleVariables = flow.DataFlowsIn.Union(flow.VariablesDeclared.Except(flow.AlwaysAssigned)) .Union(flow.WrittenInside) .Intersect(flow.WrittenOutside); if (!sensibleVariables.Count().Equals(0)) { foreach (ISymbol sensibleVariable in sensibleVariables) { bool sensibleVariableIsEncoded = false; foreach (InvocationExpressionSyntax methodInvocation in methodInvocations) { SeparatedSyntaxList <ArgumentSyntax> arguments = methodInvocation.ArgumentList.Arguments; if (!arguments.Count().Equals(0)) { if (arguments.First().ToString().Contains(sensibleVariable.Name.ToString())) { sensibleVariableIsEncoded = true; } } } if (!sensibleVariableIsEncoded) { ctx.ReportDiagnostic(Diagnostic.Create(Rule, method.GetLocation())); } } } } } }