-
Notifications
You must be signed in to change notification settings - Fork 363
Detect and fix parsing errors
To get meaningful results there should be no syntax errors in your code. This page contains a step-by-step instruction how to find and fix these kind of problems.
In case the cxx plugin is not able to parse the code this will result in warnings like the following at the end of the scanner .LOG file:
90 INFO: Executing post-job 'Final report'
91 WARN: Preprocessor: 53 include directive error(s). This is only relevant if parser creates syntax errors. The preprocessor searches for include files in the with 'sonar.cxx.includeDirectories' defined directories and order.
92 WARN: Source code parser: 15 syntax error(s) detected. Syntax errors could cause invalid software metric values. Root cause are typically missing includes, missing macros or compiler specific extensions.
93 INFO: Analysis total time: 5.032 s
In this case you should try to remove them with the hints on this page:
Provide syntactically correct code
The cxx plugin expects to be fed with syntactically correct code. This is a conscious design decision: we do not want to re-implement a compiler and try to follow the KISS principle. Therefore the first step should be always to verify your code base on your target system with your build environment for correctness. Your build environment will give you more guidance as the cxx plugin will do. Typical problems during this step are:
- libraries from build environment are missing
- not all sources are available, especially 3rd party libraries are often missing
- source code checked out in different folders: includes can't be resolved
... DEBUG: ... preprocessor cannot find include file ...
... DEBUG: ... syntax error ...
Missing preprocessor definitions
To parse the code in the same way as in your build environment you have to set the same macros as in your build environment. Copy your project specific macros to sonar.cxx.defines. For system specific macros it is in most cases easier to write once a header file for the target build environment and reuse this header file with sonar.cxx.forceIncludes in all projects. For more help see:
In case the preprocessor is not able to evaluate a macro or expression, you get a warning and the result is 0. Each source code file is analyzed independently, so that warnings can appear several times in the log file.
... WARN: ... preprocessor error evaluating expression ... assuming 0
Without the macro WINAPI
the following code is syntactically incorrect. Syntax errors within a declaration are skipped, analysis is continued with next declaration in source code file (see Error Recovery):
int WINAPI func() // syntax error
{
//...
}
Results in:
... DEBUG: ... skip declaration: ...
... DEBUG: ... syntax error: ...
In contrast, data types often do not have to be known to produce syntactically correct code:
int func()
{
TYPE a; // syntactically correct, even without declaration
}
Missing include paths
In most cases you also have to provide include paths. The cxx plugin doesn't know any standard include paths and also no project specific paths. Copy the paths from your build environment and project to sonar.cxx.includeDirectories. In the .LOG file you will find messages like:
... DEBUG: ...: preprocessor cannot find include file ...
- Hint: Since the cxx plugin only reads macros from the include files in order to parse the code syntactically in a correct way, it is often better to provide the missing macros instead of reading in all include files. Reading many include files (e.g. STL, Boost, MFC, ATL, ...) often slows down the analysis considerably. The recommendation is to include only include files for your own code.
Automatically extract include paths and macros from the build system
The cxx plugin can also automatically extract include paths and macros from MSBuild .LOG files or JSON Compilation Databases. More information can be found at the respective parameter sonar.cxx.msbuild.reportPaths or sonar.cxx.jsonCompilationDatabase.
Missing support of compiler specific extensions
cxx plugin implements a C++ standard compatible grammar but most compilers support additional compiler specific extensions. To get no syntax errors and meaningful results you have to find workarounds for 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 by an extension of the grammar. There are only some exceptions to this, where preprocessor directives won't work, see Supported compiler specific extensions. Dealing with compiler specific code pieces gives you an introduction how to do this.
... DEBUG: ... skip declaration: ...
... DEBUG: ... syntax error: ...
Incomplete support of C++ specification
In case there are still problems check if your code fits to the latest C++ standard. In case the cxx plugin does not support the code you can try to replace it with macros, but in most cases you can only raise an issue in this forum to ask for an extension of the grammar.
... DEBUG: ... skip declaration: ...
... DEBUG: ... syntax error: ...
Parse Error Recovery
An alternative way of dealing with parse errors is using the feature Error Recovery, which swallows most of the parse errors at cost of some accuracy loss. Activate it by setting the according configuration property sonar.cxx.errorRecoveryEnabled. Now the cxx plugin will try to recover from parse errors and output messages like the following into the analysis log:
... DEBUG: ... skip declaration ...