Ejemplo n.º 1
0
 private static void AnalyzeAddRemoveNode(SyntaxNodeAnalysisContext context, ExpressionSyntax syntax)
 {
     foreach (var oneIdSyntax in syntax.ChildNodes().OfType <IdentifierNameSyntax>())
     {
         var oneIdSymbol = context.SemanticModel.GetSymbolInfo(oneIdSyntax);
         if (oneIdSymbol.Symbol != null)
         {
             if (oneIdSymbol.Symbol is IMethodSymbol methodSymbol && !methodSymbol.IsStatic)
             {
                 var diagnostic = Diagnostic.Create(DiagnosticDescriptors.ShouldCacheDelegate,
                                                    oneIdSyntax.GetLocation(), oneIdSyntax.ToString());
                 context.ReportDiagnostic(diagnostic);
             }
         }
     }
     foreach (var oneAccessSyntax in syntax.ChildNodes().OfType <MemberAccessExpressionSyntax>())
     {
         foreach (var oneIdSyntax in oneAccessSyntax.ChildNodes().OfType <IdentifierNameSyntax>())
         {
             var oneIdSymbol = context.SemanticModel.GetSymbolInfo(oneIdSyntax);
             if (oneIdSymbol.Symbol != null)
             {
                 if (oneIdSymbol.Symbol is IMethodSymbol methodSymbol && !methodSymbol.IsStatic)
                 {
                     var diagnostic = Diagnostic.Create(DiagnosticDescriptors.ShouldCacheDelegate,
                                                        oneIdSyntax.GetLocation(), oneIdSyntax.ToString());
                     context.ReportDiagnostic(diagnostic);
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
        public static bool IsAStringExpression(this ExpressionSyntax node)
        {
            if (node == null)
            {
                return(false);
            }

            return(node.Kind() == SyntaxKind.StringLiteralExpression ||
                   node.ChildNodes().Any(x => x.Kind() == SyntaxKind.StringLiteralExpression) ||
                   node.Kind() == SyntaxKind.InterpolatedStringExpression ||
                   node.ChildNodes().Any(x => x.Kind() == SyntaxKind.InterpolatedStringExpression));
        }
Ejemplo n.º 3
0
 public static string InvocationExpressionRootName(this ExpressionSyntax invocation)
 {
     if (invocation == null)
     {
         return("");
     }
     if (invocation is IdentifierNameSyntax)
     {
         if ((invocation as IdentifierNameSyntax).Identifier == null)
         {
             return("");
         }
         return((invocation as IdentifierNameSyntax).Identifier.ValueText);
     }
     foreach (SyntaxNode invchnd in invocation.ChildNodes())
     {
         if (invchnd.Kind() == SyntaxKind.SimpleMemberAccessExpression)
         {
             ExpressionSyntax exst = (invchnd as MemberAccessExpressionSyntax).Expression;
             if (exst != null)
             {
                 return(exst.InvocationExpressionRootName());
             }
         }
     }
     return("");
 }
Ejemplo n.º 4
0
        private SyntaxNode MutateWithConditionalExpressions(ExpressionSyntax currentNode)
        {
            var expressionAst = currentNode.TrackNodes(currentNode.ChildNodes().Append(currentNode));

            foreach (var childNode in currentNode.ChildNodes())
            {
                var mutatedChild = Mutate(childNode);
                if (mutatedChild != childNode)
                {
                    expressionAst = expressionAst.ReplaceNode(expressionAst.GetCurrentNode(childNode), mutatedChild);
                }
            }
            foreach (var mutant in FindMutants(currentNode))
            {
                var mutatedNode = ApplyMutant(currentNode, mutant);
                expressionAst = MutantPlacer.PlaceWithConditionalExpression(expressionAst, mutatedNode, mutant.Id);
            }
            return(expressionAst);
        }
Ejemplo n.º 5
0
        private IEnumerable <MutantInfo> GenerateExpressionMutants(ExpressionSyntax expressionSyntax, int lineID)
        {
            var baseResult = base.Visit(expressionSyntax);

            if (baseResult != null)
            {
                foreach (var mutantInfo in baseResult)
                {
                    yield return(mutantInfo);
                }
            }

            if (!(expressionSyntax is AssignmentExpressionSyntax) && !(expressionSyntax is LambdaExpressionSyntax))
            {
                foreach (var syntax in expressionSyntax.ChildNodes().OfType <ExpressionSyntax>())
                {
                    foreach (var generateExpressionMutant in GenerateExpressionMutants(syntax, lineID))
                    {
                        yield return(generateExpressionMutant);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public static FluentAPIEntityNode InvocationExpressionMethods(this ExpressionSyntax invocation, FluentAPIEntityNode entityNode = null, string[] methodNames = null)
        {
            if (invocation == null)
            {
                return(entityNode);
            }
            if (invocation is IdentifierNameSyntax)
            {
                return(entityNode);
            }
            string methodName  = "";
            string genericName = "";
            int    cnt         = invocation.ChildNodes().Count();

            foreach (SyntaxNode ndcn in invocation.ChildNodes())
            {
                if (ndcn.Kind() == SyntaxKind.SimpleMemberAccessExpression)
                {
                    MemberAccessExpressionSyntax memberAccessExpressionSyntax = ndcn as MemberAccessExpressionSyntax;
                    entityNode = InvocationExpressionMethods(memberAccessExpressionSyntax.Expression, entityNode, methodNames);
                    methodName = memberAccessExpressionSyntax.Name.Identifier.ValueText;
                    if (memberAccessExpressionSyntax.Name.Kind() == SyntaxKind.GenericName)
                    {
                        string locEntityName = "";
                        TypeArgumentListSyntax typeArgumentList = (memberAccessExpressionSyntax.Name as GenericNameSyntax).TypeArgumentList;
                        if (typeArgumentList != null)
                        {
                            if (typeArgumentList.Arguments != null)
                            {
                                foreach (TypeSyntax ts in typeArgumentList.Arguments)
                                {
                                    locEntityName = ts.ToString();
                                }
                            }
                        }
                        if (!String.IsNullOrEmpty(locEntityName))
                        {
                            if ("Entity".Equals(memberAccessExpressionSyntax.Name.Identifier.ValueText))
                            {
                                methodName  = "";
                                genericName = "";
                                if (methodNames != null)
                                {
                                    if (methodNames.Count() > 0)
                                    {
                                        if (!methodNames.Any(i => string.Equals(i, "Entity")))
                                        {
                                            continue;
                                        }
                                    }
                                }
                                if (entityNode == null)
                                {
                                    entityNode = new FluentAPIEntityNode()
                                    {
                                        EntityName = locEntityName
                                    };
                                }
                                else
                                {
                                    entityNode.EntityName = locEntityName;
                                }
                                continue;
                            }
                            else
                            {
                                genericName = locEntityName;
                            }
                        }
                        methodName = memberAccessExpressionSyntax.Name.Identifier.ValueText;
                    }
                }
                if (string.IsNullOrEmpty(methodName))
                {
                    continue;
                }
                if (methodNames != null)
                {
                    if (methodNames.Count() > 0)
                    {
                        if (!methodNames.Any(i => string.Equals(i, methodName)))
                        {
                            continue;
                        }
                    }
                }
                if (ndcn.Kind() == SyntaxKind.ArgumentList)
                {
                    if (entityNode == null)
                    {
                        entityNode = new FluentAPIEntityNode();
                    }
                    if (entityNode.Methods == null)
                    {
                        entityNode.Methods = new List <FluentAPIMethodNode>();
                    }
                    FluentAPIMethodNode methodNode = new FluentAPIMethodNode()
                    {
                        MethodName = methodName, GenericName = genericName
                    };
                    entityNode.Methods.Add(methodNode);
                    methodName  = "";
                    genericName = "";
                    ArgumentListSyntax argumentListSyntax = ndcn as ArgumentListSyntax;
                    foreach (ArgumentSyntax argument in argumentListSyntax.Arguments)
                    {
                        if (argument.Expression.Kind() == SyntaxKind.SimpleLambdaExpression)
                        {
                            SimpleLambdaExpressionSyntax sles = argument.Expression as SimpleLambdaExpressionSyntax;
                            if (sles.Body.Kind() == SyntaxKind.AnonymousObjectCreationExpression)
                            {
                                AnonymousObjectCreationExpressionSyntax aoces = sles.Body as AnonymousObjectCreationExpressionSyntax;
                                foreach (AnonymousObjectMemberDeclaratorSyntax intlzrs in aoces.Initializers)
                                {
                                    if (methodNode.MethodArguments == null)
                                    {
                                        methodNode.MethodArguments = new List <String>();
                                    }
                                    methodNode.MethodArguments.Add((intlzrs.Expression as MemberAccessExpressionSyntax).Name.ToString());
                                }
                            }
                            else
                            {
                                if (sles.Body.Kind() == SyntaxKind.SimpleMemberAccessExpression)
                                {
                                    if (methodNode.MethodArguments == null)
                                    {
                                        methodNode.MethodArguments = new List <String>();
                                    }
                                    methodNode.MethodArguments.Add((sles.Body as MemberAccessExpressionSyntax).Name.ToString());
                                }
                                else
                                {
                                    if (methodNode.MethodArguments == null)
                                    {
                                        methodNode.MethodArguments = new List <String>();
                                    }
                                    methodNode.MethodArguments.Add(sles.Body.ToString());
                                }
                            }
                        }
                        else
                        {
                            if (methodNode.MethodArguments == null)
                            {
                                methodNode.MethodArguments = new List <String>();
                            }
                            methodNode.MethodArguments.Add(argument.Expression.ToString());
                        }
                    }
                }
            }
            return(entityNode);
        }
Ejemplo n.º 7
0
        public static string InvocationExpressionRootName(this ExpressionSyntax invocation, string[] classNames)
        {
            if (invocation == null)
            {
                return("");
            }
            if (invocation is IdentifierNameSyntax)
            {
                if ((invocation as IdentifierNameSyntax).Identifier == null)
                {
                    return("");
                }
                return((invocation as IdentifierNameSyntax).Identifier.ValueText);
            }
            foreach (SyntaxNode invchnd in invocation.ChildNodes())
            {
                if (invchnd.Kind() == SyntaxKind.SimpleMemberAccessExpression)
                {
                    ExpressionSyntax exst = (invchnd as MemberAccessExpressionSyntax).Expression;
                    if (exst != null)
                    {
                        if (exst is IdentifierNameSyntax)
                        {
                            if ((exst as IdentifierNameSyntax).Identifier == null)
                            {
                                return("");
                            }
                            if (classNames != null)
                            {
                                if (classNames.Count() > 0)
                                {
                                    SimpleNameSyntax aname = (invchnd as MemberAccessExpressionSyntax).Name;
                                    if (aname.Kind() == SyntaxKind.GenericName)
                                    {
                                        if (!"Entity".Equals(aname.Identifier.ValueText))
                                        {
                                            return("");
                                        }

                                        TypeArgumentListSyntax typeArgumentList = (aname as GenericNameSyntax).TypeArgumentList;
                                        if (typeArgumentList != null)
                                        {
                                            if (typeArgumentList.Arguments != null)
                                            {
                                                string locClassName = "";
                                                foreach (TypeSyntax ts in typeArgumentList.Arguments)
                                                {
                                                    locClassName = ts.ToString();
                                                }
                                                if (classNames.Any(i => string.Equals(i, locClassName)))
                                                {
                                                    return((exst as IdentifierNameSyntax).Identifier.ValueText);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            return("");
                        }
                        else
                        {
                            return(exst.InvocationExpressionRootName(classNames));
                        }
                    }
                }
            }
            return("");
        }