Skip to content

Commit 67da125

Browse files
committedJun 6, 2018
[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 llvm-svn: 334062
1 parent 3942b2e commit 67da125

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed
 

‎clang/lib/Sema/SemaDecl.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -4642,12 +4642,14 @@ Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS,
46424642
unsigned DiagID;
46434643
if (Record->isUnion()) {
46444644
// C++ [class.union]p6:
4645+
// C++17 [class.union.anon]p2:
46454646
// Anonymous unions declared in a named namespace or in the
46464647
// global namespace shall be declared static.
4648+
DeclContext *OwnerScope = Owner->getRedeclContext();
46474649
if (DS.getStorageClassSpec() != DeclSpec::SCS_static &&
4648-
(isa<TranslationUnitDecl>(Owner) ||
4649-
(isa<NamespaceDecl>(Owner) &&
4650-
cast<NamespaceDecl>(Owner)->getDeclName()))) {
4650+
(OwnerScope->isTranslationUnit() ||
4651+
(OwnerScope->isNamespace() &&
4652+
!cast<NamespaceDecl>(OwnerScope)->isAnonymousNamespace()))) {
46514653
Diag(Record->getLocation(), diag::err_anonymous_union_not_static)
46524654
<< FixItHint::CreateInsertion(Record->getLocation(), "static ");
46534655

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %clang_cc1 -std=c++17 -fmodules-ts -emit-obj -verify %s
2+
3+
export module M;
4+
export {
5+
union { bool a; }; // expected-error{{anonymous unions at namespace or global scope must be declared 'static'}}
6+
}

‎clang/test/SemaCXX/anonymous-union.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ union { // expected-error{{anonymous unions at namespace or global scope must be
8080
float float_val;
8181
};
8282

83+
extern "C++" {
84+
union { }; // expected-error{{anonymous unions at namespace or global scope must be declared 'static'}}
85+
}
86+
8387
static union {
8488
int int_val2; // expected-note{{previous definition is here}}
8589
float float_val2;

0 commit comments

Comments
 (0)
Please sign in to comment.