Repository navigation
Expand file tree
/
Copy pathScanner.cs
More file actions
256 lines (226 loc) · 11.4 KB
/
Copy pathScanner.cs
File metadata and controls
256 lines (226 loc) · 11.4 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Text.Json;
namespace code_map;
record GraphNode(string From, string[] To);
static class Scanner
{
public static int Run(string dir)
{
var nodes = Scan(dir);
foreach (var node in nodes.Where(n => n.To.Length > 0))
Console.WriteLine($"{node.From} -> {string.Join(", ", node.To)}");
File.WriteAllText("output.json", JsonSerializer.Serialize(nodes, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
}));
return 0;
}
internal static GraphNode[] Scan(string dir)
{
if (!Directory.Exists(dir))
throw new DirectoryNotFoundException($"Input directory not found: {dir}");
var roots = SourceFiles(dir).Order(StringComparer.Ordinal).Select(file =>
{
var tree = CSharpSyntaxTree.ParseText(File.ReadAllText(file), path: file);
var errors = tree.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error).ToArray();
if (errors.Length > 0)
throw new InvalidDataException($"Invalid C# syntax: {errors[0]}");
return tree.GetCompilationUnitRoot();
}).ToArray();
var declarations = roots.SelectMany(r => r.DescendantNodes().Where(IsDeclaration)).ToArray();
var deps = declarations.Select(TypeId).Distinct(StringComparer.Ordinal)
.ToDictionary(id => id, _ => new HashSet<string>(StringComparer.Ordinal), StringComparer.Ordinal);
var lookup = deps.Keys.ToLookup(id => id.Replace('+', '.'), StringComparer.Ordinal);
var namespaces = declarations.SelectMany(d => NamespacePrefixes(GetNamespace(d)))
.ToHashSet(StringComparer.Ordinal);
var globalUsings = roots.SelectMany(r => r.Usings)
.Where(u => u.GlobalKeyword.IsKind(SyntaxKind.GlobalKeyword)).ToArray();
foreach (var declaration in declarations)
{
var from = TypeId(declaration);
var usings = declaration.Ancestors().SelectMany(scope => scope switch
{
BaseNamespaceDeclarationSyntax ns => ns.Usings.AsEnumerable(),
CompilationUnitSyntax root => root.Usings.Where(u => u.GlobalKeyword.IsKind(SyntaxKind.None)),
_ => []
}).Concat(globalUsings).Where(u => !u.StaticKeyword.IsKind(SyntaxKind.StaticKeyword)).ToArray();
foreach (var name in TypeReferences(declaration).Distinct())
{
if (IsTypeParameter(name) || name is IdentifierNameSyntax { Identifier.Text: "var" or "dynamic" })
continue;
var target = Resolve(name, declaration, usings, lookup, namespaces);
if (target != null && target != from)
deps[from].Add(target);
}
}
return deps.OrderBy(kv => kv.Key, StringComparer.Ordinal)
.Select(kv => new GraphNode(kv.Key, kv.Value.Order(StringComparer.Ordinal).ToArray())).ToArray();
}
static IEnumerable<string> SourceFiles(string dir)
{
foreach (var file in Directory.EnumerateFiles(dir, "*.cs"))
yield return file;
foreach (var child in Directory.EnumerateDirectories(dir))
{
if (Path.GetFileName(child) is "bin" or "obj" or ".git" ||
(File.GetAttributes(child) & FileAttributes.ReparsePoint) != 0)
continue;
foreach (var file in SourceFiles(child))
yield return file;
}
}
static bool IsDeclaration(SyntaxNode node) =>
node is BaseTypeDeclarationSyntax or DelegateDeclarationSyntax;
static string TypePart(SyntaxNode declaration) => declaration switch
{
TypeDeclarationSyntax t => t.Identifier.ValueText + Arity(t.TypeParameterList?.Parameters.Count ?? 0),
EnumDeclarationSyntax e => e.Identifier.ValueText,
DelegateDeclarationSyntax d => d.Identifier.ValueText + Arity(d.TypeParameterList?.Parameters.Count ?? 0),
_ => throw new InvalidOperationException("Expected a type declaration.")
};
static string Arity(int count) => count == 0 ? "" : $"`{count}";
static string TypeId(SyntaxNode declaration)
{
var nestedName = string.Join("+", declaration.AncestorsAndSelf().Where(IsDeclaration).Reverse().Select(TypePart));
var ns = GetNamespace(declaration);
return ns.Length == 0 ? nestedName : $"{ns}.{nestedName}";
}
static string GetNamespace(SyntaxNode node) => string.Join(".",
node.Ancestors().OfType<BaseNamespaceDeclarationSyntax>().Reverse().Select(ns => NameKey(ns.Name)));
static IEnumerable<string> NamespacePrefixes(string ns)
{
yield return ns;
while (ns.Contains('.'))
{
ns = ns[..ns.LastIndexOf('.')];
yield return ns;
}
if (ns.Length > 0)
yield return "";
}
static string Join(string prefix, string name) => prefix.Length == 0 ? name : $"{prefix}.{name}";
static string NameKey(NameSyntax name) => name switch
{
IdentifierNameSyntax id => id.Identifier.ValueText,
GenericNameSyntax generic => generic.Identifier.ValueText + Arity(generic.TypeArgumentList.Arguments.Count),
QualifiedNameSyntax qualified => $"{NameKey(qualified.Left)}.{NameKey(qualified.Right)}",
AliasQualifiedNameSyntax alias => $"{alias.Alias.Identifier.ValueText}::{NameKey(alias.Name)}",
_ => throw new InvalidOperationException("Unexpected name syntax.")
};
static IEnumerable<NameSyntax> TypeReferences(SyntaxNode declaration)
{
// Only explicit type positions: ordinary value/member identifiers are not type evidence.
foreach (var node in declaration.DescendantNodes(n => n == declaration || !IsDeclaration(n)))
{
TypeSyntax? type = node switch
{
BaseTypeSyntax n => n.Type,
VariableDeclarationSyntax n => n.Type,
ParameterSyntax n => n.Type,
MethodDeclarationSyntax n => n.ReturnType,
LocalFunctionStatementSyntax n => n.ReturnType,
PropertyDeclarationSyntax n => n.Type,
IndexerDeclarationSyntax n => n.Type,
EventDeclarationSyntax n => n.Type,
ObjectCreationExpressionSyntax n => n.Type,
ArrayCreationExpressionSyntax n => n.Type,
StackAllocArrayCreationExpressionSyntax n => n.Type,
CastExpressionSyntax n => n.Type,
TypeOfExpressionSyntax n => n.Type,
DefaultExpressionSyntax n => n.Type,
SizeOfExpressionSyntax n => n.Type,
DeclarationExpressionSyntax n => n.Type,
DeclarationPatternSyntax n => n.Type,
TypePatternSyntax n => n.Type,
RecursivePatternSyntax n => n.Type,
CatchDeclarationSyntax n => n.Type,
ConversionOperatorDeclarationSyntax n => n.Type,
OperatorDeclarationSyntax n => n.ReturnType,
TypeConstraintSyntax n => n.Type,
ForEachStatementSyntax n => n.Type,
ExplicitInterfaceSpecifierSyntax n => n.Name,
BinaryExpressionSyntax n when n.IsKind(SyntaxKind.AsExpression) || n.IsKind(SyntaxKind.IsExpression)
=> n.Right as TypeSyntax,
_ => null
};
if (type != null)
foreach (var name in NamesInType(type))
yield return name;
if (node is TypeArgumentListSyntax arguments)
foreach (var argument in arguments.Arguments)
foreach (var name in NamesInType(argument))
yield return name;
}
if (declaration is DelegateDeclarationSyntax d)
foreach (var name in NamesInType(d.ReturnType))
yield return name;
}
static IEnumerable<NameSyntax> NamesInType(TypeSyntax type) =>
type.DescendantNodesAndSelf().OfType<NameSyntax>()
.Where(n => n.Parent is not QualifiedNameSyntax and not AliasQualifiedNameSyntax);
static bool IsTypeParameter(NameSyntax name) => name is IdentifierNameSyntax id &&
name.Ancestors().SelectMany(n => n switch
{
TypeDeclarationSyntax t => t.TypeParameterList?.Parameters.AsEnumerable() ?? [],
DelegateDeclarationSyntax d => d.TypeParameterList?.Parameters.AsEnumerable() ?? [],
MethodDeclarationSyntax m => m.TypeParameterList?.Parameters.AsEnumerable() ?? [],
LocalFunctionStatementSyntax f => f.TypeParameterList?.Parameters.AsEnumerable() ?? [],
_ => []
}).Any(p => p.Identifier.ValueText == id.Identifier.ValueText);
static string? Resolve(NameSyntax name, SyntaxNode declaration, UsingDirectiveSyntax[] usings,
ILookup<string, string> lookup, HashSet<string> namespaces)
{
string? Match(IEnumerable<string> keys)
{
var matches = keys.SelectMany(key => lookup[key]).Distinct(StringComparer.Ordinal).ToArray();
if (matches.Length > 1)
Console.Error.WriteLine($"warning: {name.GetLocation().GetLineSpan()}: ambiguous type reference '{name}'; omitted.");
return matches.Length == 1 ? matches[0] : null;
}
var key = NameKey(name);
if (key.StartsWith("global::", StringComparison.Ordinal))
return Match([key["global::".Length..]]);
if (key.Contains("::", StringComparison.Ordinal))
return null; // extern aliases require project/reference information.
var first = key.Split('.')[0];
var alias = usings.FirstOrDefault(u => u.Alias?.Name.Identifier.ValueText == first);
if (alias != null)
{
if (alias.Name == null)
return null;
var target = NameKey(alias.Name);
var suffix = key[first.Length..];
foreach (var candidate in ImportTargets(target, GetNamespace(alias)))
{
if (namespaces.Contains(candidate) || lookup.Contains(candidate))
return Match([candidate + suffix]);
}
return null;
}
foreach (var enclosing in declaration.AncestorsAndSelf().Where(IsDeclaration))
{
var candidate = Join(TypeId(enclosing).Replace('+', '.'), key);
if (lookup.Contains(candidate))
return Match([candidate]);
}
foreach (var prefix in NamespacePrefixes(GetNamespace(declaration)))
{
var local = Join(prefix, key);
if (lookup.Contains(local))
return Match([local]);
var imported = usings.Where(u => u.Alias == null && u.Name != null && GetNamespace(u) == prefix)
.Select(u => ImportTargets(NameKey(u.Name!), GetNamespace(u)).FirstOrDefault(namespaces.Contains))
.OfType<string>().Select(ns => Join(ns, key)).Where(lookup.Contains).ToArray();
if (imported.Length > 0)
return Match(imported);
}
return null;
}
static IEnumerable<string> ImportTargets(string target, string ns) =>
target.StartsWith("global::", StringComparison.Ordinal)
? [target["global::".Length..]]
: NamespacePrefixes(ns).Select(prefix => Join(prefix, target));
}