Compare commits

...

2 Commits

Author SHA1 Message Date
Gordon Lam (SH)
8cfec56b26 Update props 2025-07-30 13:49:03 +08:00
Gordon Lam (SH)
d7f28e7361 Dump types, given folder of cs, and the build out of cs 2025-07-30 13:46:24 +08:00
3 changed files with 88 additions and 0 deletions

View File

@@ -33,6 +33,7 @@
<PackageVersion Include="Markdig.Signed" Version="0.34.0" />
<!-- Including MessagePack to force version, since it's used by StreamJsonRpc but contains vulnerabilities. After StreamJsonRpc updates the version of MessagePack, we can upgrade StreamJsonRpc instead. -->
<PackageVersion Include="MessagePack" Version="3.1.3" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="9.0.7" />
<!-- Including Microsoft.Bcl.AsyncInterfaces to force version, since it's used by Microsoft.SemanticKernel. -->

View File

@@ -0,0 +1,72 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: dotnet run -- <CSharpSourceFolder> <BinFolderWithReferences>");
return;
}
string sourceFolder = args[0];
string refsFolder = args[1];
if (!Directory.Exists(sourceFolder) || !Directory.Exists(refsFolder))
{
Console.WriteLine("Invalid folder path(s).");
return;
}
Console.WriteLine($"Analyzing source: {sourceFolder}");
Console.WriteLine($"Using references: {refsFolder}");
var trees = Directory.GetFiles(sourceFolder, "*.cs", SearchOption.AllDirectories)
.Select(file => CSharpSyntaxTree.ParseText(File.ReadAllText(file), path: file))
.ToList();
var references = Directory.GetFiles(refsFolder, "*.dll")
.Select(dll => MetadataReference.CreateFromFile(dll))
.ToList();
var compilation = CSharpCompilation.Create("TypeAnalysis", trees, references);
var typesUsed = new HashSet<string>();
foreach (var tree in trees)
{
var model = compilation.GetSemanticModel(tree);
var root = tree.GetRoot();
foreach (var node in root.DescendantNodes())
{
ISymbol? symbol = node switch
{
IdentifierNameSyntax ins => model.GetSymbolInfo(ins).Symbol,
ObjectCreationExpressionSyntax oces => model.GetTypeInfo(oces).Type,
TypeOfExpressionSyntax toes => model.GetTypeInfo(toes.Type).Type,
BaseTypeSyntax bts => model.GetTypeInfo(bts.Type).Type,
_ => null
};
if (symbol is INamedTypeSymbol typeSymbol && typeSymbol.TypeKind != TypeKind.Error)
{
typesUsed.Add(typeSymbol.ToDisplayString());
}
}
}
Console.WriteLine("\n✅ Referenced types:");
foreach (var type in typesUsed.OrderBy(t => t))
{
Console.WriteLine("- " + type);
}
}
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\Common.Dotnet.CsWinRT.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>SA1400;SA1208;SA1413;CA1852;CS8600;CS8604;SA1633</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp"/>
</ItemGroup>
</Project>