https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106712

            Bug ID: 106712
           Summary: Incorrect propagation of C++11 attributes to
                    subsequent function declarations
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ed at catmur dot uk
  Target Milestone: ---

In [[first-attribute]] int f [[second-attribute]](), g();

the [[first-attribute]] should apply to both f and g, the [[second-attribute]]
to f only:

> The attribute-specifier-seq appertains to each of the entities declared by 
> the declarators of the init-declarator-list.
http://eel.is/c++draft/dcl.dcl#dcl.pre-3.sentence-5

> The attribute-specifier-seq affects the type only for the declaration it 
> appears in, not other declarations involving the same type.
http://eel.is/c++draft/dcl.dcl#dcl.spec.general-1.sentence-3

However, gcc instead applies [[second-attribute]] to both functions, but only
if [[first-attribute]] is non-empty.

This can lead to rejects-valid:

[[gnu::cold]] int g() { return 1; }
[[gnu::cold]] int f [[gnu::fastcall]](), g(); // error: ambiguating new
declaration of 'int g()'

Or to miscompilation, e.g.:

[[gnu::cold]] int f [[gnu::const]](), g();
int g() {
    static int i;
    return ++i;
}
#include <cassert>
int main() { assert(g() + g() == 3); } // fails

Or erroneous warnings (with only standard attributes):

[[noreturn]] int f [[nodiscard]](), g();
int main() { g(); }

Reply via email to