r333574 - [AST] Fix loss of enum forward decl from decl context

Joel E. Denny via cfe-commits cfe-commits at lists.llvm.org
Wed May 30 11:33:53 PDT 2018


Author: jdenny
Date: Wed May 30 11:33:53 2018
New Revision: 333574

URL: http://llvm.org/viewvc/llvm-project?rev=333574&view=rev
Log:
[AST] Fix loss of enum forward decl from decl context

For example, given:

  enum __attribute__((deprecated)) T *p;

-ast-print produced:

  enum T *p;

The attribute was lost because the enum forward decl was lost.

Another example is the loss of enum forward decls from C++ namespaces
(in MS compatibility mode).

The trouble was that the EnumDecl node was suppressed, as revealed by
-ast-dump.  The suppression of the EnumDecl was intentional in
r116122, but I don't understand why.  The suppression isn't needed for
the test suite to behave.

Reviewed by: rsmith

Differential Revision: https://reviews.llvm.org/D46846

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/ast-print.c
    cfe/trunk/test/SemaCXX/MicrosoftCompatibility.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=333574&r1=333573&r2=333574&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed May 30 11:33:53 2018
@@ -14287,7 +14287,6 @@ CreateNewDecl:
   // PrevDecl.
   TagDecl *New;
 
-  bool IsForwardReference = false;
   if (Kind == TTK_Enum) {
     // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.:
     // enum X { A, B, C } D;    D should chain to X.
@@ -14317,12 +14316,6 @@ CreateNewDecl:
         else if (getLangOpts().CPlusPlus)
           DiagID = diag::err_forward_ref_enum;
         Diag(Loc, DiagID);
-
-        // If this is a forward-declared reference to an enumeration, make a
-        // note of it; we won't actually be introducing the declaration into
-        // the declaration context.
-        if (TUK == TUK_Reference)
-          IsForwardReference = true;
       }
     }
 
@@ -14480,9 +14473,7 @@ CreateNewDecl:
         PushOnScopeChains(New, EnclosingScope, /* AddToContext = */ false);
   } else if (Name) {
     S = getNonFieldDeclScope(S);
-    PushOnScopeChains(New, S, !IsForwardReference);
-    if (IsForwardReference)
-      SearchDC->makeDeclVisibleInContext(New);
+    PushOnScopeChains(New, S, true);
   } else {
     CurContext->addDecl(New);
   }

Modified: cfe/trunk/test/Sema/ast-print.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/ast-print.c?rev=333574&r1=333573&r2=333574&view=diff
==============================================================================
--- cfe/trunk/test/Sema/ast-print.c (original)
+++ cfe/trunk/test/Sema/ast-print.c Wed May 30 11:33:53 2018
@@ -4,6 +4,8 @@
 // RUN: echo >> %t.c "// expected""-warning@* {{use of GNU old-style field designator extension}}"
 // RUN: echo >> %t.c "// expected""-warning@* {{'EnumWithAttributes' is deprecated}}"
 // RUN: echo >> %t.c "// expected""-note@* {{'EnumWithAttributes' has been explicitly marked deprecated here}}"
+// RUN: echo >> %t.c "// expected""-warning@* {{'EnumWithAttributes2' is deprecated}}"
+// RUN: echo >> %t.c "// expected""-note@* {{'EnumWithAttributes2' has been explicitly marked deprecated here}}"
 // RUN: echo >> %t.c "// expected""-warning@* {{'EnumWithAttributes3' is deprecated}}"
 // RUN: echo >> %t.c "// expected""-note@* {{'EnumWithAttributes3' has been explicitly marked deprecated here}}"
 // RUN: %clang_cc1 -fsyntax-only %t.c -verify
@@ -86,8 +88,7 @@ enum EnumWithAttributes { // expected-wa
   // CHECK-NEXT: } *EnumWithAttributesPtr;
 } __attribute__((deprecated)) *EnumWithAttributesPtr; // expected-note {{'EnumWithAttributes' has been explicitly marked deprecated here}}
 
-// FIXME: If enum is forward-declared at file scope, attributes are lost.
-// CHECK-LABEL: enum EnumWithAttributes2 *EnumWithAttributes2Ptr;
+// CHECK-LABEL: enum __attribute__((deprecated(""))) EnumWithAttributes2 *EnumWithAttributes2Ptr;
 // expected-warning at +2 {{'EnumWithAttributes2' is deprecated}}
 // expected-note at +1 {{'EnumWithAttributes2' has been explicitly marked deprecated here}}
 enum __attribute__((deprecated)) EnumWithAttributes2 *EnumWithAttributes2Ptr;

Modified: cfe/trunk/test/SemaCXX/MicrosoftCompatibility.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/MicrosoftCompatibility.cpp?rev=333574&r1=333573&r2=333574&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/MicrosoftCompatibility.cpp (original)
+++ cfe/trunk/test/SemaCXX/MicrosoftCompatibility.cpp Wed May 30 11:33:53 2018
@@ -239,6 +239,15 @@ enum ENUM2 {
 	ENUM2_c = 0x100000000 // expected-warning {{enumerator value is not representable in the underlying type 'int'}}
 };
 
+namespace NsEnumForwardDecl {
+  enum E *p; // expected-warning {{forward references to 'enum' types are a Microsoft extension}}
+  extern E e;
+}
+// Clang used to complain that NsEnumForwardDecl::E was undeclared below.
+NsEnumForwardDecl::E NsEnumForwardDecl_e;
+namespace NsEnumForwardDecl {
+  extern E e;
+}
 
 namespace PR11791 {
   template<class _Ty>




More information about the cfe-commits mailing list