C'è un modo per avere macro per forzare avvertimenti ed errori durante la compilazione?#warning e #error come Macro
Al momento ho qualcosa di simile:
#if defined(__clang__)
# define PRAGMA(x) _Pragma(#x)
#elif defined(__GNUC__)
# define PRAGMA(x) _Pragma(#x)
#elif defined(_MSC_VER)
# define PRAGMA(x) __pragma(x)
#endif
#define STRINGISIZE(str) #str
#define STR(str) STRINGISIZE(str)
#define LINE STR(__LINE__)
#define FILE __FILE__
#define FILE_LINE __FILE__ "(" LINE ")"
#define INFO(info , msg) \
PRAGMA(message(FILE_LINE ": " #info ": " msg))
#define MESSAGE(m) INFO(msg , m)
#define WARNING(w) INFO(warning , w)
#define ERROR(e) INFO(error , e)
#define TODO(t) INFO(TODO , t)
int main()
{
MESSAGE("MSG")
TODO("TODO")
WARNING("WARN")
ERROR("ERROR")
}
Visual Studio 2013 sarà il trattamento di queste macro come avvertimenti/errori e questo esempio non verrà compilato. Esiste un equivalente per GCC e Clang?
#if defined(_MSC_VER)
#define INFO(info , msg) \
PRAGMA(message(FILE_LINE ": " #info ": " msg))
#define MESSAGE(m) INFO(info , m)
#define WARNING(w) INFO(warning , w)
#define ERROR(e) INFO(error , e)
#define TODO(t) INFO(todo t)
#elif defined(__GNUC__) || defined(__clang__)
#define INFO(info , msg) \
PRAGMA(#info " : " #msg))
#define MESSAGE(m) INFO(info , m)
#define WARNING(w) INFO(GCC warning , w)
#define ERROR(e) INFO(GCC error , e)
#define TODO(t) INFO(, "todo" t)
#endif
Leggi la documentazione, amico. –
Vorrei davvero che le persone non usassero #pragma todo's ecc. Ingombrano l'output della build e non vengono mai corretti – paulm