[PATCH] D28596: [compiler-rt] General definition for weak functions.
Marcos Pividori via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 17 16:54:52 PST 2017
mpividori added a comment.
@aizatsky I updated the code 2 minutes before your comment.
I think the two macros that I am defining: `WEAK_DEF()` and `WEAK_INTERFACE_DECL()` are very simple to understand and simplify the code removing redundant code.
The solution that you propose has some disadvantages:
- For Linux, we don't need to define a different function for the default implementation. So, with your proposed solution we have 2 approaches:
+) or we define the default function and we create a weak alias in linux too:
SANITIZER_INTERFACE_ATTRIBUTE const char *__ubsan_options__def() { return ""; }
#ifdef SANITIZER_WINDOWS
SANITIZER_INTERFACE_ATTRIBUTE const char *__ubsan_options();
WIN_WEAK_ALIAS(__ubsan_options, __ubsan_options__def);
#else
SANITIZER_INTERFACE_ATTRIBUTE const char *__ubsan_options() __attribute__ ((weak, alias ("___ubsan_options__def")));;
#endif
(I guess weak aliases also work for APPLE)
We could generalize the weak alias in a macro to avoid the #ifdefs .
+) Or we can use some `#ifdef ` to define the default function only for Windows:
#ifdef SANITIZER_WINDOWS
SANITIZER_INTERFACE_ATTRIBUTE const char *__ubsan_options__def() { return ""; }
SANITIZER_INTERFACE_ATTRIBUTE const char *__ubsan_options();
WIN_WEAK_ALIAS(__ubsan_options, __ubsan_options__def);
#else
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE const char *__ubsan_options(){ return ""; }
#endif
In this case we have to duplicate the code for the default implementation.
With the macro `WEAK_DEF()` that I define, you can see that I simply the code, since we don't need any special code for different platforms:
WEAK_DEF(const char *, __ubsan_options, ()) { return ""; }
This works fine for Windows and other platforms.
- Also, we need to export default functions but only on Windows, with your approach, this means I should write this code in the header file:
#ifdef SANITIZER_WINDOWS
const char *__ubsan_options();
SANITIZER_INTERFACE_ATTRIBUTE const char *__ubsan_options__def();
#else
SANITIZER_INTERFACE_ATTRIBUTE const char *__ubsan_options();
#endif
Which I achieve with the macro `WEAK_INTERFACE_DECL()`, and simplifies that code to:
WEAK_INTERFACE_DECL(const char *, __ubsan_options, ());
https://reviews.llvm.org/D28596
More information about the llvm-commits
mailing list