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><pre>", list.Count () > 1 ? GroupName : ElementName);
187 Output.Write ("<font color='green'>");
190 public override void Added (XElement target)
192 var o = GetObsoleteMessage (target);
193 if (!first && (o.Length > 0))
195 Indent ().WriteLine ("\t{0}{1}", o, GetDescription (target));
199 public virtual void AfterAdding ()
202 Output.Write ("</font>");
203 Output.WriteLine ("</pre>");
206 public override void Modified (XElement source, XElement target, ApiChanges change)
210 public virtual void BeforeRemoving (IEnumerable<XElement> list)
213 Output.WriteLine ("<p>Removed {0}:</p>\n<pre>", list.Count () > 1 ? GroupName : ElementName);
215 Output.Write ("<font color='red'>");
218 public override void Removed (XElement source)
220 var o = GetObsoleteMessage (source);
221 if (!first && (o.Length > 0))
223 Indent ().WriteLine ("\t{0}{1}", o, GetDescription (source));
227 public virtual void AfterRemoving ()
230 Output.Write ("</font>");
231 Output.WriteLine ("</pre>");
234 protected void RenderGenericParameters (XElement source, XElement target, ApiChange change)
236 var src = source.DescendantList ("generic-parameters", "generic-parameter");
237 var tgt = target.DescendantList ("generic-parameters", "generic-parameter");
238 var srcCount = src == null ? 0 : src.Count;
239 var tgtCount = tgt == null ? 0 : tgt.Count;
241 if (srcCount == 0 && tgtCount == 0)
244 change.Append ("<");
245 for (int i = 0; i < Math.Max (srcCount, tgtCount); i++) {
247 change.Append (", ");
249 change.AppendAdded (tgt [i].GetTypeName ("name"), true);
250 } else if (i >= tgtCount) {
251 change.AppendRemoved (src [i].GetTypeName ("name"), true);
253 var srcName = src [i].GetTypeName ("name");
254 var tgtName = tgt [i].GetTypeName ("name");
256 if (srcName != tgtName) {
257 change.AppendModified (srcName, tgtName, true);
259 change.Append (srcName);
263 change.Append (">");
266 protected string FormatValue (string type, string value)
271 if (type == "string")
272 return "\"" + value + "\"";
273 else if (type == "bool") {
287 protected void RenderParameters (XElement source, XElement target, ApiChange change)
289 var src = source.DescendantList ("parameters", "parameter");
290 var tgt = target.DescendantList ("parameters", "parameter");
291 var srcCount = src == null ? 0 : src.Count;
292 var tgtCount = tgt == null ? 0 : tgt.Count;
294 change.Append (" (");
295 for (int i = 0; i < Math.Max (srcCount, tgtCount); i++) {
297 change.Append (", ");
300 change.AppendAdded (tgt [i].GetTypeName ("type") + " " + tgt [i].GetAttribute ("name"), true);
301 } else if (i >= tgtCount) {
302 change.AppendRemoved (src [i].GetTypeName ("type") + " " + src [i].GetAttribute ("name"), true);
304 var paramSourceType = src [i].GetTypeName ("type");
305 var paramTargetType = tgt [i].GetTypeName ("type");
307 var paramSourceName = src [i].GetAttribute ("name");
308 var paramTargetName = tgt [i].GetAttribute ("name");
310 if (paramSourceType != paramTargetType) {
311 change.AppendModified (paramSourceType, paramTargetType, true);
313 change.Append (paramSourceType);
316 if (paramSourceName != paramTargetName) {
317 change.AppendModified (paramSourceName, paramTargetName, false);
319 change.Append (paramSourceName);
322 var optSource = src [i].Attribute ("optional");
323 var optTarget = tgt [i].Attribute ("optional");
324 var srcValue = FormatValue (paramSourceType, src [i].GetAttribute ("defaultValue"));
325 var tgtValue = FormatValue (paramTargetType, tgt [i].GetAttribute ("defaultValue"));
327 if (optSource != null) {
328 if (optTarget != null) {
329 change.Append (" = ");
330 if (srcValue != tgtValue) {
331 change.AppendModified (srcValue, tgtValue, false);
333 change.Append (tgtValue);
336 change.AppendRemoved (" = " + srcValue);
339 if (optTarget != null)
340 change.AppendAdded (" = " + tgtValue);
347 // Ignore any parameter name changes if requested.
348 if (State.IgnoreParameterNameChanges && !change.Breaking) {
349 change.AnyChange = false;
350 change.HasIgnoredChanges = true;
354 void RenderVTable (MethodAttributes source, MethodAttributes target, ApiChange change)
356 var srcAbstract = (source & MethodAttributes.Abstract) == MethodAttributes.Abstract;
357 var tgtAbstract = (target & MethodAttributes.Abstract) == MethodAttributes.Abstract;
358 var srcFinal = (source & MethodAttributes.Final) == MethodAttributes.Final;
359 var tgtFinal = (target & MethodAttributes.Final) == MethodAttributes.Final;
360 var srcVirtual = (source & MethodAttributes.Virtual) == MethodAttributes.Virtual;
361 var tgtVirtual = (target & MethodAttributes.Virtual) == MethodAttributes.Virtual;
362 var srcOverride = (source & MethodAttributes.VtableLayoutMask) != MethodAttributes.NewSlot;
363 var tgtOverride = (target & MethodAttributes.VtableLayoutMask) != MethodAttributes.NewSlot;
365 var srcWord = srcVirtual ? (srcOverride ? "override" : "virtual") : string.Empty;
366 var tgtWord = tgtVirtual ? (tgtOverride ? "override" : "virtual") : string.Empty;
367 var breaking = srcWord.Length > 0 && tgtWord.Length == 0;
371 change.Append ("abstract ");
372 } else if (tgtVirtual) {
373 change.AppendModified ("abstract", tgtWord, false).Append (" ");
375 change.AppendRemoved ("abstract").Append (" ");
379 change.AppendAdded ("abstract", true).Append (" ");
380 } else if (srcWord != tgtWord) {
382 change.AppendModified (srcWord, tgtWord, breaking).Append (" ");
383 } else if (tgtWord.Length > 0) {
384 change.Append (tgtWord).Append (" ");
385 } else if (srcWord.Length > 0) {
386 change.AppendRemoved (srcWord, breaking).Append (" ");
392 change.Append ("final ");
394 change.AppendRemoved ("final", false).Append (" "); // removing 'final' is not a breaking change.
397 if (tgtFinal && srcVirtual) {
398 change.AppendModified ("virtual", "final", true).Append (" "); // adding 'final' is a breaking change if the member was virtual
402 if (!srcVirtual && !srcFinal && tgtVirtual && tgtFinal) {
403 // existing member implements a member from a new interface
404 // this would show up as 'virtual final', which is redundant, so show nothing at all.
405 change.HasIgnoredChanges = true;
408 // Ignore non-breaking virtual changes.
409 if (State.IgnoreVirtualChanges && !change.Breaking) {
410 change.AnyChange = false;
411 change.HasIgnoredChanges = true;
415 protected string GetVisibility (MethodAttributes attr)
418 case MethodAttributes.Private:
419 case MethodAttributes.PrivateScope:
421 case MethodAttributes.Assembly:
423 case MethodAttributes.FamANDAssem:
424 return "private internal";
425 case MethodAttributes.FamORAssem:
426 return "protected"; // customers don't care about 'internal';
427 case MethodAttributes.Family:
429 case MethodAttributes.Public:
432 throw new NotImplementedException ();
436 protected void RenderVisibility (MethodAttributes source, MethodAttributes target, ApiChange diff)
438 source = source & MethodAttributes.MemberAccessMask;
439 target = target & MethodAttributes.MemberAccessMask;
441 if (source == target) {
442 diff.Append (GetVisibility (target));
444 var breaking = false;
446 case MethodAttributes.Private:
447 case MethodAttributes.Assembly:
448 case MethodAttributes.FamANDAssem:
449 break; // these are not publicly visible, thus not breaking
450 case MethodAttributes.FamORAssem:
451 case MethodAttributes.Family:
453 case MethodAttributes.Public:
454 // to public is not a breaking change
456 case MethodAttributes.Family:
457 case MethodAttributes.FamORAssem:
458 // not a breaking change, but should still show up in diff
461 // anything else is a breaking change
466 case MethodAttributes.Public:
468 // any change from public is breaking.
473 diff.AppendModified (GetVisibility (source), GetVisibility (target), breaking);
478 protected void RenderStatic (MethodAttributes src, MethodAttributes tgt, ApiChange diff)
480 var srcStatic = (src & MethodAttributes.Static) == MethodAttributes.Static;
481 var tgtStatic = (tgt & MethodAttributes.Static) == MethodAttributes.Static;
483 if (srcStatic != tgtStatic) {
485 diff.AppendRemoved ("static", true).Append (" ");
487 diff.AppendAdded ("static", true).Append (" ");
492 protected void RenderMethodAttributes (MethodAttributes src, MethodAttributes tgt, ApiChange diff)
494 RenderStatic (src, tgt, diff);
495 RenderVisibility (src & MethodAttributes.MemberAccessMask, tgt & MethodAttributes.MemberAccessMask, diff);
496 RenderVTable (src, tgt, diff);
499 protected void RenderMethodAttributes (XElement source, XElement target, ApiChange diff)
501 RenderMethodAttributes (source.GetMethodAttributes (), target.GetMethodAttributes (), diff);
504 protected void RemoveAttributes (XElement element)
506 var srcAttributes = element.Element ("attributes");
507 if (srcAttributes != null)
508 srcAttributes.Remove ();
510 foreach (var el in element.Elements ())
511 RemoveAttributes (el);
514 protected void RenderAttributes (XElement source, XElement target, ApiChanges changes)
516 var srcObsolete = source.GetObsoleteMessage ();
517 var tgtObsolete = target.GetObsoleteMessage ();
519 if (srcObsolete == tgtObsolete)
520 return; // nothing changed
522 if (srcObsolete == null) {
523 if (tgtObsolete == null)
524 return; // neither is obsolete
525 var change = new ApiChange ();
526 change.Header = "Obsoleted " + GroupName;
528 change.Append ("<font color='gray'>");
529 change.Append ("[Obsolete (");
530 if (tgtObsolete != string.Empty)
531 change.Append ("\"").Append (tgtObsolete).Append ("\"");
532 change.Append ("]\n");
533 change.Append (GetDescription (target));
535 change.Append ("</font>");
536 change.AnyChange = true;
537 changes.Add (source, target, change);
538 } else if (tgtObsolete == null) {
539 // Made non-obsolete. Do we care to report this?
541 // Obsolete message changed. Do we care to report this?
545 protected void RenderName (XElement source, XElement target, ApiChange change)
547 var name = target.GetAttribute ("name");
548 // show the constructor as it would be defined in C#
549 name = name.Replace (".ctor", State.Type);
551 var p = name.IndexOf ('(');
553 name = name.Substring (0, p);
555 change.Append (name);