Skip to content

Dealing with compiler specific code pieces

guwirth edited this page Mar 23, 2021 · 8 revisions

cxx plugin implements a C++ compatible grammar but most compilers support additional compiler specific extensions. To get no syntax errors and meaningful results you have to workaround these extensions.

To keep the compiler grammar as simple as possible it was a conscious design decision to support such extensions mainly with macro definitions and not an extension of the grammar. There are only some exceptions to this, where preprocessor directives won't work, see Supported compiler specific extensions.

In the following description you will find some typical procedures to remove the extensions or replace them with supported alternatives.

Additional data types are typically replaced with known data types:

#define __int32 int
__int32 n=0;

after preprocessing this will result in:

int n=0;

Things like additional function calling conventions should be removed with a #define:

#define __stdcall
void __stdcall example() {
}

after preprocessing this will result in:

void example() {
}

For more complex issues, variadic macros often provide a solution:

#define __declspec(...)
__declspec(align(32)) struct example {
   int a;
};

after preprocessing this will result in:

struct example {
   int a;
};

There are two ways to add preprocessor directives to the cxx plugin. Via the sonar.cxx.defines or the sonar.cxx.forceIncludes configuration property, see Supported configuration properties for details. In most cases it is easier to write once a header file for the target build environment and reuse this header file with force include in all projects.

To get an overview about available compiler specific extensions the links below can help you:

Force Include Examples

For some compilers there is already an example how to deal with the compiler specific extensions. You can use this as an template for your own adaptions.

Clone this wiki locally