Polyspace Results in Lines Containing Macros
Macros in C/C++ can improve readability and maintainability of code. A macro is a
named fragment of code defined with the #define
directive, for
instance:
#define MAXSIZE 64
MAXSIZE
, it is replaced with 64 during preprocessing.Polyspace® provides several conveniences for reviewing results in lines containing macros.
Macros in Source Lines Can Be Expanded in Place
If a source code line contains a macro, the Source pane displays the line with an icon on the left. You can click the icon to expand the macro, that is, see the macro definition, and click again to collapse the macro. See also:
Bug Finder:
Code Prover:
Source Code in Polyspace Desktop User Interface (Polyspace Code Prover)
Source Code in Polyspace Access Web Interface (Polyspace Access)
If a macro expansion contains multiple Code Prover run-time checks, the line with the macro collapsed has the same color as the worst run-time check. See also Code Prover Result and Source Code Colors (Polyspace Code Prover).
Results in Function-Like Macros Shown Only Once
A function-like macro is a macro that takes parameters, for instance:
#define max(x,y) x>y?x:y
For instance:
In this example, the definition of macro
LEFTOVER()
contains a lowercasel
and violatesMISRA C:2012 Rule 7.3
. This result is shown on the macro definition.The event list below the result message shows the instances where the macro is used. You can click an Expansion of macro event to navigate to the macro usage in the source code.#define LEFTOVER(size) 10000ul - size /* Noncompliant */ #define REMAINDER(size) 10000UL - size /* Compliant */ void func(int arrSize, int arrCopySize) { int n = LEFTOVER(arrSize); int nCopy = LEFTOVER(arrCopySize); int m = REMAINDER(arrSize); }
In this example, the definition of macro
COPY_ELEMENT()
results in an ambiguous evaluation order and violatesMISRA C:2012 Rule 13.2
only when the parameteri++
is passed to it. This result is shown on the macro expansion, specifically on the parameter in the expansion.int a[10], b[10]; #define COPY_ELEMENT(index) (a[(index)]=b[(index)]) void main () { int i=0, k=0; COPY_ELEMENT (k); /* Compliant */ COPY_ELEMENT (i++); /* Noncompliant */ }
This way of showing results in function-like macros enables you to easily fix them:
For issues caused by the macro definition, you can implement the fix once. Tools that report on the macro expansion can show multiple violations for one root cause.
In the preceding example, you can change the lowercase
l
inLEFTOVER()
to fix the issue. TheREMAINDER()
macro shows this fix.For issues caused by the macro parameters, you can also implement the fix once.
In the preceding example, you can compute
i++
in a separate step, and then passi
to theCOPY_ELEMENT()
macro to fix the issue.