📄
Core/SolutionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Reacher.MemberCollectors; namespace Reacher; /// <summary></summary> public static class SolutionExtensions { /// <summary> /// Runs a reachability analysis from all entrypoints of all projects in the solution. /// </summary> public static async Task<ReachabilityAnalysis> AnalyzeReachabilityFromEntryPoints( this Solution solution, CancellationToken cancellationToken ) { var compilations = await solution.GetCompilations(cancellationToken); return AnalyzeReachability( compilations, [.. compilations.Select(c => c.GetEntryPoint(cancellationToken)).OfType<ISymbol>()], cancellationToken ); } /// <summary> /// Runs a reachability analysis from all public members of public types of all projects in the solution. /// </summary> public static async Task<ReachabilityAnalysis> AnalyzeReachabilityFromPublicMembers( this Solution solution, CancellationToken cancellationToken ) { var compilations = await solution.GetCompilations(cancellationToken); return AnalyzeReachability( compilations, PublicMembersCollector.CollectMembers(compilations), cancellationToken ); } /// <summary> /// Runs a reachability analysis from the provided Documentation Comment IDs for members. /// </summary> /// <remarks> /// Documentation Comment IDs is usually the kind of member (M for method, P for property), the full namespace, the /// type (and any nested types), and the member name. If it is a method (with one or more parameters) or an indexer, /// the types of the parameters are listed too, with full namespace and type name. /// /// For example, this method would be called <c>M:Reacher.SolutionExtensions.AnalyzeReachabilityFromDocumentationCommentIds(Microsoft.CodeAnalysis.Solution,System.Collections.Generic.IEnumerable{System.String},System.Threading.CancellationToken)</c>. /// </remarks> public static async Task<ReachabilityAnalysis> AnalyzeReachabilityFromDocumentationCommentIds( this Solution solution, IEnumerable<string> documentationIds, CancellationToken cancellationToken ) { var compilations = await solution.GetCompilations(cancellationToken); return AnalyzeReachability( compilations, DocumentationCommentIdMemberCollector.CollectMembers(documentationIds, compilations), cancellationToken ); } private static ReachabilityAnalysis AnalyzeReachability( IReadOnlyCollection<Compilation> compilations, IEnumerable<ISymbol> entryPoints, CancellationToken cancellationToken ) { var analyzer = new ReachabilityAnalyzer(compilations); foreach (var entryPoint in entryPoints) { analyzer.Analyze(entryPoint, cancellationToken); } return analyzer.GetAnalysis(); } private static async Task<Compilation[]> GetCompilations( this Solution solution, CancellationToken cancellationToken ) => await solution .Projects.ToAsyncEnumerable() .Select(async (p, ct) => await p.GetCompilationAsync(ct)) .OfType<Compilation>() .ToArrayAsync(cancellationToken); }