Lap Around Roslyn CTP: Syntax Analysis and Flow Analysis

Thursday, October 27, 2011

How many times have you seen in code reviews a piece of code that calls a method, say Dictionary<K,V>.TryGetValue, and ignores the return value? We are going on a quest to find all such invocations and produce a warning. We’re going to derive from SyntaxWalker (and not SyntaxRewriter), because we won’t be doing any rewriting—just issue detection*. There are two major cases we need to consider: The method is invoked without storing its result in a local variable or using it as part of an expression. Two examples: ...
no comments

Lap Around Roslyn CTP: Syntax Rewriting with Symbol Information

Tuesday, October 25, 2011

Last time around, we were replacing the 42 numeric literal with 43. This time let’s pretend to do something more useful. Suppose you really don’t like developers calling the Console.Write method and insist on using Console.WriteLine instead. You might be slightly reluctant to use find-and-replace, because—just like last time—you don’t want to modify Console.Write calls within comments, within string literals, or—and this is vicious—calls to the Console.Write method on something that is not the System.Console class from the mscorlib assembly, like maybe a property called Console! The C# parser, which we met in its SyntaxTree incarnation, doesn’t bind...
no comments

Lap Around Roslyn CTP: Syntax Rewriting

Sunday, October 23, 2011

To start doing something useful with Roslyn, we’re going to inspect a syntax tree, locate something interesting—and then modify it! The complex structure of a C# program’s syntax tree (SyntaxTree class) is exposed through a fairly intuitive object model, featuring three types of entities: Nodes are the major elements of the language; for example, an IfStatementSyntax is a node representing an “if” statement and a LiteralExpressionSyntax is a node representing a literal expression. Tokens are secondary elements—which are nonetheless very important—such as identifiers, string literals, and numeric literals. Tokens are always attached to a node. For...
no comments

Lap Around Roslyn CTP: Introduction

Friday, October 21, 2011

The Roslyn project is the Microsoft implementation of C# and VB compilers-as-a-service. Roslyn provides a transparent view into the inner workings of the compiler, including syntax tree inspection and modification. An initial CTP of Roslyn has been released for download a couple of days ago—it requires Visual Studio 2010 SP1 and the VS2010 SP1 SDK. ...
no comments