Exemplo n.º 1
0
        /// <summary>
        /// Gets all the declared local variables in the given <paramref name="declarationStatement"/>.
        /// </summary>
        /// <param name="declarationStatement">Variable declaration statement</param>
        public static ImmutableArray <ILocalSymbol> GetDeclaredVariables(this IVariableDeclarationsOperation declarationStatement)
        {
            if (declarationStatement == null)
            {
                throw new ArgumentNullException(nameof(declarationStatement));
            }

            var arrayBuilder = ArrayBuilder <ILocalSymbol> .GetInstance();

            foreach (IVariableDeclarationOperation group in declarationStatement.Declarations)
            {
                foreach (ILocalSymbol symbol in group.Variables)
                {
                    arrayBuilder.Add(symbol);
                }
            }

            return(arrayBuilder.ToImmutableAndFree());
        }
        public sealed override void Initialize(AnalysisContext context)
        {
            context.RegisterOperationBlockStartAction(
                (operationBlockContext) =>
            {
                if (operationBlockContext.OwningSymbol is IMethodSymbol containingMethod)
                {
                    HashSet <ILocalSymbol> mightBecomeConstLocals = new HashSet <ILocalSymbol>();
                    HashSet <ILocalSymbol> assignedToLocals       = new HashSet <ILocalSymbol>();

                    operationBlockContext.RegisterOperationAction(
                        (operationContext) =>
                    {
                        if (operationContext.Operation is IAssignmentOperation assignment)
                        {
                            AssignTo(assignment.Target, assignedToLocals, mightBecomeConstLocals);
                        }
                        else if (operationContext.Operation is IIncrementOrDecrementOperation increment)
                        {
                            AssignTo(increment.Target, assignedToLocals, mightBecomeConstLocals);
                        }
                        else
                        {
                            throw TestExceptionUtilities.UnexpectedValue(operationContext.Operation);
                        }
                    },
                        OperationKind.SimpleAssignment,
                        OperationKind.CompoundAssignment,
                        OperationKind.Increment);

                    operationBlockContext.RegisterOperationAction(
                        (operationContext) =>
                    {
                        IInvocationOperation invocation = (IInvocationOperation)operationContext.Operation;
                        foreach (IArgumentOperation argument in invocation.Arguments)
                        {
                            if (argument.Parameter.RefKind == RefKind.Out || argument.Parameter.RefKind == RefKind.Ref)
                            {
                                AssignTo(argument.Value, assignedToLocals, mightBecomeConstLocals);
                            }
                        }
                    },
                        OperationKind.Invocation);

                    operationBlockContext.RegisterOperationAction(
                        (operationContext) =>
                    {
                        IVariableDeclarationsOperation declaration = (IVariableDeclarationsOperation)operationContext.Operation;
                        foreach (IVariableDeclarationOperation variable in declaration.Declarations)
                        {
                            foreach (ILocalSymbol local in variable.Variables)
                            {
                                if (!local.IsConst && !assignedToLocals.Contains(local))
                                {
                                    var localType = local.Type;
                                    if ((!localType.IsReferenceType || localType.SpecialType == SpecialType.System_String) && localType.SpecialType != SpecialType.None)
                                    {
                                        if (variable.Initializer != null && variable.Initializer.Value.ConstantValue.HasValue)
                                        {
                                            mightBecomeConstLocals.Add(local);
                                        }
                                    }
                                }
                            }
                        }
                    },
                        OperationKind.VariableDeclarations);

                    operationBlockContext.RegisterOperationBlockEndAction(
                        (operationBlockEndContext) =>
                    {
                        foreach (ILocalSymbol couldBeConstLocal in mightBecomeConstLocals)
                        {
                            Report(operationBlockEndContext, couldBeConstLocal, LocalCouldBeConstDescriptor);
                        }
                    });
                }
            });
        }
 public override IOperation VisitVariableDeclarations(IVariableDeclarationsOperation operation, object argument)
 {
     return(new VariableDeclarationStatement(VisitArray(operation.Declarations), ((Operation)operation).SemanticModel, operation.Syntax, operation.Type, operation.ConstantValue, operation.IsImplicit));
 }
 public override void VisitVariableDeclarations(IVariableDeclarationsOperation operation)
 {
     base.VisitVariableDeclarations(operation);
 }
Exemplo n.º 5
0
 public virtual void VisitVariableDeclarations(IVariableDeclarationsOperation operation)
 {
     DefaultVisit(operation);
 }