Skip to content

Commit 8a97c8c

Browse files
committed
C#: Add QL support for InlineArrayType.
1 parent fcb9e47 commit 8a97c8c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

csharp/ql/lib/semmle/code/csharp/Type.qll

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private import dotnet
1111
private import semmle.code.csharp.metrics.Coupling
1212
private import TypeRef
1313
private import semmle.code.csharp.frameworks.System
14+
private import semmle.code.csharp.frameworks.system.runtime.CompilerServices
1415

1516
/**
1617
* A type.
@@ -1008,6 +1009,58 @@ class NullableType extends ValueType, ConstructedType, @nullable_type {
10081009
override string getAPrimaryQlClass() { result = "NullableType" }
10091010
}
10101011

1012+
/**
1013+
* An inline array type, for example `MyInlineArray` in
1014+
* ```csharp
1015+
* [System.Runtime.CompilerServices.InlineArray(10)]
1016+
* public struct MyInlineArray
1017+
* {
1018+
* private int _elements0;
1019+
* }
1020+
* ```
1021+
*/
1022+
class InlineArrayType extends ValueType, @inline_array_type {
1023+
private SystemRuntimeCompilerServicesInlineArrayAttribute inline_attribute;
1024+
private Field element_type_field;
1025+
1026+
InlineArrayType() {
1027+
inline_attribute = this.(Attributable).getAnAttribute() and
1028+
element_type_field = this.getAField() and
1029+
not element_type_field.isStatic() and
1030+
not element_type_field.isConst()
1031+
}
1032+
1033+
/**
1034+
* Gets the element type of this inline array.
1035+
*/
1036+
Type getElementType() { result = element_type_field.getType() }
1037+
1038+
/**
1039+
* Gets the rank of this inline array (inline arrays always have rank 1).
1040+
*/
1041+
int getRank() { result = 1 }
1042+
1043+
/**
1044+
* Gets the length of this inline array.
1045+
*/
1046+
int getLength() { result = inline_attribute.getLength() }
1047+
1048+
/**
1049+
* Gets the dimension of this inline array.
1050+
*/
1051+
int getDimension() {
1052+
exists(Type elem | elem = this.getElementType() |
1053+
result = elem.(InlineArrayType).getDimension() + 1
1054+
or
1055+
result = elem.(ArrayType).getDimension() + 1
1056+
or
1057+
not elem instanceof ArrayType and not elem instanceof InlineArrayType and result = 1
1058+
)
1059+
}
1060+
1061+
override string getAPrimaryQlClass() { result = "InlineArrayType" }
1062+
}
1063+
10111064
/**
10121065
* An array type, for example `int[]`.
10131066
*/

csharp/ql/lib/semmle/code/csharp/frameworks/system/runtime/CompilerServices.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,15 @@ class SystemRuntimeCompilerServicesConfiguredTaskAwaitableTConfiguredTaskAwaiter
7070
/** Gets the field that stores the underlying task. */
7171
Field getUnderlyingTaskField() { result = this.getAField() and result.hasName("m_task") }
7272
}
73+
74+
/** An attribute of type `System.Runtime.CompilerServices.InlineArrayAttribute`. */
75+
class SystemRuntimeCompilerServicesInlineArrayAttribute extends Attribute {
76+
SystemRuntimeCompilerServicesInlineArrayAttribute() {
77+
this.getType().hasFullyQualifiedName("System.Runtime.CompilerServices", "InlineArrayAttribute")
78+
}
79+
80+
/**
81+
* Gets the length of the inline array.
82+
*/
83+
int getLength() { result = this.getConstructorArgument(0).getValue().toInt() }
84+
}

0 commit comments

Comments
 (0)