中獲取當前編譯單元名稱我的當前目標是創建一個(或儘可能少的)代碼行,以將活動編譯單元的其餘部分切換爲未優化的調試配置。我的第一直覺是兩種:在#include
FORCE_DEBUG;
// code below here will be forced to be unoptimized and in a debug environment
或
#include "ForceDebug.h"
// code below here will be forced to be unoptimized and in a debug environment
將是理想的。在我的工作區中,要轉換爲未優化的調試配置,需要更改pragma優化級別,還需要#undef一些宏和#define其他宏。
FORCE_DEBUG宏不起作用,因爲它需要執行預處理器指令#undef和#define,我瞭解這些指令在宏內不可評估。
相反,我有#include「ForceDebug.h」的工作版本。但是我想告訴開發者他們已經禁用了給定編譯單元的優化(所以他們不檢查它,或者如果他們檢查它可以被捕獲並修復)。理想情況下,此消息包含文件#includes「ForceDebug.h」或當前編譯單元的文件名。
這裏有一個約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
因此調用位置看起來像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
我不能使用__FILE__
因爲有關ForceDebug.h的消息,當我想要它報告有關Foo.cpp。
如果我能評價__FILE__
內Foo.cpp中並通過評估版本到ForceDebug.h這將是可以接受的,但我想遞歸宏調用,它仍然報道ForceDebug.h
有沒有什麼辦法讓它將「Foo.cpp」傳遞給include還是通過用於clang或Visual Studio的其他方式派生該值?
當前文件的名稱是'__FILE__'。 – greyfade
糟糕,我知道'__FILE__',但它不能正確地從Foo.cpp轉移到ForceDebug.h,它報告「ForceDebug.h」而不是「Foo.cpp」 –
我修改了這個問題以反映我瞭解'__FILE__',但是這並沒有讓我報告Foo.cpp –