Merge pull request #3213 from henricm/fix-for-win-securestring-to-bstr
[mono.git] / mcs / tools / mdoc / Mono.Documentation / exceptions.cs
1 //
2 // Mono.Documentation/exceptions.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2008 Novell, Inc.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.Linq;
32
33 using Mono.Cecil;
34 using Mono.Cecil.Cil;
35
36 namespace Mono.Documentation {
37
38         [Flags]
39         public enum ExceptionLocations {
40                 Member              = 0x0,
41                 Assembly            = 0x1,
42                 DependentAssemblies = 0x2,
43                 AddedMembers        = 0x4,
44         }
45          
46         public class ExceptionSources {
47                 internal ExceptionSources (TypeReference exception)
48                 {
49                         Exception = exception;
50                         Sources   = new HashSet<MemberReference> ();
51                 }
52
53                 public TypeReference Exception { get; private set; }
54                 internal  HashSet<MemberReference>  Sources;
55         }
56
57
58         public class ExceptionLookup {
59
60                 SlashDocMemberFormatter xdoc = new SlashDocMemberFormatter ();
61
62                 // xdoc(MemberRef) -> xdoc(TypeRef) -> ExceptionSource
63                 //   where ExceptionSource.Exception == xdoc(TypeRef)
64                 Dictionary<string, Dictionary<string, ExceptionSources>> db = new Dictionary<string, Dictionary<string, ExceptionSources>> ();
65
66                 ExceptionLocations locations;
67
68                 public ExceptionLookup (ExceptionLocations locations)
69                 {
70                         this.locations = locations;
71                 }
72
73                 public IEnumerable<ExceptionSources> this [MemberReference member] {
74                         get {
75                                 if (member == null)
76                                         throw new ArgumentNullException ("member");
77
78                                 var memberDef = member.Resolve ();
79                                 if (memberDef == null) {
80                                         ArrayType array = member.DeclaringType as ArrayType;
81                                         if (array != null && array.Rank > 1) {
82                                                 // Multi-dimensional array; the member is runtime generated, 
83                                                 // doesn't "really" exist (in a form that we can resolve),
84                                                 // so we can't do anything further.
85                                                 return new ExceptionSources[0];
86                                         }
87                                         throw new NotSupportedException (string.Format (
88                                                                 "Unable to resolve member {0}::{1}.",
89                                                                 member.DeclaringType.FullName, member.Name));
90                                 }
91                                 string memberDecl = xdoc.GetDeclaration (member);
92                                 Dictionary<string, ExceptionSources> e;
93                                 if (!db.TryGetValue (memberDecl, out e)) {
94                                         e = new Dictionary<string, ExceptionSources> ();
95                                         db.Add (memberDecl, e);
96                                         var bodies = GetMethodBodies (member);
97                                         foreach (var body in bodies) {
98                                                 if (body == null)
99                                                         continue;
100                                                 FillExceptions (body, e);
101                                         }
102                                 }
103                                 return e.Values;
104                         }
105                 }
106
107                 MethodBody[] GetMethodBodies (MemberReference member)
108                 {
109                         if (member is MethodReference) {
110                                 return new[]{ (((MethodReference) member).Resolve ()).Body };
111                         }
112                         if (member is PropertyReference) {
113                                 PropertyDefinition prop = (PropertyDefinition) member;
114                                 return new[]{
115                                         prop.GetMethod != null ? prop.GetMethod.Body : null,
116                                         prop.SetMethod != null ? prop.SetMethod.Body : null,
117                                 };
118                         }
119                         if (member is FieldReference)
120                                 return new MethodBody[]{};
121                         if (member is EventReference) {
122                                 EventDefinition ev = (EventDefinition) member;
123                                 return new[]{
124                                         ev.AddMethod != null ? ev.AddMethod.Body : null,
125                                         ev.InvokeMethod != null ? ev.InvokeMethod.Body : null, 
126                                         ev.RemoveMethod != null ? ev.RemoveMethod.Body : null,
127                                 };
128                         }
129                         throw new NotSupportedException ("Unsupported member type: " + member.GetType().FullName);
130                 }
131
132                 void FillExceptions (MethodBody body, Dictionary<string, ExceptionSources> exceptions)
133                 {
134                         for (int i = 0; i < body.Instructions.Count; ++i) {
135                                 Instruction instruction = body.Instructions [i];
136                                 switch (instruction.OpCode.Code) {
137                                         case Code.Call:
138                                         case Code.Callvirt: {
139                                                 if ((locations & ExceptionLocations.Assembly) == 0 && 
140                                                                 (locations & ExceptionLocations.DependentAssemblies) == 0)
141                                                         break;
142                                                 MemberReference memberRef = ((MemberReference) instruction.Operand);
143                                                 if (((locations & ExceptionLocations.Assembly) != 0 && 
144                                                                         body.Method.DeclaringType.Scope.Name == memberRef.DeclaringType.Scope.Name) ||
145                                                                 ((locations & ExceptionLocations.DependentAssemblies) != 0 && 
146                                                                         body.Method.DeclaringType.Scope.Name != memberRef.DeclaringType.Scope.Name)) {
147
148                                                         IEnumerable<ExceptionSources> memberExceptions = this [memberRef];
149                                                         AddExceptions (body, instruction, 
150                                                                         memberExceptions.Select (es => es.Exception),
151                                                                         memberExceptions.SelectMany (es => es.Sources),
152                                                                         exceptions);
153                                                 }
154                                                 break;
155                                         }
156                                         case Code.Newobj: {
157                                                 MethodReference ctor = (MethodReference) instruction.Operand;
158                                                 if (IsExceptionConstructor (ctor)) {
159                                                         AddExceptions (body, instruction,
160                                                                         new TypeReference[]{ctor.DeclaringType},
161                                                                         new MemberReference[]{body.Method},
162                                                                         exceptions);
163                                                 }
164                                                 break;
165                                         }
166                                 }
167                         }
168                 }
169
170                 void AddExceptions (MethodBody body, Instruction instruction, IEnumerable<TypeReference> add, IEnumerable<MemberReference> sources,
171                                 Dictionary<string, ExceptionSources> exceptions)
172                 {
173                         var handlers = body.ExceptionHandlers.Cast<ExceptionHandler> ()
174                                 .Where (eh => instruction.Offset >= eh.TryStart.Offset && 
175                                                 instruction.Offset <= eh.TryEnd.Offset);
176                         foreach (var ex in add) {
177                                 if (!handlers.Any (h => IsExceptionCaught (ex, h.CatchType))) {
178                                         ExceptionSources s;
179                                         string eName = xdoc.GetDeclaration (ex);
180                                         if (!exceptions.TryGetValue (eName, out s)) {
181                                                 s = new ExceptionSources (ex);
182                                                 exceptions.Add (eName, s);
183                                         }
184                                         foreach (var m in sources)
185                                                 s.Sources.Add (m);
186                                 }
187                         }
188                 }
189
190                 static bool IsExceptionConstructor (MethodReference ctor)
191                 {
192                         return GetBases (ctor.DeclaringType)
193                                 .Any (t => t.FullName == "System.Exception");
194                 }
195
196                 bool IsExceptionCaught (TypeReference exception, TypeReference catcher)
197                 {
198                         return GetBases (exception).Select (e => xdoc.GetDeclaration (e))
199                                 .Union (GetBases (catcher).Select (e => xdoc.GetDeclaration (e)))
200                                 .Any ();
201                 }
202
203                 static IEnumerable<TypeReference> GetBases (TypeReference type)
204                 {
205                         yield return type;
206                         TypeDefinition def = type.Resolve ();
207                         while (def != null && def.BaseType != null) {
208                                 yield return def.BaseType;
209                                 def = def.BaseType.Resolve ();
210                         }
211                 }
212         }
213 }