I create some useful macros for dealing with pragma directives in Windows.
Also, after some experience with "alternatename" pragma, I decided to add some information for future users.
Thanks.
Details
Diff Detail
- Repository
- rL LLVM
Event Timeline
lib/ubsan/ubsan_flags.cc | ||
---|---|---|
20 ↗ | (On Diff #83841) | I think this needs to be included only on Windows, because sanitizer_win_defs.h errors out if _MCS_VER is not defined. |
Thanks for the refactoring! I'd been thinking of doing it for a while.
lib/asan/asan_globals_win.h | ||
---|---|---|
1 ↗ | (On Diff #83841) | I think with your change, this header is no longer needed. We can replace all instances of ASAN_LINK_GLOBALS_WIN() with WIN_FORCE_LINK(__asan_dso_reg_hook). |
lib/sanitizer_common/sanitizer_win_defs.h | ||
17 ↗ | (On Diff #83841) | The existing pattern in sanitizer_linux.h and sanitizer_mac.h is to allow the header to be included everywhere but ifdef out the contents. I think this reduced ifdefs and is in general consistent with how Kostya likes to do things in the sanitizer libraries. Let's do that. So, please change this to #if SANITIZER_WINDOWS. |
lib/ubsan/ubsan_flags.cc | ||
20 ↗ | (On Diff #83841) | Let's fix the header to make it easy to include instead. |
@kubabrecka , you are right. Sorry, I was going to test on linux before submitting the commit to svn.
Hi, also I think we could create a general macro, like:
#if SANITIZER_WINDOWS #define WEAK_ALIAS(Name, Default) \ __pragma(comment(linker, "/alternatename:" #Name "=" #Default)) #else #define STRINGIFY(A) #A #define WEAK_ALIAS(Name, Default) \ _Pragma( STRINGIFY(weak Name = Default) ) #endif
And always use that macro when need to support Windows. For example:
int some_fun_def() { .. // Default impl. } WEAK_ALIAS(some_fun, some_fun_def)
This definitely simplifies the code, but has the disadvantage that we always need to define a default function, which is not strictly necessary for weak functions in non-windows systems, and as they have the same visibility, we would also be exporting the default functions (unless we find some way to avoid this).
Another option is to define another macro:
#if SANITIZER_WINDOWS #define WEAK(ReturnType, Function) \ WEAK_ALIAS(Function, Function##_def) ReturnType Function##_def #else #define WEAK(function) \ ReturnType __attribute__((weak)) Function #endif
Then we can use:
WEAK(int, some_fun) { .. // Default impl. }
What do you think?
Thanks.
Defining a cross-platform weak mechanism seems like a great idea, even if we can only use it when we have default implementations. It greatly simplify our Windows porting efforts. Adding new weak functions is one of the more common ways to break the WinASan build.
Hi, I updated the diff to use the macro STRINGIFY() instead of directly # , so we ensure we expand the parameters before stringifying.