-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
75 lines (62 loc) · 2.73 KB
/
Program.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;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using RoslynCSharpCallGraph.Walker;
namespace RoslynCSharpCallGraph
{
class Program
{
static void Main()
{
// workspaceの構築
var projectName = "TestProject";
var assemblyName = "TestProject";
var files = new List<string> { @"HelloWorld/A.cs", @"HelloWorld/B.cs" };
var projectId = ProjectId.CreateNewId(projectName);
var metadataReferences = new List<MetadataReference> {
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Console).Assembly.Location)
};
var documentInfos = new List<DocumentInfo>();
foreach (var file in files)
{
var debugName = file;
var documentId = DocumentId.CreateNewId(projectId, debugName);
var source = File.ReadAllText(file);
var version = VersionStamp.Create();
var textAndVersion = TextAndVersion.Create(SourceText.From(source), version);
var loader = TextLoader.From(textAndVersion);
var filePath = file;
var documentName = file;
var documentInfo = DocumentInfo.Create(
documentId,
documentName,
sourceCodeKind: SourceCodeKind.Regular,
loader: loader,
filePath: filePath
);
documentInfos.Add(documentInfo);
}
var workspace = new AdhocWorkspace();
var solution = workspace.CurrentSolution
.AddProject(projectId, projectName, assemblyName, LanguageNames.CSharp)
.AddMetadataReferences(projectId, metadataReferences)
.AddDocuments(documentInfos.ToImmutableArray());
// 各Documentを解析してgraphvizを出力
Console.WriteLine("digraph graph_name {");
Console.WriteLine("\tgraph [ rankdir = LR ];");
foreach (var documentInfo in documentInfos)
{
var document = solution.GetDocument(documentInfo.Id);
var semanticModel = document.GetSemanticModelAsync().Result;
var syntexTree = document.GetSyntaxTreeAsync().Result;
var walker = new CallGraphWalker(semanticModel);
walker.Visit(syntexTree.GetRoot());
}
Console.WriteLine("}");
}
}
}