Contenuto principale

Fix Polyspace Compilation Errors About Undefined Identifiers

Issue

Polyspace® verification can fail during the compilation phase with an error message about undefined identifiers even if your source code compiles using your regular build commands.

The issue typically indicates a mismatch between Polyspace analysis options and compiler options.

Possible Solutions

Automate Analysis Configuration

The compilation phase of a Bug Finder or Code Prover analysis emulates your actual compilation using the source files and options that you specified during analysis. To make sure that the sources and Polyspace analysis configuration accurately reflect your actual compilation, create the Polyspace project or options file from your compilation or build command. For more information on creating from build command, see Configure Sources and Build Options in Project for Polyspace Analysis and Testing.

If your build command is not supported for automatic project creation, you might be creating a project manually. If you encounter the error during manual build of a project, try to trace the error to one of the following causes and apply appropriate fixes.

Check for Missing Files

Check if the source code you provided contains the variable definition.

For instance, the variable might be defined in an include file that Polyspace cannot find. If you #include-d the include file in your source code but did not add it to your Polyspace project, you see a previous warning:

Warning: could not find include file "my_include.h"

If the variable definition occurs in an include file, add the folder that contains the include file. For more information specifying include folders, see Include paths (-I).

Check for Unrecognized Keyword

Check if the variable is a compiler-specific keyword that is not part of the C or C++ standard. For instance, some compilers interpret __SP as a reference to the stack pointer.

First, make sure that the option Compilation toolchain (Static Analysis) is set correctly. If the compiler-specific keyword is still not recognized and if the keyword is not relevant for static analysis, replace or remove the keyword from your source code or preprocessed code.

If you remove or replace the keyword from the preprocessed code, you can avoid the compilation error while keeping your source code intact. You can do one of the following:

Check for Declarations Embedded in #ifdef Statements

Check if the variable is defined in a branch of an #ifdef macro_name preprocessor directive.

For instance, the declaration of a variable max_power might be inside an #ifdef statement as follows:

#ifdef _WIN32
  #define max_power 31
#endif

Your compilation toolchain might consider the macro _WIN32 as implicitly defined and execute the #ifdef branch. However, the Polyspace compilation might not consider the macro as defined. Therefore, the #ifdef branch is not executed and the variable max_power is not defined.

To work around the compilation error, first, make sure that the option Compilation toolchain (Static Analysis) is set correctly. If the error still occurs, define the macro explicitly using the option Preprocessor definitions (-D).

Check for Undefined Identifiers in assert Statements

This is a special case of the previous issue where an identifier is defined in a preprocessor directive surrounded by the NDEBUG macro like this:

#ifndef NDEBUG
    int my_identifier;
#endif
And the identifier is later used in an assert statement (or an equivalent Visual C++® macro such as ASSERT or VERIFY). Typically, the error can occur if you create a project from a build command within an IDE.

The C standard states that when the NDEBUG macro is defined, all assert statements must be disabled. However, there is a difference between compilation in IDE-s and Polyspace compilation when it comes to assert statements:

  • Most IDEs define the NDEBUG macro in their build systems. When you build your source code in your IDE in non-debug mode, code in a #ifndef NDEBUG statement is removed during preprocessing. For instance, in the preceding example, my_identifier is not defined. If my_identifier occurs only in assert statements, it is not used either, because NDEBUG disables assert statements. You do not have compilation errors from undefined identifiers and your build system executes successfully.

  • Polyspace does not disable assert statements even if NDEBUG macro is defined because the software uses assert statements internally to enhance verification. When you create a Polyspace project from your build system, if your build system defines the NDEBUG macro, it is also defined for your Polyspace project. Polyspace removes code in  a #ifndef NDEBUG statement during preprocessing, but does not disable assert statements. If assert statements in your code rely on the code in a #ifndef NDEBUG statement, compilation errors can occur.

To work around this issue, create a Polyspace project from your build system in debug mode. When you execute your build system in debug mode, NDEBUG is not defined. When you create a Polyspace project from this build, NDEBUG is not defined for your Polyspace project.

See Also

|

Topics