Add pre-processing and external plugins to a Universal Analyzer language

Run a script that pre-processes source code before the Universal Analyzer analyzes it, or a third-party tool that adds quality rule violations after the analysis
Updated

Overview

A Universal Analyzer language package can run two kinds of script automatically:

  • A pre-processor, in the prepro folder, which transforms the source code before the analysis. Use it when the language needs pre-processing, as Cobol or C++ do, so that the language pattern can recognize its objects.
  • An external plugin, in the plugin folder, which runs a third-party code checker after the analysis and adds the quality rule violations it finds to the results.

Both folders sit in the language package folder, at the same level as the metamodel:

PHP\
    PHPMetaModel.xml
    PHPLanguagePattern.xml
    prepro\
        launch.bat
        launch.sh
        ...
    plugin\
        launch.bat
        launch.sh
        ...

Each folder contains a launch script. The analysis node runs launch.bat on Microsoft Windows and launch.sh on Linux (Docker, Podman or Kubernetes). Provide both scripts, with the same behavior, so that the extension works on any analysis node. Make the scripts call the same tools where possible, for example a .jar file run with Java, so that only the launch commands differ. In the rest of this page, “the launch script” means either file.

Pre-processing

Step 1 - Write the launch scripts

Create a prepro folder in the language package and add launch.bat and launch.sh files that contain the pre-processing instructions. You can add other scripts and tools to the folder: they run only if the launch script calls them.

The analyzer calls the launch script with two parameters:

  1. The input folder, which contains the source code as delivered.
  2. The output folder, a temporary folder in which the launch script must write the pre-processed source code.

The pre-processing can change the content of the files but not the structure of the source code: the output folder must contain the same files and folders, with the same names and hierarchy, as the input folder.

For example, the official PHP extension passes both parameters to a Java pre-processor. Its launch.sh is:

#!/usr/bin/env bash

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
java -Xmx1024m -jar $SCRIPT_DIR/com.castsoftware.php.prepro.jar $1 $2 $3 $4

Its launch.bat runs the same .jar file with %JAVA_HOME%\bin\java.exe, and exits with an error if JAVA_HOME is not set. The third and fourth parameters are specific to the PHP pre-processor.

Step 2 - Analyze the pre-processed code

Before the analysis, the analyzer checks whether the language package contains a prepro launch script. If it does, the analyzer runs it, then analyzes the source code in the output folder instead of the input folder. If it does not, the analysis uses the delivered source code.

External plugin

Step 1 - Write the launch scripts

Create a plugin folder in the language package and add launch.bat and launch.sh files that run the third-party tool. Put the tool and any other scripts it needs, such as .exe or .jar files, in the plugin folder or in subfolders.

After the analysis, the analyzer checks whether the language package contains a plugin launch script. If it does, the analyzer runs it with one parameter: the folder that contains the analyzed source code. If the source code is organized in several root folders that have no common parent, the launch script is called once for each root folder.

Step 2 - Write the results to result.xml

The launch script must write its results to a file named result.xml in the plugin folder, next to the launch script:

  • The root element is plugin.
  • plugin contains one file element per source file, with the full path of the file in its name attribute.
  • Each file element contains one violation element per quality rule violation that the tool found in the file. Only two attributes are required:
    • rule: the name of the quality rule that is violated
    • beginline: the line where the object that violates the rule starts, which the analyzer uses to find the object

Other attributes are allowed and ignored. For example:

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
    <file name="E:\SOURCE\PHP\TestCases\ActiveDynamicPage.php">
        <violation beginline="28" column="5" rule="Squiz.Commenting.ClosingDeclarationComment.Missing" severity="5">Expected //end main()</violation>
        <violation beginline="42" column="5" rule="Squiz.Commenting.ClosingDeclarationComment.Missing" severity="5">Expected //end bar()</violation>
    </file>
    <file name="E:\SOURCE\PHP\TestCases\unusedFun.php">
        <violation beginline="5" endline="5" rule="UnusedPrivateMethod" class="Something" method="foo" priority="3">Avoid unused private methods such as 'foo'.</violation>
    </file>
</plugin>

Rules for plugin results

  • The value of rule must match the format of the properties defined for the objects in the metamodel.
  • If the analyzer cannot find an object that starts at beginline, it assigns the violation to the file. Define the metamodel so that every property the plugin can return can also be assigned to a sourceFile object.
  • Results for a file that the language package does not handle are ignored.