Skip to content

base: fix ABSL_ATTRIBUTE_WARN_UNUSED with gcc <= 12#2044

Open
jolivain wants to merge 1 commit intoabseil:masterfrom
jolivain:fix_warn_unused_with_gcc12
Open

base: fix ABSL_ATTRIBUTE_WARN_UNUSED with gcc <= 12#2044
jolivain wants to merge 1 commit intoabseil:masterfrom
jolivain:fix_warn_unused_with_gcc12

Conversation

@jolivain
Copy link
Copy Markdown

Gcc <= 12 does not support mixing standard C++ attributes with GNU attributes. See [1].

This can lead to build failures such as [2] [3] and [4]. In those situations, the compilation fails with error such as:

/usr/include/absl/base/attributes.h:1076:36: error: expected identifier before '[' token

Gcc maintainers mentioned in [1] comment 9 that this bugfix will not be backported in Gcc 12. Gcc 12 is still used in LTS distributions. For example, it is included in Debian 12 (Bookworm), which is still supported until 2028. See [5].

This commit adds a workaround for gcc <= 12 which uses attribute in that case, which fixes the compilation failure.

[1] https://gcc.gnu.org/PR69585
[2] protocolbuffers/protobuf#26383
[3] https://autobuild.buildroot.org/results/33f6cfd37cb48c15a53b3e7123d5ce8388a0f2ab/build-end.log
[4] https://gitlab.com/buildroot.org/buildroot/-/jobs/13904066346
[5] https://www.debian.org/releases/bookworm/

Gcc <= 12 does not support mixing standard C++ attributes with
GNU attributes. See [1].

This can lead to build failures such as [2] [3] and [4]. In those
situations, the compilation fails with error such as:

    /usr/include/absl/base/attributes.h:1076:36: error: expected identifier before '[' token

Gcc maintainers mentioned in [1] comment 9 that this bugfix will
not be backported in Gcc 12. Gcc 12 is still used in LTS
distributions. For example, it is included in Debian 12 (Bookworm),
which is still supported until 2028. See [5].

This commit adds a workaround for gcc <= 12 which uses
__attribute__ in that case, which fixes the compilation failure.

[1] https://gcc.gnu.org/PR69585
[2] protocolbuffers/protobuf#26383
[3] https://autobuild.buildroot.org/results/33f6cfd37cb48c15a53b3e7123d5ce8388a0f2ab/build-end.log
[4] https://gitlab.com/buildroot.org/buildroot/-/jobs/13904066346
[5] https://www.debian.org/releases/bookworm/

Signed-off-by: Julien Olivain <ju.o@free.fr>
@derekmauro
Copy link
Copy Markdown
Member

The proposed patch happens to solve this particular problem, but it is not a general solution.

Consider what would happen if we had flipped the definition syntax so that ABSL_ATTRIBUTE_WARN_UNUSED uses GNU syntax and PROTOBUF_EXPORT uses C++ attributes:

#define ABSL_ATTRIBUTE_WARN_UNUSED __attribute__((warn_unused))
#define PROTOBUF_EXPORT [[gnu::visibility("default")]]

We are left with the same problem, and by making this change, we might actually introduce the same problem in a new location.

It is almost as if we need an all-or-none approach to how we declare these attributes.

@jolivain
Copy link
Copy Markdown
Author

jolivain commented May 8, 2026

I see.

Since gcc-12 is phasing out and this issue seems a specific case on class declaration only, we could simply define an empty macro, as if the compile was not supporting it.

#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 13
#define ABSL_ATTRIBUTE_WARN_UNUSED
#else
#define ABSL_ATTRIBUTE_WARN_UNUSED [[gnu::warn_unused]]
#endif
#else
#define ABSL_ATTRIBUTE_WARN_UNUSED
#endif

What do you think?

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.

2 participants