Skip to content

Commit 859ad1d

Browse files
authored
Merge pull request #21877 from michaelnebel/csharp/spanaccessrange
C#: Extract `.Slice` method call when using a span in conjunction with a range.
2 parents 0df9aac + 66db0d4 commit 859ad1d

8 files changed

Lines changed: 229 additions & 11 deletions

File tree

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

Lines changed: 136 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO;
22
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.CSharp;
34
using Microsoft.CodeAnalysis.CSharp.Syntax;
45
using Semmle.Extraction.Kinds;
56

@@ -8,7 +9,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
89
internal abstract class ElementAccess : Expression<ExpressionSyntax>
910
{
1011
protected ElementAccess(ExpressionNodeInfo info, ExpressionSyntax qualifier, BracketedArgumentListSyntax argumentList)
11-
: base(info.SetKind(GetKind(info.Context, qualifier)))
12+
: base(info.SetKind(GetKind(info.Context, info.Node, qualifier)))
1213
{
1314
this.qualifier = qualifier;
1415
this.argumentList = argumentList;
@@ -17,6 +18,125 @@ protected ElementAccess(ExpressionNodeInfo info, ExpressionSyntax qualifier, Bra
1718
private readonly ExpressionSyntax qualifier;
1819
private readonly BracketedArgumentListSyntax argumentList;
1920

21+
22+
private ISymbol? GetTargetSymbol()
23+
{
24+
return Context.GetSymbolInfo(base.Syntax).Symbol;
25+
}
26+
27+
private static void SetExprArgument(TextWriter trapFile, Expression left, Expression right)
28+
{
29+
trapFile.expr_argument(left, 0);
30+
trapFile.expr_argument(right, 0);
31+
}
32+
33+
private Expression MakeZeroFromEndExpression(IExpressionParentEntity parent, int child)
34+
{
35+
var info = new ExpressionInfo(
36+
Context,
37+
AnnotatedTypeSymbol.CreateNotAnnotated(Context.Compilation.GetSpecialType(SpecialType.System_Int32)),
38+
Location,
39+
ExprKind.INDEX,
40+
parent,
41+
child,
42+
isCompilerGenerated: true,
43+
null);
44+
45+
var index = new Expression(info);
46+
47+
MakeZeroLiteral(index, 0);
48+
return index;
49+
}
50+
51+
private Expression MakeZeroLiteral(IExpressionParentEntity parent, int child)
52+
{
53+
return Literal.CreateGenerated(Context, parent, child, Context.Compilation.GetSpecialType(SpecialType.System_Int32), 0, Location);
54+
}
55+
56+
57+
/// <summary>
58+
/// It is assumed that either the input is
59+
/// 1. A normal expression that can be used as endpoint (e.g a constant like "3").
60+
/// 2. An index expression indicating that we should read from the end (e.g "^1").
61+
/// </summary>
62+
/// <param name="syntax">The syntax node representing the range endpoint.</param>
63+
/// <param name="parent">The parent expression entity.</param>
64+
/// <param name="child">The child index within the parent.</param>
65+
/// <returns>An expression representing the endpoint of a range to be used in conjunction with a slice operation.</returns>
66+
private Expression MakeFromRangeEndpoint(ExpressionSyntax syntax, IExpressionParentEntity parent, int child)
67+
{
68+
var info = new ExpressionNodeInfo(Context, syntax, parent, child);
69+
70+
return syntax.Kind() == SyntaxKind.IndexExpression
71+
? PrefixUnary.Create(info.SetKind(ExprKind.INDEX))
72+
: Factory.Create(info);
73+
}
74+
75+
/// <summary>
76+
/// Determines whether the given method is a slice method, which is defined as a method with
77+
/// the name "Slice" or "Substring" and two parameters.
78+
/// </summary>
79+
/// <param name="method">The method symbol to check.</param>
80+
/// <returns>True if the method is a slice method; false otherwise.</returns>
81+
private bool IsSlice(IMethodSymbol method, out RangeExpressionSyntax? range)
82+
{
83+
range = null;
84+
85+
if (argumentList.Arguments.Count == 1)
86+
{
87+
range = argumentList.Arguments[0].Expression as RangeExpressionSyntax;
88+
}
89+
90+
return (method.Name == "Slice" || method.Name == "Substring")
91+
&& method.Parameters.Length == 2;
92+
}
93+
94+
/// <summary>
95+
/// Populates a slice method call based on the given range.
96+
/// Roslyn translates indexer accesses with range expressions in the following way.
97+
/// 1. s[a..b] -> s.Slice(a, b - a)
98+
/// 2. s[..b] -> s.Slice(0, b)
99+
/// 3. s[a..] -> s.Slice(a, s.Length - a)
100+
/// 4. s[..] -> s.Slice(0, s.Length)
101+
/// However, it is possible that both the qualifier or the index endpoints may contain method calls.
102+
/// If we want to translate this accurately, we would need to introduce synthetic statements for qualifier and
103+
/// the endpoints, which should then be used in the slice method call.
104+
/// To avoid this, we translate as follows.
105+
/// 1. s[a..b] -> s.Slice(a, b)
106+
/// 2. s[..b] -> s.Slice(0, b)
107+
/// 3. s[a..] -> s.Slice(a, ^0)
108+
/// 4. s[..] -> s.Slice(0, ^0)
109+
///
110+
/// Even though index expressions can't technically be used in this way, they signal that we
111+
/// could perceive ^b as "length - b".
112+
///
113+
/// Call arguments are only populated when a range expression is directly available in
114+
/// the list of arguments.
115+
/// This means that cases like below are not handled.
116+
/// System.Range x = 1..3;
117+
/// s[x]
118+
/// </summary>
119+
/// <param name="trapFile">The trap file to write to.</param>
120+
/// <param name="slice">The slice method symbol.</param>
121+
/// <param name="range">The range expression syntax.</param>
122+
private void PopulateSlice(TextWriter trapFile, IMethodSymbol slice, RangeExpressionSyntax? range)
123+
{
124+
if (range is not null)
125+
{
126+
// Populate the call arguments
127+
var left = range.LeftOperand is ExpressionSyntax lsyntax
128+
? MakeFromRangeEndpoint(lsyntax, this, 0)
129+
: MakeZeroLiteral(this, 0);
130+
131+
var right = range.RightOperand is ExpressionSyntax rsyntax
132+
? MakeFromRangeEndpoint(rsyntax, this, 1)
133+
: MakeZeroFromEndExpression(this, 1);
134+
135+
SetExprArgument(trapFile, left, right);
136+
}
137+
trapFile.expr_call(this, Method.Create(Context, slice));
138+
}
139+
20140
protected override void PopulateExpression(TextWriter trapFile)
21141
{
22142
if (Kind == ExprKind.POINTER_INDIRECTION)
@@ -30,11 +150,19 @@ protected override void PopulateExpression(TextWriter trapFile)
30150
else
31151
{
32152
Create(Context, qualifier, this, -1);
33-
PopulateArguments(trapFile, argumentList, 0);
34153

35-
var symbolInfo = Context.GetSymbolInfo(base.Syntax);
154+
var target = GetTargetSymbol();
155+
if (target is IMethodSymbol method && IsSlice(method, out var range))
156+
{
157+
// When an indexer on a span or string is used in conjunction with a range expression, the compiler translates
158+
// this into a call to the "Slice" or "Substring" method.
159+
// In this case, we want to populate a slice/substring method call instead of an indexer access.
160+
PopulateSlice(trapFile, method, range);
161+
return;
162+
}
36163

37-
if (symbolInfo.Symbol is IPropertySymbol indexer)
164+
PopulateArguments(trapFile, argumentList, 0);
165+
if (target is IPropertySymbol { IsIndexer: true } indexer)
38166
{
39167
trapFile.expr_access(this, Indexer.Create(Context, indexer));
40168
}
@@ -46,8 +174,11 @@ protected override void PopulateExpression(TextWriter trapFile)
46174
private static bool IsArray(ITypeSymbol symbol) =>
47175
symbol.TypeKind == Microsoft.CodeAnalysis.TypeKind.Array || symbol.IsInlineArray();
48176

49-
private static ExprKind GetKind(Context cx, ExpressionSyntax qualifier)
177+
private static ExprKind GetKind(Context cx, ExpressionSyntax syntax, ExpressionSyntax qualifier)
50178
{
179+
if (cx.GetSymbolInfo(syntax).Symbol is IMethodSymbol)
180+
return ExprKind.METHOD_INVOCATION;
181+
51182
var qualifierType = cx.GetType(qualifier);
52183

53184
// This is a compilation error, so make a guess and continue.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Improved extraction of range-access expressions on spans and strings (for example, `a[0..3]`). These expressions are now extracted as `Slice` (span) or `Substring` (string) calls.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
public class C
4+
{
5+
public void M(int a, int b)
6+
{
7+
var s = "hello world";
8+
var sub1 = s[1..a];
9+
var sub2 = s[..2];
10+
var sub3 = s[3..];
11+
var sub4 = s[..^4];
12+
var sub5 = s[a..^b];
13+
var sub6 = s[..];
14+
15+
Range range = 1..a;
16+
var sub7 = s[range];
17+
18+
Span<int> sp = null;
19+
var slice1 = sp[5..a];
20+
var slice2 = sp[..6];
21+
var slice3 = sp[7..];
22+
var slice4 = sp[..^8];
23+
var slice5 = sp[a..^b];
24+
var slice6 = sp[..];
25+
26+
Range range2 = 1..a;
27+
var slice7 = sp[range2];
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
methodArguments
2+
| Slice.cs:8:20:8:26 | call to method Substring | Substring(int, int) | 0 | 1 |
3+
| Slice.cs:8:20:8:26 | call to method Substring | Substring(int, int) | 1 | access to parameter a |
4+
| Slice.cs:9:20:9:25 | call to method Substring | Substring(int, int) | 0 | 0 |
5+
| Slice.cs:9:20:9:25 | call to method Substring | Substring(int, int) | 1 | 2 |
6+
| Slice.cs:10:20:10:25 | call to method Substring | Substring(int, int) | 0 | 3 |
7+
| Slice.cs:10:20:10:25 | call to method Substring | Substring(int, int) | 1 | ^0 |
8+
| Slice.cs:11:20:11:26 | call to method Substring | Substring(int, int) | 0 | 0 |
9+
| Slice.cs:11:20:11:26 | call to method Substring | Substring(int, int) | 1 | ^4 |
10+
| Slice.cs:12:20:12:27 | call to method Substring | Substring(int, int) | 0 | access to parameter a |
11+
| Slice.cs:12:20:12:27 | call to method Substring | Substring(int, int) | 1 | ^access to parameter b |
12+
| Slice.cs:13:20:13:24 | call to method Substring | Substring(int, int) | 0 | 0 |
13+
| Slice.cs:13:20:13:24 | call to method Substring | Substring(int, int) | 1 | ^0 |
14+
| Slice.cs:19:22:19:29 | call to method Slice | Slice(int, int) | 0 | 5 |
15+
| Slice.cs:19:22:19:29 | call to method Slice | Slice(int, int) | 1 | access to parameter a |
16+
| Slice.cs:20:22:20:28 | call to method Slice | Slice(int, int) | 0 | 0 |
17+
| Slice.cs:20:22:20:28 | call to method Slice | Slice(int, int) | 1 | 6 |
18+
| Slice.cs:21:22:21:28 | call to method Slice | Slice(int, int) | 0 | 7 |
19+
| Slice.cs:21:22:21:28 | call to method Slice | Slice(int, int) | 1 | ^0 |
20+
| Slice.cs:22:22:22:29 | call to method Slice | Slice(int, int) | 0 | 0 |
21+
| Slice.cs:22:22:22:29 | call to method Slice | Slice(int, int) | 1 | ^8 |
22+
| Slice.cs:23:22:23:30 | call to method Slice | Slice(int, int) | 0 | access to parameter a |
23+
| Slice.cs:23:22:23:30 | call to method Slice | Slice(int, int) | 1 | ^access to parameter b |
24+
| Slice.cs:24:22:24:27 | call to method Slice | Slice(int, int) | 0 | 0 |
25+
| Slice.cs:24:22:24:27 | call to method Slice | Slice(int, int) | 1 | ^0 |
26+
methodCalls
27+
| Slice.cs:3:14:3:14 | call to method <object initializer> | <object initializer>() |
28+
| Slice.cs:8:20:8:26 | call to method Substring | Substring(int, int) |
29+
| Slice.cs:9:20:9:25 | call to method Substring | Substring(int, int) |
30+
| Slice.cs:10:20:10:25 | call to method Substring | Substring(int, int) |
31+
| Slice.cs:11:20:11:26 | call to method Substring | Substring(int, int) |
32+
| Slice.cs:12:20:12:27 | call to method Substring | Substring(int, int) |
33+
| Slice.cs:13:20:13:24 | call to method Substring | Substring(int, int) |
34+
| Slice.cs:16:20:16:27 | call to method Substring | Substring(int, int) |
35+
| Slice.cs:19:22:19:29 | call to method Slice | Slice(int, int) |
36+
| Slice.cs:20:22:20:28 | call to method Slice | Slice(int, int) |
37+
| Slice.cs:21:22:21:28 | call to method Slice | Slice(int, int) |
38+
| Slice.cs:22:22:22:29 | call to method Slice | Slice(int, int) |
39+
| Slice.cs:23:22:23:30 | call to method Slice | Slice(int, int) |
40+
| Slice.cs:24:22:24:27 | call to method Slice | Slice(int, int) |
41+
| Slice.cs:27:22:27:31 | call to method Slice | Slice(int, int) |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import csharp
2+
3+
private string printExpr(Expr e) {
4+
e = any(IndexExpr index | result = "^" + index.getExpr().toString())
5+
or
6+
not e instanceof IndexExpr and
7+
result = e.toString()
8+
}
9+
10+
query predicate methodArguments(MethodCall mc, string target, int i, string arg) {
11+
target = mc.getTarget().toStringWithTypes() and
12+
arg = printExpr(mc.getArgument(i))
13+
}
14+
15+
query predicate methodCalls(MethodCall mc, string target) {
16+
target = mc.getTarget().toStringWithTypes()
17+
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
| Quality.cs:26:19:26:26 | access to indexer | Call without target $@. | Quality.cs:26:19:26:26 | access to indexer | access to indexer |
2-
| Quality.cs:29:21:29:27 | access to indexer | Call without target $@. | Quality.cs:29:21:29:27 | access to indexer | access to indexer |

csharp/ql/test/query-tests/Telemetry/DatabaseQuality/NoTarget.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@
77
| Quality.cs:20:13:20:23 | access to property MyProperty6 | Call without target $@. | Quality.cs:20:13:20:23 | access to property MyProperty6 | access to property MyProperty6 |
88
| Quality.cs:23:9:23:14 | access to event Event1 | Call without target $@. | Quality.cs:23:9:23:14 | access to event Event1 | access to event Event1 |
99
| Quality.cs:23:9:23:30 | delegate call | Call without target $@. | Quality.cs:23:9:23:30 | delegate call | delegate call |
10-
| Quality.cs:26:19:26:26 | access to indexer | Call without target $@. | Quality.cs:26:19:26:26 | access to indexer | access to indexer |
11-
| Quality.cs:29:21:29:27 | access to indexer | Call without target $@. | Quality.cs:29:21:29:27 | access to indexer | access to indexer |
1210
| Quality.cs:38:16:38:26 | access to property MyProperty2 | Call without target $@. | Quality.cs:38:16:38:26 | access to property MyProperty2 | access to property MyProperty2 |
1311
| Quality.cs:50:20:50:26 | object creation of type T | Call without target $@. | Quality.cs:50:20:50:26 | object creation of type T | object creation of type T |

csharp/ql/test/query-tests/Telemetry/DatabaseQuality/Quality.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public Test()
2323
Event1.Invoke(this, 5);
2424

2525
var str = "abcd";
26-
var sub = str[..3]; // TODO: this is not an indexer call, but rather a `str.Substring(0, 3)` call.
26+
var sub = str[..3];
2727

2828
Span<int> sp = null;
29-
var slice = sp[..3]; // TODO: this is not an indexer call, but rather a `sp.Slice(0, 3)` call.
29+
var slice = sp[..3];
3030

3131
Span<byte> guidBytes = stackalloc byte[16];
3232
guidBytes[08] = 1;

0 commit comments

Comments
 (0)