Reject unprototyped functions with callback_only attribute.
Compiler now rejects functions lacking prototypes when used with the callback_only attribute.
GCC now rejects functions that do not have prototypes when they are used with the callback_only attribute. This change addresses a potential issue where unprototyped functions could lead to undefined behavior or compilation errors in certain contexts.
This update enhances type safety and clarifies attribute usage, preventing misuse of callback_only with incomplete function declarations. Documentation and a new test case have been added to reflect this change.
In Details
The handle_callback_only_attribute function in gcc/c-family/c-attribs.cc processes the callback_only attribute. Previously, it did not explicitly check if the function to which the attribute was applied had a prototype. This could lead to issues because the behavior of calling unprototyped functions is generally undefined or platform-dependent. This commit adds a check to reject such functions, ensuring that only functions with explicit prototypes are used with callback_only. This aligns with better C programming practices and addresses a reported problem (PR c/127464).
- prototype
- In C, a function prototype declares a function's name, return type, and the types of its parameters without providing the function's body. It allows the compiler to check function calls for correctness.
- callback_only attribute
- A GCC attribute that may impose restrictions on how a function can be called or used, often related to its role as a callback in a system.
- unprototyped function
- A function declaration that does not specify the types of its parameters, or a function definition that lacks a parameter list. In modern C, this is generally discouraged.