Selectively built-in functions (for debugging purposes)?

I would like it to be an assembly configuration in which functions are not built-in, except for some selected functions (which may or may not be built-in, which would be before the compiler).

Even better would be some kind of โ€œinlining levelโ€ where I could specify such a level for each function, plus a minimum level when building, and only functions above the minimum level would be allowed. I know that there is no standard solution for this, but hackers specific to the compiler will be just as desirable.

I would like to perform most of my functions not included in the debugger, but some of them should be built in, partly for performance reasons, and partly to avoid crazy call stacks. The code includes some pretty nasty template metaprogramming, but this part is mostly done, so I would like to focus on the rest. Therefore, it would be nice to have functions that belong to template templates, nested but not other built-in functions.

Is there a way to achieve something like this?

+4
source share
2 answers

Depending on your compiler, yes. For g ++, the following will work:

void foo() __attribute__ ((noinline)); void foo() __attribute__ ((always_inline)); 

In MSVC ++:

 __declspec(noinline) void foo(); __forceinline void foo(); 

Note that g ++ requires that attributes apply only to prototypes, not definitions. Therefore, if your function is for definition only (without a separate prototype), you must create a prototype to apply this attribute. MSVC does not have this requirement.

__forceinline has some exceptions . Be sure to read them carefully so that you know if this will have any effect in your specific situation. g ++ does not document any exceptions to the always_inline attribute, but some things are obvious (inserting a call to a virtual method will only work when the method is called statically, for example).

You can generalize this with macros:

 #ifdef _MSC_VER #define NOINLINE(x) __declspec(noinline) x #define INLINE(X) __forceinline x #else #ifdef __GNUC__ #define NOINLINE(x) x __attribute__ ((noinline)) #define INLINE(x) x __attribute__ ((always_inline)) #else #error "I don't know how to force inline/noinline on your compiler." #endif #endif INLINE(void foo()); NOINLINE(void foo()); 
+8
source

If there is a set of functions that you want inline d, you can #define add a macro (suppose GCC or Clang) to __attribute__(__always_inline__) , which will

  • Always embed function
  • give a compilation error if this is not possible for technical reasons.
+1
source

All Articles