From: Michal Privoznik <mpriv...@redhat.com> Currently, if we want to mock a function the noinline attribute is appended after the function (via G_NO_INLINE macro). This used to work for non pure functions. But there are some trivial functions (for instance virQEMUCapsProbeHVF()) that are pure, i.e. have no side effect, and while their call from other parts of the code is not optimized out, their call from within the same compilation unit (qemu_capabilities.c) is optimized out.
This is because inlining and semantic interposition are two different things. Even GCC's documentation for noinline attribute [1] states that clearly: This function attribute prevents a function from being considered for inlining. It also disables some other interprocedural optimizations; it’s preferable to use the more comprehensive noipa attribute instead if that is your goal. Even if a function is declared with the noinline attribute, there are optimizations other than inlining that can cause calls to be optimized away if it does not have side effects, although the function call is live. Unfortunately, despite attempts [2] Clang still does not support the attribute and thus we have to rely on noinline + -fsemantic-interposition combo. 1: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute 2: https://reviews.llvm.org/D101011 Signed-off-by: Michal Privoznik <mpriv...@redhat.com> --- docs/coding-style.rst | 2 +- scripts/cocci-macro-file.h | 1 + src/internal.h | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/coding-style.rst b/docs/coding-style.rst index fe5fe9a906..a4934ef333 100644 --- a/docs/coding-style.rst +++ b/docs/coding-style.rst @@ -633,7 +633,7 @@ analysis tools understand the code better: ``G_GNUC_FALLTHROUGH`` allow code reuse by multiple switch cases -``G_NO_INLINE`` +``ATTRIBUTE_MOCKABLE`` the function is mocked in the test suite ``G_GNUC_NORETURN`` diff --git a/scripts/cocci-macro-file.h b/scripts/cocci-macro-file.h index c3112663d1..4f70c6b4cc 100644 --- a/scripts/cocci-macro-file.h +++ b/scripts/cocci-macro-file.h @@ -21,6 +21,7 @@ #pragma once +#define ATTRIBUTE_MOCKABLE #define ATTRIBUTE_NONNULL(x) #define ATTRIBUTE_PACKED diff --git a/src/internal.h b/src/internal.h index 20aa9b1d41..092b02120a 100644 --- a/src/internal.h +++ b/src/internal.h @@ -177,6 +177,28 @@ # endif #endif +/** + * + * ATTRIBUTE_MOCKABLE + * + * Force compiler to disable interprocedural optimizations between the + * function with this attribute and its callers. On compilers that + * support it (gcc), this expands to noipa attribute which implies + * noinline attribute and some others and allows us to mock functions + * even if they are pure. + * + * On compilers which don't support the noipa attribute (clang) this + * expands to noinline attribute which in combination with + * -fsemantic-interposition option does roughly the same. + */ +#ifndef ATTRIBUTE_MOCKABLE +# if defined(__has_attribute) && __has_attribute(noipa) +# define ATTRIBUTE_MOCKABLE __attribute__((noipa)) +# else +# define ATTRIBUTE_MOCKABLE G_NO_INLINE +# endif +#endif + #define VIR_WARNINGS_NO_CAST_ALIGN \ _Pragma ("GCC diagnostic push") \ _Pragma ("GCC diagnostic ignored \"-Wcast-align\"") -- 2.49.0