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
committed by GitHub
6 changed files with 43 additions and 8 deletions

View File

@@ -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);
}

View File

@@ -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;