0
votes

I'm trying an approach to flag warnings on my code, but I did some mistake and can't understand where. My compiler gives me too much warnings about each use of this macro:

#define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__);

#define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);

Example of use:

MPL_ERROR_MESSAGE("Test %d", 2);
 

    In file included from ../trunk/mpl_position.h:5:0,
                     from ../trunk/mpl_baseenvironment.h:6,
                     from ../trunk/mpl_baseenvironment.cpp:1:
    ../trunk/mpl_baseenvironment.cpp: In member function ‘int MPL_BaseEnvironment::getEnemyRobotAt(MPL_Position&, int)’:
    ../trunk/mpl_utils.h:7:167: warning: too many arguments for format [-Wformat-extra-args]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_baseenvironment.cpp:28:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("(invalid value) not in [0,%d) ", i_ER, qlMPLP_enemyRobot__.size());
             ^
    ../trunk/mpl_baseenvironment.cpp: In member function ‘int MPL_BaseEnvironment::deleteEnemyRobotAt(int)’:
    ../trunk/mpl_utils.h:7:167: warning: too many arguments for format [-Wformat-extra-args]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_baseenvironment.cpp:45:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("(invalid value) not in [0,%d) ", i_ER, qlMPLP_enemyRobot__.size());
             ^
    ../trunk/mpl_baseenvironment.cpp: In member function ‘int MPL_BaseEnvironment::getMovableObstacleAt(MPL_Position&, int)’:
    ../trunk/mpl_utils.h:7:167: warning: too many arguments for format [-Wformat-extra-args]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__

EDIT: UPDATED Warnings list


    ../trunk/mpl_gridenvironment.h: In member function ‘long int MPL_GridEnvironment::arrayIndex(MPL_Position&)’:
    ../trunk/mpl_utils.h:7:167: warning: unknown conversion type character ‘,’ in format [-Wformat=]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_gridenvironment.h:253:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("Invalid Position at (%l, %l, %l)", x, y, z);
             ^
    ../trunk/mpl_utils.h:7:167: warning: unknown conversion type character ‘,’ in format [-Wformat=]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_gridenvironment.h:253:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("Invalid Position at (%l, %l, %l)", x, y, z);
             ^
    ../trunk/mpl_utils.h:7:167: warning: unknown conversion type character ‘)’ in format [-Wformat=]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_gridenvironment.h:253:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("Invalid Position at (%l, %l, %l)", x, y, z);
             ^
    ../trunk/mpl_utils.h:7:167: warning: too many arguments for format [-Wformat-extra-args]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_gridenvironment.h:253:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("Invalid Position at (%l, %l, %l)", x, y, z);

EDIT 2: as was requested, one of the places that triggers warnings:

inline long arrayIndex(MPL_Position& sMPLP_p)
{
    long x, y, z;
    x = MPL_ROUND_INT((d_delta*sMPLP_p.getX())/d_sizeX__);
    y = MPL_ROUND_INT((d_delta*sMPLP_p.getY())/d_sizeY__);
    z = MPL_ROUND_INT((d_delta*sMPLP_p.getZ())/d_sizeZ__);

    if( (x>=0) && (x<n_sizeX__) && (y>=0) && (y<n_sizeY__) && (z>=0) && (z<n_sizeZ__) ){ //isValidPosition(...)
        return( (x*n_sizeY__ + y)*n_sizeZ__ + z );
    }

    MPL_ERROR_MESSAGE("Invalid Position at (%l, %l, %l)", x, y, z);

    MPL_GE_InvalidPosition(sMPLP_p);

    return( -1 );
}

(n_size?__ and d_size?__ are class attributes; dont bother trying to understand that... :P )

2
what are those warnings, please? - Sourav Ghosh
I tried to post them here, but I couldn't format them... I dont know how to paste them in a legible state. Are there some tags that let raw data be posted? - Marcelo Silva
You could use <pre> and </pre> for that kind of text. - Some programmer dude
Now it worked! =] Thanks! - Marcelo Silva
On an unrelated note, you don't need to terminate preprocessor statements like C++ statements, you don't need the trailing semi-colon. - Some programmer dude

2 Answers

0
votes

Regarding the warnings listed in your edit:

unknown conversion type character ‘,’ in format 

This

... "Invalid Position at (%l, %l, %l)", ...

should be

... "Invalid Position at (%ld, %ld, %ld)", ...

You pass long signed int. The conversion specifier for signed int is d. When passing long prefix it with the length modifier l.


And, although unrelated, but please remove the trailing ; from the marcos.

0
votes

I discovered the difference between

#define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__)

and

#define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, __VA_ARGS__)

When using without arguments, like

MPL_ERROR_MESSAGE("Just a message! Hi!");

The first one leads to

MPL_ERROR_MESSAGE2(stderr, "Just a message! Hi!", __VA_ARGS__)

While the second one leads to

MPL_ERROR_MESSAGE2(stderr, "Just a message! Hi!", __VA_ARGS__, )

Which is wrong. Damn wrong. And just dont compile, because MPL_ERROR_MESSAGE2 wasn't expecting that last comma. Do you know why this happens? Should I post a new question?