Skip to content

Commit bc9f937

Browse files
committed
[Clang][Sema] Fix type of enumerators in incomplete enumerations
Enumerators dont have the type of their enumeration before the closing brace. In these cases Expr::getEnumCoercedType() incorrectly returned the enumeration type. Introduced in PR #81418
1 parent 13bb726 commit bc9f937

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

clang/lib/AST/Expr.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,13 @@ namespace {
264264
}
265265

266266
QualType Expr::getEnumCoercedType(const ASTContext &Ctx) const {
267-
if (isa<EnumType>(this->getType()))
267+
if (isa<EnumType>(this->getType())) {
268268
return this->getType();
269-
else if (const auto *ECD = this->getEnumConstantDecl())
270-
return Ctx.getTypeDeclType(cast<EnumDecl>(ECD->getDeclContext()));
269+
} else if (const auto *ECD = this->getEnumConstantDecl()) {
270+
const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
271+
if (ED->isCompleteDefinition())
272+
return Ctx.getTypeDeclType(ED);
273+
}
271274
return this->getType();
272275
}
273276

clang/test/Sema/warn-compare-enum-types-mismatch.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ typedef enum EnumA {
66
} EnumA;
77

88
enum EnumB {
9-
B
9+
B,
10+
B1 = 1,
11+
B2 = A == B1
1012
};
1113

1214
enum {

0 commit comments

Comments
 (0)