|
CodeSonar C++ API
|
The C++ API for CodeSonar is provided within the cs namespace.
Visit the cs Namespace Reference page for an index of classes and typedefs.
See Writing C++ Plug-Ins For CodeSonar for information about writing and building a C++ plug-in.
Users who have previously used the C API and are considering switching to the C++ API may also wish to read Comparison: C++ API vs C API.
To use the C++ API, include csonar_api.hpp and use the 'cs' namespace.
The C++ API has the same link-time dependencies as the C API. The entire C++ API is implemented in header files that wrap the C API, in order to avoid any C++ linkage. This ensures compatibility with many compiler toolchains.
However, the C++ API does use some global variables. In order to get definitions for these globals, place this at the beginning of one C++ file in your plug-in:
Having CS_CPP_IMPL defined will cause all global variables in the API to be defined by that compilation unit. This is necessary to avoid undefined symbol errors when linking.
The following simple plug-in reimplements the "Goto Statement" warning class. It flags all goto statements. It calls the warning "Use of Goto".
Most methods in the C++ API potentially throw a cs::result. CodeSonar does not catch uncaught exceptions – the C++ runtime will call std::terminate() and abort if you do not use a try/catch construct to catch the exception.
Uncaught exceptions are one of the most common causes of crashes when using the C++ API.
If you wish to compile your plug-in without support for unwinding exceptions, define the CS_CPP_NO_EXCEPTIONS preprocessor symbol. This will prevent the API from catching any exceptions internally; all exceptions will become fatal errors. This configuration can be very challenging.