2 // Mono.Documentation/exceptions.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2008 Novell, Inc.
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
36 namespace Mono.Documentation {
39 public enum ExceptionLocations {
42 DependentAssemblies = 0x2,
43 All = Assembly | DependentAssemblies
46 public class ExceptionSources {
47 internal ExceptionSources (TypeReference exception)
49 Exception = exception;
50 SourcesList = new List<IMemberReference> ();
51 Sources = new ReadOnlyCollection<IMemberReference> (SourcesList);
54 public TypeReference Exception { get; private set; }
55 public ReadOnlyCollection<IMemberReference> Sources { get; private set; }
56 internal List<IMemberReference> SourcesList;
60 public class ExceptionLookup {
62 SlashDocMemberFormatter xdoc = new SlashDocMemberFormatter ();
64 // xdoc(MemberRef) -> xdoc(TypeRef) -> ExceptionSource
65 // where ExceptionSource.Exception == xdoc(TypeRef)
66 Dictionary<string, Dictionary<string, ExceptionSources>> db = new Dictionary<string, Dictionary<string, ExceptionSources>> ();
68 ExceptionLocations locations;
70 public ExceptionLookup (ExceptionLocations locations)
72 this.locations = locations;
75 public IEnumerable<ExceptionSources> this [IMemberReference member] {
78 throw new ArgumentNullException ("member");
79 string memberDecl = xdoc.GetDeclaration (member.GetDefinition ());
80 Dictionary<string, ExceptionSources> e;
81 if (!db.TryGetValue (memberDecl, out e)) {
82 e = new Dictionary<string, ExceptionSources> ();
83 var bodies = GetMethodBodies (member);
84 foreach (var body in bodies) {
87 FillExceptions (body, e);
89 db.Add (memberDecl, e);
95 MethodBody[] GetMethodBodies (IMemberReference member)
97 if (member is MethodReference) {
98 return new[]{ ((MethodDefinition) member.GetDefinition ()).Body };
100 if (member is PropertyReference) {
101 PropertyDefinition prop = (PropertyDefinition) member.GetDefinition ();
103 prop.GetMethod != null ? prop.GetMethod.Body : null,
104 prop.SetMethod != null ? prop.SetMethod.Body : null,
107 if (member is FieldReference)
108 return new MethodBody[]{};
109 if (member is EventReference) {
110 EventDefinition ev = (EventDefinition) member.GetDefinition ();
112 ev.AddMethod != null ? ev.AddMethod.Body : null,
113 ev.InvokeMethod != null ? ev.InvokeMethod.Body : null,
114 ev.RemoveMethod != null ? ev.RemoveMethod.Body : null,
117 throw new NotSupportedException ("Unsupported member type: " + member.GetType().FullName);
120 void FillExceptions (MethodBody body, Dictionary<string, ExceptionSources> exceptions)
122 for (int i = 0; i < body.Instructions.Count; ++i) {
123 Instruction instruction = body.Instructions [i];
124 switch (instruction.OpCode.Code) {
126 case Code.Callvirt: {
127 if ((locations & ExceptionLocations.Assembly) == 0 &&
128 (locations & ExceptionLocations.DependentAssemblies) == 0)
130 IMemberReference memberRef = ((IMemberReference) instruction.Operand);
131 if (((locations & ExceptionLocations.Assembly) != 0 &&
132 body.Method.DeclaringType.Scope.Name == memberRef.DeclaringType.Scope.Name) ||
133 ((locations & ExceptionLocations.DependentAssemblies) != 0 &&
134 body.Method.DeclaringType.Scope.Name != memberRef.DeclaringType.Scope.Name)) {
135 IEnumerable<ExceptionSources> memberExceptions = this [memberRef];
136 AddExceptions (body, instruction,
137 memberExceptions.Select (es => es.Exception),
138 memberExceptions.SelectMany (es => es.Sources),
144 MethodReference ctor = (MethodReference) instruction.Operand;
145 if (IsExceptionConstructor (ctor)) {
146 AddExceptions (body, instruction,
147 new TypeReference[]{ctor.DeclaringType},
148 new IMemberReference[]{body.Method},
157 void AddExceptions (MethodBody body, Instruction instruction, IEnumerable<TypeReference> add, IEnumerable<IMemberReference> sources,
158 Dictionary<string, ExceptionSources> exceptions)
160 var handlers = body.ExceptionHandlers.Cast<ExceptionHandler> ()
161 .Where (eh => instruction.Offset >= eh.TryStart.Offset &&
162 instruction.Offset <= eh.TryEnd.Offset);
163 foreach (var ex in add) {
164 if (!handlers.Any (h => IsExceptionCaught (ex, h.CatchType))) {
166 string eName = xdoc.GetDeclaration (ex);
167 if (!exceptions.TryGetValue (eName, out s)) {
168 s = new ExceptionSources (ex);
169 exceptions.Add (eName, s);
171 s.SourcesList.AddRange (sources);
176 static bool IsExceptionConstructor (MethodReference ctor)
178 return GetBases (ctor.DeclaringType)
179 .Any (t => t.FullName == "System.Exception");
182 bool IsExceptionCaught (TypeReference exception, TypeReference catcher)
184 return GetBases (exception).Select (e => xdoc.GetDeclaration (e))
185 .Union (GetBases (catcher).Select (e => xdoc.GetDeclaration (e)))
189 static IEnumerable<TypeReference> GetBases (TypeReference type)
192 TypeDefinition def = DocUtils.GetTypeDefinition (type);
193 while (def != null && def.BaseType != null) {
194 yield return def.BaseType;
195 def = DocUtils.GetTypeDefinition (def.BaseType);