Roslyn Analyzer

08 januari 2021 om 10:00 by ParTech Media - Post a comment

Before the advent of Visual Studio 2015, the main problem faced by developers while writing VS extensions was the lack of a reliable way of obtaining information about the code to be extended. But after 2015, Visual Studio started taking full advantage of a .NET Compiler Platform known as Roslyn.

Many of the code editor functionalities are implemented using Roslyn public APIs such as automatic code formatting and coloring, IntelliSense, code navigation, and refactoring. Microsoft wanted to make the C# and VB compiler useful in other scenarios such as diagnostics, static analysis, source code transformation, etc. To achieve that, Microsoft created a compiler that not only converts source code into binaries but also acts as a service, that provides a public API for understanding the code.

In this post, we will understand what is Roslyn Analyzer and how it set it up in your system.

Table of Contents

  1. What is Roslyn Analyzer?
  2. Roslyn public API
  3. Setting up Roslyn
  4. Wrapping Up

What is Roslyn Analyzer?

Project Roslyn is a part of the .NET Foundation along with other projects like .NET Runtime. Roslyn is an open-source provider of C# and Visual Basic compilers developed by Microsoft, with rich code analysis APIs.. Roslyn is just the code name provided to the .NET Compiler Platform. Roslyn can produce warnings in your code even before you have finished the line, thus it is aptly termed an analyzer. Roslyn can also suggest automatic error fixation.

Roslyn Analyzers are used to inspect your C# or Visual Basic code for style, quality, design, maintainability, and other aspects. Processing of source code consists of three main phases: syntax analysis, semantic analysis, and code generation. The output of each phase acts as the input for the next phase, but these intermediate results and internal data structures are not visible/accessible to the user. Thus, developers consider the compiler as a Black box.

Here is the link to the source code of Roslyn on a GitHub repository - here

Roslyn Public API

Roslyn APIs use the pipeline architecture of a traditional compiler, providing access to each step of the compiler’s source code analysis and processing:

  • Syntax tree API shows the lexical and syntactic structure of your code, including formatting and comments. The latter two are important since they help to manipulate the code and keep the formatting and code comments intact.
  • Symbol API shows you the symbol table containing names declared in your source code, as well as those originating from referenced assemblies without corresponding source code.
  • Binding and Flow Analysis APIs expose the complete semantic model of your code that then becomes available after the binding phase. These APIs possess all the information about the code that is required for generating the binaries. This includes any errors and warnings that were detected in the code.
  • Emit API provides access to services for forming the IL byte code.

The scope of Roslyn goes further than just being only a compiler API. Roslyn codebase is now being used in two scenarios with almost contrasting requirements. Considering the compiler, high throughput and correctness are the most important factors whereas while considering the interactive editor, responsiveness and error detection are of higher priority. Roslyn has managed to achieve comparable, if not better performance than Visual Studio 2013, in both of the aforementioned fields. However, this also comes with some downsides viz. all of the data structures are immutable i.e. they cannot be modified.

Every time a developer makes a single character change in any of the files, a new copy of all the data structures is created, leaving the previous version unchanged. This shows a great level of parallelism and concurrency in the Roslyn engine, preventing any race conditions which could occur in any case. This is considered a huge advantage of the Roslyn analyzer. In the interest of performance, these operations are highly optimized and can be reused in as much as the existing data structures as possible.

Fig.: Roslyn’s spellcheck analyzer that is built into Visual Studio

Fig.- Architectural and functional flowchart of Roslyn

Setting up Roslyn

  • Download the NuGet command line exe from here - https://www.nuget.org/downloads .
  • Download the Roslyn Compiler Platform SDK Templates from here. The important thing is that the templates do not come with Visual Studio 2015. To install them go to: Tools > Extensions and Updates > Online.
  • Now just search for “Roslyn SDK”. This will help you to find the templates that correspond to your version.
  • After installing the templates, you have to restart Visual Studio.
  • Now that you have installed the .NET Compiler Platform SDK, you can effortlessly start using the built-in templates to create a new analyzer project. Navigate to File->New->Project. Now select Extensibility under Templates->Visual C#.

Now, just make sure that version 4.6.2 of the.NET framework is selected. You will also be able to see a template called Analyzer with Code Fix (NuGet + VSIX).

Once you have created your new project the template will create three separate projects for you:

  • A portable class library: This will contain the code of your analyzer. This project is also configured in order to produce a NuGet package post building.
  • A test project: this is a handy test project that gives you a good starting point to test your analyzer
  • A VSIX project: this has two use cases. It produces a VSIX file that can be used to install your analyzers as a Visual Studio Extension and it also enables you to debug your analyzer.

Now you are ready to start. We advise you to explore the default analyzer (DiagnosticAnalyzer.cs) and

code fix provider (CodeFixProvider.cs) that are automatically created as part of the built-in template in

the portable class library project along with the tests (UnitTests.cs) that are provided in the test project.

Wrapping up

Roslyn is currently used in the Microsoft Visual Studio 2015 environment. Various innovative use-cases like code fixes are implemented by integrating the Roslyn platform. In this article, we have tried to give a thorough overview of the Roslyn analyzer which is an open-source analyzer developed by Microsoft for compiling and maintaining C# and Visual Studio codes. There are numerous ways to get your own analyzer project started with Roslyn.