Skip to content

Commit 3d6fb12

Browse files
committed
[C23] More improved type compatibility for enumerations (llvm#150946)
The structural equivalence checker was not paying attention to whether enumerations had compatible fixed underlying types or not. Fixes llvm#150594
1 parent 400d8b0 commit 3d6fb12

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

clang/include/clang/Basic/DiagnosticASTKinds.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,14 @@ def note_odr_number_of_bases : Note<
507507
"class has %0 base %plural{1:class|:classes}0">;
508508
def note_odr_enumerator : Note<"enumerator %0 with value %1 here">;
509509
def note_odr_missing_enumerator : Note<"no corresponding enumerator here">;
510+
def note_odr_incompatible_fixed_underlying_type : Note<
511+
"enumeration %0 declared with incompatible fixed underlying types (%1 vs. "
512+
"%2)">;
513+
def note_odr_fixed_underlying_type : Note<
514+
"enumeration %0 has fixed underlying type here">;
515+
def note_odr_missing_fixed_underlying_type : Note<
516+
"enumeration %0 missing fixed underlying type here">;
517+
510518
def err_odr_field_type_inconsistent : Error<
511519
"field %0 declared with incompatible types in different "
512520
"translation units (%1 vs. %2)">;

clang/lib/AST/ASTStructuralEquivalence.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,48 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
20892089
!CheckStructurallyEquivalentAttributes(Context, D1, D2))
20902090
return false;
20912091

2092+
// In C23, if one enumeration has a fixed underlying type, the other shall
2093+
// have a compatible fixed underlying type (6.2.7).
2094+
if (Context.LangOpts.C23) {
2095+
if (D1->isFixed() != D2->isFixed()) {
2096+
if (Context.Complain) {
2097+
Context.Diag2(D2->getLocation(),
2098+
Context.getApplicableDiagnostic(
2099+
diag::err_odr_tag_type_inconsistent))
2100+
<< Context.ToCtx.getTypeDeclType(D2)
2101+
<< (&Context.FromCtx != &Context.ToCtx);
2102+
Context.Diag1(D1->getLocation(),
2103+
D1->isFixed()
2104+
? diag::note_odr_fixed_underlying_type
2105+
: diag::note_odr_missing_fixed_underlying_type)
2106+
<< D1;
2107+
Context.Diag2(D2->getLocation(),
2108+
D2->isFixed()
2109+
? diag::note_odr_fixed_underlying_type
2110+
: diag::note_odr_missing_fixed_underlying_type)
2111+
<< D2;
2112+
}
2113+
return false;
2114+
}
2115+
if (D1->isFixed()) {
2116+
assert(D2->isFixed() && "enums expected to have fixed underlying types");
2117+
if (!IsStructurallyEquivalent(Context, D1->getIntegerType(),
2118+
D2->getIntegerType())) {
2119+
if (Context.Complain) {
2120+
Context.Diag2(D2->getLocation(),
2121+
Context.getApplicableDiagnostic(
2122+
diag::err_odr_tag_type_inconsistent))
2123+
<< Context.ToCtx.getTypeDeclType(D2)
2124+
<< (&Context.FromCtx != &Context.ToCtx);
2125+
Context.Diag2(D2->getLocation(),
2126+
diag::note_odr_incompatible_fixed_underlying_type)
2127+
<< D2 << D2->getIntegerType() << D1->getIntegerType();
2128+
}
2129+
return false;
2130+
}
2131+
}
2132+
}
2133+
20922134
llvm::SmallVector<const EnumConstantDecl *, 8> D1Enums, D2Enums;
20932135
auto CopyEnumerators =
20942136
[](auto &&Range, llvm::SmallVectorImpl<const EnumConstantDecl *> &Cont) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// [C23] missing underlying types
2+
enum E1 : int {
3+
E1Enumerator1
4+
};
5+
6+
enum E2 {
7+
E2Enumerator1
8+
};
9+
10+
// [C23] Incompatible underlying types
11+
enum E3 : long {
12+
E3Enumerator1
13+
};
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// [C23] missing underlying types
2+
enum E1 {
3+
E1Enumerator1
4+
};
5+
6+
enum E2 : int {
7+
E2Enumerator1
8+
};
9+
10+
// [C23] Incompatible underlying types
11+
enum E3 : short {
12+
E3Enumerator1
13+
};
14+

clang/test/ASTMerge/enum/test2.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -std=c23 -emit-pch -o %t.1.ast %S/Inputs/enum3.c
2+
// 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
4+
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
7+
// CHECK: enum3.c:2:6: warning: type 'enum E1' has incompatible definitions in different translation units
8+
// CHECK: enum4.c:2:6: note: enumeration 'E1' missing fixed underlying type here
9+
// CHECK: enum3.c:2:6: note: enumeration 'E1' has fixed underlying type here
10+
// CHECK: enum3.c:6:6: warning: type 'enum E2' has incompatible definitions in different translation units
11+
// CHECK: enum4.c:6:6: note: enumeration 'E2' has fixed underlying type here
12+
// CHECK: enum3.c:6:6: note: enumeration 'E2' missing fixed underlying type here
13+
// CHECK: enum3.c:11:6: warning: type 'enum E3' has incompatible definitions in different translation units
14+
// 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
16+

clang/test/C/C23/n3037.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,41 @@ void gh149965(void) {
437437
enum E2 *eptr2;
438438
[[maybe_unused]] __typeof__(x2.h) *ptr2 = eptr2;
439439
}
440+
441+
// Test that enumerations with mixed underlying types are properly handled.
442+
enum GH150594_E1 : int { GH150594_Val1 };
443+
enum GH150594_E2 : int { GH150594_Val2 };
444+
enum GH150594_E3 { GH150594_Val3 };
445+
enum GH150594_E4 : int { GH150594_Val4 };
446+
void GH150594(void) {
447+
extern enum GH150594_E1 Fn1(void); // both-note {{previous declaration is here}}
448+
extern enum GH150594_E2 Fn2(void); // c17-note {{previous declaration is here}}
449+
extern enum GH150594_E3 Fn3(void); // both-note {{previous declaration is here}}
450+
extern enum GH150594_E4 Fn4(void); // both-note {{previous declaration is here}}
451+
enum GH150594_E1 { GH150594_Val1 };
452+
enum GH150594_E2 : int { GH150594_Val2 };
453+
enum GH150594_E3 : int { GH150594_Val3 };
454+
enum GH150594_E4 : short { GH150594_Val4 };
455+
extern enum GH150594_E1 Fn1(void); // both-error {{conflicting types for 'Fn1'}}
456+
extern enum GH150594_E2 Fn2(void); // c17-error {{conflicting types for 'Fn2'}}
457+
extern enum GH150594_E3 Fn3(void); // both-error {{conflicting types for 'Fn3'}}
458+
extern enum GH150594_E4 Fn4(void); // both-error {{conflicting types for 'Fn4'}}
459+
460+
// Show that two declarations in the same scope give expected diagnostics.
461+
enum E1 { e1 }; // both-note {{previous declaration is here}}
462+
enum E1 : int { e1 }; // both-error {{enumeration previously declared with nonfixed underlying type}}
463+
464+
enum E2 : int { e2 }; // both-note {{previous declaration is here}}
465+
enum E2 { e2 }; // both-error {{enumeration previously declared with fixed underlying type}}
466+
467+
enum E3 : int { e3 }; // both-note {{previous declaration is here}}
468+
enum E3 : short { e3 }; // both-error {{enumeration redeclared with different underlying type 'short' (was 'int')}}
469+
470+
typedef short foo;
471+
enum E4 : foo { e4 }; // c17-note 2 {{previous definition is here}}
472+
enum E4 : short { e4 }; // c17-error {{redefinition of 'E4'}} \
473+
c17-error {{redefinition of enumerator 'e4'}}
474+
475+
enum E5 : foo { e5 }; // both-note {{previous declaration is here}}
476+
enum E5 : int { e5 }; // both-error {{enumeration redeclared with different underlying type 'int' (was 'foo' (aka 'short'))}}
477+
}

0 commit comments

Comments
 (0)