Skip to content

Promote anonymous-name prefixes to consts with helper predicates#718

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-refactor-anonymous-name-consts
Jul 13, 2026
Merged

Promote anonymous-name prefixes to consts with helper predicates#718
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-refactor-anonymous-name-consts

Conversation

@tannergooding

@tannergooding tannergooding commented Jul 13, 2026

Copy link
Copy Markdown
Member

The generator checked scattered __Anonymous*_ magic string prefixes inline across several files. This promotes each distinct prefix to a private const string and routes the produce side through a shared base const, so the produce and consume sides share the exact same literals.

Consts added (grouped near the top of PInvokeGenerator.cs):

  • AnonymousNamePrefix = "__Anonymous"
  • AnonymousBasePrefix = $"{AnonymousNamePrefix}Base_"
  • AnonymousEnumPrefix = $"{AnonymousNamePrefix}Enum_"
  • AnonymousFieldDeclPrefix = $"{AnonymousNamePrefix}FieldDecl_"
  • AnonymousRecordPrefix = $"{AnonymousNamePrefix}Record_"

The specific prefixes are built via compile-time const interpolation from the shared base, and GetAnonymousName now emits $"{AnonymousNamePrefix}{kind}_..." so the produce side is tied to the same base literal.

Helpers added (static, next to GetAnonymousName):

  • IsAnonymousEnum(string) -- 4 call sites
  • IsAnonymousRecord(string) -- 2 call sites

Single-use FieldDecl/Base prefix checks reference the const directly rather than a one-off helper.

Sites touched: PInvokeGenerator.cs, PInvokeGenerator.Naming.cs, PInvokeGenerator.VisitDecl.cs, PInvokeGenerator.VisitStmt.cs.

Behavior-preserving: the literal string values are unchanged, so generated output is byte-identical. Build is clean (0 warnings) and all 3708 golden-file tests pass with no expectation changes.

The generator checked scattered `__Anonymous*_` magic string prefixes inline across several files. Promote each distinct prefix to a `private const string` grouped near the top of `PInvokeGenerator.cs` and route the produce side (`GetAnonymousName`) through the shared `AnonymousNamePrefix` base so produce and consume share the same literals.

Consts added: `AnonymousNamePrefix`, `AnonymousBasePrefix`, `AnonymousEnumPrefix`, `AnonymousFieldDeclPrefix`, `AnonymousRecordPrefix`.

Helpers added: `IsAnonymousEnum` (4 call sites) and `IsAnonymousRecord` (2 call sites); single-use `FieldDecl`/`Base` checks use the const directly.

Behavior-preserving: the literal string values are unchanged, so generated output is byte-identical.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding
tannergooding merged commit 70a35b1 into dotnet:main Jul 13, 2026
14 checks passed
@tannergooding
tannergooding deleted the tannergooding-refactor-anonymous-name-consts branch July 13, 2026 03:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant