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.0p0 Hot Tips | CONFIDENTIAL | CodeSecure Inc |
This section describes how to write and install C++ plug-ins for CodeSonar.
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 C++ plug-in, do the following:
Suppose you want to define a plug-in called pname_plugin.
| File content requirements |
The C++ source file(s) for the plug-in must include the
following elements.
#define CS_CPP_IMPL // must precede #include "csonar_api.hpp" #include "csonar_api.hpp" // uses the 'cs' namespace // ... static void cs_plug_main(void){}// must be present but can be empty CS_DEFINE_PLUGIN(pname_plugin) // must be final line; built plug-in library must have corresponding name |
||||
|---|---|---|---|---|---|
| File name requirements |
Source file: none Built library: must correspond to the name registered with CS_DEFINE_PLUGIN(pname_plugin) in the source file:
|
||||
| Add visitors | In top-level scope, or in cs_plug_main(). | ||||
| Define new warning classes; define new metric classes |
In top-level scope, or in cs_plug_main(). | ||||
| Key API Classes | analysis, visitor, warningclass, project_metricclass, compunit_metricclass, sfile_metricclass, procedure_metricclass. | ||||
| Annotated Examples | Classes Plug-in Tutorial: C++, AST Tutorial: C++ |
(Further implementation notes are provided below.)
Once the plug-in source files are created, compile the plug-in into a library. The library name must meet the file name requirements listed above.
There are two important requirements for building a C++ plug-in.
Compilation instructions vary slightly depending on your operating system.
| Compiler | Command Line | Notes | |
|---|---|---|---|
| Windows | |||
| cl |
cl /LD /MD /EHsc
"/I%CSONAR%\codesonar\include"
"/I%CSONAR%\csurf\include" pname_plugin.cpp "%CSONAR%\codesonar\lib\codesonar.lib" |
|
|
| Cygwin g++ |
Note: Cygwin g++ is only suitable for building 32-bit CodeSonar plugins. Cygwin
g++ only builds Cygwin
binaries, so is not suitable for building CodeSonar plug-ins.
If you do not have an alternative g++, you will need to download and install
one. One option is mingw-w64.
|
|
|
| MinGW g++ |
g++ -shared
"-I$CSONAR/codesonar/include"
"-I$CSONAR/csurf/include" pname_plugin.cpp -o pname_plugin.dll "-L$CSONAR/codesonar/lib" -lcodesonar |
|
|
| otherwise |
|
||
| All
Other Systems We provide a full example for g++. For other compilers, build pname_plugin.so from pname_plugin.cpp, including $CSONAR/codesonar/include and $CSONAR/csurf/include. |
|||
| g++ |
g++ -fPIC -shared -o pname_plugin.so
pname_plugin.cpp
-I$CSONAR/codesonar/include -I$CSONAR/csurf/include |
|
|
If the build fails and reports errors in cs_basic_types.h, this generally indicates that your build has a different pointer width than that of the CodeSonar installation. If this happens:
Save the plug-in in directory $CSONAR/codesonar/plugins, where $CSONAR is the CodeSonar installation directory. The plug-in 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.
Note: you will not be able to modify plug-in .dll files in $CSONAR/codesonar/plugins, or .dll files specified by the PLUGINS option, while a CodeSonar process is running. If you encounter this problem while trying to update an existing plug-in, use the Windows Task Manager (or a similar tool) to end all codesonar.exe processes.
| Add visitors | in the top-level scope of the plug-in, or in cs_plug_main(). |
|---|---|
| Adding: all visitors except step visitors |
|
| Adding: step visitors |
|
The following table lists the available methods 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, or in cs_plug_main(). |
| 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::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, or in cs_plug_main(). |
| Defined with | The create() method of the
metric class manager with the corresponding granularity: project_metricclass_manager::create() compunit_metricclass_manager::create() sfile_metricclass_manager::create() procedure_metricclass_manager::create() |
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. compunit_metricclass::retract() sfile_metricclass::retract() procedure_metricclass::retract() (All analysis-granularity metrics are automatically retracted.) |
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. |
Specific API Language:
| Plug-In Guidelines | Key API References | |
|---|---|---|
| C++ | Writing C++ Plug-Ins (this page) |
classes analysis, visitor, warningclass, project_metricclass, compunit_metricclass, sfile_metricclass, procedure_metricclass. |
| Python | Writing Python Plug-Ins | 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 |