3 // Sebastien Pouliot <sebastien@xamarin.com>
5 // Copyright 2013-2014 Xamarin Inc. http://www.xamarin.com
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections.Generic;
30 using System.Reflection;
32 using System.Xml.Linq;
34 namespace Xamarin.ApiDiff {
36 public abstract class MemberComparer : Comparer {
38 // true if this is the first element being added or removed in the group being rendered
41 public abstract string GroupName { get; }
42 public abstract string ElementName { get; }
44 public void Compare (XElement source, XElement target)
46 var s = source.Element (GroupName);
47 var t = target.Element (GroupName);
48 if (XNode.DeepEquals (s, t))
52 Add (t.Elements (ElementName));
53 } else if (t == null) {
54 Remove (s.Elements (ElementName));
56 Compare (s.Elements (ElementName), t.Elements (ElementName));
60 public override void SetContext (XElement current)
64 public XElement Source { get; set; }
66 public virtual bool Find (XElement e)
68 return e.GetAttribute ("name") == Source.GetAttribute ("name");
71 XElement Find (IEnumerable<XElement> target)
73 return State.Lax ? target.FirstOrDefault (Find) : target.SingleOrDefault (Find);
76 public override void Compare (IEnumerable<XElement> source, IEnumerable<XElement> target)
81 foreach (var s in source) {
84 var t = Find (target);
86 // not in target, it was removed
91 if (Equals (s, t, modified))
94 Modified (s, t, modified);
97 // delayed, that way we show "Modified", "Added" and then "Removed"
102 // remaining == newly added in target
106 void Add (IEnumerable<XElement> elements)
109 foreach (var item in elements) {
111 if (State.IgnoreAdded.Any (re => re.IsMatch (GetDescription (item))))
114 BeforeAdding (elements);
123 void Modify (ApiChanges modified)
125 foreach (var changes in modified) {
126 Output.WriteLine ("<p>{0}:</p>", changes.Key);
127 Output.WriteLine ("<pre>");
128 foreach (var element in changes.Value) {
129 foreach (var line in element.Member.ToString ().Split ('\n'))
130 Output.WriteLine ("\t{0}", line);
133 Output.WriteLine ("</pre>");
137 void Remove (IEnumerable<XElement> elements)
140 foreach (var item in elements) {
141 if (State.IgnoreRemoved.Any (re => re.IsMatch (GetDescription (item))))
145 BeforeRemoving (elements);
154 public abstract string GetDescription (XElement e);
156 protected StringBuilder GetObsoleteMessage (XElement e)
158 var sb = new StringBuilder ();
159 string o = e.GetObsoleteMessage ();
161 sb.Append ("[Obsolete");
163 sb.Append (" (\"").Append (o).Append ("\")");
165 for (int i = 0; i < State.Indent + 1; i++)
171 public override bool Equals (XElement source, XElement target, ApiChanges changes)
173 RenderAttributes (source, target, changes);
175 // We don't want to compare attributes.
176 RemoveAttributes (source);
177 RemoveAttributes (target);
179 return base.Equals (source, target, changes);
182 public virtual void BeforeAdding (IEnumerable<XElement> list)
185 Output.WriteLine ("<p>Added {0}:</p>", list.Count () > 1 ? GroupName : ElementName);
186 Output.WriteLine (State.Colorize ? "<pre style='color: green'>" : "<pre>");
189 public override void Added (XElement target)
191 var o = GetObsoleteMessage (target);
192 if (!first && (o.Length > 0))
194 Indent ().WriteLine ("\t{0}{1}", o, GetDescription (target));
198 public virtual void AfterAdding ()
200 Output.WriteLine ("</pre>");;
203 public override void Modified (XElement source, XElement target, ApiChanges change)
207 public virtual void BeforeRemoving (IEnumerable<XElement> list)
210 Output.WriteLine ("<p>Removed {0}:</p>\n", list.Count () > 1 ? GroupName : ElementName);
211 Output.WriteLine (State.Colorize ? "<pre style='color: red'>" : "<pre>");
214 public override void Removed (XElement source)
216 var o = GetObsoleteMessage (source);
217 if (!first && (o.Length > 0))
219 Indent ().WriteLine ("\t{0}{1}", o, GetDescription (source));
223 public virtual void AfterRemoving ()
225 Output.WriteLine ("</pre>");;
228 string RenderGenericParameter (XElement gp)
230 var sb = new StringBuilder ();
231 sb.Append (gp.GetTypeName ("name"));
233 var constraints = gp.DescendantList ("generic-parameter-constraints", "generic-parameter-constraint");
234 if (constraints != null && constraints.Count > 0) {
236 for (int i = 0; i < constraints.Count; i++) {
239 sb.Append (constraints [i].GetTypeName ("name"));
242 return sb.ToString ();
245 protected void RenderGenericParameters (XElement source, XElement target, ApiChange change)
247 var src = source.DescendantList ("generic-parameters", "generic-parameter");
248 var tgt = target.DescendantList ("generic-parameters", "generic-parameter");
249 var srcCount = src == null ? 0 : src.Count;
250 var tgtCount = tgt == null ? 0 : tgt.Count;
252 if (srcCount == 0 && tgtCount == 0)
255 change.Append ("<");
256 for (int i = 0; i < Math.Max (srcCount, tgtCount); i++) {
258 change.Append (", ");
260 change.AppendAdded (RenderGenericParameter (tgt [i]), true);
261 } else if (i >= tgtCount) {
262 change.AppendRemoved (RenderGenericParameter (src [i]), true);
264 var srcName = RenderGenericParameter (src [i]);
265 var tgtName = RenderGenericParameter (tgt [i]);
267 if (srcName != tgtName) {
268 change.AppendModified (srcName, tgtName, true);
270 change.Append (srcName);
274 change.Append (">");
277 protected string FormatValue (string type, string value)
282 if (type == "string")
283 return "\"" + value + "\"";
284 else if (type == "bool") {
298 protected void RenderParameters (XElement source, XElement target, ApiChange change)
300 var src = source.DescendantList ("parameters", "parameter");
301 var tgt = target.DescendantList ("parameters", "parameter");
302 var srcCount = src == null ? 0 : src.Count;
303 var tgtCount = tgt == null ? 0 : tgt.Count;
305 change.Append (" (");
306 for (int i = 0; i < Math.Max (srcCount, tgtCount); i++) {
308 change.Append (", ");
311 change.AppendAdded (tgt [i].GetTypeName ("type") + " " + tgt [i].GetAttribute ("name"), true);
312 } else if (i >= tgtCount) {
313 change.AppendRemoved (src [i].GetTypeName ("type") + " " + src [i].GetAttribute ("name"), true);
315 var paramSourceType = src [i].GetTypeName ("type");
316 var paramTargetType = tgt [i].GetTypeName ("type");
318 var paramSourceName = src [i].GetAttribute ("name");
319 var paramTargetName = tgt [i].GetAttribute ("name");
321 if (paramSourceType != paramTargetType) {
322 change.AppendModified (paramSourceType, paramTargetType, true);
324 change.Append (paramSourceType);
327 if (paramSourceName != paramTargetName) {
328 change.AppendModified (paramSourceName, paramTargetName, false);
330 change.Append (paramSourceName);
333 var optSource = src [i].Attribute ("optional");
334 var optTarget = tgt [i].Attribute ("optional");
335 var srcValue = FormatValue (paramSourceType, src [i].GetAttribute ("defaultValue"));
336 var tgtValue = FormatValue (paramTargetType, tgt [i].GetAttribute ("defaultValue"));
338 if (optSource != null) {
339 if (optTarget != null) {
340 change.Append (" = ");
341 if (srcValue != tgtValue) {
342 change.AppendModified (srcValue, tgtValue, false);
344 change.Append (tgtValue);
347 change.AppendRemoved (" = " + srcValue);
350 if (optTarget != null)
351 change.AppendAdded (" = " + tgtValue);
358 // Ignore any parameter name changes if requested.
359 if (State.IgnoreParameterNameChanges && !change.Breaking) {
360 change.AnyChange = false;
361 change.HasIgnoredChanges = true;
365 void RenderVTable (MethodAttributes source, MethodAttributes target, ApiChange change)
367 var srcAbstract = (source & MethodAttributes.Abstract) == MethodAttributes.Abstract;
368 var tgtAbstract = (target & MethodAttributes.Abstract) == MethodAttributes.Abstract;
369 var srcFinal = (source & MethodAttributes.Final) == MethodAttributes.Final;
370 var tgtFinal = (target & MethodAttributes.Final) == MethodAttributes.Final;
371 var srcVirtual = (source & MethodAttributes.Virtual) == MethodAttributes.Virtual;
372 var tgtVirtual = (target & MethodAttributes.Virtual) == MethodAttributes.Virtual;
373 var srcOverride = (source & MethodAttributes.VtableLayoutMask) != MethodAttributes.NewSlot;
374 var tgtOverride = (target & MethodAttributes.VtableLayoutMask) != MethodAttributes.NewSlot;
376 var srcWord = srcVirtual ? (srcOverride ? "override" : "virtual") : string.Empty;
377 var tgtWord = tgtVirtual ? (tgtOverride ? "override" : "virtual") : string.Empty;
378 var breaking = srcWord.Length > 0 && tgtWord.Length == 0;
382 change.Append ("abstract ");
383 } else if (tgtVirtual) {
384 change.AppendModified ("abstract", tgtWord, false).Append (" ");
386 change.AppendRemoved ("abstract").Append (" ");
390 change.AppendAdded ("abstract", true).Append (" ");
391 } else if (srcWord != tgtWord) {
393 change.AppendModified (srcWord, tgtWord, breaking).Append (" ");
394 } else if (tgtWord.Length > 0) {
395 change.Append (tgtWord).Append (" ");
396 } else if (srcWord.Length > 0) {
397 change.AppendRemoved (srcWord, breaking).Append (" ");
403 change.Append ("final ");
405 change.AppendRemoved ("final", false).Append (" "); // removing 'final' is not a breaking change.
408 if (tgtFinal && srcVirtual) {
409 change.AppendModified ("virtual", "final", true).Append (" "); // adding 'final' is a breaking change if the member was virtual
413 if (!srcVirtual && !srcFinal && tgtVirtual && tgtFinal) {
414 // existing member implements a member from a new interface
415 // this would show up as 'virtual final', which is redundant, so show nothing at all.
416 change.HasIgnoredChanges = true;
419 // Ignore non-breaking virtual changes.
420 if (State.IgnoreVirtualChanges && !change.Breaking) {
421 change.AnyChange = false;
422 change.HasIgnoredChanges = true;
425 var tgtSecurity = (source & MethodAttributes.HasSecurity) == MethodAttributes.HasSecurity;
426 var srcSecurity = (target & MethodAttributes.HasSecurity) == MethodAttributes.HasSecurity;
428 if (tgtSecurity != srcSecurity)
429 change.HasIgnoredChanges = true;
431 var srcPInvoke = (source & MethodAttributes.PinvokeImpl) == MethodAttributes.PinvokeImpl;
432 var tgtPInvoke = (target & MethodAttributes.PinvokeImpl) == MethodAttributes.PinvokeImpl;
433 if (srcPInvoke != tgtPInvoke)
434 change.HasIgnoredChanges = true;
437 protected string GetVisibility (MethodAttributes attr)
440 case MethodAttributes.Private:
441 case MethodAttributes.PrivateScope:
443 case MethodAttributes.Assembly:
445 case MethodAttributes.FamANDAssem:
446 return "private internal";
447 case MethodAttributes.FamORAssem:
448 return "protected"; // customers don't care about 'internal';
449 case MethodAttributes.Family:
451 case MethodAttributes.Public:
454 throw new NotImplementedException ();
458 protected void RenderVisibility (MethodAttributes source, MethodAttributes target, ApiChange diff)
460 source = source & MethodAttributes.MemberAccessMask;
461 target = target & MethodAttributes.MemberAccessMask;
463 if (source == target) {
464 diff.Append (GetVisibility (target));
466 var breaking = false;
468 case MethodAttributes.Private:
469 case MethodAttributes.Assembly:
470 case MethodAttributes.FamANDAssem:
471 break; // these are not publicly visible, thus not breaking
472 case MethodAttributes.FamORAssem:
473 case MethodAttributes.Family:
475 case MethodAttributes.Public:
476 // to public is not a breaking change
478 case MethodAttributes.Family:
479 case MethodAttributes.FamORAssem:
480 // not a breaking change, but should still show up in diff
483 // anything else is a breaking change
488 case MethodAttributes.Public:
490 // any change from public is breaking.
495 diff.AppendModified (GetVisibility (source), GetVisibility (target), breaking);
500 protected void RenderStatic (MethodAttributes src, MethodAttributes tgt, ApiChange diff)
502 var srcStatic = (src & MethodAttributes.Static) == MethodAttributes.Static;
503 var tgtStatic = (tgt & MethodAttributes.Static) == MethodAttributes.Static;
505 if (srcStatic != tgtStatic) {
507 diff.AppendRemoved ("static", true).Append (" ");
509 diff.AppendAdded ("static", true).Append (" ");
514 protected void RenderMethodAttributes (MethodAttributes src, MethodAttributes tgt, ApiChange diff)
516 RenderStatic (src, tgt, diff);
517 RenderVisibility (src & MethodAttributes.MemberAccessMask, tgt & MethodAttributes.MemberAccessMask, diff);
518 RenderVTable (src, tgt, diff);
521 protected void RenderMethodAttributes (XElement source, XElement target, ApiChange diff)
523 RenderMethodAttributes (source.GetMethodAttributes (), target.GetMethodAttributes (), diff);
526 protected void RemoveAttributes (XElement element)
528 var srcAttributes = element.Element ("attributes");
529 if (srcAttributes != null)
530 srcAttributes.Remove ();
532 foreach (var el in element.Elements ())
533 RemoveAttributes (el);
536 protected void RenderAttributes (XElement source, XElement target, ApiChanges changes)
538 var srcObsolete = source.GetObsoleteMessage ();
539 var tgtObsolete = target.GetObsoleteMessage ();
541 if (srcObsolete == tgtObsolete)
542 return; // nothing changed
544 if (srcObsolete == null) {
545 if (tgtObsolete == null)
546 return; // neither is obsolete
547 var change = new ApiChange ();
548 change.Header = "Obsoleted " + GroupName;
550 change.Append ("<span style='color:gray'>");
551 change.Append ("[Obsolete (");
552 if (tgtObsolete != string.Empty)
553 change.Append ("\"").Append (tgtObsolete).Append ("\"");
554 change.Append (")]\n");
555 change.Append (GetDescription (target));
557 change.Append ("</span>");
558 change.AnyChange = true;
559 changes.Add (source, target, change);
560 } else if (tgtObsolete == null) {
561 // Made non-obsolete. Do we care to report this?
563 // Obsolete message changed. Do we care to report this?
567 protected void RenderName (XElement source, XElement target, ApiChange change)
569 var name = target.GetAttribute ("name");
570 // show the constructor as it would be defined in C#
571 name = name.Replace (".ctor", State.Type);
573 var p = name.IndexOf ('(');
575 name = name.Substring (0, p);
577 change.Append (name);