[clang] [Clang][Sema] Diagnose friend declarations with enum elaborated-type-specifier in all language modes (PR #80171)

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 7 14:07:24 PST 2024


================
@@ -17242,6 +17242,23 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
       return true;
   }
 
+  if (TUK == TUK_Friend && Kind == TagTypeKind::Enum) {
+    // C++23 [dcl.type.elab]p4:
+    //   If an elaborated-type-specifier appears with the friend specifier as
+    //   an entire member-declaration, the member-declaration shall have one
+    //   of the following forms:
+    //     friend class-key nested-name-specifier(opt) identifier ;
+    //     friend class-key simple-template-id ;
+    //     friend class-key nested-name-specifier template(opt)
+    //       simple-template-id ;
+    //
+    // Since enum is not a class-key, so declarations like "friend enum E;"
+    // are ill-formed. Although CWG2363 reaffirms that such declarations are
+    // invalid, most implementations accept so we issue a pedantic warning.
+    Diag(KWLoc, diag::ext_enum_friend) << FixItHint::CreateRemoval(
----------------
Endilll wrote:

Fixit message should definitely be easily actionable. Wording we settled with for diagnostic message is not the one. You can reuse the wording I proposed in that discussion (`Omit 'enum' keyword to befriend an enum`), or come up with your own. This should be much easier to word anyway.

https://github.com/llvm/llvm-project/pull/80171


More information about the cfe-commits mailing list