Skip to content

Commit 30a5d56

Browse files
authored
[C23] AST equivalence of attributes (#151196)
Implicitly declared types (like __NSConstantString_tag, etc) will be declared with visibility attributes. This causes problems when merging ASTs because we currently reject declaration merging for declarations with attributes. This relaxes that restriction somewhat; implicit declarations can now have attributes when merging; we assume that if the compiler generated it, it's fine.
1 parent 5ae79ba commit 30a5d56

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

clang/lib/AST/ASTStructuralEquivalence.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,9 @@ CheckStructurallyEquivalentAttributes(StructuralEquivalenceContext &Context,
456456
const Decl *D1, const Decl *D2,
457457
const Decl *PrimaryDecl = nullptr) {
458458
// If either declaration has an attribute on it, we treat the declarations
459-
// as not being structurally equivalent.
459+
// as not being structurally equivalent unless both declarations are implicit
460+
// (ones generated by the compiler like __NSConstantString_tag).
461+
//
460462
// FIXME: this should be handled on a case-by-case basis via tablegen in
461463
// Attr.td. There are multiple cases to consider: one declaration with the
462464
// attribute, another without it; different attribute syntax|spellings for
@@ -468,7 +470,7 @@ CheckStructurallyEquivalentAttributes(StructuralEquivalenceContext &Context,
468470
D1Attr = *D1->getAttrs().begin();
469471
if (D2->hasAttrs())
470472
D2Attr = *D2->getAttrs().begin();
471-
if (D1Attr || D2Attr) {
473+
if ((D1Attr || D2Attr) && !D1->isImplicit() && !D2->isImplicit()) {
472474
const auto *DiagnoseDecl = cast<TypeDecl>(PrimaryDecl ? PrimaryDecl : D2);
473475
Context.Diag2(DiagnoseDecl->getLocation(),
474476
diag::warn_odr_tag_type_with_attributes)

clang/test/ASTMerge/enum/test2.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// RUN: %clang_cc1 -std=c23 -emit-pch -o %t.1.ast %S/Inputs/enum3.c
22
// RUN: %clang_cc1 -std=c23 -emit-pch -o %t.2.ast %S/Inputs/enum4.c
3-
// RUN: not %clang_cc1 -std=c23 -ast-merge %t.1.ast -ast-merge %t.2.ast -fsyntax-only %s 2>&1 | FileCheck %s
3+
// RUN: %clang_cc1 -std=c23 -ast-merge %t.1.ast -ast-merge %t.2.ast -fsyntax-only %s 2>&1 | FileCheck %s
44

5-
// FIXME: this error should not happen!
6-
// CHECK: error: type 'struct __NSConstantString_tag' has an attribute which currently causes the types to be treated as though they are incompatible
75
// CHECK: enum3.c:2:6: warning: type 'enum E1' has incompatible definitions in different translation units
86
// CHECK: enum4.c:2:6: note: enumeration 'E1' missing fixed underlying type here
97
// CHECK: enum3.c:2:6: note: enumeration 'E1' has fixed underlying type here
@@ -12,5 +10,5 @@
1210
// CHECK: enum3.c:6:6: note: enumeration 'E2' missing fixed underlying type here
1311
// CHECK: enum3.c:11:6: warning: type 'enum E3' has incompatible definitions in different translation units
1412
// CHECK: enum3.c:11:6: note: enumeration 'E3' declared with incompatible fixed underlying types ('long' vs. 'short')
15-
// CHECK: 3 warnings and 1 error generated
13+
// CHECK: 3 warnings generated
1614

0 commit comments

Comments
 (0)