C#: Add generated cast expression for nuint and nint parameter defaults.

This commit is contained in:
Michael Nebel 2023-12-07 13:13:29 +01:00
Родитель 9390b48228
Коммит aac3ec81f2
2 изменённых файлов: 31 добавлений и 4 удалений

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

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