Skip to content

Escape strings in annotate attributes #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2024
Merged

Conversation

dpaoliello
Copy link
Contributor

It's possible for C++ annotate attributes to contain strings literals, thus they should be correctly escaped before generating the corresponding C# attribute. The easiest way to do this is to use C# verbatim strings, and escape double-quotes (by adding a double-quote).

@@ -6817,8 +6817,8 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform =

case CX_AttrKind_Annotate:
{
var annotationText = attr.Spelling;
outputBuilder.WriteCustomAttribute($"""NativeAnnotation("{annotationText}")""");
var annotationText = attr.Spelling.Replace("\"", "\"\"", StringComparison.Ordinal);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should rather use the EscapeString(string value) helper which centralizes all the escape string handling and attempts to handle already escaped sequences as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@tannergooding tannergooding merged commit c5a6a76 into dotnet:main Sep 24, 2024
13 checks passed
@dpaoliello dpaoliello deleted the escann branch September 24, 2024 19:10
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.

2 participants