Skip to content

Commit a8bd6b8

Browse files
authored
Merge pull request #15024 from tamasvajk/fix/interpolated-strings-stubs
C#: Add interpolated string handler attributes to generated stubs
2 parents 04c2ce9 + 50b7ab8 commit a8bd6b8

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

csharp/extractor/Semmle.Extraction.CSharp.StubGenerator/StubVisitor.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,12 @@ private void StubTypedConstant(TypedConstant c)
203203

204204
private static readonly HashSet<string> attributeAllowList = new() {
205205
"System.FlagsAttribute",
206-
"System.AttributeUsageAttribute"
206+
"System.AttributeUsageAttribute",
207+
"System.Runtime.CompilerServices.InterpolatedStringHandlerAttribute",
208+
"System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute",
207209
};
208210

209-
private void StubAttribute(AttributeData a, string prefix)
211+
private void StubAttribute(AttributeData a, string prefix, bool addNewLine)
210212
{
211213
if (a.AttributeClass is not INamedTypeSymbol @class)
212214
return;
@@ -232,14 +234,18 @@ private void StubAttribute(AttributeData a, string prefix)
232234
});
233235
stubWriter.Write(")");
234236
}
235-
stubWriter.WriteLine("]");
237+
stubWriter.Write("]");
238+
if (addNewLine)
239+
{
240+
stubWriter.WriteLine();
241+
}
236242
}
237243

238-
public void StubAttributes(IEnumerable<AttributeData> a, string prefix = "")
244+
public void StubAttributes(IEnumerable<AttributeData> a, string prefix = "", bool addNewLine = true)
239245
{
240246
foreach (var attribute in a)
241247
{
242-
StubAttribute(attribute, prefix);
248+
StubAttribute(attribute, prefix, addNewLine);
243249
}
244250
}
245251

@@ -513,6 +519,8 @@ private void StubParameters(ICollection<IParameterSymbol> parameters)
513519
{
514520
WriteCommaSep(parameters, parameter =>
515521
{
522+
StubAttributes(parameter.GetAttributes(), addNewLine: false);
523+
516524
switch (parameter.RefKind)
517525
{
518526
case RefKind.None:

csharp/ql/test/resources/stubs/_frameworks/Microsoft.NETCore.App/System.Memory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,9 @@ public static partial class MemoryExtensions
436436
public static System.ReadOnlySpan<T> TrimStart<T>(this System.ReadOnlySpan<T> span, T trimElement) where T : System.IEquatable<T> => throw null;
437437
public static System.Span<T> TrimStart<T>(this System.Span<T> span, System.ReadOnlySpan<T> trimElements) where T : System.IEquatable<T> => throw null;
438438
public static System.Span<T> TrimStart<T>(this System.Span<T> span, T trimElement) where T : System.IEquatable<T> => throw null;
439-
public static bool TryWrite(this System.Span<char> destination, ref System.MemoryExtensions.TryWriteInterpolatedStringHandler handler, out int charsWritten) => throw null;
440-
public static bool TryWrite(this System.Span<char> destination, System.IFormatProvider provider, ref System.MemoryExtensions.TryWriteInterpolatedStringHandler handler, out int charsWritten) => throw null;
439+
public static bool TryWrite(this System.Span<char> destination, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("destination")] ref System.MemoryExtensions.TryWriteInterpolatedStringHandler handler, out int charsWritten) => throw null;
440+
public static bool TryWrite(this System.Span<char> destination, System.IFormatProvider provider, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument(new[] { "destination", "provider" })] ref System.MemoryExtensions.TryWriteInterpolatedStringHandler handler, out int charsWritten) => throw null;
441+
[System.Runtime.CompilerServices.InterpolatedStringHandler]
441442
public struct TryWriteInterpolatedStringHandler
442443
{
443444
public bool AppendFormatted(System.ReadOnlySpan<char> value) => throw null;

csharp/ql/test/resources/stubs/_frameworks/Microsoft.NETCore.App/System.Runtime.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,11 +2697,12 @@ public sealed class ConditionalAttribute : System.Attribute
26972697
public static class Debug
26982698
{
26992699
public static void Assert(bool condition) => throw null;
2700-
public static void Assert(bool condition, ref System.Diagnostics.Debug.AssertInterpolatedStringHandler message) => throw null;
2701-
public static void Assert(bool condition, ref System.Diagnostics.Debug.AssertInterpolatedStringHandler message, ref System.Diagnostics.Debug.AssertInterpolatedStringHandler detailMessage) => throw null;
2700+
public static void Assert(bool condition, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("condition")] ref System.Diagnostics.Debug.AssertInterpolatedStringHandler message) => throw null;
2701+
public static void Assert(bool condition, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("condition")] ref System.Diagnostics.Debug.AssertInterpolatedStringHandler message, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("condition")] ref System.Diagnostics.Debug.AssertInterpolatedStringHandler detailMessage) => throw null;
27022702
public static void Assert(bool condition, string message) => throw null;
27032703
public static void Assert(bool condition, string message, string detailMessage) => throw null;
27042704
public static void Assert(bool condition, string message, string detailMessageFormat, params object[] args) => throw null;
2705+
[System.Runtime.CompilerServices.InterpolatedStringHandler]
27052706
public struct AssertInterpolatedStringHandler
27062707
{
27072708
public void AppendFormatted(object value, int alignment = default(int), string format = default(string)) => throw null;
@@ -2731,12 +2732,13 @@ public struct AssertInterpolatedStringHandler
27312732
public static void Write(object value, string category) => throw null;
27322733
public static void Write(string message) => throw null;
27332734
public static void Write(string message, string category) => throw null;
2734-
public static void WriteIf(bool condition, ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message) => throw null;
2735-
public static void WriteIf(bool condition, ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message, string category) => throw null;
2735+
public static void WriteIf(bool condition, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("condition")] ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message) => throw null;
2736+
public static void WriteIf(bool condition, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("condition")] ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message, string category) => throw null;
27362737
public static void WriteIf(bool condition, object value) => throw null;
27372738
public static void WriteIf(bool condition, object value, string category) => throw null;
27382739
public static void WriteIf(bool condition, string message) => throw null;
27392740
public static void WriteIf(bool condition, string message, string category) => throw null;
2741+
[System.Runtime.CompilerServices.InterpolatedStringHandler]
27402742
public struct WriteIfInterpolatedStringHandler
27412743
{
27422744
public void AppendFormatted(object value, int alignment = default(int), string format = default(string)) => throw null;
@@ -2756,8 +2758,8 @@ public struct WriteIfInterpolatedStringHandler
27562758
public static void WriteLine(string message) => throw null;
27572759
public static void WriteLine(string format, params object[] args) => throw null;
27582760
public static void WriteLine(string message, string category) => throw null;
2759-
public static void WriteLineIf(bool condition, ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message) => throw null;
2760-
public static void WriteLineIf(bool condition, ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message, string category) => throw null;
2761+
public static void WriteLineIf(bool condition, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("condition")] ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message) => throw null;
2762+
public static void WriteLineIf(bool condition, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("condition")] ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message, string category) => throw null;
27612763
public static void WriteLineIf(bool condition, object value) => throw null;
27622764
public static void WriteLineIf(bool condition, object value, string category) => throw null;
27632765
public static void WriteLineIf(bool condition, string message) => throw null;
@@ -9036,6 +9038,7 @@ public sealed class DefaultDependencyAttribute : System.Attribute
90369038
public DefaultDependencyAttribute(System.Runtime.CompilerServices.LoadHint loadHintArgument) => throw null;
90379039
public System.Runtime.CompilerServices.LoadHint LoadHint { get => throw null; }
90389040
}
9041+
[System.Runtime.CompilerServices.InterpolatedStringHandler]
90399042
public struct DefaultInterpolatedStringHandler
90409043
{
90419044
public void AppendFormatted(object value, int alignment = default(int), string format = default(string)) => throw null;
@@ -10876,8 +10879,8 @@ public sealed class String : System.ICloneable, System.IComparable, System.IComp
1087610879
public static string Copy(string str) => throw null;
1087710880
public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) => throw null;
1087810881
public void CopyTo(System.Span<char> destination) => throw null;
10879-
public static string Create(System.IFormatProvider provider, ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler handler) => throw null;
10880-
public static string Create(System.IFormatProvider provider, System.Span<char> initialBuffer, ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler handler) => throw null;
10882+
public static string Create(System.IFormatProvider provider, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("provider")] ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler handler) => throw null;
10883+
public static string Create(System.IFormatProvider provider, System.Span<char> initialBuffer, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument(new[] { "provider", "initialBuffer" })] ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler handler) => throw null;
1088110884
public static string Create<TState>(int length, TState state, System.Buffers.SpanAction<char, TState> action) => throw null;
1088210885
public unsafe String(char* value) => throw null;
1088310886
public unsafe String(char* value, int startIndex, int length) => throw null;
@@ -11419,7 +11422,7 @@ public sealed class StringBuilder : System.Runtime.Serialization.ISerializable
1141911422
public System.Text.StringBuilder Append(char[] value, int startIndex, int charCount) => throw null;
1142011423
public System.Text.StringBuilder Append(decimal value) => throw null;
1142111424
public System.Text.StringBuilder Append(double value) => throw null;
11422-
public System.Text.StringBuilder Append(System.IFormatProvider provider, ref System.Text.StringBuilder.AppendInterpolatedStringHandler handler) => throw null;
11425+
public System.Text.StringBuilder Append(System.IFormatProvider provider, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument(new[] { "", "provider" })] ref System.Text.StringBuilder.AppendInterpolatedStringHandler handler) => throw null;
1142311426
public System.Text.StringBuilder Append(short value) => throw null;
1142411427
public System.Text.StringBuilder Append(int value) => throw null;
1142511428
public System.Text.StringBuilder Append(long value) => throw null;
@@ -11432,7 +11435,7 @@ public sealed class StringBuilder : System.Runtime.Serialization.ISerializable
1143211435
public System.Text.StringBuilder Append(string value, int startIndex, int count) => throw null;
1143311436
public System.Text.StringBuilder Append(System.Text.StringBuilder value) => throw null;
1143411437
public System.Text.StringBuilder Append(System.Text.StringBuilder value, int startIndex, int count) => throw null;
11435-
public System.Text.StringBuilder Append(ref System.Text.StringBuilder.AppendInterpolatedStringHandler handler) => throw null;
11438+
public System.Text.StringBuilder Append([System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("")] ref System.Text.StringBuilder.AppendInterpolatedStringHandler handler) => throw null;
1143611439
public System.Text.StringBuilder Append(ushort value) => throw null;
1143711440
public System.Text.StringBuilder Append(uint value) => throw null;
1143811441
public System.Text.StringBuilder Append(ulong value) => throw null;
@@ -11444,6 +11447,7 @@ public sealed class StringBuilder : System.Runtime.Serialization.ISerializable
1144411447
public System.Text.StringBuilder AppendFormat(string format, object arg0, object arg1) => throw null;
1144511448
public System.Text.StringBuilder AppendFormat(string format, object arg0, object arg1, object arg2) => throw null;
1144611449
public System.Text.StringBuilder AppendFormat(string format, params object[] args) => throw null;
11450+
[System.Runtime.CompilerServices.InterpolatedStringHandler]
1144711451
public struct AppendInterpolatedStringHandler
1144811452
{
1144911453
public void AppendFormatted(object value, int alignment = default(int), string format = default(string)) => throw null;
@@ -11466,9 +11470,9 @@ public struct AppendInterpolatedStringHandler
1146611470
public System.Text.StringBuilder AppendJoin<T>(char separator, System.Collections.Generic.IEnumerable<T> values) => throw null;
1146711471
public System.Text.StringBuilder AppendJoin<T>(string separator, System.Collections.Generic.IEnumerable<T> values) => throw null;
1146811472
public System.Text.StringBuilder AppendLine() => throw null;
11469-
public System.Text.StringBuilder AppendLine(System.IFormatProvider provider, ref System.Text.StringBuilder.AppendInterpolatedStringHandler handler) => throw null;
11473+
public System.Text.StringBuilder AppendLine(System.IFormatProvider provider, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgument(new[] { "", "provider" })] ref System.Text.StringBuilder.AppendInterpolatedStringHandler handler) => throw null;
1147011474
public System.Text.StringBuilder AppendLine(string value) => throw null;
11471-
public System.Text.StringBuilder AppendLine(ref System.Text.StringBuilder.AppendInterpolatedStringHandler handler) => throw null;
11475+
public System.Text.StringBuilder AppendLine([System.Runtime.CompilerServices.InterpolatedStringHandlerArgument("")] ref System.Text.StringBuilder.AppendInterpolatedStringHandler handler) => throw null;
1147211476
public int Capacity { get => throw null; set { } }
1147311477
public struct ChunkEnumerator
1147411478
{

0 commit comments

Comments
 (0)