[clang] [llvm] [clang] Add scoped enum support to `StreamingDiagnostic` (PR #138089)
Vlad Serebrennikov via cfe-commits
cfe-commits at lists.llvm.org
Thu May 1 01:41:34 PDT 2025
================
@@ -1429,6 +1429,22 @@ operator<<(const StreamingDiagnostic &DB, T *DC) {
return DB;
}
+// Convert scope enums to their underlying type, so that we don't have
+// clutter the emitting code with `llvm::to_underlying()`.
+// We also need to disable implicit conversion for the first argument,
+// because classes that derive from StreamingDiagnostic define their own
+// templated operator<< that accept a wide variety of types, leading
+// to ambiguity.
+template <typename T, typename U>
+inline std::enable_if_t<
+ std::is_same_v<std::remove_const_t<T>, StreamingDiagnostic> &&
+ llvm::is_scoped_enum_v<std::remove_reference_t<U>>,
+ const StreamingDiagnostic &>
+operator<<(const T &DB, U &&SE) {
----------------
Endilll wrote:
If I do what you suggest, this pre-existing `operator<<`
https://github.com/llvm/llvm-project/blob/1121a496c0c5ba03d9751162fb851a9036c795ea/clang/include/clang/Basic/Diagnostic.h#L1297-L1302
causes the following ambiguity:
```
/home/user/endill/llvm-project/clang/lib/Parse/Parser.cpp:229:9: error: use of overloaded operator '<<' is ambiguous (with operand types 'DiagnosticBuilder' and 'ExtraSemiKind')
228 | Diag(StartLoc, diag::ext_extra_semi)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
229 | << Kind
| ^ ~~~~
/home/user/endill/llvm-project/clang/include/clang/Basic/Diagnostic.h:1297:50: note: candidate function [with T = clang::ExtraSemiKind]
1297 | template <typename T> const DiagnosticBuilder &operator<<(const T &V) const {
| ^
/home/user/endill/llvm-project/clang/include/clang/Basic/Diagnostic.h:1443:1: note: candidate function [with U = clang::ExtraSemiKind &]
1443 | operator<<(const StreamingDiagnostic &DB, U &&SE) {
| ^
```
This patch is far from my first attempt to implement this, but the first one that didn't run into one of those ambiguity errors. Overload set of `operator<<` for diagnostics is somewhat convoluted.
https://github.com/llvm/llvm-project/pull/138089
More information about the cfe-commits
mailing list