Continuous Inspection

Custom Coding Rules

The COBOL plugin enables analysis and reporting on COBOL projects.


New custom Cobol coding rules can be added into SonarQube. There are two different ways to achieve it:

  • Writing an XPath rule directly via the web interface
  • Writing a plugin that is a small Java piece of code

The COBOL plugin parses the source code, creates an Abstract Syntax Tree (AST) and then walks through the entire tree. A coding rule can subscribe to be notified every time a type of node is visited. As soon as the coding rule is notified, it can navigate the tree around the node and log issues if necessary.

Writing an XPath Rule

New Cobol coding rules can be defined directly via the SonarQube web interface using XPath. See complete documentation here.

The SSLR Cobol Toolkit can be downloaded here.

Writing a Plugin

Writing new COBOL coding rules is a six-step process:

  • Create a standard SonarQube plugin.
  • Attach this plugin to the SonarQube COBOL plugin (see the pom.xml file of the provided sample plugin project).
  • Create as many custom COBOL coding rules as required by extending com.sonarsource.api.ast.COBOLAstCheck and add them to the previous repository.
  • Generate the SonarQube plugin (jar file).
  • Place this jar file in the SONARQUBE_HOME/extensions/plugins directory.
  • Restart the SonarQube server.

Plugin Project Sample

To get started, download the sample plugin project and follow the steps below:

  • Install Maven
  • Build the plugin by running “mvn install”. This will generate a SonarQube plugin jar file in the target directory.
  • Add your newly created jar into the SONARQUBE_HOME/extensions/plugins directory
  • Restart the SonarQube server

If you now look at the COBOL quality profiles, you will find the new coding rule (“Sample check”). Don’t forget to activate it. Run an analysis of a COBOL project, and you will find that an issue was logged at line 5 on every file.


Subscribing to a NodeType

Very often when writing a coding rule, you will want to subscribe to a NodeType. A NodeType can be either a rule of the grammar or a keyword of the language. As an example, here is the code of the implementation of the “Avoid using Merge statement” coding rule:

public class MergeStatementUsageCheck extends CobolAstCheck {

  public void init() {

  public void visitNode(AstNode node) {
    log("Avoid using MERGE statement.", node);

Note that CICS and SQL grammars can be accessed using getCicsGrammar() and getSqlGrammar().

Coding Rule Lifecycle

A coding rule can optionally override six methods inherited from the COBOLAstCheck abstract class. Those methods are called sequentially in the following order:

  • public void init() {…}: This method is called only once and should be used to subscribe to one or more NodeType(s).
  • public void visitFile(AstNode astNode) {…}: This method is called on each file before starting the parsing.
  • public void visitNode(AstNode astNode) {…}: This method is called when an AstNode matches a subscribed NodeType (see Subscribing to a NodeType) and before analyzing its content.
  • public void leaveNode(AstNode astNode) {…}: This method is called when a nAstNode matches a desired NodeType (see Subscribing to a NodeType) and after analyzing its content.
  • public void leaveFile(AstNode astNode) {…}: This method is called before exiting a file.
  • public void destroy() {…}: This method is called before shutting down the coding rule.

The different kind of log(…) methods, used to log an issue, should be called only inside the visitFile(…), visitNode(…), leaveNode(…) and leaveFile(…) methods. Indeed, the file context isn’t known when the init() and destroy() methods are called, so the issue can’t be associated to a file.

More advanced features are documented in the API Javadoc.

Navigating the AST (Abstract Syntax Tree) with the SSLR COBOL Toolkit

When starting to write a new COBOL coding rule, the main difficulty is to understand the COBOL AST in order to know which NodeType(s) need to be visited. This can be achieved by using the SSLR COBOL Toolkit, a Swing application that enables loading a COBOL file and displaying its representation as an Abstract Syntax Tree.


Each node in the AST is a COBOL grammar rule and each leaf in the AST is a COBOL token. Let’s say you want to visit the node ‘ifStatement’. In this case, the init() method of your COBOL coding rule must contain the following statement: “subscribeTo(getCOBOLGrammar().ifStatement);”.