[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
Tue Jun 5 22:20:53 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rC334062: [Sema] Fix parsing of anonymous union in language linkage specification (authored by jkorous, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D45884?vs=143736&id=150069#toc

Repository:
  rC Clang

https://reviews.llvm.org/D45884

Files:
  lib/Sema/SemaDecl.cpp
  test/SemaCXX/anonymous-union-export.cpp
  test/SemaCXX/anonymous-union.cpp


Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -4642,12 +4642,14 @@
     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.
+      DeclContext *OwnerScope = Owner->getRedeclContext();
       if (DS.getStorageClassSpec() != DeclSpec::SCS_static &&
-          (isa<TranslationUnitDecl>(Owner) ||
-           (isa<NamespaceDecl>(Owner) &&
-            cast<NamespaceDecl>(Owner)->getDeclName()))) {
+          (OwnerScope->isTranslationUnit() ||
+           (OwnerScope->isNamespace() &&
+            !cast<NamespaceDecl>(OwnerScope)->isAnonymousNamespace()))) {
         Diag(Record->getLocation(), diag::err_anonymous_union_not_static)
           << FixItHint::CreateInsertion(Record->getLocation(), "static ");
 
Index: test/SemaCXX/anonymous-union.cpp
===================================================================
--- test/SemaCXX/anonymous-union.cpp
+++ test/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: test/SemaCXX/anonymous-union-export.cpp
===================================================================
--- test/SemaCXX/anonymous-union-export.cpp
+++ test/SemaCXX/anonymous-union-export.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -std=c++17 -fmodules-ts -emit-obj -verify %s
+
+export module M;
+export {
+    union { bool a; }; // expected-error{{anonymous unions at namespace or global scope must be declared 'static'}}
+}


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


More information about the cfe-commits mailing list