/// <summary>Common method for adding a reference to given declaration item</summary>
        private static void AddReference(Declaration itemToAdd, IdentifierReference reference)
        {
            var declaration = _declarations.Items.ToList().FirstOrDefault(x => x.Equals(itemToAdd));
            if (declaration == null) return;

            declaration.AddReference(reference);
        }
        public void Resolve(VBAParser.AsTypeClauseContext context)
        {
            var asType = context.type();

            if (asType == null)
            {
                return;
            }

            Declaration         type      = null;
            IdentifierReference reference = null;

            var baseType = asType.baseType();

            if (baseType != null)
            {
                var collection = baseType.COLLECTION();
                if (collection != null)
                {
                    type      = _declarations[collection.GetText()].SingleOrDefault(item => item.IsBuiltIn && item.DeclarationType == DeclarationType.Class);
                    reference = CreateReference(baseType, type);
                }
            }
            else
            {
                type      = ResolveType(asType.complexType());
                reference = CreateReference(asType.complexType(), type);
            }

            if (type != null)
            {
                type.AddReference(reference);
                _alreadyResolved.Add(reference.Context);
            }
        }
        private Declaration ParentDeclaration(IdentifierReference reference)
        {
            var declarationTypes = new[] {DeclarationType.Function, DeclarationType.Procedure, DeclarationType.Property};

            return UserDeclarations.SingleOrDefault(d =>
                        reference.ParentScoping.Equals(d) && declarationTypes.Contains(d.DeclarationType) &&
                        d.QualifiedName.QualifiedModuleName.Equals(reference.QualifiedModuleName));
        }
Ejemplo n.º 4
0
        //public void AddReference(IdentifierReference reference)
        //{
        //    if (reference == null || reference.Declaration.Context == reference.Context)
        //    {
        //        return;
        //    }
        //    if (reference.Context.Parent != _context
        //        && !_references.Select(r => r.Context).Contains(reference.Context.Parent)
        //        && !_references.Any(r => r.QualifiedModuleName == reference.QualifiedModuleName
        //            && r.Selection.StartLine == reference.Selection.StartLine
        //            && r.Selection.EndLine == reference.Selection.EndLine
        //            && r.Selection.StartColumn == reference.Selection.StartColumn
        //            && r.Selection.EndColumn == reference.Selection.EndColumn))
        //    {
        //        _references.Add(reference);
        //    }
        //}

        public void AddMemberCall(IdentifierReference reference)
        {
            if (reference == null || reference.Declaration == null || reference.Declaration.Context == reference.Context)
            {
                return;
            }

            _memberCalls.Add(reference);
        }
 public ObjectVariableNotSetInspectionResult(IInspection inspection, IdentifierReference reference)
     :base(inspection, reference.QualifiedModuleName, reference.Context)
 {
     _reference = reference;
     _quickFixes = new CodeInspectionQuickFix[]
     {
         new SetObjectVariableQuickFix(_reference), 
         new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName),
     };
 }
Ejemplo n.º 6
0
        public void AddReference(
            QualifiedModuleName module,
            Declaration scope,
            Declaration parent,
            ParserRuleContext callSiteContext,
            string identifier,
            Declaration callee,
            Selection selection,
            IEnumerable <IParseTreeAnnotation> annotations,
            bool isAssignmentTarget                  = false,
            bool hasExplicitLetStatement             = false,
            bool isSetAssigned                       = false,
            bool isIndexedDefaultMemberAccess        = false,
            bool isNonIndexedDefaultMemberAccess     = false,
            int defaultMemberRecursionDepth          = 0,
            bool isArrayAccess                       = false,
            bool isProcedureCoercion                 = false,
            bool isInnerRecursiveDefaultMemberAccess = false
            )
        {
            var oldReference = _references.FirstOrDefault(r =>
                                                          r.Key.QualifiedModuleName == module &&
                                                          // ReSharper disable once PossibleUnintendedReferenceComparison
                                                          r.Key.ParentScoping == scope &&
                                                          // ReSharper disable once PossibleUnintendedReferenceComparison
                                                          r.Key.ParentNonScoping == parent &&
                                                          r.Key.IdentifierName == identifier &&
                                                          r.Key.Selection == selection);

            if (oldReference.Key != null)
            {
                _references.TryRemove(oldReference.Key, out _);
            }

            var newReference = new IdentifierReference(
                module,
                scope,
                parent,
                identifier,
                selection,
                callSiteContext,
                callee,
                isAssignmentTarget,
                hasExplicitLetStatement,
                annotations,
                isSetAssigned,
                isIndexedDefaultMemberAccess,
                isNonIndexedDefaultMemberAccess,
                defaultMemberRecursionDepth,
                isArrayAccess,
                isProcedureCoercion,
                isInnerRecursiveDefaultMemberAccess);

            _references.AddOrUpdate(newReference, 1, (key, value) => 1);
        }
        public NavigateCodeEventArgs(IdentifierReference reference)
        {
            if (reference == null)
            {
                return;
            }

            _reference = reference;
            _qualifiedName = reference.QualifiedModuleName;
            _selection = reference.Selection;
        }
        public override void EnterVsAssign(VBAParser.VsAssignContext context)
        {
            /* named parameter syntax */

            // one of these is null...
            var callStatementA = context.Parent.Parent.Parent as VBAParser.ICS_S_ProcedureOrArrayCallContext;
            var callStatementB = context.Parent.Parent.Parent as VBAParser.ICS_S_VariableOrProcedureCallContext;
            var callStatementC = context.Parent.Parent.Parent as VBAParser.ICS_B_MemberProcedureCallContext;
            var callStatementD = context.Parent.Parent.Parent as VBAParser.ICS_B_ProcedureCallContext;

            var procedureName = string.Empty;
            ParserRuleContext identifierContext = null;

            if (callStatementA != null)
            {
                procedureName     = callStatementA.ambiguousIdentifier().GetText();
                identifierContext = callStatementA.ambiguousIdentifier();
            }
            else if (callStatementB != null)
            {
                procedureName     = callStatementB.ambiguousIdentifier().GetText();
                identifierContext = callStatementB.ambiguousIdentifier();
            }
            else if (callStatementC != null)
            {
                procedureName     = callStatementC.ambiguousIdentifier().GetText();
                identifierContext = callStatementC.ambiguousIdentifier();
            }
            else if (callStatementD != null)
            {
                procedureName     = callStatementD.certainIdentifier().GetText();
                identifierContext = callStatementD.certainIdentifier();
            }

            var procedure = FindProcedureDeclaration(procedureName, identifierContext);

            if (procedure == null)
            {
                return;
            }

            var call = context.implicitCallStmt_InStmt();
            var arg  = Resolve(call.iCS_S_VariableOrProcedureCall())
                       ?? Resolve(call.iCS_S_ProcedureOrArrayCall())
                       ?? Resolve(call.iCS_S_DictionaryCall())
                       ?? Resolve(call.iCS_S_MembersCall());

            if (arg != null)
            {
                var reference = new IdentifierReference(_qualifiedName, arg.IdentifierName, context.GetSelection(), context, arg);
                arg.AddReference(reference);
            }
        }
Ejemplo n.º 9
0
        public void AddReference(IdentifierReference reference)
        {
            if (reference == null || reference.Declaration.Context == reference.Context)
            {
                return;
            }

            if (reference.Context.Parent != _context 
                && !_references.Select(r => r.Context).Contains(reference.Context.Parent)
                && !_references.Any(r => r.QualifiedModuleName == reference.QualifiedModuleName 
                    && r.Selection.StartLine == reference.Selection.StartLine
                    && r.Selection.EndLine == reference.Selection.EndLine
                    && r.Selection.StartColumn == reference.Selection.StartColumn
                    && r.Selection.EndColumn == reference.Selection.EndColumn))
            {
                _references.Add(reference);
            }
        }
Ejemplo n.º 10
0
        public void AddReference(IdentifierReference reference)
        {
            if (reference == null || reference.Declaration.Context == reference.Context)
            {
                return;
            }

            if (reference.Context.Parent != _context &&
                !_references.Select(r => r.Context).Contains(reference.Context.Parent) &&
                !_references.Any(r => r.QualifiedModuleName == reference.QualifiedModuleName &&
                                 r.Selection.StartLine == reference.Selection.StartLine &&
                                 r.Selection.EndLine == reference.Selection.EndLine &&
                                 r.Selection.StartColumn == reference.Selection.StartColumn &&
                                 r.Selection.EndColumn == reference.Selection.EndColumn))
            {
                _references.Add(reference);
            }
        }
        public void EnterWithBlock(VBAParser.WithStmtContext context)
        {
            Declaration         qualifier = null;
            IdentifierReference reference = null;

            if (context.NEW() == null)
            {
                // with block is using an identifier declared elsewhere.
                var callee = ResolveInternal(context.implicitCallStmt_InStmt(), _currentScope, ContextAccessorType.GetValueOrReference);
                qualifier = ResolveType(callee);
            }
            else
            {
                // with block is using an anonymous declaration.
                // i.e. object variable reference is held by the with block itself.
                var typeContext     = context.type();
                var baseTypeContext = typeContext.baseType();
                if (baseTypeContext != null)
                {
                    var collectionContext = baseTypeContext.COLLECTION();
                    if (collectionContext != null)
                    {
                        // object variable is a built-in Collection class instance
                        qualifier = _declarations.Items.Single(item => item.IsBuiltIn &&
                                                               item.IdentifierName == collectionContext.GetText() &&
                                                               item.DeclarationType == DeclarationType.Class);
                        reference = CreateReference(baseTypeContext, qualifier);
                    }
                }
                else
                {
                    qualifier = ResolveType(typeContext.complexType());
                }
            }

            if (qualifier != null && reference != null)
            {
                qualifier.AddReference(reference);
                _alreadyResolved.Add(reference.Context);
            }
            _withBlockQualifiers.Push(qualifier); // note: pushes null if unresolved
        }
        private bool EnterIdentifier(ParserRuleContext context, Selection selection, bool isAssignmentTarget = false, bool hasExplicitLetStatement = false, DeclarationType accessorType = DeclarationType.PropertyGet)
        {
            var name    = context.GetText();
            var matches = _declarations[name].Where(IsInScope);

            var declaration = GetClosestScopeDeclaration(matches, context, accessorType);

            if (declaration != null)
            {
                var reference = new IdentifierReference(_qualifiedName, name, selection, context, declaration, isAssignmentTarget, hasExplicitLetStatement);

                if (!declaration.References.Select(r => r.Context).Contains(reference.Context))
                {
                    declaration.AddReference(reference);
                    return(true);
                }
                // note: non-matching names are not necessarily undeclared identifiers, e.g. "String" in "Dim foo As String".
            }

            return(false);
        }
        public override void EnterVsNew(VBAParser.VsNewContext context)
        {
            _skipIdentifiers = true;
            var identifiers = context.valueStmt().GetRuleContexts <VBAParser.ImplicitCallStmt_InStmtContext>();

            var lastIdentifier = identifiers.Last();
            var name           = lastIdentifier.GetText();

            var matches = _declarations[name].Where(d => d.DeclarationType == DeclarationType.Class).ToList();
            var result  = matches.Count <= 1
                ? matches.SingleOrDefault()
                : GetClosestScopeDeclaration(matches, context, DeclarationType.Class);

            if (result == null)
            {
                return;
            }

            var reference = new IdentifierReference(_qualifiedName, result.IdentifierName, lastIdentifier.GetSelection(), context, result);

            result.AddReference(reference);
        }
Ejemplo n.º 14
0
 private static bool IsSelectedReference(QualifiedSelection selection, IdentifierReference reference)
 {
     return reference.QualifiedModuleName.Equals(selection.QualifiedName)
            && reference.Selection.ContainsFirstCharacter(selection.Selection);
 }
Ejemplo n.º 15
0
        public void AcquireTarget_MethodRenamingMoreComponents_CorrectTargetChosen()
        {
            // arange
            // initial selection
            var symbolSelection = new Selection(4, 5, 4, 8);
            var selectedComponent = new QualifiedModuleName("TestProject", "Module1");
            var qualifiedSelection = new QualifiedSelection(selectedComponent, symbolSelection);

            // just for passing null reference exception            
            var context = new Mock<ParserRuleContext>();
            context.SetupGet(c => c.Start.Line).Returns(-1);
            context.SetupGet(c => c.Stop.Line).Returns(-1);
            context.SetupGet(c => c.Stop.Text).Returns("Fake");

            // simulate all the components and symbols   
            IdentifierReference reference;
            var differentComponent = new QualifiedModuleName("TestProject", "Module2");
            var differentMember = new QualifiedMemberName(differentComponent, "Module2");
            AddDeclarationItem(context, new Selection(4, 9, 4, 16), differentMember, DeclarationType.Variable, "FooTest");

            // add references to the Foo declaration item to simulate prod usage
            AddDeclarationItem(context, new Selection(3, 5, 3, 8), differentMember, DeclarationType.Procedure, "Foo");
            var declarationItem = _listDeclarations[_listDeclarations.Count - 1];
            reference = new IdentifierReference(selectedComponent, "Foo", new Selection(7, 5, 7, 11), context.Object, declarationItem);
            AddReference(declarationItem, reference);
            reference = new IdentifierReference(selectedComponent, "Foo", symbolSelection, context.Object, declarationItem);
            AddReference(declarationItem, reference);

            AddDeclarationItem(context, new Selection(1, 1, 1, 1), differentMember, DeclarationType.Module, "Module2");
            var member = new QualifiedMemberName(selectedComponent, "fakeModule");
            AddDeclarationItem(context, new Selection(7, 5, 7, 11), member, DeclarationType.Procedure, "RunFoo");
            AddDeclarationItem(context, new Selection(3, 5, 3, 9), member, DeclarationType.Procedure, "Main");
            AddDeclarationItem(context, new Selection(1, 1, 1, 1), member, DeclarationType.Module, "Module1");

            _view.Setup(view => view.ShowDialog()).Returns(DialogResult.Cancel);
            _view.SetupProperty(view => view.Target);

            //act
            //var presenter = new RenamePresenter(_vbe.Object, _view.Object, _declarations, qualifiedSelection);
            //presenter.Show();

            Assert.Inconclusive("This test is broken");

            //assert
            var retVal = _view.Object.Target;
            Assert.AreEqual("Foo", retVal.IdentifierName, "Selected the correct symbol name");
            Assert.AreEqual(declarationItem.References.Count(), retVal.References.Count());
        }
 public static void OnNavigateIdentifierReference(VBE vbe, IdentifierReference reference)
 {
     vbe.SetSelection(reference.QualifiedModuleName.Project, reference.Selection, reference.QualifiedModuleName.Component.Name);
 }
 public IdentifierReferenceListItem(IdentifierReference reference)
 {
     _reference = reference;
 }
 private bool IsAddressOfCall(IdentifierReference usage)
 {
     RuleContext current = usage.Context;
     while (current != null && !(current is VBAParser.VsAddressOfContext)) current = current.Parent;
     return current != null;
 }
Ejemplo n.º 19
0
 public void AddReference(IdentifierReference reference)
 {
     _references.Add(reference);
 }
 private bool IsCallWithoutAssignment(IdentifierReference usage)
 {
     return usage.Context.Parent != null && usage.Context.Parent.Parent is VBAParser.ImplicitCallStmt_InBlockContext;
 }
 private bool IsReturnStatement(Declaration function, IdentifierReference assignment)
 {
     return assignment.ParentScoping.Equals(function);
 }
 public SetObjectVariableQuickFix(IdentifierReference reference)
     : base(context: reference.Context.Parent.Parent as ParserRuleContext, // ImplicitCallStmt_InStmtContext 
            selection: new QualifiedSelection(reference.QualifiedModuleName, reference.Selection), 
            description: InspectionsUI.SetObjectVariableQuickFix)
 {
 }
 public IdentifierReferenceListItem(IdentifierReference reference, ICodePaneWrapperFactory wrapperFactory)
 {
     _reference = reference;
     _wrapperFactory = wrapperFactory;
 }