A compiler is a program which translates the source code into the executable code.
It also detects syntax and semantic error and it used to be a black-box operation.
However, the Microsoft .NET compiler platform ("Roslyn") has variety of code analysis API which could be extended into the existing compiler to make own custom code analysis program.
https://Capgemini.github.io/dotnet-code-analysis/
Capgemini IP code analyzer enforces the following rules
- CommentsAnalyzer - Enforces that excessive amounts of comments are added to variables
- ExplicitAccessModifiersAnalyzer - Enforces that methods, types and variables have explicit modifiers
- FileHierarchyAnalyzer - Enforces that namespaces align with file hierarchy structure
- GodClassAnalyzer - Enforces that no class contains more than 20 methods
- HardCodeAnalyzer - Enforces that literal strings are not part of the general code but moved into constants.
- IfStatementAnalyzer - Enforces that all If statements are guarded by curly braces
- LargeCommentedCodeAnalyzer - Enforces that excessive amounts of comments are added to any part of the code base
- LoopStatementAnalyzer - Enforces that all loop statements are guarded by curly braces
- MethodComplexityAnalyzer - Enforces that no method's cyclomatic complexity is greater than 15
- MonsterMethodAnalyzer - Enforces that no method has more than 80 executable lines of code
- NamingConventionAnalyzer - Enforces that all artifacts within the code base are named according to predefined patterns
- OneTypePerFileAnalyzer - Enforces that only one type is defined within a C# file
- StaticClassAnalyzer - Enforces that static classes are not used unless absolutely required
- XmlCommentsAnalyzer - Identifies public methods which do not have valid comments
- ConstructorParametersAnalyzer - Identifies constructors with > 5 parameters (Warning) and >= 10 (Error)
- MethodParametersAnalyzer - Identifies methods with > 5 parameters (Warning) and >= 10 (Error),
- PrivateFieldNamingUnderscoreAnalyzerId - Separate prefix for underscores (previously in NamingConventionAnalyzer). Includes code-fix.
- PrivateFieldNameCasingAnalyzerId - Separate private field name casing analyzer (previously in NamingConventionAnalyzer). Includes code-fix.
In order to write custom code analyzers, refactoring, and stand-alone .NET applications that utilizes the Roslyn APIs, you need to install the .NET Compiler Platform SDK.
Open Visual Studio 2015 -> Select Tools > Extensions and Updates. In the Extensions and Updates dialog box,select On-line on the left, and then in the search box, type .NET Compiler Platform SDK.
For more information about Roslyn click this link
It is critical for the DevOps team to communicate well with other scrum teams to gather the right code analysis logics and implement correctly to maintain the good coding standard.
In order to create custom code analysis program, you have to investigate the syntax of the code which will be analyzed. This can be done by using Syntax visualizer.
The Syntax Visualizer can be enabled via View > Other Windows > Syntax visualizer
Syntax visualizer allows you to inspect the syntax tree by the .NET platform "Roslyn"
The analysis logic requires two fundamental steps:
- Write a method that will perform the code analysis over a given syntax node
- Register the action at the analyzer's startup so that the analyzer can respond to compiler events
RegisterSyntaxNodeAction - Register an action that is executed at the completion of the semantic analysis of a syntax node.
Example
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(MethodName, SyntaxKind.IdentifierName);
}
private void MethodName(SyntaxNodeAnalysisContext context)
{
var root = context.Node;
//Code analysis logic implementation
//Create a diagnostic at the node location
var diagnostics = Diagnostics.Create(Rule, root.GetLocation(), message);
context.ReportDiagnostic(diagnostics);
}
Once the code analysis logics are implemented and tested, the project will produce an analyzer .dll, as well as the
- A NuGet Package(.nupkg file) which will add your assembly as a project-local analyzer that participates in builds
- A VSIX extension(.vsix file) that will apply your analyzer to all projects and works just in the IDE
- Create a local NuGet feed
- Copy the .nukpg file into that folder
- Right-click on the project node in Solution Explorer and choose Mange NuGet Packages
- Select the NuGet feed you created on the left
- Choose your analyzer from the list and click install
Note: Capgemini.CodeAnalysis is currently integrated into a continuous integration pipeline in VSTS which generates associated nugget packages.
Currently, we have the following code analyzers:
- Capgemini.CodeAnalysis.CoreAnalysers - Contains analyzers that are applicable to any C# code bases
- Capgemini.CodeAnalysis.XrmAnalysers - Contains analyzers that are designed to enforce rules that are applicable to CRM code bases
These packages can be consumed by adding the Capgemini NuGet Feed to the NuGet config of the target C# solution