Program.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Reflection;
  9. using System.Runtime.CompilerServices;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace ApiCompare
  13. {
  14. class Program
  15. {
  16. private static readonly Type asyncInterfaceType = typeof(IAsyncEnumerable<>);
  17. private static readonly Type syncInterfaceType = typeof(IEnumerable<>);
  18. private static readonly Type asyncOrderedInterfaceType = typeof(IOrderedAsyncEnumerable<>);
  19. private static readonly Type syncOrderedInterfaceType = typeof(IOrderedEnumerable<>);
  20. private static readonly string[] exceptions = new[]
  21. {
  22. "SkipLast", // In .NET Core 2.0
  23. "TakeLast", // In .NET Core 2.0
  24. "ToHashSet", // In .NET Core 2.0
  25. "Cast", // Non-generic methods
  26. "OfType", // Non-generic methods
  27. "AsEnumerable", // Trivially renamed
  28. "AsAsyncEnumerable", // Trivially renamed
  29. "ForEachAsync", // "foreach await" language substitute for the time being
  30. "ToAsyncEnumerable", // First-class conversions
  31. "ToEnumerable", // First-class conversions
  32. "ToObservable", // First-class conversions
  33. };
  34. private static readonly TypeSubstitutor subst = new TypeSubstitutor(new Dictionary<Type, Type>
  35. {
  36. { asyncInterfaceType, syncInterfaceType },
  37. { asyncOrderedInterfaceType, syncOrderedInterfaceType },
  38. { typeof(IAsyncGrouping<,>), typeof(IGrouping<,>) },
  39. });
  40. static void Main()
  41. {
  42. var asyncOperatorsType = typeof(AsyncEnumerable);
  43. var syncOperatorsType = typeof(Enumerable);
  44. Compare(syncOperatorsType, asyncOperatorsType);
  45. }
  46. static void Compare(Type syncOperatorsType, Type asyncOperatorsType)
  47. {
  48. var syncOperators = GetQueryOperators(new[] { syncInterfaceType, syncOrderedInterfaceType }, syncOperatorsType, exceptions);
  49. var asyncOperators = GetQueryOperators(new[] { asyncInterfaceType, asyncOrderedInterfaceType }, asyncOperatorsType, exceptions);
  50. CompareFactories(syncOperators.Factories, asyncOperators.Factories);
  51. CompareQueryOperators(syncOperators.QueryOperators, asyncOperators.QueryOperators);
  52. CompareAggregates(syncOperators.Aggregates, asyncOperators.Aggregates);
  53. }
  54. static void CompareFactories(ILookup<string, MethodInfo> syncFactories, ILookup<string, MethodInfo> asyncFactories)
  55. {
  56. CompareSets(syncFactories, asyncFactories, CompareFactoryOverloads);
  57. }
  58. static void CompareFactoryOverloads(string name, IEnumerable<MethodInfo> syncMethods, IEnumerable<MethodInfo> asyncMethods)
  59. {
  60. var sync = GetSignatures(syncMethods).ToArray();
  61. var async = GetRewrittenSignatures(asyncMethods).ToArray();
  62. //
  63. // Ensure that async is a superset of sync.
  64. //
  65. var notInAsync = sync.Except(async);
  66. if (notInAsync.Any())
  67. {
  68. foreach (var signature in notInAsync)
  69. {
  70. Console.WriteLine("MISSING " + ToString(signature.Method));
  71. }
  72. }
  73. //
  74. // Check for excess overloads.
  75. //
  76. var notInSync = async.Except(sync);
  77. if (notInSync.Any())
  78. {
  79. foreach (var signature in notInSync)
  80. {
  81. Console.WriteLine("EXCESS " + ToString(signature.Method));
  82. }
  83. }
  84. }
  85. static void CompareQueryOperators(ILookup<string, MethodInfo> syncOperators, ILookup<string, MethodInfo> asyncOperators)
  86. {
  87. CompareSets(syncOperators, asyncOperators, CompareQueryOperatorsOverloads);
  88. }
  89. static void CompareQueryOperatorsOverloads(string name, IEnumerable<MethodInfo> syncMethods, IEnumerable<MethodInfo> asyncMethods)
  90. {
  91. var sync = GetSignatures(syncMethods).ToArray();
  92. var async = GetRewrittenSignatures(asyncMethods).ToArray();
  93. //
  94. // Ensure that async is a superset of sync.
  95. //
  96. var notInAsync = sync.Except(async);
  97. if (notInAsync.Any())
  98. {
  99. foreach (var signature in notInAsync)
  100. {
  101. Console.WriteLine("MISSING " + ToString(signature.Method));
  102. }
  103. }
  104. //
  105. // Find Task-based overloads.
  106. //
  107. var taskBasedSignatures = new List<Signature>();
  108. foreach (var signature in sync)
  109. {
  110. if (signature.ParameterTypes.Any(IsFuncOrActionType))
  111. {
  112. taskBasedSignatures.Add(GetAsyncVariant(signature));
  113. }
  114. }
  115. if (taskBasedSignatures.Count > 0)
  116. {
  117. var notInAsyncTaskBased = taskBasedSignatures.Except(async);
  118. if (notInAsyncTaskBased.Any())
  119. {
  120. foreach (var signature in notInAsyncTaskBased)
  121. {
  122. Console.WriteLine("MISSING " + name + " :: " + signature);
  123. }
  124. }
  125. }
  126. //
  127. // Excess overloads that are neither carbon copies of sync nor Task-based variants of sync.
  128. //
  129. var notInSync = async.Except(sync.Union(taskBasedSignatures));
  130. if (notInSync.Any())
  131. {
  132. foreach (var signature in notInSync)
  133. {
  134. Console.WriteLine("EXCESS " + ToString(signature.Method));
  135. }
  136. }
  137. }
  138. static void CompareAggregates(ILookup<string, MethodInfo> syncAggregates, ILookup<string, MethodInfo> asyncAggregates)
  139. {
  140. CompareSets(syncAggregates, asyncAggregates, CompareAggregateOverloads);
  141. }
  142. static void CompareAggregateOverloads(string name, IEnumerable<MethodInfo> syncMethods, IEnumerable<MethodInfo> asyncMethods)
  143. {
  144. var sync = GetSignatures(syncMethods).Select(GetAsyncAggregateSignature).ToArray();
  145. var async = GetRewrittenSignatures(asyncMethods).ToArray();
  146. //
  147. // Ensure that async is a superset of sync.
  148. //
  149. var notInAsync = sync.Except(async);
  150. if (notInAsync.Any())
  151. {
  152. foreach (var signature in notInAsync)
  153. {
  154. Console.WriteLine("MISSING " + ToString(signature.Method));
  155. }
  156. }
  157. //
  158. // Find Task-based overloads.
  159. //
  160. var taskBasedSignatures = new List<Signature>();
  161. foreach (var signature in sync)
  162. {
  163. if (signature.ParameterTypes.Any(IsFuncOrActionType))
  164. {
  165. taskBasedSignatures.Add(GetAsyncVariant(signature));
  166. }
  167. }
  168. if (taskBasedSignatures.Count > 0)
  169. {
  170. var notInAsyncTaskBased = taskBasedSignatures.Except(async);
  171. if (notInAsyncTaskBased.Any())
  172. {
  173. foreach (var signature in notInAsyncTaskBased)
  174. {
  175. Console.WriteLine("MISSING " + name + " :: " + signature);
  176. }
  177. }
  178. }
  179. //
  180. // Check for overloads with CancellationToken.
  181. //
  182. var withCancellationToken = new List<Signature>();
  183. foreach (var signature in sync)
  184. {
  185. withCancellationToken.Add(AppendCancellationToken(signature));
  186. }
  187. foreach (var signature in taskBasedSignatures)
  188. {
  189. withCancellationToken.Add(AppendCancellationToken(signature));
  190. }
  191. var notInAsyncWithCancellationToken = withCancellationToken.Except(async);
  192. if (notInAsyncWithCancellationToken.Any())
  193. {
  194. foreach (var signature in notInAsyncWithCancellationToken)
  195. {
  196. Console.WriteLine("MISSING " + name + " :: " + signature);
  197. }
  198. }
  199. //
  200. // Excess overloads that are neither carbon copies of sync nor Task-based variants of sync.
  201. //
  202. var notInSync = async.Except(sync.Union(taskBasedSignatures).Union(withCancellationToken));
  203. if (notInSync.Any())
  204. {
  205. foreach (var signature in notInSync)
  206. {
  207. Console.WriteLine("EXCESS " + ToString(signature.Method));
  208. }
  209. }
  210. }
  211. private static bool IsFuncOrActionType(Type type)
  212. {
  213. if (type.IsConstructedGenericType)
  214. {
  215. var defName = type.GetGenericTypeDefinition().Name;
  216. return defName.StartsWith("Func`") || defName.StartsWith("Action`");
  217. }
  218. if (type == typeof(Action))
  219. {
  220. return true;
  221. }
  222. return false;
  223. }
  224. private static Signature GetAsyncVariant(Signature signature)
  225. {
  226. return new Signature
  227. {
  228. ParameterTypes = signature.ParameterTypes.Select(GetAsyncVariant).ToArray(),
  229. ReturnType = signature.ReturnType
  230. };
  231. }
  232. private static Signature AppendCancellationToken(Signature signature)
  233. {
  234. return new Signature
  235. {
  236. ParameterTypes = signature.ParameterTypes.Concat(new[] { typeof(CancellationToken) }).ToArray(),
  237. ReturnType = signature.ReturnType
  238. };
  239. }
  240. private static Type GetAsyncVariant(Type type)
  241. {
  242. if (IsFuncOrActionType(type))
  243. {
  244. if (type == typeof(Action))
  245. {
  246. return typeof(Func<Task>);
  247. }
  248. else
  249. {
  250. var args = type.GetGenericArguments();
  251. var defName = type.GetGenericTypeDefinition().Name;
  252. if (defName.StartsWith("Func`"))
  253. {
  254. var ret = typeof(Task<>).MakeGenericType(args.Last());
  255. return Expression.GetFuncType(Enumerable.SkipLast(args, 1).Append(ret).ToArray());
  256. }
  257. else
  258. {
  259. return Expression.GetFuncType(args.Append(typeof(Task)).ToArray());
  260. }
  261. }
  262. }
  263. return type;
  264. }
  265. static void CompareSets(ILookup<string, MethodInfo> sync, ILookup<string, MethodInfo> async, Action<string, IEnumerable<MethodInfo>, IEnumerable<MethodInfo>> compareCore)
  266. {
  267. var syncNames = sync.Select(g => g.Key).ToArray();
  268. var asyncNames = async.Select(g => g.Key).ToArray();
  269. //
  270. // Analyze that async is a superset of sync.
  271. //
  272. var notInAsync = syncNames.Except(asyncNames);
  273. foreach (var n in notInAsync)
  274. {
  275. foreach (var o in sync[n])
  276. {
  277. Console.WriteLine("MISSING " + ToString(o));
  278. }
  279. }
  280. //
  281. // Need to find the same overloads.
  282. //
  283. var inBoth = syncNames.Intersect(asyncNames);
  284. foreach (var n in inBoth)
  285. {
  286. var s = sync[n];
  287. var a = async[n];
  288. compareCore(n, s, a);
  289. }
  290. //
  291. // Report excessive API surface.
  292. //
  293. var onlyInAsync = asyncNames.Except(syncNames);
  294. foreach (var n in onlyInAsync)
  295. {
  296. foreach (var o in async[n])
  297. {
  298. Console.WriteLine("EXCESS " + ToString(o));
  299. }
  300. }
  301. }
  302. static Operators GetQueryOperators(Type[] interfaceTypes, Type operatorsType, string[] exclude)
  303. {
  304. //
  305. // Get all the static methods.
  306. //
  307. var methods = operatorsType.GetMethods(BindingFlags.Public | BindingFlags.Static).Where(m => !exclude.Contains(m.Name));
  308. //
  309. // Get extension methods. These can be either operators or aggregates.
  310. //
  311. var extensionMethods = methods.Where(m => m.IsDefined(typeof(ExtensionAttribute))).ToArray();
  312. //
  313. // Static methods that aren't extension methods can be factories.
  314. //
  315. var factories = methods.Except(extensionMethods).Where(m => m.ReturnType.IsConstructedGenericType && interfaceTypes.Contains(m.ReturnType.GetGenericTypeDefinition())).ToArray();
  316. //
  317. // Extension methods that return the interface type are operators.
  318. //
  319. var queryOperators = extensionMethods.Where(m => m.ReturnType.IsConstructedGenericType && interfaceTypes.Contains(m.ReturnType.GetGenericTypeDefinition())).ToArray();
  320. //
  321. // Extension methods that return another type are aggregates.
  322. //
  323. var aggregates = extensionMethods.Except(queryOperators).ToArray();
  324. //
  325. // Return operators.
  326. //
  327. return new Operators
  328. {
  329. Factories = factories.ToLookup(m => m.Name, m => m),
  330. QueryOperators = queryOperators.ToLookup(m => m.Name, m => m),
  331. Aggregates = aggregates.ToLookup(m => m.Name, m => m),
  332. };
  333. }
  334. static IEnumerable<Signature> GetSignatures(IEnumerable<MethodInfo> methods)
  335. {
  336. return methods.Select(m => GetSignature(m));
  337. }
  338. static IEnumerable<Signature> GetRewrittenSignatures(IEnumerable<MethodInfo> methods)
  339. {
  340. return GetSignatures(methods).Select(s => RewriteSignature(s));
  341. }
  342. static Signature GetSignature(MethodInfo method)
  343. {
  344. if (method.IsGenericMethodDefinition)
  345. {
  346. var newArgs = method.GetGenericArguments().Select((t, i) => Wildcards[i]).ToArray();
  347. method = method.MakeGenericMethod(newArgs);
  348. }
  349. return new Signature
  350. {
  351. Method = method,
  352. ReturnType = method.ReturnType,
  353. ParameterTypes = method.GetParameters().Select(p => p.ParameterType).ToArray()
  354. };
  355. }
  356. static Signature RewriteSignature(Signature signature)
  357. {
  358. return new Signature
  359. {
  360. Method = signature.Method,
  361. ReturnType = subst.Visit(signature.ReturnType),
  362. ParameterTypes = subst.Visit(signature.ParameterTypes)
  363. };
  364. }
  365. static Signature GetAsyncAggregateSignature(Signature signature)
  366. {
  367. var retType = signature.ReturnType == typeof(void) ? typeof(Task) : typeof(Task<>).MakeGenericType(signature.ReturnType);
  368. return new Signature
  369. {
  370. Method = signature.Method,
  371. ReturnType = retType,
  372. ParameterTypes = signature.ParameterTypes
  373. };
  374. }
  375. static string ToString(MethodInfo method)
  376. {
  377. if (method == null)
  378. {
  379. return "UNKNOWN";
  380. }
  381. if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
  382. {
  383. method = method.GetGenericMethodDefinition();
  384. }
  385. return method.ToString();
  386. }
  387. class Operators
  388. {
  389. public ILookup<string, MethodInfo> Factories;
  390. public ILookup<string, MethodInfo> QueryOperators;
  391. public ILookup<string, MethodInfo> Aggregates;
  392. }
  393. class Signature : IEquatable<Signature>
  394. {
  395. public MethodInfo Method;
  396. public Type ReturnType;
  397. public Type[] ParameterTypes;
  398. public static bool operator ==(Signature s1, Signature s2)
  399. {
  400. if ((object)s1 == null && (object)s2 == null)
  401. {
  402. return true;
  403. }
  404. if ((object)s1 == null || (object)s2 == null)
  405. {
  406. return false;
  407. }
  408. return s1.Equals(s2);
  409. }
  410. public static bool operator !=(Signature s1, Signature s2)
  411. {
  412. return !(s1 == s2);
  413. }
  414. public bool Equals(Signature s)
  415. {
  416. return (object)s != null && ReturnType.Equals(s.ReturnType) && ParameterTypes.SequenceEqual(s.ParameterTypes);
  417. }
  418. public override bool Equals(object obj)
  419. {
  420. if (obj is Signature s)
  421. {
  422. return Equals(s);
  423. }
  424. return false;
  425. }
  426. public override int GetHashCode()
  427. {
  428. return ParameterTypes.Concat(new[] { ReturnType }).Aggregate(0, (a, t) => a * 17 + t.GetHashCode());
  429. }
  430. public override string ToString()
  431. {
  432. return "(" + string.Join(", ", ParameterTypes.Select(t => t.ToCSharp())) + ") -> " + ReturnType.ToCSharp();
  433. }
  434. }
  435. class TypeVisitor
  436. {
  437. public virtual Type Visit(Type type)
  438. {
  439. if (type.IsArray)
  440. {
  441. if (type.GetElementType().MakeArrayType() == type)
  442. {
  443. return VisitArray(type);
  444. }
  445. else
  446. {
  447. return VisitMultidimensionalArray(type);
  448. }
  449. }
  450. else if (type.GetTypeInfo().IsGenericTypeDefinition)
  451. {
  452. return VisitGenericTypeDefinition(type);
  453. }
  454. else if (type.IsConstructedGenericType)
  455. {
  456. return VisitGeneric(type);
  457. }
  458. else if (type.IsByRef)
  459. {
  460. return VisitByRef(type);
  461. }
  462. else if (type.IsPointer)
  463. {
  464. return VisitPointer(type);
  465. }
  466. else
  467. {
  468. return VisitSimple(type);
  469. }
  470. }
  471. protected virtual Type VisitArray(Type type)
  472. {
  473. return Visit(type.GetElementType()).MakeArrayType();
  474. }
  475. protected virtual Type VisitMultidimensionalArray(Type type)
  476. {
  477. return Visit(type.GetElementType()).MakeArrayType(type.GetArrayRank());
  478. }
  479. protected virtual Type VisitGenericTypeDefinition(Type type)
  480. {
  481. return type;
  482. }
  483. protected virtual Type VisitGeneric(Type type)
  484. {
  485. return Visit(type.GetGenericTypeDefinition()).MakeGenericType(Visit(type.GenericTypeArguments));
  486. }
  487. protected virtual Type VisitByRef(Type type)
  488. {
  489. return Visit(type.GetElementType()).MakeByRefType();
  490. }
  491. protected virtual Type VisitPointer(Type type)
  492. {
  493. return Visit(type.GetElementType()).MakePointerType();
  494. }
  495. protected virtual Type VisitSimple(Type type)
  496. {
  497. return type;
  498. }
  499. public Type[] Visit(Type[] types)
  500. {
  501. return types.Select(Visit).ToArray();
  502. }
  503. }
  504. class TypeSubstitutor : TypeVisitor
  505. {
  506. private readonly Dictionary<Type, Type> map;
  507. public TypeSubstitutor(Dictionary<Type, Type> map)
  508. {
  509. this.map = map;
  510. }
  511. public override Type Visit(Type type)
  512. {
  513. if (map.TryGetValue(type, out var subst))
  514. {
  515. return subst;
  516. }
  517. return base.Visit(type);
  518. }
  519. }
  520. private static readonly Type[] Wildcards = new[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4) };
  521. class T1 { }
  522. class T2 { }
  523. class T3 { }
  524. class T4 { }
  525. }
  526. static class TypeExtensions
  527. {
  528. public static string ToCSharp(this Type type)
  529. {
  530. if (type.IsArray)
  531. {
  532. if (type.GetElementType().MakeArrayType() == type)
  533. {
  534. return type.GetElementType().ToCSharp() + "[]";
  535. }
  536. else
  537. {
  538. return type.GetElementType().ToCSharp() + "[" + new string(',', type.GetArrayRank() - 1) + "]";
  539. }
  540. }
  541. else if (type.IsConstructedGenericType)
  542. {
  543. var def = type.GetGenericTypeDefinition();
  544. var defName = def.Name.Substring(0, def.Name.IndexOf('`'));
  545. return defName + "<" + string.Join(", ", type.GetGenericArguments().Select(ToCSharp)) + ">";
  546. }
  547. else
  548. {
  549. return type.Name;
  550. }
  551. }
  552. }
  553. }