Repository navigation
[Clang][Sema] Disable checking invalid template id in initializer of primary variable template std::format_kind with libstdc++ - #139560
Conversation
…le template `std::format_kind` with libstdc++
|
@llvm/pr-subscribers-clang Author: Yanzuo Liu (zwuis) ChangesFixes #139067 Full diff: https://github.com/llvm/llvm-project/pull/139560.diff 1 Files Affected:
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 7940340064eda..19142d7c16abb 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4383,6 +4383,11 @@ Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc,
};
if (VarDecl *Var = Template->getTemplatedDecl();
+ // Skipping std::format_kind in libstdc++ is a hack for
+ // GH139067 / https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120190
+ !(Var->getName() == "format_kind" &&
+ Var->getDeclContext()->isStdNamespace() &&
+ PP.isMacroDefined("__GLIBCXX__")) &&
ParsingInitForAutoVars.count(Var) &&
llvm::equal(
CTAI.CanonicalConverted,
|
erichkeane
left a comment
There was a problem hiding this comment.
Can you add a test for this too?
| // GH139067 / https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120190 | ||
| !(Var->getName() == "format_kind" && | ||
| Var->getDeclContext()->isStdNamespace() && | ||
| PP.isMacroDefined("__GLIBCXX__")) && |
There was a problem hiding this comment.
It would be great if we could find some sort of way to check the actual VERSION of the hack once they fix this, but it doesn't look like they have a fix in flight.
There was a problem hiding this comment.
Clang you add something like // HACK workaround libstdc++ 14.x (2025-02) so it's easier to find?
shafik
left a comment
There was a problem hiding this comment.
Can you please describe the issue in the summary instead of just saying it fixes .... Also add details such as this is fixing an issue introduced by ...
Also please as Erich asked, add a test too
|
@cor3ntin @erichkeane I question if a hack is really necessary here. We had run into a similar situation when we broke libstdc++ 14.1 (#92439), and the consensus then was to wait for the next dot release. I'd expect they'll fix this issue in the next dot release too? @jwakely Will the next GCC release come before Clang 21, i.e. before September? |
|
@zyn0217 At the very minimum, it breaks compiler explorer and anyone who uses trunk; it is disruptive |
|
I think we should
I don't have time at the moment but it would be a useful improvement :) |
*4/ Do an audit of all our libcxx hackery in place right now, and apply the new function to it :D And perhaps instead of |
|
@zwuis Can you land that? Thanks! |
|
@cor3ntin I don't have commit access. Please help me land this. By the way, should I request commit access? |
It might be a good idea ! https://llvm.org/docs/DeveloperPolicy.html#obtaining-commit-access |
…d` (#140831) We can use 20250520 as the final date, see the following commits. - GCC releases/gcc-15 branch: - https://gcc.gnu.org/g:fedf81ef7b98e5c9ac899b8641bb670746c51205 - https://gcc.gnu.org/g:53680c1aa92d9f78e8255fbf696c0ed36f160650 - GCC master branch: - https://gcc.gnu.org/g:9361966d80f625c5accc25cbb439f0278dd8b278 - https://gcc.gnu.org/g:c65725eccbabf3b9b5965f27fff2d3b9f6c75930 Follows-up #139560.
We have a libstdc++ workaround in place to address a hack in libstdc++15 that is used in the definition of `std::format_kind`. GCC accepts the hack, while Clang does not. libstdc++ was eventually updated to remove the hack, but we still need the workaround for some versions of libstdc++15 (see #139560 for more information). Whether this workaround (and others that address libstdc++ hacks) is active depends on the value of `__GLIBCXX__`. This stops working if someone first preprocesses the input (via `-E`) and then attempts to compile the preprocessed code with Clang (see #160314): since preprocessing has already happened, `__GLIBCXX__` will be undefined and the workaround will not be applied. In case of this particular workaround, this results in `#include <format>` failing to compile on some versions of libstdc++ if you’re using separate preprocessing. To resolve this problem, this patch introduces a new pragma (`#pragma clang __set_pp_state MACRO_NAME INTEGER`). This is intended as a general solution to preserving preprocessor state across runs of the preprocessor. Currently, the only supported value for `MACRO_NAME`is `__GLIBCXX__`. When we encounter this form of the pragma, we update `Preprocessor::CXXStandardLibraryVersion` (we don’t actually define `__GLIBCXX__` or do anything w/ that macro here). In `-E` mode, the pragma is retained in the output, and a pragma is emitted for `#define __GLIBCXX__` if printing macro definitions is disabled. Things like `#undef __GLIBCXX__` and redefinitions of `__GLIBCXX__` are deliberately left unhandled (i.e. you get what you get), since users shouldn’t ever be undefining or redefining this macro anyway (and since it starts w/ `__`, it would also be UB to do so). Fixes #160314.
We have a libstdc++ workaround in place to address a hack in libstdc++15 that is used in the definition of `std::format_kind`. GCC accepts the hack, while Clang does not. libstdc++ was eventually updated to remove the hack, but we still need the workaround for some versions of libstdc++15 (see llvm#139560 for more information). Whether this workaround (and others that address libstdc++ hacks) is active depends on the value of `__GLIBCXX__`. This stops working if someone first preprocesses the input (via `-E`) and then attempts to compile the preprocessed code with Clang (see llvm#160314): since preprocessing has already happened, `__GLIBCXX__` will be undefined and the workaround will not be applied. In case of this particular workaround, this results in `#include <format>` failing to compile on some versions of libstdc++ if you’re using separate preprocessing. To resolve this problem, this patch introduces a new pragma (`#pragma clang __set_pp_state MACRO_NAME INTEGER`). This is intended as a general solution to preserving preprocessor state across runs of the preprocessor. Currently, the only supported value for `MACRO_NAME`is `__GLIBCXX__`. When we encounter this form of the pragma, we update `Preprocessor::CXXStandardLibraryVersion` (we don’t actually define `__GLIBCXX__` or do anything w/ that macro here). In `-E` mode, the pragma is retained in the output, and a pragma is emitted for `#define __GLIBCXX__` if printing macro definitions is disabled. Things like `#undef __GLIBCXX__` and redefinitions of `__GLIBCXX__` are deliberately left unhandled (i.e. you get what you get), since users shouldn’t ever be undefining or redefining this macro anyway (and since it starts w/ `__`, it would also be UB to do so). Fixes llvm#160314.
#134522 triggers compilation error with libstdc++, in which primary variable template
std::format_kindis defined likeSee #139067 or https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120190.
This PR disables checking template id in initializer of primary variable template
std::format_kindin libstdc++ (by checking__GLIBCXX__).Fixes #139067