| Name | Message | Date |
|---|---|---|
| 📄 CalleBuilder.cs | 7 days ago | |
| 📄 CallGraph.cs | 7 days ago | |
| 📄 Cfa.csproj | 7 days ago | |
| 📄 CompilationsExtensions.cs | 7 days ago | |
| 📄 packages.lock.json | 7 days ago | |
| 📄 Scanner.cs | 7 days ago |
📄
core/src/CalleBuilder.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Threading; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Cfa; internal sealed class CalleeBuilder { public static IImmutableDictionary<ISymbol, IImmutableList<Call>> FindCallees( IReadOnlyCollection<Compilation> compilations, ISymbol symbol, CancellationToken cancellationToken ) { var builder = new CalleeBuilder( compilations, [ .. compilations .SelectMany(compilation => compilation.GetSymbolsWithName(_ => true, SymbolFilter.Type, cancellationToken) ) .OfType<INamedTypeSymbol>(), ] ); builder.FindCallees(symbol, cancellationToken); return builder.reachableFromSymbols.ToImmutable(); } private readonly IReadOnlyCollection<Compilation> compilations; private readonly IReadOnlyCollection<INamedTypeSymbol> types; private readonly HashSet<ISymbol> seenMember = [with(SymbolEqualityComparer.Default)]; private readonly ImmutableDictionary<ISymbol, IImmutableList<Call>>.Builder reachableFromSymbols = ImmutableDictionary.CreateBuilder<ISymbol, IImmutableList<Call>>(SymbolEqualityComparer.Default); private CalleeBuilder(IReadOnlyCollection<Compilation> compilations, IReadOnlyCollection<INamedTypeSymbol> types) { this.compilations = compilations; this.types = types; } private void FindCallees(ISymbol symbol, CancellationToken cancellationToken) { if (!seenMember.Add(symbol)) { return; } var seenCall = new Dictionary<ISymbol, LinkKind>(SymbolEqualityComparer.Default); foreach (var reference in symbol.DeclaringSyntaxReferences) { var syntax = reference.GetSyntax(cancellationToken); var semanticModelForSyntax = compilations.GetSemanticModel(syntax.SyntaxTree); var accesses = syntax .DescendantNodesAndSelf() .Where(node => node is SimpleNameSyntax or ElementAccessExpressionSyntax or ElementBindingExpressionSyntax or BaseObjectCreationExpressionSyntax or ConstructorInitializerSyntax ); foreach (var access in accesses) { var accessedSymbol = semanticModelForSyntax.GetSymbolInfo(access, cancellationToken).Symbol; if (accessedSymbol is not (IMethodSymbol or IPropertySymbol)) { continue; } if (accessedSymbol.IsAbstract || accessedSymbol.IsVirtual) { foreach (var implementation in FindImplementations(accessedSymbol)) { AddCall(implementation, LinkKind.Abstract); } } if (!accessedSymbol.IsAbstract) { AddCall(accessedSymbol, LinkKind.Direct); } } } reachableFromSymbols.Add(symbol, seenCall.Select(call => new Call(call.Key, call.Value)).ToImmutableList()); void AddCall(ISymbol target, LinkKind linkKind) { if ( !IsDeclaredInSource(target) || linkKind == LinkKind.Abstract && target.IsAbstract || target is IPropertySymbol property && !HasBody(property, cancellationToken) ) { return; } if (seenCall.TryAdd(target, linkKind)) { FindCallees(target, cancellationToken); } else if (linkKind is LinkKind.Direct) { seenCall[target] = LinkKind.Direct; } } } private IEnumerable<ISymbol> FindImplementations(ISymbol member) { foreach (var type in types) { if (member.ContainingType.TypeKind is TypeKind.Interface) { if (type.FindImplementationForInterfaceMember(member) is { } implementation) { yield return implementation; } continue; } foreach (var candidate in type.GetMembers(member.Name)) { for ( var overridden = GetOverriddenMember(candidate); overridden is not null; overridden = GetOverriddenMember(overridden) ) { if (SymbolEqualityComparer.Default.Equals(overridden.OriginalDefinition, member.OriginalDefinition)) { yield return candidate; break; } } } } } private static ISymbol? GetOverriddenMember(ISymbol member) => member switch { IMethodSymbol method => method.OverriddenMethod, IPropertySymbol property => property.OverriddenProperty, _ => null, }; private static bool IsDeclaredInSource(ISymbol symbol) => symbol.DeclaringSyntaxReferences.Length > 0 && !symbol.IsImplicitlyDeclared; private static bool HasBody(IPropertySymbol property, CancellationToken cancellationToken) => property.DeclaringSyntaxReferences.Any(reference => reference.GetSyntax(cancellationToken) switch { PropertyDeclarationSyntax { ExpressionBody: not null } => true, IndexerDeclarationSyntax { ExpressionBody: not null } => true, BasePropertyDeclarationSyntax declaration => declaration.AccessorList?.Accessors.Any(accessor => accessor.Body is not null || accessor.ExpressionBody is not null ) is true, _ => false, } ); }