From eb8c22674926483cfc58cbb79f3f4eb85ced3793 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Wed, 26 Jan 2022 15:40:10 +0100 Subject: [PATCH] C#: Add support for explicit return types in the extractor. --- .../Entities/Expressions/Lambda.cs | 15 +++++++++++---- .../extractor/Semmle.Extraction.CSharp/Tuples.cs | 3 +++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Lambda.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Lambda.cs index 78b5d9dba98..ed762872e81 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Lambda.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Lambda.cs @@ -22,7 +22,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions Parameter.Create(Context, symbol, this); } - private Lambda(ExpressionNodeInfo info, CSharpSyntaxNode body, IEnumerable @params) + private Lambda(ExpressionNodeInfo info, CSharpSyntaxNode body, IEnumerable @params, TypeSyntax? @return) : base(info) { if (Context.GetModel(info.Node).GetSymbolInfo(info.Node).Symbol is IMethodSymbol symbol) @@ -40,6 +40,13 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions foreach (var param in @params) VisitParameter(param); + if (@return is not null) + { + var symbol = Context.GetType(@return); + var type = Entities.Type.Create(Context, symbol); + var trapFile = Context.TrapWriter.Writer; + trapFile.lambda_expr_return_type(this, type.TypeRef); + } if (body is ExpressionSyntax exprBody) Create(Context, exprBody, this, 0); else if (body is BlockSyntax blockBody) @@ -50,17 +57,17 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions } private Lambda(ExpressionNodeInfo info, ParenthesizedLambdaExpressionSyntax node) - : this(info.SetKind(ExprKind.LAMBDA), node.Body, node.ParameterList.Parameters) { } + : this(info.SetKind(ExprKind.LAMBDA), node.Body, node.ParameterList.Parameters, node.ReturnType) { } public static Lambda Create(ExpressionNodeInfo info, ParenthesizedLambdaExpressionSyntax node) => new Lambda(info, node); private Lambda(ExpressionNodeInfo info, SimpleLambdaExpressionSyntax node) - : this(info.SetKind(ExprKind.LAMBDA), node.Body, Enumerators.Singleton(node.Parameter)) { } + : this(info.SetKind(ExprKind.LAMBDA), node.Body, Enumerators.Singleton(node.Parameter), null) { } public static Lambda Create(ExpressionNodeInfo info, SimpleLambdaExpressionSyntax node) => new Lambda(info, node); private Lambda(ExpressionNodeInfo info, AnonymousMethodExpressionSyntax node) : - this(info.SetKind(ExprKind.ANONYMOUS_METHOD), node.Body, node.ParameterList is null ? Enumerable.Empty() : node.ParameterList.Parameters) + this(info.SetKind(ExprKind.ANONYMOUS_METHOD), node.Body, node.ParameterList is null ? Enumerable.Empty() : node.ParameterList.Parameters, null) { } public static Lambda Create(ExpressionNodeInfo info, AnonymousMethodExpressionSyntax node) => new Lambda(info, node); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs b/csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs index 58346b157a5..3476aa027bf 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs @@ -215,6 +215,9 @@ namespace Semmle.Extraction.CSharp internal static void indexers(this TextWriter trapFile, Indexer propKey, string name, Type declaringType, Type memberType, Indexer unboundProperty) => trapFile.WriteTuple("indexers", propKey, name, declaringType, memberType, unboundProperty); + internal static void lambda_expr_return_type(this TextWriter trapFile, Lambda expr, Type returnType) => + trapFile.WriteTuple("lambda_expr_return_type", expr, returnType); + internal static void local_function_stmts(this TextWriter trapFile, Entities.Statements.LocalFunction fnStmt, LocalFunction fn) => trapFile.WriteTuple("local_function_stmts", fnStmt, fn);