[cfe-dev] Pointers to inline functions

Richard Smith richard at metafoo.co.uk
Wed Oct 16 17:37:04 PDT 2013


On Wed, Oct 16, 2013 at 4:30 PM, Justin Bogner <mail at justinbogner.com>wrote:

> The following program fails to link when built with -O0:
>
>     inline void func(void) { }
>
>     int main(int argc, const char *argv[]) {
>       void (*f)(void) = func;
>       f();
>       return 0;
>     }
>
> This is because `func` has GVA_C99Inline linkage, so we decide that an
> external definition is available and we don't need to emit the inline
> definition. Since we use `func` through a function pointer, we end up
> referencing and undefined symbol.
>
> Is this correct? The C99 and C11 standards aren't very clear on whether
> or not this is allowed. The relevant paragraph is n1570 6.7.4, p7:
>
>     Any function with internal linkage can be an inline function. For a
>     function with external linkage, the following restrictions apply: If
>     a function is declared with an inline function specifier, then it
>     shall also be defined in the same translation unit. If all of the
>     file scope declarations for a function in a translation unit include
>     the inline function specifier without extern, then the definition in
>     that translation unit is an inline definition. An inline definition
>     does not provide an external definition for the function, and does
>     not forbid an external definition in another translation unit. An
>     inline definition provides an alternative to an external definition,
>     which a translator may use to implement any call to the function in
>     the same translation unit. It is unspecified whether a call to the
>     function uses the inline definition or the external definition.
>
> I think this says that a separate extern definition is needed, but also
> that it's acceptable to call the inline function here.


Right. It's unspecified whether we use the inline definition, so we're
allowed (but not required) to reject this code.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131016/11a7809a/attachment.html>


More information about the cfe-dev mailing list