Skip to content

ShiftLeftSecurity/SharpSyntaxRewriter

Repository files navigation

The (C) Sharp Syntax Rewriter Tool

This is a Roslyn-based tool for C# syntax rewriting with the purpose of source-code desugaring.

Design

There are 2 varieties of rewriter:

  • a purely syntactic one, Rewriter, and
  • a symbolic one, SymbolicRewriter.

For the rewriting, the former relies on syntactic information only; the latter also relies on symbolic information by means of a SemanticModel.

Rewriters

  • BlockifyExpressionBody
  • DeanonymizeType
  • DecomposeNullConditional
  • EmplaceGlobalStatement
  • EnsureVisibleConstructor
  • ExpandForeach
  • ImplementAutoProperty
  • ImposeExplicitReturn
  • ImposeThisPrefix
  • InitializeOutArgument
  • ReplicateLocalInitialization
  • StoreObjectCreation
  • TranslateLinq
  • UncoalesceCoalescedNull
  • UninterpolateStrings
  • UnparameterizeRecordDeclaration

Hint: A "description" of the exact rewrite performed by a given rewriter, you might want to check the tests.

Example

void ApplyRewrite(IRewriter rewriter, SyntaxTree tree)
{
    SyntaxTree tree_P;
    if (rewriter.IsPurelySyntactic())
    {
        tree_P = ((Rewriter)rewriter).Apply(tree);
    }
    else
    {
        Compilation compilation = /* ... */
        tree_P = ((SymbolicRewriter)rewriter).Apply(tree,
                                                    compilation.GetSemanticModel(tree));
    }
}