r334062 - [Sema] Fix parsing of anonymous union in language linkage specification

Jan Korous via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 5 22:16:34 PDT 2018


Author: jkorous
Date: Tue Jun  5 22:16:34 2018
New Revision: 334062

URL: http://llvm.org/viewvc/llvm-project?rev=334062&view=rev
Log:
[Sema] Fix parsing of anonymous union in language linkage specification

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.

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

rdar://problem/37545925

Added:
    cfe/trunk/test/SemaCXX/anonymous-union-export.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/anonymous-union.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=334062&r1=334061&r2=334062&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Jun  5 22:16:34 2018
@@ -4642,12 +4642,14 @@ Decl *Sema::BuildAnonymousStructOrUnion(
     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 ");
 

Added: cfe/trunk/test/SemaCXX/anonymous-union-export.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/anonymous-union-export.cpp?rev=334062&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/anonymous-union-export.cpp (added)
+++ cfe/trunk/test/SemaCXX/anonymous-union-export.cpp Tue Jun  5 22:16:34 2018
@@ -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'}}
+}

Modified: cfe/trunk/test/SemaCXX/anonymous-union.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/anonymous-union.cpp?rev=334062&r1=334061&r2=334062&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/anonymous-union.cpp (original)
+++ cfe/trunk/test/SemaCXX/anonymous-union.cpp Tue Jun  5 22:16:34 2018
@@ -80,6 +80,10 @@ union { // expected-error{{anonymous uni
   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;




More information about the cfe-commits mailing list