Skip to content

Commit 6de27eb

Browse files
committed
Add eventsource event method generate
1 parent c87cbf6 commit 6de27eb

24 files changed

+1079
-5
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.DotnetRuntime.Extensions;
9+
10+
namespace Generators
11+
{
12+
public partial class EventSourceEventGenerator
13+
{
14+
private const string Category = "System.Diagnostics.Tracing";
15+
16+
private static DiagnosticDescriptor EventSourceNoSupportType { get; } = DiagnosticDescriptorHelper.Create(
17+
id: "SYSLIB2000", //I don't know what id need I set
18+
title: new LocalizableResourceString(nameof(SR.EventSourceGeneratorTitle), SR.ResourceManager, typeof(FxResources.System.Private.CoreLib.Generators.SR)),
19+
messageFormat: new LocalizableResourceString(nameof(SR.EventSourceNoSupportType), SR.ResourceManager, typeof(FxResources.System.Private.CoreLib.Generators.SR)),
20+
category: Category,
21+
DiagnosticSeverity.Error,
22+
isEnabledByDefault: true,
23+
customTags: WellKnownDiagnosticTags.NotConfigurable);
24+
25+
private static DiagnosticDescriptor ContextClassesMustBePartial { get; } = DiagnosticDescriptorHelper.Create(
26+
id: "SYSLIB2001", //I don't know what id need I set
27+
title: new LocalizableResourceString(nameof(SR.ContextClassesMustBePartialTitle), SR.ResourceManager, typeof(FxResources.System.Private.CoreLib.Generators.SR)),
28+
messageFormat: new LocalizableResourceString(nameof(SR.ContextClassesMustBePartialMessageFormat), SR.ResourceManager, typeof(FxResources.System.Private.CoreLib.Generators.SR)),
29+
category: Category,
30+
DiagnosticSeverity.Error,
31+
isEnabledByDefault: true,
32+
customTags: WellKnownDiagnosticTags.NotConfigurable);
33+
}
34+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.CodeDom.Compiler;
6+
using System.Collections.Generic;
7+
using System.Diagnostics;
8+
using System.IO;
9+
using System.Private.CoreLib.Generators.Models;
10+
using System.Text;
11+
12+
namespace Generators
13+
{
14+
public partial class EventSourceEventGenerator
15+
{
16+
private sealed partial class Emiitter
17+
{
18+
private const string NonEventAttribute = "global::System.Diagnostics.Tracing.NonEventAttribute";
19+
private const string EventData = "global::System.Diagnostics.Tracing.EventSource.EventData";
20+
private const string IntPtrZero = "global::System.IntPtr.Zero";
21+
private const string AsPointer = "global::System.Runtime.CompilerServices.Unsafe.AsPointer";
22+
private const string GetReference = "global::System.Runtime.InteropServices.MemoryMarshal.GetReference";
23+
private const string AsSpan = "global::System.MemoryExtensions.AsSpan";
24+
25+
public void Emit(EventMethodsParsedResult result, StringBuilder stringBuilder)
26+
{
27+
var writer = new IndentedTextWriter(new StringWriter(stringBuilder));
28+
writer.WriteLine("// <auto-generated/>");
29+
30+
if (!string.IsNullOrEmpty(result.Namespace))
31+
{
32+
writer.WriteLine($"namespace {result.Namespace}");
33+
writer.WriteLine('{');
34+
writer.Indent++;
35+
}
36+
37+
foreach (string classDeclaration in result.ContextClassDeclarations)
38+
{
39+
writer.WriteLine(classDeclaration);
40+
writer.WriteLine('{');
41+
writer.Indent++;
42+
}
43+
44+
EmitClass(result, writer);
45+
46+
foreach (string _ in result.ContextClassDeclarations)
47+
{
48+
writer.WriteLine('}');
49+
writer.Indent--;
50+
}
51+
52+
if (!string.IsNullOrEmpty(result.Namespace))
53+
{
54+
writer.WriteLine('}');
55+
writer.Indent--;
56+
}
57+
}
58+
59+
private void EmitClass(EventMethodsParsedResult result, IndentedTextWriter writer)
60+
{
61+
Debug.Assert(result != null);
62+
Debug.Assert(writer != null);
63+
64+
if (result.Methods.Length == 0)
65+
{
66+
return;
67+
}
68+
69+
foreach (EventMethod method in result.Methods)
70+
{
71+
EmitMethod(method, writer);
72+
}
73+
74+
}
75+
76+
///<summary>Emits the event partial method</summary>
77+
private void EmitMethod(EventMethod method, IndentedTextWriter writer)
78+
{
79+
Debug.Assert(method != null);
80+
Debug.Assert(writer != null);
81+
Debug.Assert(method.Arguments != null);
82+
83+
//Write method header
84+
writer.WriteLine("");
85+
writer.WriteLine('{');
86+
writer.Indent++;
87+
88+
if (method.Arguments.Count != 0)
89+
{
90+
writer.WriteLine($"{EventData}* datas = stackalloc {EventData}[{method.Arguments.Count}]");
91+
92+
foreach (EventMethodArgument item in method.Arguments)
93+
{
94+
EmitArgument(item, writer);
95+
}
96+
97+
writer.WriteLine($"WriteEventCore({method.EventId}, {method.Arguments.Count}, datas);");
98+
}
99+
else
100+
{
101+
writer.WriteLine($"WriteEvent({method.EventId});");
102+
}
103+
104+
writer.WriteLine($"On{method.Name}({string.Join(", ", method.Arguments.Select(x => x.Name))});");
105+
106+
writer.Indent--;
107+
writer.WriteLine('}');
108+
writer.WriteLine();
109+
writer.WriteLine($"[{NonEventAttribute}]");
110+
writer.WriteLine($"partial void On{method.Name}({string.Join(", ", method.Arguments.Select(x => $"{x.TypeName} {x.Name}"))});");
111+
writer.WriteLine();
112+
}
113+
114+
///<summary>Emits the argument set the EventData to the `datas` variable</summary>
115+
private static void EmitArgument(EventMethodArgument argument, IndentedTextWriter writer)
116+
{
117+
writer.WriteLine($"datas[{argument.Index}] = new {EventData}");
118+
writer.WriteLine('{');
119+
writer.Indent++;
120+
writer.WriteLine($"DataPointer = {argument.Name} == null ? {IntPtrZero} : (nint){AsPointer}(ref {GetReference}({AsSpan}({argument.Name}))),");
121+
writer.WriteLine($"Size = {argument.Name} == null ? 0 : (({argument.Name}.Length + 1) * sizeof(char)),");
122+
writer.WriteLine("Reserved = 0");//I don't know if this is necessary or not
123+
writer.Indent--;
124+
writer.WriteLine('}');
125+
}
126+
127+
}
128+
129+
}
130+
}

0 commit comments

Comments
 (0)