| Name | Message | Date |
|---|---|---|
| 📄 BuildCallGraphShould.cs | 6 days ago | |
| 📄 FindCalleesShould.cs | 6 days ago | |
| 📄 FindCallersShould.cs | 6 days ago | |
| 📄 MessageSource.cs | 6 days ago | |
| 📄 SourceToCallGraphHelper.cs | 6 days ago |
📄
core/tests/CallGraphBuilderTests/SourceToCallGraphHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections.Immutable; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Text; using Shouldly; namespace Cfa.Tests.CallGraphBuilderTests; internal static class SourceToCallGraphHelper { public static async Task<(ISymbol, IImmutableDictionary<ISymbol, IImmutableList<Call>>)> FindCallees( this string source, MessageOptions? messages = null ) { var (start, callGraphBuilder) = await source.GetStartSymbolAndCallGraphBuilder(messages); await callGraphBuilder.FindCallees(start, CancellationToken.None); return (start, callGraphBuilder.ReachableFromSymbols.ToImmutable()); } public static async Task<(ISymbol, IImmutableDictionary<ISymbol, IImmutableList<Call>>)> FindCallers( this string source ) { var (start, callGraphBuilder) = await source.GetStartSymbolAndCallGraphBuilder(); await callGraphBuilder.FindCallers(start, CancellationToken.None); return (start, callGraphBuilder.ReachesToSymbols.ToImmutable()); } private static async Task<(ISymbol, CallGraphBuilder)> GetStartSymbolAndCallGraphBuilder( this string source, MessageOptions? messages = null ) { var workspace = new AdhocWorkspace(); var projectInfo = ProjectInfo.Create( ProjectId.CreateNewId(), VersionStamp.Default, name: "Example", assemblyName: "Example", language: LanguageNames.CSharp, compilationOptions: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary), metadataReferences: [MetadataReference.CreateFromFile(typeof(object).Assembly.Location)] ); workspace .TryApplyChanges( workspace .AddSolution(SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Default)) .AddProject(projectInfo) ) .ShouldBeTrue(); workspace.AddDocument(projectInfo.Id, "Example.cs", SourceText.From(source)); var project = workspace.CurrentSolution.GetProject(projectInfo.Id).ShouldNotBeNull(); var compilation = (await project.GetCompilationAsync()).ShouldNotBeNull(); compilation.GetDiagnostics().ShouldBeEmpty(); var start = compilation .GetTypeByMetadataName("Example") .ShouldNotBeNull() .GetMembers("Start") .ShouldHaveSingleItem(); var callGraphBuilder = new CallGraphBuilder( new ScannerOptions { Messages = messages ?? (source.Contains(MessageSource.Contracts) ? MessageSource.Options : new()), }, workspace.CurrentSolution, [compilation] ); return (start, callGraphBuilder); } }