Skip to content

Commit 3a3f9a2

Browse files
authored
Merge pull request github#13298 from michaelnebel/csharp/paramdefaultimplicitconversion
C#: Extract default parameter values.
2 parents d7c3ac4 + 9aeb238 commit 3a3f9a2

File tree

8 files changed

+106
-23
lines changed

8 files changed

+106
-23
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ private static bool ContainsPattern(SyntaxNode node) =>
211211
return Default.CreateGenerated(cx, parent, childIndex, location, ValueAsString(null));
212212
}
213213

214+
if (type.SpecialType is SpecialType.None)
215+
{
216+
return ImplicitCast.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
217+
}
218+
214219
if (type.SpecialType is SpecialType.System_DateTime)
215220
{
216221
return DateTimeObjectCreation.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);

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

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using Microsoft.CodeAnalysis;
23
using Semmle.Extraction.Kinds;
34

@@ -11,33 +12,73 @@ public Expression Expr
1112
private set;
1213
}
1314

14-
public ImplicitCast(ExpressionNodeInfo info)
15+
private ImplicitCast(ExpressionNodeInfo info)
1516
: base(new ExpressionInfo(info.Context, info.ConvertedType, info.Location, ExprKind.CAST, info.Parent, info.Child, true, info.ExprValue))
1617
{
1718
Expr = Factory.Create(new ExpressionNodeInfo(Context, info.Node, this, 0));
1819
}
1920

20-
public ImplicitCast(ExpressionNodeInfo info, IMethodSymbol method)
21+
private ImplicitCast(ExpressionNodeInfo info, IMethodSymbol method)
2122
: base(new ExpressionInfo(info.Context, info.ConvertedType, info.Location, ExprKind.OPERATOR_INVOCATION, info.Parent, info.Child, true, info.ExprValue))
2223
{
2324
Expr = Factory.Create(info.SetParent(this, 0));
2425

26+
AddOperatorCall(method);
27+
}
28+
29+
private ImplicitCast(ExpressionInfo info, IMethodSymbol method, object value) : base(info)
30+
{
31+
Expr = Literal.CreateGenerated(Context, this, 0, method.Parameters[0].Type, value, info.Location);
32+
33+
AddOperatorCall(method);
34+
}
35+
36+
private void AddOperatorCall(IMethodSymbol method)
37+
{
2538
var target = Method.Create(Context, method);
26-
if (target is not null)
27-
Context.TrapWriter.Writer.expr_call(this, target);
39+
Context.TrapWriter.Writer.expr_call(this, target);
40+
}
41+
42+
private static IMethodSymbol? GetImplicitConversionMethod(ITypeSymbol type, object value) =>
43+
type
44+
.GetMembers()
45+
.OfType<IMethodSymbol>()
46+
.Where(method =>
47+
method.GetName() == "op_Implicit" &&
48+
method.Parameters.Length == 1 &&
49+
method.Parameters[0].Type.Name == value.GetType().Name
50+
)
51+
.FirstOrDefault();
52+
53+
// Creates a new generated expression with an implicit cast added, if needed.
54+
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, object value,
55+
Extraction.Entities.Location location)
56+
{
57+
ExpressionInfo create(ExprKind kind, string? v) =>
58+
new ExpressionInfo(
59+
cx,
60+
AnnotatedTypeSymbol.CreateNotAnnotated(type),
61+
location,
62+
kind,
63+
parent,
64+
childIndex,
65+
true,
66+
v);
67+
68+
var method = GetImplicitConversionMethod(type, value);
69+
if (method is not null)
70+
{
71+
var info = create(ExprKind.OPERATOR_INVOCATION, null);
72+
return new ImplicitCast(info, method, value);
73+
}
2874
else
29-
Context.ModelError(info.Node, "Failed to resolve target for operator invocation");
75+
{
76+
cx.ModelError(location, "Failed to resolve target for implicit operator invocation for a parameter default.");
77+
return new Expression(create(ExprKind.UNKNOWN, ValueAsString(value)));
78+
}
3079
}
3180

32-
/// <summary>
33-
/// Creates a new expression, adding casts as required.
34-
/// </summary>
35-
/// <param name="cx">The extraction context.</param>
36-
/// <param name="node">The expression node.</param>
37-
/// <param name="parent">The parent of the expression.</param>
38-
/// <param name="child">The child number.</param>
39-
/// <param name="type">A type hint.</param>
40-
/// <returns>A new expression.</returns>
81+
// Creates a new expression, adding casts as required.
4182
public static Expression Create(ExpressionNodeInfo info)
4283
{
4384
var resolvedType = info.ResolvedType;

csharp/extractor/Semmle.Extraction.CSharp/SymbolExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public AnnotatedTypeSymbol(ITypeSymbol? symbol, NullableAnnotation nullability)
2525
Nullability = nullability;
2626
}
2727

28-
public static AnnotatedTypeSymbol? CreateNotAnnotated(ITypeSymbol symbol) =>
28+
public static AnnotatedTypeSymbol? CreateNotAnnotated(ITypeSymbol? symbol) =>
2929
symbol is null ? (AnnotatedTypeSymbol?)null : new AnnotatedTypeSymbol(symbol, NullableAnnotation.None);
3030
}
3131

csharp/ql/test/library-tests/parameters/Parameters.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ public class Parameters
2525
public void M17([Optional, DefaultParameterValue(null)] object arg7) => throw null;
2626
public void M18([Optional, DefaultParameterValue(3)] int? arg8) => throw null;
2727
public void M19([Optional, DecimalConstant(1, 0, 0, 0, 103)] decimal arg9) => throw null;
28+
public void M20([Optional, DefaultParameterValue(7)] MyStruct arg10) => throw null;
29+
public void M21([Optional, DefaultParameterValue("mystring")] MyStruct arg10) => throw null;
2830

29-
public struct MyStruct { }
31+
public struct MyStruct
32+
{
33+
public static implicit operator MyStruct(int i) => new MyStruct();
34+
public static implicit operator MyStruct(string s) => new MyStruct();
35+
36+
}
3037
public enum MyEnum { A = 1, B = 2 }
3138
}

csharp/ql/test/library-tests/parameters/Parameters.cs_

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ public class ParametersDll
2525
public void M17([Optional, DefaultParameterValue(null)] object arg7) => throw null;
2626
public void M18([Optional, DefaultParameterValue(3)] int? arg8) => throw null;
2727
public void M19([Optional, DecimalConstant(1, 0, 0, 0, 103)] decimal arg9) => throw null;
28+
public void M20([Optional, DefaultParameterValue(7)] MyStruct arg10) => throw null;
29+
public void M21([Optional, DefaultParameterValue("mystring")] MyStruct arg10) => throw null;
2830

29-
public struct MyStruct { }
31+
public struct MyStruct
32+
{
33+
public static implicit operator MyStruct(int i) => new MyStruct();
34+
public static implicit operator MyStruct(string s) => new MyStruct();
35+
36+
}
3037
public enum MyEnum { A = 1, B = 2 }
3138
}
Binary file not shown.

csharp/ql/test/library-tests/parameters/Parameters.expected

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ noDefaultValue
55
| Parameters.cs:8:17:8:18 | M2 | Parameters.cs:8:24:8:24 | a | 0 |
66
| Parameters.cs:12:17:12:18 | M6 | Parameters.cs:12:29:12:30 | s1 | 0 |
77
| Parameters.cs:13:17:13:18 | M7 | Parameters.cs:13:27:13:28 | e1 | 0 |
8+
| Parameters.cs:33:32:33:39 | implicit conversion | Parameters.cs:33:54:33:54 | i | 0 |
9+
| Parameters.cs:34:32:34:39 | implicit conversion | Parameters.cs:34:57:34:57 | s | 0 |
810
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | a | 0 |
911
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | b | 1 |
1012
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | c | 2 |
1113
| Parameters.dll:0:0:0:0 | M2 | Parameters.dll:0:0:0:0 | a | 0 |
1214
| Parameters.dll:0:0:0:0 | M6 | Parameters.dll:0:0:0:0 | s1 | 0 |
1315
| Parameters.dll:0:0:0:0 | M7 | Parameters.dll:0:0:0:0 | e1 | 0 |
16+
| Parameters.dll:0:0:0:0 | implicit conversion | Parameters.dll:0:0:0:0 | i | 0 |
17+
| Parameters.dll:0:0:0:0 | implicit conversion | Parameters.dll:0:0:0:0 | s | 0 |
1418
withDefaultValue
1519
| Parameters.cs:8:17:8:18 | M2 | Parameters.cs:8:34:8:34 | b | 1 | Parameters.cs:8:38:8:41 | null | null |
1620
| Parameters.cs:8:17:8:18 | M2 | Parameters.cs:8:51:8:51 | c | 2 | Parameters.cs:8:55:8:70 | "default string" | default string |
@@ -39,6 +43,8 @@ withDefaultValue
3943
| Parameters.cs:25:17:25:19 | M17 | Parameters.cs:25:68:25:71 | arg7 | 0 | Parameters.cs:25:21:25:71 | default | null |
4044
| Parameters.cs:26:17:26:19 | M18 | Parameters.cs:26:63:26:66 | arg8 | 0 | Parameters.cs:26:21:26:66 | 3 | 3 |
4145
| Parameters.cs:27:17:27:19 | M19 | Parameters.cs:27:74:27:77 | arg9 | 0 | Parameters.cs:27:21:27:77 | 10.3 | 10.3 |
46+
| Parameters.cs:28:17:28:19 | M20 | Parameters.cs:28:67:28:71 | arg10 | 0 | Parameters.cs:28:21:28:71 | call to operator implicit conversion | - |
47+
| Parameters.cs:29:17:29:19 | M21 | Parameters.cs:29:76:29:80 | arg10 | 0 | Parameters.cs:29:21:29:80 | call to operator implicit conversion | - |
4248
| Parameters.dll:0:0:0:0 | M2 | Parameters.dll:0:0:0:0 | b | 1 | Parameters.dll:0:0:0:0 | default | null |
4349
| Parameters.dll:0:0:0:0 | M2 | Parameters.dll:0:0:0:0 | c | 2 | Parameters.dll:0:0:0:0 | "default string" | default string |
4450
| Parameters.dll:0:0:0:0 | M3 | Parameters.dll:0:0:0:0 | a | 0 | Parameters.dll:0:0:0:0 | 1 | 1 |
@@ -66,8 +72,15 @@ withDefaultValue
6672
| Parameters.dll:0:0:0:0 | M17 | Parameters.dll:0:0:0:0 | arg7 | 0 | Parameters.dll:0:0:0:0 | default | null |
6773
| Parameters.dll:0:0:0:0 | M18 | Parameters.dll:0:0:0:0 | arg8 | 0 | Parameters.dll:0:0:0:0 | 3 | 3 |
6874
| Parameters.dll:0:0:0:0 | M19 | Parameters.dll:0:0:0:0 | arg9 | 0 | Parameters.dll:0:0:0:0 | 10.3 | 10.3 |
75+
| Parameters.dll:0:0:0:0 | M20 | Parameters.dll:0:0:0:0 | arg10 | 0 | Parameters.dll:0:0:0:0 | call to operator implicit conversion | - |
76+
| Parameters.dll:0:0:0:0 | M21 | Parameters.dll:0:0:0:0 | arg10 | 0 | Parameters.dll:0:0:0:0 | call to operator implicit conversion | - |
6977
dateTimeDefaults
7078
| Parameters.cs:22:17:22:19 | M14 | Parameters.cs:22:64:22:67 | arg4 | Parameters.cs:22:21:22:67 | object creation of type DateTime | DateTime(long) | 14 |
7179
| Parameters.cs:23:17:23:19 | M15 | Parameters.cs:23:68:23:71 | arg5 | Parameters.cs:23:21:23:71 | object creation of type DateTime | DateTime(long) | 10001 |
7280
| Parameters.dll:0:0:0:0 | M14 | Parameters.dll:0:0:0:0 | arg4 | Parameters.dll:0:0:0:0 | object creation of type DateTime | DateTime(long) | 14 |
7381
| Parameters.dll:0:0:0:0 | M15 | Parameters.dll:0:0:0:0 | arg5 | Parameters.dll:0:0:0:0 | object creation of type DateTime | DateTime(long) | 10001 |
82+
implicitConversionDefaults
83+
| Parameters.cs:28:17:28:19 | M20 | Parameters.cs:28:67:28:71 | arg10 | Parameters.cs:28:21:28:71 | call to operator implicit conversion | Parameters.cs:28:21:28:71 | 7 | Int32 | 7 |
84+
| Parameters.cs:29:17:29:19 | M21 | Parameters.cs:29:76:29:80 | arg10 | Parameters.cs:29:21:29:80 | call to operator implicit conversion | Parameters.cs:29:21:29:80 | "mystring" | String | mystring |
85+
| Parameters.dll:0:0:0:0 | M20 | Parameters.dll:0:0:0:0 | arg10 | Parameters.dll:0:0:0:0 | call to operator implicit conversion | Parameters.dll:0:0:0:0 | 7 | Int32 | 7 |
86+
| Parameters.dll:0:0:0:0 | M21 | Parameters.dll:0:0:0:0 | arg10 | Parameters.dll:0:0:0:0 | call to operator implicit conversion | Parameters.dll:0:0:0:0 | "mystring" | String | mystring |

csharp/ql/test/library-tests/parameters/Parameters.ql

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,33 @@ query predicate noDefaultValue(Parameterizable container, Parameter p, int i) {
1616
not compilerGeneratedAttribute(container)
1717
}
1818

19-
query predicate withDefaultValue(Parameterizable container, Parameter p, int i, Expr e, string value) {
19+
private predicate defaultValue(Parameterizable container, Parameter p, int i, Expr e) {
2020
fromTestLocation(container) and
2121
p.hasDefaultValue() and
2222
container.getParameter(i) = p and
23-
p.getDefaultValue() = e and
23+
p.getDefaultValue() = e
24+
}
25+
26+
query predicate withDefaultValue(Parameterizable container, Parameter p, int i, Expr e, string value) {
27+
defaultValue(container, p, i, e) and
2428
(if exists(e.getValue()) then value = e.getValue() else value = "-") and
2529
not compilerGeneratedAttribute(container)
2630
}
2731

2832
query predicate dateTimeDefaults(
2933
Parameterizable container, Parameter p, ObjectCreation o, string constructor, string value
3034
) {
31-
fromTestLocation(container) and
32-
p.hasDefaultValue() and
33-
container.getAParameter() = p and
34-
p.getDefaultValue() = o and
35+
defaultValue(container, p, _, o) and
3536
o.getTarget().toStringWithTypes() = constructor and
3637
o.getAnArgument().getValue() = value and
3738
not compilerGeneratedAttribute(container)
3839
}
40+
41+
query predicate implicitConversionDefaults(
42+
Parameterizable container, Parameter p, OperatorCall o, Expr e, string type, string value
43+
) {
44+
defaultValue(container, p, _, o) and
45+
o.getAnArgument() = e and
46+
type = e.getType().toString() and
47+
value = e.getValue()
48+
}

0 commit comments

Comments
 (0)