Ensure SAL/C++ attributes on fields generate a CppAttributeList#743
Merged
tannergooding merged 3 commits intoJul 13, 2026
Merged
Conversation
Under generate-cpp-attributes, C++ attributes on a parameter were surfaced as a CppAttributeList but the same attribute on a struct field was dropped, since CppAttributes was only wired for ParameterDesc/BeginParameter. Mirror the parameter path for fields so SAL buffer annotations (e.g. _Field_size_full_) surfaced as annotate attributes are emitted. Fixes dotnet#444 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…oding-fix-sal-field-attributes
…oding-fix-sal-field-attributes
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.
Under
generate-cpp-attributes, a C++ attribute on a parameter was surfaced as[CppAttributeList("...")], but the same attribute on a struct field was silently dropped.CppAttributeswas only ever wired forParameterDesc/BeginParameter, never forFieldDesc/BeginField, so SAL buffer annotations (e.g._Field_size_full_,_Field_size_) surfaced asannotateattributes never reached the field'sCppAttributeList.This mirrors the existing parameter path for fields:
Abstractions/FieldDesc.cs— addIEnumerable<string>? CppAttributes.PInvokeGenerator.VisitDecl.csVisitFieldDecl— populate it fromfieldDecl.AttrswhenGenerateCppAttributesis set (identical shape to the parameter path).CSharp/CSharpOutputBuilder.VisitDecl.csBeginField— emit it viaAddCppAttributes, right after theNativeTypeNameblock so ordering matches parameters (CppAttributeListbeforeNativeAnnotation).CppAttributeListis the opt-in, full-fidelity dump of the raw C++ attribute list (all kinds,^-joined, gated ongenerate-cpp-attributes), distinct from the always-onNativeAnnotationthat surfaces onlyannotatespellings. Only the former was missing on fields.Example (field now matches the equivalent parameter):
Adds a standalone regression test (
SalFieldAttributeTest,[Platform("win")]) undergenerate-cpp-attributesasserting the field'sCppAttributeList. No existing goldens changed (no prior test enabledgenerate-cpp-attributes). FullClangSharp.PInvokeGenerator.UnitTestsrun: 3725 passed, 0 failed, 0 warnings.The indirect-field accessor path (nested-anonymous accessor properties) is intentionally left out of scope.
Fixes #444
Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com