[PATCH] D45884: [Sema] Fix parsing of anonymous union in language linkage specification

Jan Korous via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 20 08:07:46 PDT 2018


jkorous created this revision.
jkorous added reviewers: vsapsai, arphaman, rsmith.
jkorous added a project: clang.
Herald added a subscriber: cfe-commits.

C++17 [dcl.link]p4:
A linkage specification does not establish a scope.

C++17 [class.union.anon]p2:
Namespace level anonymous unions shall be declared static.

The test produces a crash on master.


Repository:
  rC Clang

https://reviews.llvm.org/D45884

Files:
  Sema/SemaDecl.cpp
  SemaCXX/anonymous-union.cpp


Index: SemaCXX/anonymous-union.cpp
===================================================================
--- SemaCXX/anonymous-union.cpp
+++ SemaCXX/anonymous-union.cpp
@@ -80,6 +80,10 @@
   float float_val;
 };
 
+extern "C++" {
+union { }; // expected-error{{anonymous unions at namespace or global scope must be declared 'static'}}
+}
+
 static union {
   int int_val2; // expected-note{{previous definition is here}}
   float float_val2;
Index: Sema/SemaDecl.cpp
===================================================================
--- Sema/SemaDecl.cpp
+++ Sema/SemaDecl.cpp
@@ -4645,12 +4645,20 @@
     unsigned DiagID;
     if (Record->isUnion()) {
       // C++ [class.union]p6:
+      // C++17 [class.union.anon]p2:
       //   Anonymous unions declared in a named namespace or in the
       //   global namespace shall be declared static.
+
+      // We need to get to the parent scope (extern {} is not a scope).
+      DeclContext *OwnerScope = Owner;
+      while (OwnerScope->getDeclKind() == Decl::LinkageSpec) {
+        OwnerScope = OwnerScope->getParent();
+      }
+
       if (DS.getStorageClassSpec() != DeclSpec::SCS_static &&
-          (isa<TranslationUnitDecl>(Owner) ||
-           (isa<NamespaceDecl>(Owner) &&
-            cast<NamespaceDecl>(Owner)->getDeclName()))) {
+          (isa<TranslationUnitDecl>(OwnerScope) ||
+           (isa<NamespaceDecl>(OwnerScope) &&
+            cast<NamespaceDecl>(OwnerScope)->getDeclName()))) {
         Diag(Record->getLocation(), diag::err_anonymous_union_not_static)
           << FixItHint::CreateInsertion(Record->getLocation(), "static ");
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45884.143317.patch
Type: text/x-patch
Size: 1612 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180420/36700969/attachment.bin>


More information about the cfe-commits mailing list