Skip to content

Commit ee5c014

Browse files
authored
Merge pull request github#14609 from hvitved/csharp/stub-gen-attribute-usage
C#: Include `AttributeUsages` in stub generator
2 parents 9cae488 + 08d6379 commit ee5c014

File tree

48 files changed

+513
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+513
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ private void StubTypedConstant(TypedConstant c)
186186
}
187187
break;
188188
case TypedConstantKind.Enum:
189-
stubWriter.Write("throw null");
189+
stubWriter.Write($"({c.Type!.GetQualifiedName()}) ");
190+
stubWriter.Write(c.Value!.ToString());
190191
break;
191192
case TypedConstantKind.Array:
192193
stubWriter.Write("new []{");
@@ -200,7 +201,8 @@ private void StubTypedConstant(TypedConstant c)
200201
}
201202

202203
private static readonly HashSet<string> attributeAllowList = new() {
203-
"System.FlagsAttribute"
204+
"System.FlagsAttribute",
205+
"System.AttributeUsageAttribute"
204206
};
205207

206208
private void StubAttribute(AttributeData a, string prefix)
@@ -219,6 +221,14 @@ private void StubAttribute(AttributeData a, string prefix)
219221
{
220222
stubWriter.Write("(");
221223
WriteCommaSep(a.ConstructorArguments, StubTypedConstant);
224+
if (a.ConstructorArguments.Any() && a.NamedArguments.Any())
225+
stubWriter.Write(",");
226+
WriteCommaSep(a.NamedArguments, arg =>
227+
{
228+
stubWriter.Write(arg.Key);
229+
stubWriter.Write(" = ");
230+
StubTypedConstant(arg.Value);
231+
});
222232
stubWriter.Write(")");
223233
}
224234
stubWriter.WriteLine("]");

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Authorization.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace AspNetCore
66
{
77
namespace Authorization
88
{
9+
[System.AttributeUsage((System.AttributeTargets)68, AllowMultiple = false, Inherited = true)]
910
public class AllowAnonymousAttribute : System.Attribute, Microsoft.AspNetCore.Authorization.IAllowAnonymous
1011
{
1112
public AllowAnonymousAttribute() => throw null;
@@ -120,6 +121,7 @@ public static partial class AuthorizationServiceExtensions
120121
public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Authorization.AuthorizationResult> AuthorizeAsync(this Microsoft.AspNetCore.Authorization.IAuthorizationService service, System.Security.Claims.ClaimsPrincipal user, Microsoft.AspNetCore.Authorization.AuthorizationPolicy policy) => throw null;
121122
public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Authorization.AuthorizationResult> AuthorizeAsync(this Microsoft.AspNetCore.Authorization.IAuthorizationService service, System.Security.Claims.ClaimsPrincipal user, string policyName) => throw null;
122123
}
124+
[System.AttributeUsage((System.AttributeTargets)68, AllowMultiple = true, Inherited = true)]
123125
public class AuthorizeAttribute : System.Attribute, Microsoft.AspNetCore.Authorization.IAuthorizeData
124126
{
125127
public string AuthenticationSchemes { get => throw null; set { } }

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Components.Web.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace AspNetCore
66
{
77
namespace Components
88
{
9+
[System.AttributeUsage((System.AttributeTargets)4, AllowMultiple = true, Inherited = true)]
910
public sealed class BindInputElementAttribute : System.Attribute
1011
{
1112
public string ChangeAttribute { get => throw null; }

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Components.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public static class BindConverter
7373
public static bool TryConvertToTimeOnly(object obj, System.Globalization.CultureInfo culture, out System.TimeOnly value) => throw null;
7474
public static bool TryConvertToTimeOnly(object obj, System.Globalization.CultureInfo culture, string format, out System.TimeOnly value) => throw null;
7575
}
76+
[System.AttributeUsage((System.AttributeTargets)4, AllowMultiple = true, Inherited = true)]
7677
public sealed class BindElementAttribute : System.Attribute
7778
{
7879
public string ChangeAttribute { get => throw null; }
@@ -81,11 +82,13 @@ public sealed class BindElementAttribute : System.Attribute
8182
public string Suffix { get => throw null; }
8283
public string ValueAttribute { get => throw null; }
8384
}
85+
[System.AttributeUsage((System.AttributeTargets)128, AllowMultiple = false, Inherited = true)]
8486
public sealed class CascadingParameterAttribute : System.Attribute
8587
{
8688
public CascadingParameterAttribute() => throw null;
8789
public string Name { get => throw null; set { } }
8890
}
91+
[System.AttributeUsage((System.AttributeTargets)4, AllowMultiple = true, Inherited = false)]
8992
public sealed class CascadingTypeParameterAttribute : System.Attribute
9093
{
9194
public CascadingTypeParameterAttribute(string name) => throw null;
@@ -161,6 +164,7 @@ public class DynamicComponent : Microsoft.AspNetCore.Components.IComponent
161164
public System.Threading.Tasks.Task SetParametersAsync(Microsoft.AspNetCore.Components.ParameterView parameters) => throw null;
162165
public System.Type Type { get => throw null; set { } }
163166
}
167+
[System.AttributeUsage((System.AttributeTargets)128, AllowMultiple = false)]
164168
public sealed class EditorRequiredAttribute : System.Attribute
165169
{
166170
public EditorRequiredAttribute() => throw null;
@@ -300,6 +304,7 @@ public struct EventCallbackWorkItem
300304
public static readonly Microsoft.AspNetCore.Components.EventCallbackWorkItem Empty;
301305
public System.Threading.Tasks.Task InvokeAsync(object arg) => throw null;
302306
}
307+
[System.AttributeUsage((System.AttributeTargets)4, AllowMultiple = true, Inherited = true)]
303308
public sealed class EventHandlerAttribute : System.Attribute
304309
{
305310
public string AttributeName { get => throw null; }
@@ -336,6 +341,7 @@ public class ComponentStatePersistenceManager
336341
public Microsoft.AspNetCore.Components.PersistentComponentState State { get => throw null; }
337342
}
338343
}
344+
[System.AttributeUsage((System.AttributeTargets)128, AllowMultiple = false, Inherited = true)]
339345
public sealed class InjectAttribute : System.Attribute
340346
{
341347
public InjectAttribute() => throw null;
@@ -345,6 +351,7 @@ public interface IPersistentComponentStateStore
345351
System.Threading.Tasks.Task<System.Collections.Generic.IDictionary<string, byte[]>> GetPersistedStateAsync();
346352
System.Threading.Tasks.Task PersistStateAsync(System.Collections.Generic.IReadOnlyDictionary<string, byte[]> state);
347353
}
354+
[System.AttributeUsage((System.AttributeTargets)4, AllowMultiple = false, Inherited = true)]
348355
public sealed class LayoutAttribute : System.Attribute
349356
{
350357
public LayoutAttribute(System.Type layoutType) => throw null;
@@ -447,6 +454,7 @@ public abstract class OwningComponentBase<TService> : Microsoft.AspNetCore.Compo
447454
protected OwningComponentBase() => throw null;
448455
protected TService Service { get => throw null; }
449456
}
457+
[System.AttributeUsage((System.AttributeTargets)128, AllowMultiple = false, Inherited = true)]
450458
public sealed class ParameterAttribute : System.Attribute
451459
{
452460
public bool CaptureUnmatchedValues { get => throw null; set { } }
@@ -647,6 +655,7 @@ public enum RenderTreeFrameType : short
647655
Markup = 8,
648656
}
649657
}
658+
[System.AttributeUsage((System.AttributeTargets)4, AllowMultiple = true, Inherited = false)]
650659
public sealed class RouteAttribute : System.Attribute
651660
{
652661
public RouteAttribute(string template) => throw null;
@@ -714,6 +723,7 @@ public class Router : Microsoft.AspNetCore.Components.IComponent, System.IDispos
714723
public System.Threading.Tasks.Task SetParametersAsync(Microsoft.AspNetCore.Components.ParameterView parameters) => throw null;
715724
}
716725
}
726+
[System.AttributeUsage((System.AttributeTargets)128, AllowMultiple = false, Inherited = true)]
717727
public sealed class SupplyParameterFromQueryAttribute : System.Attribute
718728
{
719729
public SupplyParameterFromQueryAttribute() => throw null;

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Cors.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ public class CorsPolicyMetadata : Microsoft.AspNetCore.Cors.Infrastructure.ICors
2525
public CorsPolicyMetadata(Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicy policy) => throw null;
2626
public Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicy Policy { get => throw null; }
2727
}
28+
[System.AttributeUsage((System.AttributeTargets)68, AllowMultiple = false, Inherited = false)]
2829
public class DisableCorsAttribute : System.Attribute, Microsoft.AspNetCore.Cors.Infrastructure.ICorsMetadata, Microsoft.AspNetCore.Cors.Infrastructure.IDisableCorsAttribute
2930
{
3031
public DisableCorsAttribute() => throw null;
3132
}
33+
[System.AttributeUsage((System.AttributeTargets)68, AllowMultiple = false, Inherited = true)]
3234
public class EnableCorsAttribute : System.Attribute, Microsoft.AspNetCore.Cors.Infrastructure.ICorsMetadata, Microsoft.AspNetCore.Cors.Infrastructure.IEnableCorsAttribute
3335
{
3436
public EnableCorsAttribute() => throw null;

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Hosting.Abstractions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static partial class HostingEnvironmentExtensions
3434
public static bool IsProduction(this Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment) => throw null;
3535
public static bool IsStaging(this Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment) => throw null;
3636
}
37+
[System.AttributeUsage((System.AttributeTargets)1, Inherited = false, AllowMultiple = true)]
3738
public sealed class HostingStartupAttribute : System.Attribute
3839
{
3940
public HostingStartupAttribute(System.Type hostingStartupType) => throw null;

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Http.Abstractions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public interface ICorsMetadata
105105
}
106106
namespace Http
107107
{
108+
[System.AttributeUsage((System.AttributeTargets)2048, Inherited = false, AllowMultiple = false)]
108109
public sealed class AsParametersAttribute : System.Attribute
109110
{
110111
public AsParametersAttribute() => throw null;

csharp/ql/test/resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.Http.Extensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ namespace AspNetCore
66
{
77
namespace Http
88
{
9+
[System.AttributeUsage((System.AttributeTargets)64, Inherited = false, AllowMultiple = false)]
910
public sealed class EndpointDescriptionAttribute : System.Attribute, Microsoft.AspNetCore.Http.Metadata.IEndpointDescriptionMetadata
1011
{
1112
public EndpointDescriptionAttribute(string description) => throw null;
1213
public string Description { get => throw null; }
1314
}
15+
[System.AttributeUsage((System.AttributeTargets)64, Inherited = false, AllowMultiple = false)]
1416
public sealed class EndpointSummaryAttribute : System.Attribute, Microsoft.AspNetCore.Http.Metadata.IEndpointSummaryMetadata
1517
{
1618
public EndpointSummaryAttribute(string summary) => throw null;
@@ -195,6 +197,7 @@ public static partial class SessionExtensions
195197
public static void SetInt32(this Microsoft.AspNetCore.Http.ISession session, string key, int value) => throw null;
196198
public static void SetString(this Microsoft.AspNetCore.Http.ISession session, string key, string value) => throw null;
197199
}
200+
[System.AttributeUsage((System.AttributeTargets)4164, Inherited = false, AllowMultiple = false)]
198201
public sealed class TagsAttribute : System.Attribute, Microsoft.AspNetCore.Http.Metadata.ITagsMetadata
199202
{
200203
public TagsAttribute(params string[] tags) => throw null;

0 commit comments

Comments
 (0)