简体   繁体   English

单步执行Roslyn C#编译器

[英]Stepping through the Roslyn C# compiler

I've built the Roslyn source as described here . 我已经构建了如此处所述的Roslyn源代码。

I'd like to add a breakpoint in the C# compiler and step through the compliation of this simple program: 我想在C#编译器中添加一个断点,并逐步完成这个简单程序的补充:

using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var result = 1 + 2;

            Console.WriteLine(result);
        }
    }
}

Where should I set a breakpoint? 我应该在哪里设置断点? It should be early in the compilation process as I'd like to step through parsing and even lexing. 它应该在编译过程的早期,因为我想逐步解析甚至是lexing。

If I set CompilerExtension as the startup project and hit F5 (Start Debugging), a copy of Visual Studio is launched running the newly built compiler. 如果我将CompilerExtension设置为启动项目并按F5(开始调试),则会启动运行新构建的编译器的Visual Studio副本。 I'd like to avoid having to launch a new instance of Visual Studio each time I'd like to step through the compiler. 我想避免每次我想要逐步完成编译器时都启动一个新的Visual Studio实例。 What's a good way to setup a small program which invokes the compiler code on the above source directly? 设置一个直接在上面的源上调用编译器代码的小程序有什么好方法?

Here's one approach as suggested by Josh in a comment above. 以下是Josh在上述评论中提出的一种方法。

  • Add a new "Console Application" project to the Roslyn solution. 向Roslyn解决方案添加一个新的“控制台应用程序”项目。

  • Add these two references to the project: 将这两个引用添加到项目中:

在此输入图像描述

A simple program to test the parser: 一个测试解析器的简单程序:

using Microsoft.CodeAnalysis.CSharp;

namespace TestCompiler
{
    class Program
    {
        static void Main(string[] args)
        {
            var program_text = @"

                using System;

                namespace ConsoleApplication2
                {
                    class Program
                    {
                        static void Main(string[] args) 
                        { var result = 2 + 3; Console.WriteLine(result); }
                    }
                }
            ";

            var result = CSharpSyntaxTree.ParseText(program_text);
        }
    }
}
  • Add a breakpoint to the line that calls ParseText . 在调用ParseText的行中添加断点。

  • "Start Debugging" and step into that line to delve into the parser. “开始调试”并进入该行以深入研究解析器。

A simple program to test the compiler via Emit : 一个通过Emit测试编译器的简单程序:

using System;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace TestCompiler
{
    class Program
    {
        static void Main(string[] args)
        {
            var program_text = @"

                using System;

                namespace ConsoleApplication2
                {
                    class Program
                    {
                        static void Main(string[] args) 
                        { var result = 2 + 3; Console.WriteLine(result); }
                    }
                }
            ";

            var syntax_tree = CSharpSyntaxTree.ParseText(program_text);

            var compilation = CSharpCompilation.Create(
                Guid.NewGuid().ToString("D"),
                new[] { syntax_tree },
                new[] { MetadataReference.CreateFromFile(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll") });

            var emit_result = compilation.Emit(new MemoryStream());
        }
    }
}

If you want to have a simple program that invokes the compiler, just consider using csc as your startup project. 如果你想拥有一个调用编译器的简单程序,只需考虑使用csc作为你的启动项目。 You can specify the arguments to pass (like source files) from the debug settings on the project. 您可以从项目的调试设置中指定要传递的参数(如源文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM