-
Notifications
You must be signed in to change notification settings - Fork 1.7k
C#: Roslyn-based stub generation #14095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2429a53
C#: Move `NestPaths` to `Semmle.Util`
hvitved 6021d00
C#: Move some methods into newly created `Semmle.Extraction.CSharp.Ut…
hvitved e021fb4
C#: Roslyn based stub generation
hvitved 8b2c233
C#: Use new stub generator in `make_stubs_nuget.py`
hvitved 58f45ea
C#: Regenerate `Newtonsoft.Json` stubs
hvitved c547adc
C#: Regenerate `Microsoft.NetCore.App` stubs
hvitved e944b90
C#: Regenerate `Microsoft.AspNetCore.App` stubs
hvitved 2343e5e
C#: Regenerate `NHibernate` stubs
hvitved 04c4e73
Address review comments
hvitved 4805e2a
Address more review comments
hvitved f07d02b
Regenerate stubs
hvitved 831baa8
C#: Refactor and regenerate stubs
hvitved File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
csharp/extractor/Semmle.Extraction.CSharp.DependencyStubGenerator/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Semmle.Extraction.CSharp.DependencyFetching; | ||
using Semmle.Extraction.CSharp.StubGenerator; | ||
using Semmle.Util.Logging; | ||
|
||
var logger = new ConsoleLogger(Verbosity.Info, logThreadId: false); | ||
using var dependencyManager = new DependencyManager(".", DependencyOptions.Default, logger); | ||
StubGenerator.GenerateStubs(logger, dependencyManager.ReferenceFiles, "codeql_csharp_stubs"); | ||
|
||
return 0; |
18 changes: 18 additions & 0 deletions
18
...on.CSharp.DependencyStubGenerator/Semmle.Extraction.CSharp.DependencyStubGenerator.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<AssemblyName>Semmle.Extraction.CSharp.DependencyStubGenerator</AssemblyName> | ||
<RootNamespace>Semmle.Extraction.CSharp.DependencyStubGenerator</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Semmle.Util\Semmle.Util.csproj" /> | ||
<ProjectReference Include="..\Semmle.Extraction.CSharp.DependencyFetching\Semmle.Extraction.CSharp.DependencyFetching.csproj" /> | ||
<ProjectReference Include="..\Semmle.Extraction.CSharp.StubGenerator\Semmle.Extraction.CSharp.StubGenerator.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
33 changes: 33 additions & 0 deletions
33
csharp/extractor/Semmle.Extraction.CSharp.StubGenerator/RelevantSymbol.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Linq; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
using Semmle.Util; | ||
|
||
namespace Semmle.Extraction.CSharp.StubGenerator; | ||
|
||
internal class RelevantSymbol | ||
{ | ||
private readonly IAssemblySymbol assembly; | ||
private readonly MemoizedFunc<INamedTypeSymbol, bool> isRelevantNamedType; | ||
private readonly MemoizedFunc<INamespaceSymbol, bool> isRelevantNamespace; | ||
|
||
public RelevantSymbol(IAssemblySymbol assembly) | ||
{ | ||
this.assembly = assembly; | ||
|
||
isRelevantNamedType = new(symbol => | ||
StubVisitor.IsRelevantBaseType(symbol) && | ||
SymbolEqualityComparer.Default.Equals(symbol.ContainingAssembly, assembly)); | ||
|
||
isRelevantNamespace = new(symbol => | ||
symbol.GetTypeMembers().Any(IsRelevantNamedType) || | ||
symbol.GetNamespaceMembers().Any(IsRelevantNamespace)); | ||
} | ||
|
||
public bool IsRelevantNamedType(INamedTypeSymbol symbol) => isRelevantNamedType.Invoke(symbol); | ||
|
||
public bool IsRelevantNamespace(INamespaceSymbol symbol) => isRelevantNamespace.Invoke(symbol); | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
...ctor/Semmle.Extraction.CSharp.StubGenerator/Semmle.Extraction.CSharp.StubGenerator.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<AssemblyName>Semmle.Extraction.CSharp.StubGenerator</AssemblyName> | ||
<RootNamespace>Semmle.Extraction.CSharp.StubGenerator</RootNamespace> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Semmle.Util\Semmle.Util.csproj" /> | ||
<ProjectReference Include="..\Semmle.Extraction.CSharp.DependencyFetching\Semmle.Extraction.CSharp.DependencyFetching.csproj" /> | ||
<ProjectReference Include="..\Semmle.Extraction.CSharp.Util\Semmle.Extraction.CSharp.Util.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" /> | ||
</ItemGroup> | ||
</Project> |
81 changes: 81 additions & 0 deletions
81
csharp/extractor/Semmle.Extraction.CSharp.StubGenerator/StubGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
using Semmle.Util; | ||
using Semmle.Util.Logging; | ||
|
||
namespace Semmle.Extraction.CSharp.StubGenerator; | ||
|
||
public static class StubGenerator | ||
{ | ||
/// <summary> | ||
/// Generates stubs for all the provided assembly paths. | ||
/// </summary> | ||
/// <param name="referencesPaths">The paths of the assemblies to generate stubs for.</param> | ||
/// <param name="outputPath">The path in which to store the stubs.</param> | ||
public static void GenerateStubs(ILogger logger, IEnumerable<string> referencesPaths, string outputPath) | ||
{ | ||
var stopWatch = new System.Diagnostics.Stopwatch(); | ||
stopWatch.Start(); | ||
|
||
var threads = EnvironmentVariables.GetDefaultNumberOfThreads(); | ||
|
||
using var references = new BlockingCollection<(MetadataReference Reference, string Path)>(); | ||
|
||
Parallel.ForEach(referencesPaths, new ParallelOptions { MaxDegreeOfParallelism = threads }, path => | ||
{ | ||
var reference = MetadataReference.CreateFromFile(path); | ||
references.Add((reference, path)); | ||
}); | ||
|
||
logger.Log(Severity.Info, $"Generating stubs for {references.Count} assemblies."); | ||
|
||
var compilation = CSharpCompilation.Create( | ||
"stubgenerator.dll", | ||
null, | ||
references.Select(tuple => tuple.Item1), | ||
new CSharpCompilationOptions(OutputKind.ConsoleApplication, allowUnsafe: true)); | ||
tamasvajk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Parallel.ForEach(references, new ParallelOptions { MaxDegreeOfParallelism = threads }, @ref => | ||
{ | ||
StubReference(logger, compilation, outputPath, @ref.Reference, @ref.Path); | ||
}); | ||
|
||
stopWatch.Stop(); | ||
logger.Log(Severity.Info, $"Stub generation took {stopWatch.Elapsed}."); | ||
} | ||
|
||
private static void StubReference(ILogger logger, CSharpCompilation compilation, string outputPath, MetadataReference reference, string path) | ||
{ | ||
if (compilation.GetAssemblyOrModuleSymbol(reference) is not IAssemblySymbol assembly) | ||
return; | ||
|
||
var relevantSymbol = new RelevantSymbol(assembly); | ||
|
||
if (!assembly.Modules.Any(m => relevantSymbol.IsRelevantNamespace(m.GlobalNamespace))) | ||
return; | ||
|
||
using var fileStream = new FileStream(FileUtils.NestPaths(logger, outputPath, path.Replace(".dll", ".cs")), FileMode.Create, FileAccess.Write); | ||
using var writer = new StreamWriter(fileStream, new UTF8Encoding(false)); | ||
|
||
var visitor = new StubVisitor(writer, relevantSymbol); | ||
|
||
writer.WriteLine("// This file contains auto-generated code."); | ||
writer.WriteLine($"// Generated from `{assembly.Identity}`."); | ||
|
||
visitor.StubAttributes(assembly.GetAttributes(), "assembly: "); | ||
hvitved marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
foreach (var module in assembly.Modules) | ||
{ | ||
module.GlobalNamespace.Accept(visitor); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.