~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/CallToVirtualFunctionFromConstructorIssue.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// CallToVirtualFunctionFromConstructorIssue.cs
 
3
//
 
4
// Author:
 
5
//       Simon Lindgren <simon.n.lindgren@gmail.com>
 
6
//
 
7
// Copyright (c) 2012 Simon Lindgren
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
27
using System.Collections.Generic;
 
28
using ICSharpCode.NRefactory.Semantics;
 
29
 
 
30
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
31
{
 
32
        [IssueDescription("Constructors should not call virtual members",
 
33
                          Description = "Warns about calls to virtual member functions occuring in the constructor.",
 
34
                          Category = IssueCategories.CodeQualityIssues,
 
35
                          Severity = Severity.Warning,
 
36
                      ResharperDisableKeyword = "DoNotCallOverridableMethodsInConstructor")]
 
37
        public class CallToVirtualFunctionFromConstructorIssue : ICodeIssueProvider
 
38
        {
 
39
                public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
 
40
                {
 
41
                        var gv = new GatherVisitor(context);
 
42
                        context.RootNode.AcceptVisitor(gv);
 
43
                        return gv.CallFinder.FoundIssues;
 
44
                }
 
45
                
 
46
                class GatherVisitor : GatherVisitorBase<CallToVirtualFunctionFromConstructorIssue>
 
47
                {
 
48
                        internal readonly VirtualCallFinderVisitor CallFinder;
 
49
 
 
50
                        public GatherVisitor(BaseRefactoringContext context) : base (context)
 
51
                        {
 
52
                                CallFinder = new VirtualCallFinderVisitor(context);
 
53
                        }
 
54
 
 
55
                        bool isSealedType;
 
56
 
 
57
                        public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
 
58
                        {
 
59
                                if (typeDeclaration.ClassType != ClassType.Class && typeDeclaration.ClassType != ClassType.Struct)
 
60
                                        return;
 
61
                                bool oldIsSealedType = isSealedType;
 
62
                                isSealedType = typeDeclaration.Modifiers.HasFlag(Modifiers.Sealed);
 
63
                                base.VisitTypeDeclaration(typeDeclaration);
 
64
                                isSealedType = oldIsSealedType;
 
65
                        }
 
66
 
 
67
                        public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
 
68
                        {
 
69
                                if (isSealedType)
 
70
                                        return;
 
71
                                var body = constructorDeclaration.Body;
 
72
                                if (body == null || body.IsNull)
 
73
                                        return;
 
74
                                body.AcceptVisitor(CallFinder);
 
75
                        }
 
76
 
 
77
                        public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
 
78
                        {
 
79
                                // nothing
 
80
                        }
 
81
 
 
82
                        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
 
83
                        {
 
84
                                // nothing
 
85
                        }
 
86
 
 
87
                        public override void VisitIndexerExpression(IndexerExpression indexerExpression)
 
88
                        {
 
89
                                // nothing
 
90
                        }
 
91
 
 
92
                        public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration)
 
93
                        {
 
94
                                // nothing
 
95
                        }
 
96
 
 
97
                        public override void VisitEventDeclaration(EventDeclaration eventDeclaration)
 
98
                        {
 
99
                                // nothing
 
100
                        }
 
101
 
 
102
                        public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration)
 
103
                        {
 
104
                                // nothing
 
105
                        }
 
106
 
 
107
                        public override void VisitFixedFieldDeclaration(FixedFieldDeclaration fixedFieldDeclaration)
 
108
                        {
 
109
                                // nothing
 
110
                        }
 
111
                }
 
112
 
 
113
                class VirtualCallFinderVisitor: GatherVisitorBase<CallToVirtualFunctionFromConstructorIssue>
 
114
                {
 
115
                        readonly BaseRefactoringContext context;
 
116
 
 
117
                        public VirtualCallFinderVisitor(BaseRefactoringContext context) : base(context)
 
118
                        {
 
119
                                this.context = context;
 
120
                        }
 
121
 
 
122
                        public override void VisitInvocationExpression(InvocationExpression invocationExpression)
 
123
                        {
 
124
                                base.VisitInvocationExpression(invocationExpression);
 
125
                                if (!IsCallDependentOnCurrentInstance(invocationExpression))
 
126
                                        // Call within current class scope without 'this' or 'base'
 
127
                                        return;
 
128
                                var targetMethod = context.Resolve(invocationExpression) as InvocationResolveResult;
 
129
                                if (targetMethod == null)
 
130
                                        return;
 
131
                                if (targetMethod.IsVirtualCall) {
 
132
                                        AddIssue(invocationExpression,
 
133
                                                 context.TranslateString("Constructors should not call virtual members"));
 
134
                                }
 
135
                        }
 
136
 
 
137
                        bool IsCallDependentOnCurrentInstance(InvocationExpression invocationExpression)
 
138
                        {
 
139
                                if (invocationExpression.Target is IdentifierExpression)
 
140
                                        // Call within current class scope without 'this' or 'base'
 
141
                                        return true;
 
142
                                var expression = invocationExpression.Target as MemberReferenceExpression;
 
143
                                if (expression == null || expression.Target is ThisReferenceExpression)
 
144
                                        // Call within current class scope using 'this' or 'base'
 
145
                                        return true;
 
146
                                return false;
 
147
                        }
 
148
                        
 
149
                        public override void VisitLambdaExpression(LambdaExpression lambdaExpression)
 
150
                        {
 
151
                                // ignore lambdas
 
152
                        }
 
153
                        
 
154
                        public override void VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression)
 
155
                        {
 
156
                                // ignore anonymous methods
 
157
                        }
 
158
                }
 
159
        }
 
160
}
 
161