[[deprecated]] attribute ignored after definition (original) (raw)

If a function declaration is adding the [[deprecated]] attribute to an
already defined but not deprecated function, that attribute is ignored. It
generates a -Wignored-attribute warning if that's enabled. The attribute is
successfully added if there are only prior non-definition declarations of the
function. There's not an obvious (to me) reason to ignore the added attribute
after a definition. Redeclarations to add this attribute are permitted by the
standard. And whether there is or is not a prior definition may depend on a
multitude of factors.

Test case:

inline int frob(int x) #ifdef DEFINE_BEFORE_DEPRECATE { return x; } #else ; #endif [[deprecated]] int frob(int);

Compile without defining DEFINE_BEFORE_DEPRECATE => no warnings.

Compile with -DDEFINE_BEFORE_DEPRECATE =>

ignored-deprecation-after-definition.cpp:7:3: warning: attribute declaration must precede definition [-Wignored-attributes]
    7 | [[deprecated]] int frob(int);
      |   ^
ignored-deprecation-after-definition.cpp:1:12: note: previous definition is here
    1 | inline int frob(int x)
      |            ^