Skip to content

Clang mishandles GNU attributes on function declarations with trailing return type #118697

@zygoloid

Description

@zygoloid

Testcase demonstrating GCC behavior:

auto f() -> bool __attribute__((noreturn));
auto f() -> bool { return true; }

auto g() -> int __attribute__((vector_size(4)));
auto g() -> int { return true; }

GCC treats the noreturn attribute as applying to the function, and warns on the function definition because it does return.

GCC treats the vector_size attribute as applying to the return type, and rejects the definition because it specifies a different return type.

Clang treats the attribute as always applying to the return type. This seems like a missing case of GNU attribute "sliding".

FWIW, I encountered this specifically for the diagnose_if attribute, which can only be written as a GNU attribute, and because its argument references function parameters, can only be written in trailing position in a function declaration. There appears to currently be no way to combine trailing return types with the diagnose_if attribute (and other attributes like it, such as enable_if and the thread-safety annotations), which is a problem for coding styles / standards that mandate the use of trailing return types.

Presumably Clang should slide non-type attributes from a trailing return type onto the function declarator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clangClang issues not falling into any other categoryclang:frontendLanguage frontend issues, e.g. anything involving "Sema"diverges-from:gccDoes the clang frontend diverge from gcc on this issueextension:clangextension:gnu

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions