[Shift-Left] Auto-detect many typedefs and manual remaps by walking the AST before PInvokeGenerator runs#2240
Draft
[Shift-Left] Auto-detect many typedefs and manual remaps by walking the AST before PInvokeGenerator runs#2240
Conversation
Hosts ClangSharp.PInvokeGenerator v17.0.1 as a library (NuGet package references). Parses the translation unit, walks the AST to discover typedef-tag remappings, merges them with manual --remap entries, then runs PInvokeGenerator.GenerateBindings on the same TranslationUnit in a single parse pass. - Win32MetadataScraper: new console app tool - remapExclusions.rsp: exclusion list for unsafe auto-remaps - ScrapeHeaders.cs: launches scraper, collects .remaps, writes auto RSP - sdk.targets: includes auto-remaps RSP in @(ScraperRsp) - Graceful fallback to stock ClangSharpPInvokeGenerator if scraper not found Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… ptr fixups - Rewrite AST walker to collect ALL typedefs per tag, disambiguate multi-typedef tags automatically (no exclusion list needed) - Add UnwrapType helper for ElaboratedType/AttributedType/ParenType sugar types that were hiding typedef-tag and fn-ptr relationships - Auto-discover function pointer typedef pairs from AST: detect FunctionProtoType typedefs and their PointerType aliases, generate --remap, --exclude, and --reducePointerLevel entries automatically - Remove 12,197 of 12,705 manual --remap entries from scraper.settings.rsp (508 genuinely manual entries remain: semantic primitives, pointer remaps, nested qualifieds, uppercase renames) - Remove 160 of 184 entries from functionPointerFixups.json (24 remain: 6 alreadyPointer, 13 name-only, 5 edge cases) - Delete remapExclusions.rsp (no longer needed) - Zero conflicts: auto-discovery never picks wrong name - Winmd output identical to baseline (24,328,704 bytes) - All 36 tests pass Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Extract WalkTranslationUnit, ResolveTagRemaps, FilterTagRemaps, ResolveFunctionPointerFixups, and UnwrapType from Program.cs into a public RemapDiscovery class that can be tested independently. Add Win32MetadataScraperTests project with 22 xUnit tests covering: - Tag-typedef discovery: _Uppercase, _lowercase, tag prefix, lowercase tag, trailing underscore, enum typedef patterns - Multi-typedef disambiguation: stripped prefix match, ambiguous skip, manual hint resolution - Semantic override protection: manual remap takes priority - Built-in type filtering: _GUID excluded - Identity remap filtering: same tag/typedef skipped - Function pointer discovery: two-step LP/P/PFN patterns, direct fn ptr, calling convention (AttributedType unwrap), non-standard naming skip, typedef alias pattern, ambiguous pointer targets skip - UnwrapType: ElaboratedType and AttributedType sugar peeling Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
b5692ed to
2a5e7cc
Compare
… direction, simplified consistency check Heuristic fixes: - Add case-insensitive match to multi-typedef disambiguator (fixes in6_addr→IN6_ADDR when competing with IPv6Addr typedef) - Fix fn ptr alreadyPointer direction: when proto has P/LP prefix and alias doesn't, remap alias→proto (e.g., EXCEPTION_ROUTINE→PEXCEPTION_ROUTINE, not reverse) - Filter suffix-adding remaps (e.g., GLUnurbs→GLUnurbsObj) since tag is canonical - Skip C++ namespace-qualified tags (Gdiplus::Status, ABI::*) Cross-partition consistency: - Simplified CheckCrossPartitionRemapConsistency: scans declared type names in generated .cs files and checks against discovered remap tags. No regex needed. - Warning-only (PInvokeGenerator handles these internally) - Added 7 partition #include fixes for cross-partition typedef visibility - 5 irreducible manual remaps for types where AST walker can't discover the typedef in certain partition configurations Tests: 30 scraper tests (7 new), all 60 pass. Winmd 24,328,704 bytes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2a5e7cc to
f5e51c6
Compare
- Remove unused FilterRemaps() method from Program.cs (duplicated RemapDiscovery.FilterTagRemaps) - Tighten HasPointerPrefix to require uppercase after single P prefix - Change CheckCrossPartitionRemapConsistency to void (always returned true) - Fix trailing space in LSA_GET_EXTENDED_CALL_FLAGS in functionPointerFixups.json - Add plan docs from win32metadata repo (auto-type-remappings, shift-left, annotations) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Restore key manual remaps, tighten auto-discovery heuristics, and capture the remaining rename-drift findings. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds Win32MetadataScraper — a new tool that hosts ClangSharp.PInvokeGenerator v17.0.1 as a library and auto-discovers typedef-tag remappings from the AST in a single parse pass.
How it works
CXIndex(same Clang parse as stock CLI)TypedefDecl→TagTypepairs (e.g.,_BLUETOOTH_ADDRESS→BLUETOOTH_ADDRESS)remapExclusions.rsp) and heuristic rules--remapentries (manual wins on conflict)PInvokeGeneratorwith the full merged configGenerateBindingson the same TranslationUnit — single parse, single pass.remapssidecar file collected byScrapeHeadersPer ClangSharp owner guidance: the translation unit can be walked freely before passing to
GenerateBindings. No reflection or private field access needed.Key design points
libclang.dll/libClangSharp.dllloaded from the already-installed dotnet toolWin32MetadataScraper.dllnot found,ScrapeHeadersuses stockClangSharpPInvokeGenerator@(ScraperRsp)before manual settings so manual overrides winVerification
Files changed
sources/Win32MetadataScraper/Program.cssources/Win32MetadataScraper/Win32MetadataScraper.csprojsources/GeneratorSdk/tools/assets/scraper/remapExclusions.rspsources/GeneratorSdk/MetadataTasks/ScrapeHeaders.cssources/GeneratorSdk/sdk/sdk.targetsBuildTools/BuildTools.proj