Roslyn Analyzer

Roslyn analyzers provide a live static analysis of you code. They can detect wrong usages of APIs, security issues, performance issues etc..

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.

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.

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


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. 


Comments

Popular posts from this blog

Email Sending through O365 using OAuth Protocol

IoT Technology

What is reverse proxy?