JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
Python plug-ins are self-contained scripts that are executed automatically when CodeSonar starts up. This section describes how to write and install Python plug-ins.
CodeSonar SaaS Note: If you want to use your own custom plug-ins with CodeSonar SaaS, contact CodeSecure support for assistance. The installation instructions provided in this page are not sufficient to make plug-ins available to SaaS analyses.
To create and use a CodeSonar Python plug-in, do the following:
Note that the top-level plug-in file must be a Python source (.py) file. It can import compiled Python (.pyc), but cannot itself be compiled.
Suppose you want to define a plug-in called pname.
The following table describes plug-in file content and naming requirements, and summarizes some key implementation guidelines. Full details are provided in the remainder of this page.
| File content requirements |
Import the cs module.
import cs
|
|---|---|
| File name requirements |
Depend on plug-in's installation location.
|
| Add visitors | In top-level scope. |
| Define new warning classes; define new metric classes |
In top-level scope. |
| Key API Elements | Visitor decorators, Metric decorators; classes analysis, warningclass, project_metricclass, compunit_metricclass, sfile_metricclass, procedure_metricclass. |
| Annotated Examples | Plug-in Tutorial: Python, AST Tutorial: Python |
(Further implementation notes are provided below.)
Save the plug-in as $CSONAR/codesonar/plugins/pname_plugin.py or $CSONAR/codesonar/plugins/pname.plugin.py, where $CSONAR is the CodeSonar installation directory. It will be automatically loaded and run when CodeSonar runs.
If you want to save the plug-in in a different location, use the PLUGINS configuration file option to specify its file path so that CodeSonar can load and run it.
| Add visitors | in the top-level scope of the plug-in. |
|---|---|
| Adding: all visitors except step visitors |
|
| Adding: step visitors |
|
The following table lists the available methods and decorators for adding visitors.
| Warning Class type | warningclass |
|---|---|
| Warning Class flag type | warningclass_flags |
| Must be defined | in the top-level scope of the plug-in. |
| Defined with | analysis.create_warningclass() |
Issue warnings with one of the following.
Both methods are overloaded to account for all warning-issuing cases. See the warningclass class description for details.
| Retraction info parameter type (when warning reported) | warning_retraction_info |
|---|---|
| Manually retract with | warning.retract() |
| Warning Class Information | Method |
|---|---|
| ID (a numeric identifier) | warningclass.get_id() |
| Name | warningclass.name() |
| Are WARNING_FILTER settings such that instances of this warning class will always be ignored? | warningclass.always_discarded() |
| Retrieve class by ID or name | analysis.lookup_warningclass() (overloaded) |
| Metric class type | project_metricclass compunit_metricclass sfile_metricclass procedure_metricclass |
|---|---|
| Metric class flag type | metricclass_flags |
| Must be defined | in the top-level scope of the plug-in. |
| Defined with |
Definition mechanism depends on whether the metric is to be
reported automatically or manually.
|
Automatically-reported metrics are computed and reported automatically by the CodeSonar analysis. Manually-reported metrics must be reported explicitly by the plug-in.
Both automatic and manual retraction can take place.
| Manually report with | The metric class report()
method. project_metricclass.report() compunit_metricclass::report() sfile_metricclass::report() procedure_metricclass::report() |
|---|---|
| Manually retract with |
The metric class retract() method. |
General Information:
| Visitors | Plug-ins are based on visitors, which specify actions to be carried out on elements of the CodeSonar internal representation (IR) at various stages of the analysis. |
|---|---|
| Writing Plug-Ins | General information about creating plug-ins to attach custom functionality to the CodeSonar analysis. |
| Custom Checks: Accounting for Incrementality | Ensuring that custom checks implemented in plug-ins generate appropriate results in incremental analyses. |
| Plug-In Tutorial | Two annotated example plug-ins (each provided in all API languages), with building and installation instructions. |
| AST API Tutorial | The AST API tutorial (provided in all API languages) also uses plug-ins. |
Additional sample plugins can be found at $CSONAR/codesonar/plugins/*.py where $CSONAR is your CodeSonar installation directory.
Specific API Language:
| Plug-In Guidelines | Key API References | |
|---|---|---|
| C++ | Writing C++ Plug-Ins | classes analysis, visitor, warningclass, project_metricclass, compunit_metricclass, sfile_metricclass, procedure_metricclass. |
| Python | Writing Python Plug-Ins (this page) |
Visitor decorators, Metric decorators; classes analysis, warningclass, project_metricclass, compunit_metricclass, sfile_metricclass, procedure_metricclass. |
| C | Writing C Plug-Ins | CodeSonar Plug-In API: C Functions and Types for Visitors, Warnings, and Metrics |
To report problems with this documentation, please visit https://support.codesecure.com/.