Skip to content

Use GCC's -Wsuggest-attribute warning#160

Draft
MisterDA wants to merge 7 commits intotrunkfrom
suggest-attribute
Draft

Use GCC's -Wsuggest-attribute warning#160
MisterDA wants to merge 7 commits intotrunkfrom
suggest-attribute

Conversation

@MisterDA
Copy link
Owner

@MisterDA MisterDA commented Jan 9, 2026

The GCC -Wsuggest-attribute=attribute_name warning warns for cases where adding an attribute may be beneficial. I've used it for the format, noreturn, malloc, and returns_nonnull.
I've also tried with pure and const but found out that these mostly apply to primitives, thus adding the attribute brings no benefit.

  • About format:
    The GCC warning -Wsuggest-attribute=attribute_name can be used to suggest to apply attributes to functions. Enable it to find all candidates for the format attribute, and apply it.

    A few notes:

    • The wprintf archetype for the format attribute is not supported;
    • attributes are applied to function names, not types, thus it is not possible to apply the format attribute inside the Logger typedef, thus the macro. As this change is constrained to ocamltest, I believe it is not too invasive.
  • About noreturn:
    Use GCC -Wsuggest-attribute=noreturn warning to find functions possibly missing a noreturn attribute. Applying the noreturn attribute allows removing the placeholder returns.

    The GCC manual has this line about the noreturn attribute:

    It does not make sense for a noreturn function to have a return type other than void.

    Change the return type of some primitives when they always throw an exception.

  • About surround attribute names with __ in headers:
    The GCC docs about attribute syntax suggests this:

    You may optionally specify attribute names with __ preceding and following the name. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use the attribute name __noreturn__ instead of noreturn.

MisterDA and others added 7 commits January 7, 2026 13:37
The GCC warning [`-Wsuggest-attribute=attribute_name`][1] can be used
to suggest to apply attributes to functions. Enable it to find all
candidates for the `format` attribute, and apply it.

A few notes:
- The `wprintf` archetype for the format attribute is not supported;
- attributes are applied to function _names_, not _types_, thus it is
  not possible to apply the format attribute inside the `Logger`
  typedef, thus the macro. As this change is constrained to ocamltest,
  I believe it is not too invasive.

[1]: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wsuggest-attribute_003d
Use GCC `-Wsuggest-attribute=noreturn` warning to find functions
possibly missing a `noreturn` attribute. Applying the `noreturn`
attribute allows removing the placeholder returns.

The GCC manual has this line about the `noreturn` attribute:

> It does not make sense for a `noreturn` function to have a return
> type other than `void`.

Change the return type of some primitives when they always throw an
exception.
> You may optionally specify attribute names with `__` preceding and
> following the name. This allows you to use them in header files
> without being concerned about a possible macro of the same name. For
> example, you may use the attribute name `__noreturn__` instead of
> noreturn.

https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant