Get current compilation unit name in #include

My current goal is to create one (or as little as possible) line of code that will switch the remainder of the active compilation module to an unoptimized debug configuration. My first instincts were either:

FORCE_DEBUG;
// code below here will be forced to be unoptimized and in a debug environment

or

#include "ForceDebug.h"
// code below here will be forced to be unoptimized and in a debug environment

would be perfect. In my workspace, converting to a non-optimized debug configuration requires changing the pragma optimization level, but also #undef some macros and #define other macros.

The FORCE_DEBUG macro does not work, because it will need to execute the preprocessor directives #undef and #define, which, as I understand it, cannot be evaluated inside the macro.

Instead, I have a working version of #include "ForceDebug.h". But I want to inform the developer that they turned off optimization on this compilation module (so that they do not check it or do not check, so that it can be caught and fixed). Ideally, this message includes the file name of any file # includes "ForceDebug.h" or the current compilation unit.

Here's about ForceDebug.h

#pragma once

#pragma message("DISABLING OPTIMIZATION IN" COMPILATION_UNIT_FILE)

#undef _RELEASE
#define _DEBUG

#ifdef _MSC_VER
# pragma optimize("", off)
#else
# pragma GCC optimize("O0")
#endif

So the call site would look like Foo.cpp:

// this messages "ForceDebug.h", I want to message "Foo.cpp"
//#define COMPILATION_UNIT_FILE __FILE__ 

// double macro also messages "ForceDebug.h"
//#define COMPILATION_UNIT_FILE COMPILATION_UNIT_FILE2(__FILE__)
//#define COMPILATION_UNIT_FILE2(x) x

// this works but requires doing it manually, which I'm trying to avoid
#define COMPILATION_UNIT_FILE "Foo.cpp"
#include "ForceDebug.h"
// code below here will be forced to be unoptimized, debug environment

I can’t use __FILE__it because these are messages about ForceDebug.h when I want to report Foo.cpp.

If I could evaluate __FILE__inside Foo.cpp and pass the evaluated version to ForceDebug.h, that would be acceptable, but I tried recursive macro calls and still reported ForceDebug.h

"Foo.cpp" include clang Visual Studio?

+4
2

.

"ForceDebug.h" , . , , .

, , , . MSVC GCC , .

:

#include "ForceDebug.h"
FORCE_DEBUG;

//... rest of source

"ForceDebug.h":

#pragma once

#ifdef _MSC_VER
#define DO_PRAGMA(X) __pragma(X)
#define NO_OPT optimize("", off)
#else
#define DO_PRAGMA2(X) _Pragma(#X)
#define DO_PRAGMA(X) DO_PRAGMA2(X)
#define NO_OPT GCC optimize("O0")
#endif

#define FORCE_DEBUG \
        DO_PRAGMA(message("DISABLING OPTIMIZATION IN " __FILE__)) \
        DO_PRAGMA(NO_OPT) \
        struct __force_debug

#undef _RELEASE
#define _DEBUG
+1

__FILE__ , :

#pragma message("DISABLING OPTIMIZATION IN " __FILE__)

Live Demo

Edit:

.cpp, . ForceDebug.h :

#pragma once

#undef _RELEASE
#define _DEBUG
#define OPT_OFF
^^^^^^^^^^^^^^^

#ifdef _MSC_VER
# pragma optimize("", off)
#else
# pragma GCC optimize("O0")
#endif

.cpp :

#include "foo.h"

...

#ifdef OPT_OFF
#pragma message("DISABLING OPTIMIZATION IN " __FILE__)
#endif
0

Source: https://habr.com/ru/post/1618510/


All Articles