Download presentation
Presentation is loading. Please wait.
Published byEthelbert Malone Modified over 8 years ago
1
Joe Hummel, PhD @joehummel joe@joehummel.net http://www.joehummel.net/downloads.html the compiler is at your service Chicago Coder Conference, June 2016
2
Joe Hummel, PhD Professor:U. of Illinois, Chicago Consultant:Joe Hummel, Inc. Trainer:Pluralsight Microsoft MVP Visual C++ Chicago-based, one daughter adopted from China (now 14!) Avid Lake Michigan sailor Chicago Coder Conf 2016 2 Project Roslyn
3
Chicago Coder Conf 2016 3 Project Roslyn
4
What is Project Roslyn? The ".NET Compiler Platform" Replacement of previous.NET compilers with new ones csc for C# vbc for VB.NET Chicago Coder Conf 2016 4 Project Roslyn 000101010 101010101 010101010 csc
5
Risky if Microsoft gets this wrong, they break a lot of code --- including their own Chicago Coder Conf 2016 5 Project Roslyn
6
So why did Microsoft do this? Open up the compiler Now folks can… Extend C# and VB with new features Target other platforms --- e.g. Raspberry PI? Take advantage of the rich information the compiler has about programs --- e.g. better refactoring, analysis, testing? Chicago Coder Conf 2016 6 Project Roslyn
7
What's the benefit? Faster turnaround on new features inside and outside MSFT Grow the Visual Studio ecosystem MUCH easier to build new tools MUCH easier to extend Visual Studio, C# and VB MUCH easier to try out new ideas Chicago Coder Conf 2016 7 Project Roslyn
8
Status Integrated into Visual Studio 2015 Additional.NET Compiler Platform SDK Shipped as a VS extension https://github.com/dotnet/roslyn https://github.com/dotnet/roslyn Chicago Coder Conf 2016 8 Project Roslyn
9
Open source? Yes, open source! Apache license 2.0 You are free to GIT, fork, modify, rebuild, deploy Anders released on stage @ Build 2014 Chicago Coder Conf 2016 9 Project Roslyn
10
Chicago Coder Conf 2016 10 Project Roslyn
11
C# and VB compilers were black boxes predefined switches only way to interact… Chicago Coder Conf 2016 11 Project Roslyn csc > csc.exe main.cs /o /warn:4
12
Chicago Coder Conf 2016 12 Project Roslyn
13
The compilers are now white boxes You can: obtain information about a program modify a program syntactically / semantically impact the compilation process change the compiler itself! 13 Chicago Coder Conf 2016 Project Roslyn csc Roslyn
14
Roslyn APIs Chicago Coder Conf June 2016 14 Project Roslyn
15
15 Chicago Coder Conf 2016 Project Roslyn csc "Call me every time you see an identifier…" (because I'm renaming all global variables) "Emit this code instead…" (I'm targeting specific HW) // translate project resource strings: foreach(Project p) foreach(Document d) foreach(Resource r) replace (r, r'); // translate project resource strings: foreach(Project p) foreach(Document d) foreach(Resource r) replace (r, r'); Roslyn
16
Many of the features in Visual Studio are driven by Roslyn… Chicago Coder Conf June 2016 16 Project Roslyn
17
What can we do with this capability? Infinite possibilities: better tools — refactoring, analysis, … better enforcement of coding standards integrate C# / VB into your app target new platforms language research — DSLs, … compiler research …… Chicago Coder Conf 2016 17 Project Roslyn ? ?
18
Chicago Coder Conf 2016 18 Project Roslyn
19
Front-end vs. Back-end Front-end deals with syntax ― "grammar" Back-end deals with semantics ― "meaning" Chicago Coder Conf 2016 19 Project Roslyn
20
Chicago Coder Conf 2016 20 Project Roslyn Source language Parsing Assembly language Lexical Analysis Compiler Semantic Analysis High-level Optimizer Code Gen Low-level Optimizer tokens IRIR'IR'' IR''' // comment if (x>100) x = 100; // comment if (x>100) x = 100; if, (, x, >, 100, ), x, =, … syntax errors semantic errors
21
Roslyn Intermediate Representation (IR) Abstract Syntax Tree (AST) Symbol Table Chicago Coder Conf 2016 21 Project Roslyn + GCD program 0"int", type, … 1"void", type, … 2… 3"getint", funct, type: 0, … 4"putint", funct, type: 1, … 5"i", var, type: 0, … 6"j", var, type: 0, … ……
22
How to learn Roslyn AST? Use the Roslyn Syntax Visualizer! Install.NET Compiler Platform SDK Open a project Open a source file View menu… >> Other Windows >> Syntax Visualizer Chicago Coder Conf 2016 22 Project Roslyn
23
Chicago Coder Conf 2016 23 Project Roslyn
24
Roslyn is BIG There are many APIs… There is the source code itself… Chicago Coder Conf 2016 24 Project Roslyn +
25
Start small Let’s create a simple diagnostic that warns about empty catch blocks… Chicago Coder Conf 2016 25 Project Roslyn
26
Step 1: Install.NET Compiler Platform SDK Create new project… >> Extensibility >> Diagnostic with Code Fix Name >> EmptyCatchDiagnostic Chicago Coder Conf 2016 26 Project Roslyn `
27
Step 2: Create Analyzer to detect empty catch blocks Chicago Coder Conf 2016 27 Project Roslyn public class CatchBlockDiagnosticAnalyzer : DiagnosticAnalyzer {. public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction (AnalyzeNode, SyntaxKind.CatchClause); } // only called for things of interest: public void AnalyzeNode(SyntaxNodeAnalysisContext context) { var node = context.Node; var catchBlock = node as CatchClauseSyntax; if (catchBlock.Block.Statements.Count == 0) // empty! { var diagnostic = Diagnostic.Create(...); // create warning: context.ReportDiagnostic(diagnostic); // display: } public class CatchBlockDiagnosticAnalyzer : DiagnosticAnalyzer {... public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction (AnalyzeNode, SyntaxKind.CatchClause); } // only called for things of interest: public void AnalyzeNode(SyntaxNodeAnalysisContext context) { var node = context.Node; var catchBlock = node as CatchClauseSyntax; if (catchBlock.Block.Statements.Count == 0) // empty! { var diagnostic = Diagnostic.Create(...); // create warning: context.ReportDiagnostic(diagnostic); // display: } }
28
Step 3: Create Code Fix Provider to optionally correct problem… Chicago Coder Conf 2016 28 Project Roslyn internal class CatchBlockDiagnosticCodeFixProvider : CodeFixProvider { // only called for things of interest: public … async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(…); var diagnostic = context.Diagnostics.First(); var diagnosticSpan = diagnostic.Location.SourceSpan; var catch = root.FindToken(…).Parent.AncestorsAndSelf(). OfType.First(); context.RegisterCodeFix(CodeAction.Create("throw", c => UpdateCatchBlock(context.Document, catch, c)), diagnostic); } private async Task UpdateCatchBlock(Document doc, …) { var throwStmt = SyntaxFactory.ThrowStatement(); var newStmts = new SyntaxList ().Add(throwStmt); var newBlock = SyntaxFactory.Block().WithStatements(newStmts); var newCatchBlock = SyntaxFactory.CatchClause(). WithBlock(newBlock). WithAdditionalAnnotations(Formatter.Annotation); var root = await document.GetSyntaxRootAsync(); var newRoot = root.ReplaceNode(catchBlock, newCatchBlock); var newDocument = document.WithSyntaxRoot(newRoot); return newDocument; } internal class CatchBlockDiagnosticCodeFixProvider : CodeFixProvider { // only called for things of interest: public … async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(…); var diagnostic = context.Diagnostics.First(); var diagnosticSpan = diagnostic.Location.SourceSpan; var catch = root.FindToken(…).Parent.AncestorsAndSelf(). OfType.First(); context.RegisterCodeFix(CodeAction.Create("throw", c => UpdateCatchBlock(context.Document, catch, c)), diagnostic); } private async Task UpdateCatchBlock(Document doc, …) { var throwStmt = SyntaxFactory.ThrowStatement(); var newStmts = new SyntaxList ().Add(throwStmt); var newBlock = SyntaxFactory.Block().WithStatements(newStmts); var newCatchBlock = SyntaxFactory.CatchClause(). WithBlock(newBlock). WithAdditionalAnnotations(Formatter.Annotation); var root = await document.GetSyntaxRootAsync(); var newRoot = root.ReplaceNode(catchBlock, newCatchBlock); var newDocument = document.WithSyntaxRoot(newRoot); return newDocument; }
29
Step 4: Run! A.vsix installer is built A new instance of VS is started The.vsix is installed Open a project and test… Chicago Coder Conf 2016 29 Project Roslyn
30
Chicago Coder Conf 2016 30 Project Roslyn
31
Roslyn compilers are part of Visual Studio 2015 You’ll also want.NET Compiler Platform SDK Visual Studio extension Tools >> Updates and Extensions Chicago Coder Conf 2016 31 Project Roslyn
32
Chicago Coder Conf 2016 32 Project Roslyn
33
Thank you for attending! Joe Hummel, PhD Email: joe@joehummel.netjoe@joehummel.net Materials: http://www.joehummel.net/downloads.htmlhttp://www.joehummel.net/downloads.html For more information on Roslyn: MSDN: “C# - Adding a Code Fix to Your Roslyn Analyzer”, by Alex Turner, Feb 2015 Docs / FAQ: https://github.com/dotnet/roslyn https://github.com/dotnet/roslyn https://github.com/dotnet/Roslyn/wiki/Roslyn%20Overview https://github.com/dotnet/Roslyn/wiki/Roslyn%20Overview Microsoft’s Channel 9 Chicago Coder Conf 2016 33 Project Roslyn
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.