]> granicus.if.org Git - clang/commitdiff
Implement the GNU semantics for forward declarations of enum types in
authorDouglas Gregor <dgregor@apple.com>
Fri, 6 Mar 2009 18:34:03 +0000 (18:34 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 6 Mar 2009 18:34:03 +0000 (18:34 +0000)
C and C++. Fixes PR3688.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66282 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.def
lib/Sema/SemaDecl.cpp
test/Sema/enum.c
test/SemaCXX/enum.cpp

index ef6f7eeb9c65b7fe3e81007ee9cabb9c5cc12d24..26a6ab7821e28e14da74ec39d335f8431d42d09f 100644 (file)
@@ -676,6 +676,8 @@ DIAG(err_use_with_wrong_tag, ERROR,
      "use of %0 with tag type that does not match previous declaration")
 DIAG(ext_forward_ref_enum, EXTENSION,
      "ISO C forbids forward references to 'enum' types")
+DIAG(err_forward_ref_enum, ERROR,
+     "ISO C++ forbids forward references to 'enum' types")
 DIAG(err_redefinition_of_enumerator, ERROR,
      "redefinition of enumerator %0")
 DIAG(err_duplicate_member, ERROR,
index 953e39f92d62a23173087adc1f357647d28eeadd..f4c2788217d7fd3ac0a50d78e93ebf130064c3ba 100644 (file)
@@ -2976,8 +2976,8 @@ Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagKind TK,
       }
     }
   } else if (TK == TK_Reference && SS.isEmpty() && Name &&
-             (Kind != TagDecl::TK_enum))  {
-    // C++ [basic.scope.pdecl]p5:
+             (Kind != TagDecl::TK_enum || !getLangOptions().CPlusPlus)) {
+    // C.scope.pdecl]p5:
     //   -- for an elaborated-type-specifier of the form 
     //
     //          class-key identifier
@@ -2993,7 +2993,10 @@ Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagKind TK,
     //
     // C99 6.7.2.3p8 has a similar (but not identical!) provision for
     // C structs and unions.
-
+    //
+    // GNU C also supports this behavior as part of its incomplete
+    // enum types extension, while GNU C++ does not.
+    //
     // Find the context where we'll be declaring the tag.
     // FIXME: We would like to maintain the current DeclContext as the
     // lexical context, 
@@ -3027,7 +3030,11 @@ CreateNewDecl:
     New = EnumDecl::Create(Context, SearchDC, Loc, Name, 
                            cast_or_null<EnumDecl>(PrevDecl));
     // If this is an undefined enum, warn.
-    if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum);
+    if (TK != TK_Definition && !Invalid)  {
+      unsigned DK = getLangOptions().CPlusPlus? diag::err_forward_ref_enum
+                                              : diag::ext_forward_ref_enum;
+      Diag(Loc, DK);
+    }
   } else {
     // struct/union/class
 
index 55ddb0ddd93d5622ee03f7c80d5811e1ffa3e0bc..ce49f3064d300a04a42935d53b4322194bfb4f94 100644 (file)
@@ -73,3 +73,14 @@ typedef enum { X = 0 }; // expected-warning{{typedef requires a name}}
 enum NotYetComplete { // expected-note{{definition of 'enum NotYetComplete' is not complete until the closing '}'}}
   NYC1 = sizeof(enum NotYetComplete) // expected-error{{invalid application of 'sizeof' to an incomplete type 'enum NotYetComplete'}}
 };
+
+/// PR3688
+struct s1 {
+  enum e1 (*bar)(void); // expected-warning{{ISO C forbids forward references to 'enum' types}}
+};
+
+enum e1 { YES, NO };
+
+static enum e1 badfunc(struct s1 *q) {
+  return q->bar();
+}
index 7626e4ab6955b25ff059744e4e1d9035ecca5cdd..c7748c31ba0cf92290ebb0789535da870119a7b2 100644 (file)
@@ -23,3 +23,18 @@ void bar() {
        Foo myvar = A;
        myvar = B;
 }
+
+/// PR3688
+struct s1 {
+  enum e1 (*bar)(void); // expected-error{{ISO C++ forbids forward references to 'enum' types}}
+};
+
+enum e1 { YES, NO };
+
+static enum e1 badfunc(struct s1 *q) {
+  // FIXME: the message below should probably give context information
+  // in those types.
+  return q->bar(); // expected-error{{incompatible type returning 'enum e1', expected 'enum e1'}}
+}
+
+enum e2; // expected-error{{ISO C++ forbids forward references to 'enum' types}}