DeviceDependentGenerator added to tasks.json. It can now generate the files during VS Code builds. Ignore list updated.

This commit is contained in:
mlemanczyk committed 2025-12-18 09:39:56 +01:00
1 parent af7c9491e4
commit 8ea975f2ed
4 files changed
+72 -6

No files matched your search

+3 -1
View File
@@ -17,4 +17,6 @@ DeviceDependentGenerator.Tests/obj/*
PerfectNumbers.Core.Tests/bin/*
PerfectNumbers.Core.Tests/obj/*
.DS_Store
.vs/*
.vs/*
*.generated.cs
PerfectNumbers.Core/generated/*
+4 -2
View File
@@ -11,7 +11,8 @@
"--configuration",
"Debug",
"--framework",
"net8.0"
"net8.0",
"/p:DeviceDependentGenerator_CopyGeneratedToProjectDir=true"
],
"group": {
"kind": "build",
@@ -29,7 +30,8 @@
"--configuration",
"Release",
"--framework",
"net8.0"
"net8.0",
"/p:DeviceDependentGenerator_CopyGeneratedToProjectDir=true"
],
"group": "build",
"problemMatcher": "$msCompile"
@@ -54,9 +54,12 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
IncrementalValueProvider<GeneratorOptions> options = context.AnalyzerConfigOptionsProvider.Select(
static (provider, _) => new GeneratorOptions(provider.GlobalOptions));
context.RegisterSourceOutput(templates.Combine(options), static (productionContext, pair) =>
IncrementalValueProvider<(Compilation Compilation, GeneratorOptions Options)> compilationAndOptions =
context.CompilationProvider.Combine(options).Select(static (pair, _) => (pair.Left, pair.Right));
context.RegisterSourceOutput(templates.Combine(compilationAndOptions), static (productionContext, pair) =>
{
GenerateForTemplate(productionContext, pair.Left, pair.Right);
GenerateForTemplate(productionContext, pair.Left, pair.Right.Compilation, pair.Right.Options);
});
}
@@ -119,7 +122,7 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
return TemplateInfo.CreateValid(templateSymbol, enumTypeSymbol, attributeLocation, enumValues, suffix);
}
private static void GenerateForTemplate(SourceProductionContext context, TemplateInfo template, GeneratorOptions options)
private static void GenerateForTemplate(SourceProductionContext context, TemplateInfo template, Compilation compilation, GeneratorOptions options)
{
if (!template.Valid)
{
@@ -146,6 +149,7 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
SourceText fileText = templateSyntax.SyntaxTree.GetText(context.CancellationToken);
string filePath = templateSyntax.SyntaxTree.FilePath;
CSharpParseOptions baseParseOptions = (CSharpParseOptions)templateSyntax.SyntaxTree.Options;
SyntaxTree originalTree = templateSyntax.SyntaxTree;
foreach (string enumValueName in template.EnumValues)
{
@@ -166,6 +170,8 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
continue;
}
ReportDeviceSpecificTemplateErrors(context, compilation, originalTree, deviceTree, templateSyntax.Identifier.GetLocation(), enumValueName);
string generatedTypeName = baseName + enumValueName + template.Suffix;
TypeDeclarationSyntax generatedTypeSyntax = TransformTemplateType(deviceTemplateSyntax, generatedTypeName);
@@ -186,6 +192,45 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
}
}
private static void ReportDeviceSpecificTemplateErrors(
SourceProductionContext context,
Compilation compilation,
SyntaxTree originalTree,
SyntaxTree deviceTree,
Location fallbackLocation,
string enumValueName)
{
foreach (Diagnostic diagnostic in deviceTree.GetDiagnostics(context.CancellationToken))
{
if (diagnostic.Severity == DiagnosticSeverity.Error)
{
context.ReportDiagnostic(diagnostic);
}
}
if (compilation is not CSharpCompilation csharpCompilation)
{
return;
}
CSharpCompilation deviceCompilation = csharpCompilation.ReplaceSyntaxTree(originalTree, deviceTree);
foreach (Diagnostic diagnostic in deviceCompilation.GetDiagnostics(context.CancellationToken))
{
if (diagnostic.Severity != DiagnosticSeverity.Error)
{
continue;
}
if (diagnostic.Location is { Kind: LocationKind.SourceFile } location
&& location.SourceTree != deviceTree)
{
continue;
}
context.ReportDiagnostic(diagnostic);
}
}
private static bool IsTopLevel(TypeDeclarationSyntax typeDeclaration)
=> typeDeclaration.Parent is NamespaceDeclarationSyntax
|| typeDeclaration.Parent is FileScopedNamespaceDeclarationSyntax
@@ -5,8 +5,14 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(IntermediateOutputPath)generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Remove="**/*.generated.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="PeterO.Numbers" Version="1.8.1" />
<PackageReference Include="Open.Numeric.Primes" Version="4.0.4" />
@@ -28,4 +34,15 @@
<InternalsVisibleTo Include="EvenPerfectBitScanner.Benchmarks" />
</ItemGroup>
<Target Name="CopyDeviceDependentGeneratedToProjectDir"
AfterTargets="CoreCompile"
Condition="'$(DeviceDependentGenerator_CopyGeneratedToProjectDir)' == 'true' Or '$(BuildingInsideVisualStudio)' == 'true'">
<ItemGroup>
<_DeviceDependentGeneratedFiles Include="$(CompilerGeneratedFilesOutputPath)\DeviceDependentGenerator\PerfectNumbers.Core.Generators.DeviceDependentGenerator\**\*.generated.cs" />
</ItemGroup>
<Copy SourceFiles="@(_DeviceDependentGeneratedFiles)"
DestinationFiles="@(_DeviceDependentGeneratedFiles->'$(ProjectDir)%(RecursiveDir)%(Filename)%(Extension)')"
SkipUnchangedFiles="true" />
</Target>
</Project>