Polyspace (Mathworks) for static code analysis

  • Has anyone successfully used Polyspace (for static code analysis) with their -QT- Qt project? Ive encountered a few difficulties with the configuration. Im currently getting a compilation error related with -QT- Qt's foreach statement which stops the analysis. Any hints would be really appreciated!

    Here's the error Im getting:
    Limitation: A compound statement cannot be used where an expression is expected
    | (e.g.: #define maxint(a,b) ({int _a = (a), _b = (b); _a > _b ? _a : _b; }))
    | (use : #define max(a,b) ((a) > (b) ? (a) : (b))).
    |This construction is not supported by PolySpace Verifier.
    |The computation has been halted.
    for the code:
    Q_FOREACH(infoVariant, response.value<QVariantList>()){

  • You could probably trick it into reading something different, with the goal to make the result digestible for the tool (even if it's not functional code). Something like #undef Q_FOREACH and
    #define Q_FOREACH(variable, container) for (variable;;) might already be sufficient.

  • Polyspace for C++ does not accept actually GNU extension compound statement expressions.
    Replace your macro @#define maxint(a,b) ({int _a = (a), _b = (b); _a > _b ? _a : _b; })@ by an inline function:
    @inline int maxint(int a, int b) {int _a = (a), _b = (b); return (_a > _b ? _a : _b); }@

  • Facing the limitation when using Q_FOREACH MACRO and a Qt gnu compiler (Q_CC_GNU), it could be possible to replace the MACRO by another:

    • Copy from QtCore/qglobal.h in the configuration file folder
      and rename it mygglobal.h

    • Search for the MACRO Q_FOREACH around line 2244 and replace it with MACRO (use flag of compilation to encapsulate new MACRO definition):
      @#define Q_FOREACH(variable, container)
      QForeachContainer<typeof(container)> container(container);
      for (variable = *container.i;
      !container.brk && container.i != container.e;

    • Relaunch using -include myqglobal.h. Note that it does not work if Q_FOREACH MACRO is used more than one time in same statement.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.