Skip to content

Commit

Permalink
Merge pull request #18575 from michaelnebel/csharp/overloadresolution
Browse files Browse the repository at this point in the history
C# 13: Overload resolution priority.
  • Loading branch information
michaelnebel authored Jan 24, 2025
2 parents a54e732 + 340f3c0 commit d3d3cce
Show file tree
Hide file tree
Showing 16 changed files with 1,777 additions and 1,602 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* C# 13: Added support for the overload resolution priority attribute (`OverloadResolutionPriority`). Usages of the attribute and the corresponding priority can be found using the QL class `SystemRuntimeCompilerServicesOverloadResolutionPriorityAttribute`.
3 changes: 3 additions & 0 deletions csharp/ql/lib/semmle/code/csharp/Attribute.qll
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class Attribute extends TopLevelExprParent, @attribute {
attributes(this, _, getTypeRef(result), _)
}

/** Gets the namespace containing the attribute type declaration. */
Namespace getNamespace() { result = this.getType().getNamespace() }

/** Gets the element that this attribute is attached to. */
Attributable getTarget() { attributes(this, _, _, result) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,25 @@ class SystemRuntimeCompilerServicesConfiguredTaskAwaitableTConfiguredTaskAwaiter
/** An attribute of type `System.Runtime.CompilerServices.InlineArrayAttribute`. */
class SystemRuntimeCompilerServicesInlineArrayAttribute extends Attribute {
SystemRuntimeCompilerServicesInlineArrayAttribute() {
this.getType().hasFullyQualifiedName("System.Runtime.CompilerServices", "InlineArrayAttribute")
this.getNamespace() instanceof SystemRuntimeCompilerServicesNamespace and
this.getType().hasName("InlineArrayAttribute")
}

/**
* Gets the length of the inline array.
*/
int getLength() { result = this.getConstructorArgument(0).getValue().toInt() }
}

/** An attribute of type `System.Runtime.CompilerServices.OverloadResolutionPriority`. */
class SystemRuntimeCompilerServicesOverloadResolutionPriorityAttribute extends Attribute {
SystemRuntimeCompilerServicesOverloadResolutionPriorityAttribute() {
this.getNamespace() instanceof SystemRuntimeCompilerServicesNamespace and
this.getType().hasName("OverloadResolutionPriorityAttribute")
}

/**
* Gets the priority number.
*/
int getPriority() { result = this.getConstructorArgument(0).getValue().toInt() }
}
354 changes: 180 additions & 174 deletions csharp/ql/test/library-tests/attributes/AttributeArguments.expected

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
| attributes.cs:11:12:11:24 | [assembly: AssemblyTitle(...)] | attributes.cs:8:1:10:31 | // ... |
| attributes.cs:23:12:23:21 | [assembly: ComVisible(...)] | attributes.cs:20:1:22:58 | // ... |
| attributes.cs:26:12:26:15 | [assembly: Guid(...)] | attributes.cs:25:1:25:84 | // ... |
| attributes.cs:38:12:38:26 | [assembly: AssemblyVersion(...)] | attributes.cs:28:1:37:39 | // ... |
| attributes.cs:12:12:12:24 | [assembly: AssemblyTitle(...)] | attributes.cs:9:1:11:31 | // ... |
| attributes.cs:24:12:24:21 | [assembly: ComVisible(...)] | attributes.cs:21:1:23:58 | // ... |
| attributes.cs:27:12:27:15 | [assembly: Guid(...)] | attributes.cs:26:1:26:84 | // ... |
| attributes.cs:39:12:39:26 | [assembly: AssemblyVersion(...)] | attributes.cs:29:1:38:39 | // ... |
93 changes: 48 additions & 45 deletions csharp/ql/test/library-tests/attributes/AttributeElements.expected
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,51 @@
| Assembly1.dll:0:0:0:0 | Assembly1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | Assembly1.dll:0:0:0:0 | [Debuggable(...)] | System.Diagnostics.DebuggableAttribute |
| Assembly1.dll:0:0:0:0 | Assembly1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | Assembly1.dll:0:0:0:0 | [RuntimeCompatibility(...)] | System.Runtime.CompilerServices.RuntimeCompatibilityAttribute |
| Assembly1.dll:0:0:0:0 | Assembly1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | Assembly1.dll:0:0:0:0 | [TargetFramework(...)] | System.Runtime.Versioning.TargetFrameworkAttribute |
| attributes.cs:45:7:45:9 | Foo | attributes.cs:44:2:44:22 | [AttributeUsage(...)] | System.AttributeUsageAttribute |
| attributes.cs:48:17:48:19 | foo | attributes.cs:47:6:47:16 | [Conditional(...)] | System.Diagnostics.ConditionalAttribute |
| attributes.cs:53:23:53:23 | x | attributes.cs:53:14:53:16 | [Foo(...)] | Foo |
| attributes.cs:56:10:56:11 | M1 | attributes.cs:55:6:55:16 | [My(...)] | MyAttribute |
| attributes.cs:60:10:60:11 | M2 | attributes.cs:58:6:58:16 | [My(...)] | MyAttribute |
| attributes.cs:60:10:60:11 | M2 | attributes.cs:59:6:59:8 | [My2(...)] | My2Attribute |
| attributes.cs:79:14:79:14 | X | attributes.cs:78:2:78:5 | [Args(...)] | ArgsAttribute |
| attributes.cs:83:9:83:18 | SomeMethod | attributes.cs:81:6:81:9 | [Args(...)] | ArgsAttribute |
| attributes.cs:83:9:83:18 | SomeMethod | attributes.cs:82:14:82:17 | [return: Args(...)] | ArgsAttribute |
| attributes.cs:99:14:99:24 | Invoke | attributes.cs:98:10:98:21 | [return: My3(...)] | My3Attribute |
| attributes.cs:99:14:99:24 | My1Delegate | attributes.cs:97:2:97:13 | [My3(...)] | My3Attribute |
| attributes.cs:103:17:103:27 | Invoke | attributes.cs:101:10:101:21 | [return: My3(...)] | My3Attribute |
| attributes.cs:103:17:103:27 | My2Delegate | attributes.cs:102:8:102:19 | [My3(...)] | My3Attribute |
| attributes.cs:109:32:109:32 | + | attributes.cs:107:6:107:17 | [My3(...)] | My3Attribute |
| attributes.cs:109:32:109:32 | + | attributes.cs:108:14:108:25 | [return: My3(...)] | My3Attribute |
| attributes.cs:112:16:112:19 | Item | attributes.cs:111:6:111:17 | [My3(...)] | My3Attribute |
| attributes.cs:116:9:116:11 | get_Item | attributes.cs:114:10:114:21 | [My3(...)] | My3Attribute |
| attributes.cs:116:9:116:11 | get_Item | attributes.cs:115:18:115:29 | [return: My3(...)] | My3Attribute |
| attributes.cs:120:9:120:11 | set_Item | attributes.cs:118:18:118:29 | [My3(...)] | My3Attribute |
| attributes.cs:120:9:120:11 | value | attributes.cs:119:17:119:28 | [My3(...)] | My3Attribute |
| attributes.cs:125:16:125:20 | Prop1 | attributes.cs:124:6:124:17 | [My3(...)] | My3Attribute |
| attributes.cs:129:9:129:11 | get_Prop1 | attributes.cs:127:18:127:29 | [My3(...)] | My3Attribute |
| attributes.cs:129:9:129:11 | get_Prop1 | attributes.cs:128:18:128:29 | [return: My3(...)] | My3Attribute |
| attributes.cs:133:9:133:11 | set_Prop1 | attributes.cs:131:10:131:21 | [My3(...)] | My3Attribute |
| attributes.cs:133:9:133:11 | value | attributes.cs:132:17:132:28 | [My3(...)] | My3Attribute |
| attributes.cs:145:17:145:18 | M1 | attributes.cs:144:6:144:11 | [Params(...)] | Class1+ParamsAttribute |
| attributes.cs:148:17:148:18 | M2 | attributes.cs:147:6:147:11 | [Params(...)] | Class1+ParamsAttribute |
| attributes.cs:151:17:151:18 | M3 | attributes.cs:150:6:150:11 | [Params(...)] | Class1+ParamsAttribute |
| attributes.cs:154:17:154:18 | M4 | attributes.cs:153:6:153:11 | [Params(...)] | Class1+ParamsAttribute |
| attributes.cs:158:14:158:32 | MyExperimentalClass | attributes.cs:157:2:157:13 | [Experimental(...)] | System.Diagnostics.CodeAnalysis.ExperimentalAttribute |
| attributes.cs:161:17:161:36 | MyExperimentalMethod | attributes.cs:160:6:160:17 | [Experimental(...)] | System.Diagnostics.CodeAnalysis.ExperimentalAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:11:12:11:24 | [assembly: AssemblyTitle(...)] | System.Reflection.AssemblyTitleAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:12:12:12:30 | [assembly: AssemblyDescription(...)] | System.Reflection.AssemblyDescriptionAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:13:12:13:32 | [assembly: AssemblyConfiguration(...)] | System.Reflection.AssemblyConfigurationAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:14:12:14:26 | [assembly: AssemblyCompany(...)] | System.Reflection.AssemblyCompanyAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:15:12:15:26 | [assembly: AssemblyProduct(...)] | System.Reflection.AssemblyProductAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:16:12:16:28 | [assembly: AssemblyCopyright(...)] | System.Reflection.AssemblyCopyrightAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:17:12:17:28 | [assembly: AssemblyTrademark(...)] | System.Reflection.AssemblyTrademarkAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:18:12:18:26 | [assembly: AssemblyCulture(...)] | System.Reflection.AssemblyCultureAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:23:12:23:21 | [assembly: ComVisible(...)] | System.Runtime.InteropServices.ComVisibleAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:26:12:26:15 | [assembly: Guid(...)] | System.Runtime.InteropServices.GuidAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:38:12:38:26 | [assembly: AssemblyVersion(...)] | System.Reflection.AssemblyVersionAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:39:12:39:30 | [assembly: AssemblyFileVersion(...)] | System.Reflection.AssemblyFileVersionAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:41:12:41:15 | [assembly: Args(...)] | ArgsAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:42:10:42:13 | [module: Args(...)] | ArgsAttribute |
| attributes.cs:46:7:46:9 | Foo | attributes.cs:45:2:45:22 | [AttributeUsage(...)] | System.AttributeUsageAttribute |
| attributes.cs:49:17:49:19 | foo | attributes.cs:48:6:48:16 | [Conditional(...)] | System.Diagnostics.ConditionalAttribute |
| attributes.cs:54:23:54:23 | x | attributes.cs:54:14:54:16 | [Foo(...)] | Foo |
| attributes.cs:57:10:57:11 | M1 | attributes.cs:56:6:56:16 | [My(...)] | MyAttribute |
| attributes.cs:61:10:61:11 | M2 | attributes.cs:59:6:59:16 | [My(...)] | MyAttribute |
| attributes.cs:61:10:61:11 | M2 | attributes.cs:60:6:60:8 | [My2(...)] | My2Attribute |
| attributes.cs:80:14:80:14 | X | attributes.cs:79:2:79:5 | [Args(...)] | ArgsAttribute |
| attributes.cs:84:9:84:18 | SomeMethod | attributes.cs:82:6:82:9 | [Args(...)] | ArgsAttribute |
| attributes.cs:84:9:84:18 | SomeMethod | attributes.cs:83:14:83:17 | [return: Args(...)] | ArgsAttribute |
| attributes.cs:100:14:100:24 | Invoke | attributes.cs:99:10:99:21 | [return: My3(...)] | My3Attribute |
| attributes.cs:100:14:100:24 | My1Delegate | attributes.cs:98:2:98:13 | [My3(...)] | My3Attribute |
| attributes.cs:104:17:104:27 | Invoke | attributes.cs:102:10:102:21 | [return: My3(...)] | My3Attribute |
| attributes.cs:104:17:104:27 | My2Delegate | attributes.cs:103:8:103:19 | [My3(...)] | My3Attribute |
| attributes.cs:110:32:110:32 | + | attributes.cs:108:6:108:17 | [My3(...)] | My3Attribute |
| attributes.cs:110:32:110:32 | + | attributes.cs:109:14:109:25 | [return: My3(...)] | My3Attribute |
| attributes.cs:113:16:113:19 | Item | attributes.cs:112:6:112:17 | [My3(...)] | My3Attribute |
| attributes.cs:117:9:117:11 | get_Item | attributes.cs:115:10:115:21 | [My3(...)] | My3Attribute |
| attributes.cs:117:9:117:11 | get_Item | attributes.cs:116:18:116:29 | [return: My3(...)] | My3Attribute |
| attributes.cs:121:9:121:11 | set_Item | attributes.cs:119:18:119:29 | [My3(...)] | My3Attribute |
| attributes.cs:121:9:121:11 | value | attributes.cs:120:17:120:28 | [My3(...)] | My3Attribute |
| attributes.cs:126:16:126:20 | Prop1 | attributes.cs:125:6:125:17 | [My3(...)] | My3Attribute |
| attributes.cs:130:9:130:11 | get_Prop1 | attributes.cs:128:18:128:29 | [My3(...)] | My3Attribute |
| attributes.cs:130:9:130:11 | get_Prop1 | attributes.cs:129:18:129:29 | [return: My3(...)] | My3Attribute |
| attributes.cs:134:9:134:11 | set_Prop1 | attributes.cs:132:10:132:21 | [My3(...)] | My3Attribute |
| attributes.cs:134:9:134:11 | value | attributes.cs:133:17:133:28 | [My3(...)] | My3Attribute |
| attributes.cs:146:17:146:18 | M1 | attributes.cs:145:6:145:11 | [Params(...)] | Class1+ParamsAttribute |
| attributes.cs:149:17:149:18 | M2 | attributes.cs:148:6:148:11 | [Params(...)] | Class1+ParamsAttribute |
| attributes.cs:152:17:152:18 | M3 | attributes.cs:151:6:151:11 | [Params(...)] | Class1+ParamsAttribute |
| attributes.cs:155:17:155:18 | M4 | attributes.cs:154:6:154:11 | [Params(...)] | Class1+ParamsAttribute |
| attributes.cs:159:14:159:32 | MyExperimentalClass | attributes.cs:158:2:158:13 | [Experimental(...)] | System.Diagnostics.CodeAnalysis.ExperimentalAttribute |
| attributes.cs:162:17:162:36 | MyExperimentalMethod | attributes.cs:161:6:161:17 | [Experimental(...)] | System.Diagnostics.CodeAnalysis.ExperimentalAttribute |
| attributes.cs:168:17:168:17 | M | attributes.cs:167:6:167:31 | [OverloadResolutionPriority(...)] | System.Runtime.CompilerServices.OverloadResolutionPriorityAttribute |
| attributes.cs:171:17:171:17 | M | attributes.cs:170:6:170:31 | [OverloadResolutionPriority(...)] | System.Runtime.CompilerServices.OverloadResolutionPriorityAttribute |
| attributes.cs:174:17:174:17 | M | attributes.cs:173:6:173:31 | [OverloadResolutionPriority(...)] | System.Runtime.CompilerServices.OverloadResolutionPriorityAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:12:12:12:24 | [assembly: AssemblyTitle(...)] | System.Reflection.AssemblyTitleAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:13:12:13:30 | [assembly: AssemblyDescription(...)] | System.Reflection.AssemblyDescriptionAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:14:12:14:32 | [assembly: AssemblyConfiguration(...)] | System.Reflection.AssemblyConfigurationAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:15:12:15:26 | [assembly: AssemblyCompany(...)] | System.Reflection.AssemblyCompanyAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:16:12:16:26 | [assembly: AssemblyProduct(...)] | System.Reflection.AssemblyProductAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:17:12:17:28 | [assembly: AssemblyCopyright(...)] | System.Reflection.AssemblyCopyrightAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:18:12:18:28 | [assembly: AssemblyTrademark(...)] | System.Reflection.AssemblyTrademarkAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:19:12:19:26 | [assembly: AssemblyCulture(...)] | System.Reflection.AssemblyCultureAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:24:12:24:21 | [assembly: ComVisible(...)] | System.Runtime.InteropServices.ComVisibleAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:27:12:27:15 | [assembly: Guid(...)] | System.Runtime.InteropServices.GuidAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:39:12:39:26 | [assembly: AssemblyVersion(...)] | System.Reflection.AssemblyVersionAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:40:12:40:30 | [assembly: AssemblyFileVersion(...)] | System.Reflection.AssemblyFileVersionAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:42:12:42:15 | [assembly: Args(...)] | ArgsAttribute |
| attributes.dll:0:0:0:0 | attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | attributes.cs:43:10:43:13 | [module: Args(...)] | ArgsAttribute |
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
| attributes.cs:158:14:158:32 | MyExperimentalClass | attributes.cs:157:2:157:13 | [Experimental(...)] | MyExperimentalClassId |
| attributes.cs:161:17:161:36 | MyExperimentalMethod | attributes.cs:160:6:160:17 | [Experimental(...)] | MyExperimentalMethodId |
| attributes.cs:159:14:159:32 | MyExperimentalClass | attributes.cs:158:2:158:13 | [Experimental(...)] | MyExperimentalClassId |
| attributes.cs:162:17:162:36 | MyExperimentalMethod | attributes.cs:161:6:161:17 | [Experimental(...)] | MyExperimentalMethodId |
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| attributes.cs:168:17:168:17 | M | attributes.cs:167:6:167:31 | [OverloadResolutionPriority(...)] | -1 |
| attributes.cs:171:17:171:17 | M | attributes.cs:170:6:170:31 | [OverloadResolutionPriority(...)] | 1 |
| attributes.cs:174:17:174:17 | M | attributes.cs:173:6:173:31 | [OverloadResolutionPriority(...)] | 2 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import csharp
import semmle.code.csharp.frameworks.system.runtime.CompilerServices

from
Attributable element, SystemRuntimeCompilerServicesOverloadResolutionPriorityAttribute attribute
where attribute = element.getAnAttribute() and attribute.fromSource()
select element, attribute, attribute.getPriority()
Loading

0 comments on commit d3d3cce

Please sign in to comment.