[clang] 967137c - No longer accept scoped enumerations in C
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Thu May 5 11:00:14 PDT 2022
Author: Aaron Ballman
Date: 2022-05-05T14:00:01-04:00
New Revision: 967137ca3cb7cf38b2fedf0415946ff3ffd0ef50
URL: https://github.com/llvm/llvm-project/commit/967137ca3cb7cf38b2fedf0415946ff3ffd0ef50
DIFF: https://github.com/llvm/llvm-project/commit/967137ca3cb7cf38b2fedf0415946ff3ffd0ef50.diff
LOG: No longer accept scoped enumerations in C
We had a think-o that would allow a user to declare a scoped
enumeration in C language modes "as a C++11 extension". This is a
think-o because there's no way for the user to spell the name of the
enumerators; C does not have '::' for a fully-qualified name. See
commit d0d87b597259a2b74ae5c2825a081c7e336cb1d0 for details on why this
is unintentional for C.
Fixes #42372
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Parse/ParseDecl.cpp
clang/test/Sema/enum.c
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5e9cd941e091..87e4df50ae0f2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -142,6 +142,11 @@ Bug Fixes
- Fixed a false positive diagnostic about an unevaluated expression having no
side effects when the expression is of VLA type and is an operand of the
``sizeof`` operator. Fixes `Issue 48010 <https://github.com/llvm/llvm-project/issues/48010>`_.
+- Fixed a false positive diagnostic about scoped enumerations being a C++11
+ extension in C mode. A scoped enumeration's enumerators cannot be named in C
+ because there is no way to fully qualify the enumerator name, so this
+ "extension" was unintentional and useless. This fixes
+ `Issue 42372 <https://github.com/llvm/llvm-project/issues/42372>`_.
Improvements to Clang's diagnostics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 3d18ffe07ee6b..8d6e84b924c32 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4531,7 +4531,7 @@ void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
bool IsScopedUsingClassTag = false;
// In C++11, recognize 'enum class' and 'enum struct'.
- if (Tok.isOneOf(tok::kw_class, tok::kw_struct)) {
+ if (Tok.isOneOf(tok::kw_class, tok::kw_struct) && getLangOpts().CPlusPlus) {
Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
: diag::ext_scoped_enum);
IsScopedUsingClassTag = Tok.is(tok::kw_class);
diff --git a/clang/test/Sema/enum.c b/clang/test/Sema/enum.c
index ae4a8a357e179..0fe54d791aff5 100644
--- a/clang/test/Sema/enum.c
+++ b/clang/test/Sema/enum.c
@@ -159,3 +159,16 @@ struct EnumRedeclStruct {
PR15071_One // expected-error {{redefinition of enumerator 'PR15071_One'}}
} e;
};
+
+enum struct GH42372_1 { // expected-error {{expected identifier or '{'}} expected-warning {{declaration does not declare anything}}
+ One
+};
+
+// Because class is not a keyword in C, this looks like a forward declaration.
+// expected-error at +4 {{expected ';' after top level declarator}}
+// expected-error at +3 {{tentative definition has type 'enum class' that is never completed}}
+// expected-warning at +2 {{ISO C forbids forward references to 'enum' types}}
+// expected-note at +1 {{forward declaration of 'enum class'}}
+enum class GH42372_2 {
+ One
+};
More information about the cfe-commits
mailing list