Skip to content
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

EventSourceGenerator for Guid+Name .ctor and Metadata #45699

Merged
merged 5 commits into from Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/Versions.props
Expand Up @@ -46,6 +46,7 @@
<PropertyGroup>
<MicrosoftCodeAnalysisNetAnalyzersVersion>6.0.0-preview1.21054.10</MicrosoftCodeAnalysisNetAnalyzersVersion>
<MicrosoftCodeAnalysisCSharpCodeStyleVersion>3.8.0-4.20503.2</MicrosoftCodeAnalysisCSharpCodeStyleVersion>
<MicrosoftCodeAnalysisCSharpVersion>3.8.0</MicrosoftCodeAnalysisCSharpVersion>
<!-- Arcade dependencies -->
<MicrosoftDotNetApiCompatVersion>6.0.0-beta.21062.10</MicrosoftDotNetApiCompatVersion>
<MicrosoftDotNetBuildTasksFeedVersion>6.0.0-beta.21062.10</MicrosoftDotNetBuildTasksFeedVersion>
Expand Down
Expand Up @@ -13,6 +13,8 @@
<Configurations>Debug;Release;Checked</Configurations>
<Platforms>x64;x86;arm;arm64</Platforms>

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>

<ILLinkTrimAssembly>true</ILLinkTrimAssembly>
<ILLinkTrimXml>$(IntermediateOutputPath)System.Private.CoreLib.xml</ILLinkTrimXml>
<ILLinkDirectory>$(MSBuildThisFileDirectory)src\ILLink\</ILLinkDirectory>
Expand Down Expand Up @@ -301,7 +303,11 @@
</Compile>
</ItemGroup>
<Import Project="$(LibrariesProjectRoot)\System.Private.CoreLib\src\System.Private.CoreLib.Shared.projitems" Label="Shared" />


<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)\System.Private.CoreLib\generators\System.Private.CoreLib.Generators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<!-- This is the T4 template service and is added by VS anytime you modify a T4 template. Required for .tt files. -->
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
Expand Down
38 changes: 38 additions & 0 deletions src/coreclr/System.Private.CoreLib/System.Private.CoreLib.sln
Expand Up @@ -6,28 +6,34 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Private.CoreLib", "S
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "System.Private.CoreLib.Shared", "..\..\libraries\System.Private.CoreLib\src\System.Private.CoreLib.Shared.shproj", "{845C8B26-350B-4E63-BD11-2C8150444E28}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Private.CoreLib.Generators", "..\..\libraries\System.Private.CoreLib\generators\System.Private.CoreLib.Generators.csproj", "{7196828B-5E00-4BC6-9A1E-492C948E41A3}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\..\libraries\System.Private.CoreLib\src\System.Private.CoreLib.Shared.projitems*{3da06c3a-2e7b-4cb7-80ed-9b12916013f9}*SharedItemsImports = 5
..\..\libraries\System.Private.CoreLib\src\System.Private.CoreLib.Shared.projitems*{845c8b26-350b-4e63-bd11-2c8150444e28}*SharedItemsImports = 13
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Checked|amd64 = Checked|amd64
Checked|Any CPU = Checked|Any CPU
Checked|arm = Checked|arm
Checked|arm64 = Checked|arm64
Checked|x86 = Checked|x86
Debug|amd64 = Debug|amd64
Debug|Any CPU = Debug|Any CPU
Debug|arm = Debug|arm
Debug|arm64 = Debug|arm64
Debug|x86 = Debug|x86
Release|amd64 = Release|amd64
Release|Any CPU = Release|Any CPU
Release|arm = Release|arm
Release|arm64 = Release|arm64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Checked|amd64.ActiveCfg = Checked|x64
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Checked|amd64.Build.0 = Checked|x64
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Checked|Any CPU.ActiveCfg = Checked|x86
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Checked|arm.ActiveCfg = Checked|arm
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Checked|arm.Build.0 = Checked|arm
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Checked|arm64.ActiveCfg = Checked|arm64
Expand All @@ -36,6 +42,7 @@ Global
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Checked|x86.Build.0 = Checked|x86
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Debug|amd64.ActiveCfg = Debug|x64
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Debug|amd64.Build.0 = Debug|x64
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Debug|Any CPU.ActiveCfg = Debug|x86
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Debug|arm.ActiveCfg = Debug|arm
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Debug|arm.Build.0 = Debug|arm
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Debug|arm64.ActiveCfg = Debug|arm64
Expand All @@ -44,12 +51,43 @@ Global
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Debug|x86.Build.0 = Debug|x86
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Release|amd64.ActiveCfg = Release|x64
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Release|amd64.Build.0 = Release|x64
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Release|Any CPU.ActiveCfg = Release|x86
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Release|arm.ActiveCfg = Release|arm
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Release|arm.Build.0 = Release|arm
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Release|arm64.ActiveCfg = Release|arm64
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Release|arm64.Build.0 = Release|arm64
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Release|x86.ActiveCfg = Release|x86
{3DA06C3A-2E7B-4CB7-80ED-9B12916013F9}.Release|x86.Build.0 = Release|x86
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Checked|amd64.ActiveCfg = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Checked|amd64.Build.0 = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Checked|Any CPU.ActiveCfg = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Checked|Any CPU.Build.0 = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Checked|arm.ActiveCfg = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Checked|arm.Build.0 = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Checked|arm64.ActiveCfg = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Checked|arm64.Build.0 = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Checked|x86.ActiveCfg = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Checked|x86.Build.0 = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Debug|amd64.ActiveCfg = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Debug|amd64.Build.0 = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Debug|arm.ActiveCfg = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Debug|arm.Build.0 = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Debug|arm64.ActiveCfg = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Debug|arm64.Build.0 = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Debug|x86.ActiveCfg = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Debug|x86.Build.0 = Debug|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Release|amd64.ActiveCfg = Release|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Release|amd64.Build.0 = Release|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Release|Any CPU.Build.0 = Release|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Release|arm.ActiveCfg = Release|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Release|arm.Build.0 = Release|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Release|arm64.ActiveCfg = Release|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Release|arm64.Build.0 = Release|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Release|x86.ActiveCfg = Release|Any CPU
{7196828B-5E00-4BC6-9A1E-492C948E41A3}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
@@ -0,0 +1,109 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Text;
using System.Threading;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace Generators
{
public partial class EventSourceGenerator
{
private class Emitter
{
private readonly StringBuilder _builder = new StringBuilder(1024);
private readonly GeneratorExecutionContext _context;

public Emitter(GeneratorExecutionContext context) => _context = context;

public void Emit(EventSourceClass[] eventSources, CancellationToken cancellationToken)
{
foreach (EventSourceClass? ec in eventSources)
{
if (cancellationToken.IsCancellationRequested)
{
// stop any additional work
break;
}

_builder.AppendLine("using System;");
GenType(ec);

_context.AddSource($"{ec.ClassName}.Generated", SourceText.From(_builder.ToString(), Encoding.UTF8));

_builder.Clear();
}
}

private void GenType(EventSourceClass ec)
{
if (!string.IsNullOrWhiteSpace(ec.Namespace))
{
_builder.AppendLine($@"
namespace {ec.Namespace}
{{");
benaadams marked this conversation as resolved.
Show resolved Hide resolved
}

_builder.AppendLine($@"
partial class {ec.ClassName}
{{");
GenerateConstructor(ec);

GenerateProviderMetadata(ec.SourceName);

_builder.AppendLine($@"
}}");

if (!string.IsNullOrWhiteSpace(ec.Namespace))
{
_builder.AppendLine($@"
}}");
}
}

private void GenerateConstructor(EventSourceClass ec)
{
_builder.AppendLine($@"
private {ec.ClassName}() : base(new Guid({ec.Guid.ToString("x").Replace("{", "").Replace("}", "")}), ""{ec.SourceName}"") {{ }}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever, eliminates the parsing at execution time.

}

private void GenerateProviderMetadata(string sourceName)
{
_builder.Append(@"
private protected override ReadOnlySpan<byte> ProviderMetadata => new byte[] { ");

byte[] metadataBytes = MetadataForString(sourceName);
foreach (byte b in metadataBytes)
{
_builder.Append($"0x{b:x}, ");
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}

_builder.AppendLine(@"};");
}

// From System.Private.CoreLib
private static byte[] MetadataForString(string name)
{
CheckName(name);
int metadataSize = Encoding.UTF8.GetByteCount(name) + 3;
byte[]? metadata = new byte[metadataSize];
ushort totalSize = checked((ushort)(metadataSize));
metadata[0] = unchecked((byte)totalSize);
metadata[1] = unchecked((byte)(totalSize >> 8));
Encoding.UTF8.GetBytes(name, 0, name.Length, metadata, 2);
return metadata;
}

private static void CheckName(string? name)
{
if (name != null && 0 <= name.IndexOf('\0'))
{
throw new ArgumentOutOfRangeException(nameof(name));
}
}
}
}
}