Skip to content

[Support] Use [[gnu::warn_unused]] for LLVM_ATTRIBUTE_WARN_UNUSED (NFC) - #239486

Merged
kazutakahirata merged 1 commit into
llvm:mainfrom
kazutakahirata:cleanup_20261105_LLVM_ATTRIBUTE_WARN_UNUSED_gnu
Nov 6, 2026
Merged

kazutakahirata merged 1 commit into
llvm:mainfrom
kazutakahirata:cleanup_20261105_LLVM_ATTRIBUTE_WARN_UNUSED_gnu

Conversation

@kazutakahirata

@kazutakahirata kazutakahirata commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

This patch updates LLVM_ATTRIBUTE_WARN_UNUSED in Compiler.h to use the
C++11 attribute syntax [[gnu::warn_unused]] via LLVM_HAS_CPP_ATTRIBUTE
instead of attribute((warn_unused)).

GCC 12 and earlier reject:

class __attribute__((warn_unused)) [[nodiscard]] Foo { ... };
class [[nodiscard]] __attribute__((warn_unused)) Bar { ... };

This is exactly what happened in #237802. Specifically, it added
LLVM_ATTRIBUTE_WARN_UNUSED to APInt, which already had [[nodiscard]],
and broke GCC 12 buildbots, leading to a revert in #239152:
https://lab.llvm.org/buildbot/#/builders/181/builds/53859

This patch drops the attribute((warn_unused)) fallback because an
older compiler like Clang 5.0 would select it and fail when a class
also has [[nodiscard]]. It's always safe to expand this optional
warning macro to empty.

Assisted-by: Antigravity

This patch updates LLVM_ATTRIBUTE_WARN_UNUSED in Compiler.h to use the
C++11 attribute syntax [[gnu::warn_unused]] via LLVM_HAS_CPP_ATTRIBUTE
instead of __attribute__((warn_unused)).

GCC 12 and earlier reject:

    class __attribute__((warn_unused)) [[nodiscard]] Foo { ... };
    class [[nodiscard]] __attribute__((warn_unused)) Bar { ... };

This patch drops the __attribute__((warn_unused)) fallback because an
older compiler like Clang 5.0 would select it and fail when a class
also has [[nodiscard]].  It's always safe to expand this optional
warning macro to empty.

Assisted-by: Antigravity
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-support

Author: Kazu Hirata (kazutakahirata)

Changes

This patch updates LLVM_ATTRIBUTE_WARN_UNUSED in Compiler.h to use the
C++11 attribute syntax [[gnu::warn_unused]] via LLVM_HAS_CPP_ATTRIBUTE
instead of attribute((warn_unused)).

GCC 12 and earlier reject:

class __attribute__((warn_unused)) [[nodiscard]] Foo { ... };
class [[nodiscard]] __attribute__((warn_unused)) Bar { ... };

This patch drops the attribute((warn_unused)) fallback because an
older compiler like Clang 5.0 would select it and fail when a class
also has [[nodiscard]]. It's always safe to expand this optional
warning macro to empty.

Assisted-by: Antigravity


Full diff: https://github.com/llvm/llvm-project/pull/239486.diff

1 Files Affected:

  • (modified) llvm/include/llvm/Support/Compiler.h (+2-2)
diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
index af06dbb441751..b3afc96842ed0 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -234,8 +234,8 @@
 #define LLVM_ATTRIBUTE_USED
 #endif
 
-#if __has_attribute(warn_unused)
-#define LLVM_ATTRIBUTE_WARN_UNUSED __attribute__((warn_unused))
+#if LLVM_HAS_CPP_ATTRIBUTE(gnu::warn_unused)
+#define LLVM_ATTRIBUTE_WARN_UNUSED [[gnu::warn_unused]]
 #else
 #define LLVM_ATTRIBUTE_WARN_UNUSED
 #endif

@boomanaiden154 boomanaiden154 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable enough to me.

Is there a buildbot failure link or something that this is fixing? If so, please put it in the PR description.

@kazutakahirata

Copy link
Copy Markdown
Contributor Author

Seems reasonable enough to me.

Is there a buildbot failure link or something that this is fixing? If so, please put it in the PR description.

Thanks for the review! I've added the link just below the "GCC 12" story.

@kazutakahirata
kazutakahirata merged commit 6ff7799 into llvm:main Nov 6, 2026
12 of 13 checks passed
@kazutakahirata
kazutakahirata deleted the cleanup_20261105_LLVM_ATTRIBUTE_WARN_UNUSED_gnu branch November 6, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants