From a08ca41c199b7915eeda547f974a8aa611eecb6b Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Fri, 11 Oct 2024 12:10:12 +0200 Subject: [PATCH] C#: Add some model generator examples for higher order methods. --- .../modelgenerator/dataflow/Summaries.cs | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/csharp/ql/test/utils/modelgenerator/dataflow/Summaries.cs b/csharp/ql/test/utils/modelgenerator/dataflow/Summaries.cs index 473144358f479..f7c583330ef8c 100644 --- a/csharp/ql/test/utils/modelgenerator/dataflow/Summaries.cs +++ b/csharp/ql/test/utils/modelgenerator/dataflow/Summaries.cs @@ -497,18 +497,55 @@ public Type M6(Type t) } } -// No models as higher order methods are excluded -// from model generation. +// Methods in this class are "neutral" with respect to the heuristic model generation, but +// the content based model generation is able to produce flow summaries for them. public class HigherOrderParameters { + // neutral=Models;HigherOrderParameters;M1;(System.String,System.Func);summary;df-generated + // contentbased-summary=Models;HigherOrderParameters;false;M1;(System.String,System.Func);;Argument[0];ReturnValue;value;dfc-generated public string M1(string s, Func map) { return s; } - public object M2(Func map, object o) + // neutral=Models;HigherOrderParameters;Map;(System.Func,System.Object);summary;df-generated + // contentbased-summary=Models;HigherOrderParameters;false;Map;(System.Func,System.Object);;Argument[1];Argument[0].Parameter[0];value;dfc-generated + // contentbased-summary=Models;HigherOrderParameters;false;Map;(System.Func,System.Object);;Argument[0].ReturnValue;ReturnValue;value;dfc-generated + public object Map(Func f, object o) { - return map(o); + return f(o); + } + + // neutral=Models;HigherOrderParameters;Map2;(System.Object,System.Func);summary;df-generated + // contentbased-summary=Models;HigherOrderParameters;false;Map2;(System.Object,System.Func);;Argument[0];Argument[1].Parameter[1];value;dfc-generated + // contentbased-summary=Models;HigherOrderParameters;false;Map2;(System.Object,System.Func);;Argument[1].ReturnValue;ReturnValue;value;dfc-generated + public object Map2(object o, Func f) + { + var x = f(null, o); + return x; + } + + // neutral=Models;HigherOrderParameters;Apply;(System.Action,System.Object);summary;df-generated + // contentbased-summary=Models;HigherOrderParameters;false;Apply;(System.Action,System.Object);;Argument[1];Argument[0].Parameter[0];value;dfc-generated + public void Apply(Action a, object o) + { + a(o); + } +} + +public static class HigherOrderExtensionMethods +{ + // neutral=Models;HigherOrderExtensionMethods;Select;(System.Collections.Generic.IEnumerable,System.Func);summary;df-generated + // contentbased-summary=Models;HigherOrderExtensionMethods;false;Select;(System.Collections.Generic.IEnumerable,System.Func);;Argument[0].Element;Argument[1].Parameter[0];value;dfc-generated + // contentbased-summary=Models;HigherOrderExtensionMethods;false;Select;(System.Collections.Generic.IEnumerable,System.Func);;Argument[1].ReturnValue;ReturnValue.Element;value;dfc-generated + public static IEnumerable Select( + this IEnumerable source, + Func selector) + { + foreach (var item in source) + { + yield return selector(item); + } } }