Skip to content

Commit d133824

Browse files
authored
Handle provider names that start with a numeric digit. (#2369)
C# names can't start with numeric digits, so if a provider name starts with a numeric digit, prepend a '_' character onto the class name. This is an indication that the user should adjust the name appropriately, while at the same time, allowing the code to compile and run.
1 parent 8698727 commit d133824

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/TraceParserGen.Tests/ParserGenerationTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,5 +911,19 @@ public void VersionedEvent_EnumerateTemplatesHasEntryPerVersion()
911911
}
912912

913913
#endregion
914+
915+
#region Leading digit provider name tests
916+
917+
[Fact]
918+
public void LeadingDigitProvider_ClassNamePrefixedWithUnderscore()
919+
{
920+
string content = GenerateParserFromManifest("LeadingDigitProvider.manifest.xml");
921+
922+
// Provider "3Scale-ETW-Provider" → ToCSharpName → "_3ScaleETWProvider"
923+
// C# identifiers cannot start with a digit, so an underscore is prepended.
924+
Assert.Contains("public sealed class _3ScaleETWProviderTraceEventParser : TraceEventParser", content);
925+
}
926+
927+
#endregion
914928
}
915929
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<instrumentationManifest xmlns="http://schemas.microsoft.com/win/2004/08/events">
3+
<instrumentation xmlns:xs="http://www.w3.org/2001/XMLSchema"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:win="http://manifests.microsoft.com/win/2004/08/windows/events">
6+
7+
<events xmlns="http://schemas.microsoft.com/win/2004/08/events">
8+
<!-- Provider name starts with a digit to test ToCSharpName leading-digit fix -->
9+
<provider name="3Scale-ETW-Provider"
10+
guid="{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}"
11+
symbol="THREE_SCALE_ETW_PROVIDER"
12+
messageFileName="3Scale.dll"
13+
resourceFileName="3Scale.dll">
14+
15+
<tasks>
16+
<task name="Request" value="1" />
17+
</tasks>
18+
19+
<templates>
20+
<template tid="RequestArgs">
21+
<data name="Url" inType="win:UnicodeString" />
22+
</template>
23+
</templates>
24+
25+
<events>
26+
<event value="1"
27+
symbol="Request"
28+
task="Request"
29+
template="RequestArgs"
30+
level="win:Informational" />
31+
</events>
32+
33+
</provider>
34+
</events>
35+
</instrumentation>
36+
</instrumentationManifest>

src/TraceParserGen/TraceParserGen.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,13 @@ public static string ToCSharpName(string input)
10061006

10071007
validName.Append(c);
10081008
}
1009+
1010+
// C# identifiers cannot start with a digit - prefix with underscore if needed.
1011+
if (validName.Length > 0 && Char.IsDigit(validName[0]))
1012+
{
1013+
validName.Insert(0, '_');
1014+
}
1015+
10091016
return validName.ToString();
10101017
}
10111018

0 commit comments

Comments
 (0)