Exemplo n.º 1
0
        /// <summary>
        /// Override the analysis of invocation expressions to verify that when a variable is dereferenced
        /// it has the appropriate annotated type.  This allows us to present an error when a possibly
        /// null value is unsafely dereferenced.  We still want to exercise the functionality present in the
        /// base class, so we conclude by calling the method which we are overridding here.
        /// </summary>
        /// <param name="context">The analysis context</param>
        /// <param name="invocationExpr">The invocation expression</param>
        internal override void AnalyzeInvocationExpr(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocationExpr)
        {
            //We want to present a warning when we are dereferencing variable which may be null
            if (invocationExpr.Expression is MemberAccessExpressionSyntax memAccessExpr)
            {
                var identifierNameExpr = memAccessExpr.Expression;
                var symbol             = context.SemanticModel.GetSymbolInfo(memAccessExpr.Expression).Symbol;
                if (symbol != null)
                {
                    var dereferencedAttrs = symbol.GetAttributes();
                    var filteredAttrs     = ASTUtil.GetSharpCheckerAttributeStrings(dereferencedAttrs);
                    if (!ASTUtil.AnnotationDictionary.ContainsKey(identifierNameExpr) && filteredAttrs.Count() > 0)
                    {
                        ASTUtil.AnnotationDictionary.TryAdd(identifierNameExpr, new List <List <String> >()
                        {
                            filteredAttrs
                        });
                    }
                }
            }

            //Now perform the normal collection of attributes
            base.AnalyzeInvocationExpr(context, invocationExpr);
        }