Merge pull request #15036 from michaelnebel/csharp/intptrdefaultssimple

C#: Parameter defaults for `nint` and `nuint` in compiled code.
This commit is contained in:
Michael Nebel 2023-12-07 16:20:41 +01:00 коммит произвёл GitHub
Родитель 51adcf5e10 d9c6d4e6cb
Коммит 2e6e2eabf2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 43 добавлений и 8 удалений

Просмотреть файл

@ -214,7 +214,7 @@ namespace Semmle.Extraction.CSharp.Entities
if (type.SpecialType is SpecialType.None)
{
return ImplicitCast.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
return ImplicitCast.CreateGeneratedConversion(cx, parent, childIndex, type, defaultValue, location);
}
if (type.SpecialType is SpecialType.System_DateTime)
@ -222,6 +222,11 @@ namespace Semmle.Extraction.CSharp.Entities
return DateTimeObjectCreation.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
}
if (type.SpecialType is SpecialType.System_IntPtr || type.SpecialType is SpecialType.System_UIntPtr)
{
return ImplicitCast.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
}
// const literal:
return Literal.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
}

Просмотреть файл

@ -51,8 +51,10 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
)
.FirstOrDefault();
// Creates a new generated expression with an implicit cast added, if needed.
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, object value,
/// <summary>
/// Creates a new generated expression with an implicit conversion added.
/// </summary>
public static Expression CreateGeneratedConversion(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, object value,
Extraction.Entities.Location location)
{
ExpressionInfo create(ExprKind kind, string? v) =>
@ -79,7 +81,27 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
}
}
// Creates a new expression, adding casts as required.
/// <summary>
/// Creates a new generated cast expression.
/// </summary>
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, object value,
Extraction.Entities.Location location)
{
var info = new ExpressionInfo(cx,
AnnotatedTypeSymbol.CreateNotAnnotated(type),
location,
ExprKind.CAST,
parent,
childIndex,
true,
ValueAsString(value));
return new Expression(info);
}
/// <summary>
/// Creates a new expression, adding casts as required.
/// </summary>
public static Expression Create(ExpressionNodeInfo info)
{
var resolvedType = info.ResolvedType;

Просмотреть файл

@ -27,6 +27,8 @@ public class Parameters
public void M19([Optional, DecimalConstant(1, 0, 0, 0, 103)] decimal arg9) => throw null;
public void M20([Optional, DefaultParameterValue(7)] MyStruct arg10) => throw null;
public void M21([Optional, DefaultParameterValue("mystring")] MyStruct arg10) => throw null;
public void M22(nuint arg11 = 0) => throw null;
public void M23(nint arg12 = 0) => throw null;
public struct MyStruct
{
@ -35,4 +37,4 @@ public class Parameters
}
public enum MyEnum { A = 1, B = 2 }
}
}

Просмотреть файл

@ -27,6 +27,8 @@ public class ParametersDll
public void M19([Optional, DecimalConstant(1, 0, 0, 0, 103)] decimal arg9) => throw null;
public void M20([Optional, DefaultParameterValue(7)] MyStruct arg10) => throw null;
public void M21([Optional, DefaultParameterValue("mystring")] MyStruct arg10) => throw null;
public void M22(nuint arg11 = 0) => throw null;
public void M23(nint arg12 = 0) => throw null;
public struct MyStruct
{
@ -35,4 +37,4 @@ public class ParametersDll
}
public enum MyEnum { A = 1, B = 2 }
}
}

Двоичный файл не отображается.

Просмотреть файл

@ -5,8 +5,8 @@ noDefaultValue
| Parameters.cs:8:17:8:18 | M2 | Parameters.cs:8:24:8:24 | a | 0 |
| Parameters.cs:12:17:12:18 | M6 | Parameters.cs:12:29:12:30 | s1 | 0 |
| Parameters.cs:13:17:13:18 | M7 | Parameters.cs:13:27:13:28 | e1 | 0 |
| Parameters.cs:33:32:33:39 | implicit conversion | Parameters.cs:33:54:33:54 | i | 0 |
| Parameters.cs:34:32:34:39 | implicit conversion | Parameters.cs:34:57:34:57 | s | 0 |
| Parameters.cs:35:32:35:39 | implicit conversion | Parameters.cs:35:54:35:54 | i | 0 |
| Parameters.cs:36:32:36:39 | implicit conversion | Parameters.cs:36:57:36:57 | s | 0 |
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | a | 0 |
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | b | 1 |
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | c | 2 |
@ -45,6 +45,8 @@ withDefaultValue
| 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 |
| 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 | - |
| 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 | - |
| Parameters.cs:30:17:30:19 | M22 | Parameters.cs:30:27:30:31 | arg11 | 0 | Parameters.cs:30:35:30:35 | (...) ... | 0 |
| Parameters.cs:31:17:31:19 | M23 | Parameters.cs:31:26:31:30 | arg12 | 0 | Parameters.cs:31:34:31:34 | (...) ... | 0 |
| Parameters.dll:0:0:0:0 | M2 | Parameters.dll:0:0:0:0 | b | 1 | Parameters.dll:0:0:0:0 | default | null |
| 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 |
| Parameters.dll:0:0:0:0 | M3 | Parameters.dll:0:0:0:0 | a | 0 | Parameters.dll:0:0:0:0 | 1 | 1 |
@ -74,6 +76,8 @@ withDefaultValue
| 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 |
| 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 | - |
| 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 | - |
| Parameters.dll:0:0:0:0 | M22 | Parameters.dll:0:0:0:0 | arg11 | 0 | Parameters.dll:0:0:0:0 | (...) ... | 0 |
| Parameters.dll:0:0:0:0 | M23 | Parameters.dll:0:0:0:0 | arg12 | 0 | Parameters.dll:0:0:0:0 | (...) ... | 0 |
dateTimeDefaults
| 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 |
| 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 |