Esempio n. 1
0
            void CollectIssues(AstNode variableDecl, AstNode rootNode, LocalResolveResult resolveResult)
            {
                if (resolveResult == null)
                {
                    return;
                }
                var type    = resolveResult.Type;
                var typeDef = type.GetDefinition();

                if (typeDef == null ||
                    (typeDef.KnownTypeCode != KnownTypeCode.IEnumerable &&
                     typeDef.KnownTypeCode != KnownTypeCode.IEnumerableOfT))
                {
                    return;
                }

                FindReferences(variableDecl, rootNode, resolveResult.Variable);

                var statements = AnalysisStatementCollector.Collect(variableDecl);
                var builder    = new VariableReferenceGraphBuilder(ctx);

                foreach (var statement in statements)
                {
                    var vrNode = builder.Build(statement, references, refStatements, ctx);
                    FindMultipleEnumeration(vrNode);
                }
                foreach (var lambda in lambdaExpressions)
                {
                    var vrNode = builder.Build(references, ctx.Resolver, (Expression)lambda.Body);
                    FindMultipleEnumeration(vrNode);
                }
            }
Esempio n. 2
0
 public CfgVariableReferenceNodeBuilder(VariableReferenceGraphBuilder variableReferenceGraphBuilder)
 {
     this.variableReferenceGraphBuilder = variableReferenceGraphBuilder;
 }
            void CollectIssues(AstNode variableDecl, BlockStatement rootStatement, LocalResolveResult resolveResult)
            {
                if (rootStatement == null || resolveResult == null)
                {
                    return;
                }

                var references    = new HashSet <AstNode>();
                var refStatements = new HashSet <Statement>();
                var usedInLambda  = false;
                var results       = ctx.FindReferences(rootStatement, resolveResult.Variable);

                foreach (var result in results)
                {
                    var node = result.Node;
                    if (node == variableDecl)
                    {
                        continue;
                    }

                    var parent = node.Parent;
                    while (!(parent == null || parent is Statement || parent is LambdaExpression || parent is QueryExpression))
                    {
                        parent = parent.Parent;
                    }
                    if (parent == null)
                    {
                        continue;
                    }

                    var statement = parent as Statement;
                    if (statement != null)
                    {
                        references.Add(node);
                        refStatements.Add(statement);
                    }

                    while (parent != null && parent != rootStatement)
                    {
                        if (parent is LambdaExpression || parent is AnonymousMethodExpression || parent is QueryExpression)
                        {
                            usedInLambda = true;
                            break;
                        }
                        parent = parent.Parent;
                    }
                    if (usedInLambda)
                    {
                        break;
                    }
                }

                // stop analyzing if the variable is used in any lambda expression or anonymous method
                if (usedInLambda)
                {
                    return;
                }

                var startNode           = new VariableReferenceGraphBuilder(ctx).Build(rootStatement, references, refStatements, ctx);
                var variableInitializer = variableDecl as VariableInitializer;

                if (variableInitializer != null && !variableInitializer.Initializer.IsNull)
                {
                    startNode.References.Insert(0, variableInitializer);
                }

                ProcessNodes(startNode);
            }
Esempio n. 4
0
            void CollectIssues(AstNode variableDecl, BlockStatement rootStatement, LocalResolveResult resolveResult)
            {
                if (rootStatement == null || resolveResult == null)
                {
                    return;
                }
                var references    = new HashSet <AstNode> ();
                var refStatements = new HashSet <Statement> ();
                var usedInLambda  = false;

                refFinder.FindLocalReferences(resolveResult.Variable, ctx.UnresolvedFile, unit, ctx.Compilation,
                                              (astNode, rr) => {
                    if (usedInLambda || astNode == variableDecl)
                    {
                        return;
                    }

                    var parent = astNode.Parent;
                    while (!(parent == null || parent is Statement || parent is LambdaExpression))
                    {
                        parent = parent.Parent;
                    }
                    if (parent == null)
                    {
                        return;
                    }

                    var statement = parent as Statement;
                    if (statement != null)
                    {
                        references.Add(astNode);
                        refStatements.Add(statement);
                    }

                    while (parent != null && parent != rootStatement)
                    {
                        if (parent is LambdaExpression || parent is AnonymousMethodExpression)
                        {
                            usedInLambda = true;
                            break;
                        }
                        parent = parent.Parent;
                    }
                }, ctx.CancellationToken);

                // stop analyzing if the variable is used in any lambda expression or anonymous method
                if (usedInLambda)
                {
                    return;
                }

                var startNode           = VariableReferenceGraphBuilder.Build(rootStatement, references, refStatements, ctx);
                var variableInitializer = variableDecl as VariableInitializer;

                if (variableInitializer != null && !variableInitializer.Initializer.IsNull)
                {
                    startNode.References.Insert(0, variableInitializer);
                }

                ProcessNodes(startNode);
            }