Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2060,14 +2060,11 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform =
}

var attrText = GetSourceRangeContents(namedDecl.TranslationUnit.Handle, attr.Extent);
var message = GetDeprecatedMessage(attrText);

var textStart = attrText.IndexOf('"', StringComparison.Ordinal);
var textLength = attrText.LastIndexOf('"') - textStart;

if (textLength > 1)
if (!string.IsNullOrEmpty(message))
{
var text = attrText.AsSpan(textStart + 1, textLength - 1);
outputBuilder.WriteCustomAttribute($"Obsolete(\"{text}\")");
outputBuilder.WriteCustomAttribute($"Obsolete(\"{message}\")");
}
else
{
Expand Down Expand Up @@ -2109,6 +2106,48 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform =
}
}

private static string? GetDeprecatedMessage(string attrText)
{
// The attribute source looks like `deprecated("message")`, but C allows the message to be
// written as adjacent string literals (e.g. `deprecated("part1" "part2")`), which the
// compiler concatenates into a single value. Gather the contents of every literal and join
// them so the emitted `Obsolete("...")` is a single, valid C# string.

var builder = new StringBuilder();
var hasLiteral = false;
var index = 0;

while (index < attrText.Length)
{
if (attrText[index] != '"')
{
index++;
continue;
}

hasLiteral = true;
index++;

while ((index < attrText.Length) && (attrText[index] != '"'))
{
// Preserve escape sequences verbatim so an escaped quote isn't treated as the end
// of the literal and the contents are emitted unchanged (as before).
if ((attrText[index] == '\\') && ((index + 1) < attrText.Length))
{
_ = builder.Append(attrText[index]);
index++;
}

_ = builder.Append(attrText[index]);
index++;
}

index++;
}

return hasLiteral ? builder.ToString() : null;
}

private string GetLibraryPath(string remappedName)
{
return !_config.WithLibraryPaths.TryGetValue(remappedName, out var libraryPath) && !_config.WithLibraryPaths.TryGetValue("*", out libraryPath)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests;

/// <summary>
/// Regression test for https://github.com/dotnet/ClangSharp/issues/694.
/// A deprecated message written as adjacent C string literals must be concatenated into a single
/// C# string literal, otherwise the emitted <c>[Obsolete(...)]</c> attribute is invalid C#.
/// </summary>
[Platform("win")]
public sealed class DeprecatedAdjacentStringTest : PInvokeGeneratorTest
{
[Test]
public Task AdjacentStringLiteralsAreConcatenated()
{
var inputContents = @"extern ""C"" [[deprecated(""Use Bar() or ""
""Baz() instead"")]]
void MyFunction();
";

var expectedOutputContents = @"using System;
using System.Runtime.InteropServices;

namespace ClangSharp.Test
{
public static partial class Methods
{
[DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[Obsolete(""Use Bar() or Baz() instead"")]
public static extern void MyFunction();
}
}
";

return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents);
}
}
Loading