Skip to content

Commit 5fef77b

Browse files
committed
C#: Handle expressions of typeImplicitStackAllocArrayCreationExpressionSyntax
1 parent 3c8aeb9 commit 5fef77b

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ArrayCreation.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ class StackAllocArrayCreation : ExplicitArrayCreation<StackAllocArrayCreationExp
9393
public static Expression Create(ExpressionNodeInfo info) => new StackAllocArrayCreation(info).TryPopulate();
9494
}
9595

96+
class ImplicitStackAllocArrayCreation : ArrayCreation<ImplicitStackAllocArrayCreationExpressionSyntax>
97+
{
98+
ImplicitStackAllocArrayCreation(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.ARRAY_CREATION)) { }
99+
100+
public static Expression Create(ExpressionNodeInfo info) => new ImplicitStackAllocArrayCreation(info).TryPopulate();
101+
102+
protected override void PopulateExpression(TextWriter trapFile)
103+
{
104+
if (Syntax.Initializer != null)
105+
{
106+
ArrayInitializer.Create(new ExpressionNodeInfo(cx, Syntax.Initializer, this, -1));
107+
}
108+
109+
trapFile.implicitly_typed_array_creation(this);
110+
}
111+
}
112+
96113
class ImplicitArrayCreation : ArrayCreation<ImplicitArrayCreationExpressionSyntax>
97114
{
98115
ImplicitArrayCreation(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.ARRAY_CREATION)) { }

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Factory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ internal static Expression Create(ExpressionNodeInfo info)
207207
case SyntaxKind.StackAllocArrayCreationExpression:
208208
return StackAllocArrayCreation.Create(info);
209209

210+
case SyntaxKind.ImplicitStackAllocArrayCreationExpression:
211+
return ImplicitStackAllocArrayCreation.Create(info);
212+
210213
case SyntaxKind.ArgListExpression:
211214
return ArgList.Create(info);
212215

csharp/ql/test/library-tests/csharp7.3/ArrayCreations.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
| csharp73.cs:11:20:11:37 | array creation of type Char[] | 0 | csharp73.cs:11:20:11:37 | 1 |
44
| csharp73.cs:12:20:12:38 | array creation of type Char* | 0 | csharp73.cs:12:36:12:37 | 10 |
55
| csharp73.cs:13:20:13:31 | array creation of type Char[] | 0 | csharp73.cs:13:29:13:30 | 10 |
6-
| csharp73.cs:21:23:21:33 | array creation of type Int32[] | 0 | csharp73.cs:21:31:21:32 | 10 |
6+
| csharp73.cs:22:23:22:33 | array creation of type Int32[] | 0 | csharp73.cs:22:31:22:32 | 10 |

csharp/ql/test/library-tests/csharp7.3/ArrayElements.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
| csharp73.cs:9:20:9:49 | array creation of type Char* | 1 | csharp73.cs:9:45:9:47 | y |
33
| csharp73.cs:10:20:10:45 | array creation of type Char* | 0 | csharp73.cs:10:41:10:43 | x |
44
| csharp73.cs:11:20:11:37 | array creation of type Char[] | 0 | csharp73.cs:11:33:11:35 | x |
5+
| csharp73.cs:14:20:14:43 | array creation of type Int32* | 0 | csharp73.cs:14:35:14:35 | 1 |
6+
| csharp73.cs:14:20:14:43 | array creation of type Int32* | 1 | csharp73.cs:14:38:14:38 | 2 |
7+
| csharp73.cs:14:20:14:43 | array creation of type Int32* | 2 | csharp73.cs:14:41:14:41 | 3 |

csharp/ql/test/library-tests/csharp7.3/csharp73.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ unsafe void Fn()
1111
var arr3 = new char[] { 'x' };
1212
var arr4 = stackalloc char[10];
1313
var arr5 = new char[10];
14+
var arr6 = stackalloc[] { 1, 2, 3 };
1415
}
1516
}
1617

0 commit comments

Comments
 (0)