Skip to content

Commit 92c0ebc

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 llvm#81418
1 parent 13bb726 commit 92c0ebc

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

clang/lib/AST/Expr.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,14 @@ namespace {
264264
}
265265

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

274277
SourceLocation Expr::getExprLoc() const {

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

+3-1
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)