From aac3ec81f265d6bf8c10c27a4733800b85979cb3 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Thu, 7 Dec 2023 13:13:29 +0100 Subject: [PATCH] C#: Add generated cast expression for nuint and nint parameter defaults. --- .../Entities/Expression.cs | 7 ++++- .../Entities/Expressions/ImplicitCast.cs | 28 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs index 6c5ac993ca6..e6d610de55a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs @@ -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); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitCast.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitCast.cs index d2762f20a07..57a37d86360 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitCast.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitCast.cs @@ -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, + /// + /// Creates a new generated expression with an implicit conversion added. + /// + 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. + /// + /// Creates a new generated cast expression. + /// + 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); + } + + /// + /// Creates a new expression, adding casts as required. + /// public static Expression Create(ExpressionNodeInfo info) { var resolvedType = info.ResolvedType;