protected override (bool isResult, Declaration properties) IsResultDeclarationWithAdditionalProperties(Declaration declaration, DeclarationFinder finder)
        {
            if (!(declaration is UnboundMemberDeclaration member))
            {
                return(false, null);
            }

            var callingContext = member.CallingContext is VBAParser.NewExprContext newExprContext
                ? (newExprContext.expression() as VBAParser.LExprContext)?.lExpression()
                : member.CallingContext;

            if (callingContext == null)
            {
                return(false, null);
            }

            var callingContextSelection = new QualifiedSelection(declaration.QualifiedModuleName, callingContext.GetSelection());
            var usageReferences         = finder.IdentifierReferences(callingContextSelection);
            var calledDeclaration       = usageReferences
                                          .Select(reference => reference.Declaration)
                                          .FirstOrDefault(usageDeclaration => usageDeclaration != null &&
                                                          HasResultType(usageDeclaration));
            var isResult = calledDeclaration != null &&
                           calledDeclaration.DeclarationType != DeclarationType.Control;    //TODO - remove this exception after resolving #2592. Also simplify to inspect the type directly.

            return(isResult, calledDeclaration?.AsTypeDeclaration);
        }
Exemplo n.º 2
0
        private (IdentifierReference identifierReference, bool isUnbound) RhsImplicitDefaultMemberAccess(IdentifierReference assignment, DeclarationFinder finder)
        {
            if (!(assignment.Context.Parent is VBAParser.LetStmtContext letStatement))
            {
                return(null, false);
            }

            var rhsSelection = new QualifiedSelection(assignment.QualifiedModuleName, letStatement.expression().GetSelection());

            var boundRhsDefaultMemberAccess = finder.IdentifierReferences(rhsSelection)
                                              .FirstOrDefault(reference => reference.IsNonIndexedDefaultMemberAccess &&
                                                              !reference.IsInnerRecursiveDefaultMemberAccess);

            if (boundRhsDefaultMemberAccess != null)
            {
                return(boundRhsDefaultMemberAccess, false);
            }

            var unboundRhsDefaultMemberAccess = finder.UnboundDefaultMemberAccesses(rhsSelection.QualifiedName)
                                                .FirstOrDefault(reference => reference.IsNonIndexedDefaultMemberAccess &&
                                                                !reference.IsInnerRecursiveDefaultMemberAccess &&
                                                                reference.Selection.Equals(rhsSelection.Selection));

            return(unboundRhsDefaultMemberAccess, true);
        }
Exemplo n.º 3
0
        protected override IEnumerable <IInspectionResult> DoGetInspectionResults(QualifiedModuleName module, DeclarationFinder finder)
        {
            var userDeclarations     = finder.Members(module).ToList();
            var identifierReferences = finder.IdentifierReferences(module).ToList();
            var annotations          = finder.FindAnnotations(module);

            var unboundAnnotations = UnboundAnnotations(annotations, userDeclarations, identifierReferences)
                                     .Where(annotation => !annotation.Annotation.Target.HasFlag(AnnotationTarget.General) ||
                                            annotation.AnnotatedLine == null);

            var attributeAnnotationsOnDeclarationsNotAllowingAttributes = AttributeAnnotationsOnDeclarationsNotAllowingAttributes(userDeclarations);

            var illegalAnnotations = unboundAnnotations
                                     .Concat(attributeAnnotationsOnDeclarationsNotAllowingAttributes)
                                     .Distinct();

            if (module.ComponentType == ComponentType.Document)
            {
                var attributeAnnotationsInDocuments = AttributeAnnotationsInDocuments(userDeclarations);
                illegalAnnotations = illegalAnnotations
                                     .Concat(attributeAnnotationsInDocuments)
                                     .Distinct();
            }

            return(illegalAnnotations
                   .Select(InspectionResult)
                   .ToList());
        }
Exemplo n.º 4
0
        private (Declaration declaration, bool mightHaveSetType) SetTypeDeterminingDeclarationOfExpression(VBAParser.UnrestrictedIdentifierContext identifier, QualifiedModuleName containingModule, DeclarationFinder finder)
        {
            var declaration = finder.IdentifierReferences(identifier, containingModule)
                              .Select(reference => reference.Declaration)
                              .LastOrDefault();

            return(declaration, MightHaveSetType(declaration));
        }
Exemplo n.º 5
0
        private (Declaration declaration, bool mightHaveSetType) SetTypeDeterminingDeclarationOfExpression(VBAParser.DictionaryAccessContext dictionaryAccess, QualifiedModuleName containingModule, DeclarationFinder finder)
        {
            var qualifiedSelection = new QualifiedSelection(containingModule, dictionaryAccess.GetSelection());
            var declaration        = finder.IdentifierReferences(qualifiedSelection)
                                     .Select(reference => reference.Declaration)
                                     .LastOrDefault();

            return(declaration, MightHaveSetType(declaration));
        }
Exemplo n.º 6
0
        private static IdentifierReference AssignmentTarget(IdentifierReference reference, DeclarationFinder finder, ITree setter)
        {
            var assignedTo = finder.IdentifierReferences(reference.QualifiedModuleName)
                             .SingleOrDefault(assign =>
                                              assign.IsAssignment &&
                                              (assign.Context.GetAncestor <VBAParser.SetStmtContext>()?.Equals(setter) ?? false));

            return(assignedTo);
        }
        private IEnumerable <IInspectionResult> ReferenceResults(QualifiedModuleName module, DeclarationFinder finder)
        {
            var objectionableReferences = finder.IdentifierReferences(module)
                                          .Where(reference => reference?.Declaration != null &&
                                                 reference.Declaration.IsUserDefined &&
                                                 reference.HasTypeHint());

            return(objectionableReferences
                   .Select(reference => InspectionResult(reference, finder)));
        }
        protected sealed override IEnumerable <IInspectionResult> DoGetInspectionResults(QualifiedModuleName module, DeclarationFinder finder)
        {
            var annotations          = finder.FindAnnotations(module);
            var userDeclarations     = finder.Members(module).ToList();
            var identifierReferences = finder.IdentifierReferences(module).ToList();

            var invalidAnnotations = GetInvalidAnnotations(annotations, userDeclarations, identifierReferences);

            return(invalidAnnotations.Select(InspectionResult).ToList());
        }
Exemplo n.º 9
0
        //Please note that the lExpression is the (whitespace) index expression itself and not the lExpression it contains.
        private Declaration ResolveIndexExpressionAsArrayAccess(VBAParser.LExpressionContext actualIndexExpr, QualifiedModuleName containingModule, DeclarationFinder finder)
        {
            // An array access references the entire (whitespace)indexExpr.
            var qualifiedSelection = new QualifiedSelection(containingModule, actualIndexExpr.GetSelection());

            return(finder
                   .IdentifierReferences(qualifiedSelection)
                   .LastOrDefault(reference => reference.IsArrayAccess)
                   ?.Declaration);
        }
        private string DefaultMemberAccessCode(QualifiedSelection selection, DeclarationFinder finder)
        {
            var defaultMemberAccesses = finder.IdentifierReferences(selection).Where(reference => reference.DefaultMemberRecursionDepth > 0);
            var defaultMemberNames    = defaultMemberAccesses.Select(DefaultMemberName)
                                        .Select(declarationName => IsNotLegalIdentifierName(declarationName)
                                            ? $"[{declarationName}]"
                                            : declarationName);

            return($".{string.Join("().", defaultMemberNames)}");
        }
Exemplo n.º 11
0
        private IEnumerable <IInspectionResult> BoundInspectionResults(QualifiedModuleName module, DeclarationFinder finder)
        {
            var objectionableReferences = finder
                                          .IdentifierReferences(module)
                                          .Where(IsResultReference);

            return(objectionableReferences
                   .Select(reference => BoundInspectionResult(reference, finder))
                   .ToList());
        }
Exemplo n.º 12
0
        private Declaration ResolveIndexExpressionAsDefaultMemberAccess(VBAParser.LExpressionContext lExpressionOfIndexExpression, QualifiedModuleName containingModule, DeclarationFinder finder)
        {
            // A default member access references the entire lExpression.
            // If there are multiple, the references are ordered by recursion depth.
            var qualifiedSelection = new QualifiedSelection(containingModule, lExpressionOfIndexExpression.GetSelection());

            return(finder
                   .IdentifierReferences(qualifiedSelection)
                   .LastOrDefault(reference => reference.IsDefaultMemberAccess)
                   ?.Declaration);
        }
        private static Declaration SelectedDeclarationViaReference(QualifiedSelection qualifiedSelection, DeclarationFinder finder)
        {
            var referencesInModule = finder.IdentifierReferences(qualifiedSelection.QualifiedName);

            return(referencesInModule
                   .Where(reference => reference.IsSelected(qualifiedSelection))
                   .Select(reference => reference.Declaration)
                   .OrderByDescending(declaration => declaration.DeclarationType)
                   // they're sorted by type, so a local comes before the procedure it's in
                   .FirstOrDefault());
        }
Exemplo n.º 14
0
        private static IEnumerable <IdentifierReference> PossiblyObjectNonSetAssignments(QualifiedModuleName module, DeclarationFinder finder)
        {
            var assignments = finder.IdentifierReferences(module)
                              .Where(reference => reference.IsAssignment &&
                                     !reference.IsSetAssignment &&
                                     (reference.IsNonIndexedDefaultMemberAccess ||
                                      Tokens.Variant.Equals(reference.Declaration.AsTypeName, StringComparison.InvariantCultureIgnoreCase)));
            var unboundAssignments = finder.UnboundDefaultMemberAccesses(module)
                                     .Where(reference => reference.IsAssignment);

            return(assignments.Concat(unboundAssignments));
        }
Exemplo n.º 15
0
        private IEnumerable <IInspectionResult> BoundLhsInspectionResults(QualifiedModuleName module, DeclarationFinder finder)
        {
            var implicitDefaultMemberAssignments = finder
                                                   .IdentifierReferences(module)
                                                   .Where(IsImplicitDefaultMemberAssignment)
                                                   .ToList();

            var results = new List <IInspectionResult>();

            foreach (var assignment in implicitDefaultMemberAssignments)
            {
                var(rhsDefaultMemberAccess, isUnbound) = RhsImplicitDefaultMemberAccess(assignment, finder);

                if (rhsDefaultMemberAccess != null)
                {
                    var result = InspectionResult(assignment, rhsDefaultMemberAccess, isUnbound, finder);
                    results.Add(result);
                }
            }

            return(results);
        }
Exemplo n.º 16
0
 protected virtual IEnumerable <IdentifierReference> ReferencesInModule(QualifiedModuleName module, DeclarationFinder finder)
 {
     return(finder.IdentifierReferences(module));
 }