Can I get inlining to apply to source location information?

Viewed 80

I'm writing code in a C-like language, and am using an inline function which does actually tend to get inlined. However, the inlining maintains the location in that function's source, in terms of the debugging info added by the compiler (e.g. DWARF added by GCC).

Is it possible to get the compiler to inline the function's code while attributing the instructions to the line at which the function was inlined?

For the sake of discussion, let's assume I'm writing C++ and compiling with GCC on Linux (although a more general answer is appreciated.)

(The motivation has to do with profiling where you only sample the function you're in but don't sample the entire stack.)

1 Answers

The artificial function attribute does this:

artificial

This attribute is useful for small inline wrappers that if possible should appear during debugging as a unit. Depending on the debug info format it either means marking the function as artificial or using the caller location for all instructions within the inlined body.

Placement of the __attribute__ directive is a bit tricky. This is the recommended place:

inline void __attribute__ ((artificial))
increment (int &i)
{
  ++i;
}

This is different from the place in the C++ syntax:

[[gnu::artificial]]
inline void
increment (int &i)
{
  ++i;
}

The artificial attribute is recognized by Clang and compilers derived from it.

Related