[clang] [Clang][Sema] Fix type of enumerators in incomplete enumerations (PR #84068)

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 5 14:48:12 PST 2024


https://github.com/Kupa-Martin updated https://github.com/llvm/llvm-project/pull/84068

>From 92c0ebc1fa419404dfa8c2497248cc35c2644c5b Mon Sep 17 00:00:00 2001
From: 44-2-Kupa-Martin <kupamartinclassroom at gmail.com>
Date: Tue, 5 Mar 2024 17:21:02 -0300
Subject: [PATCH] [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
---
 clang/lib/AST/Expr.cpp                             | 13 ++++++++-----
 clang/test/Sema/warn-compare-enum-types-mismatch.c |  4 +++-
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index b4de2155adcebd..fd0faa98c07178 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -264,11 +264,14 @@ namespace {
 }
 
 QualType Expr::getEnumCoercedType(const ASTContext &Ctx) const {
-  if (isa<EnumType>(this->getType()))
-    return this->getType();
-  else if (const auto *ECD = this->getEnumConstantDecl())
-    return Ctx.getTypeDeclType(cast<EnumDecl>(ECD->getDeclContext()));
-  return this->getType();
+  if (isa<EnumType>(getType())) {
+    return getType();
+  } else if (const auto *ECD = getEnumConstantDecl()) {
+    const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
+    if (ED->isCompleteDefinition())
+      return Ctx.getTypeDeclType(ED);
+  }
+  return getType();
 }
 
 SourceLocation Expr::getExprLoc() const {
diff --git a/clang/test/Sema/warn-compare-enum-types-mismatch.c b/clang/test/Sema/warn-compare-enum-types-mismatch.c
index 2b72aae16b977a..1094a6972e778d 100644
--- a/clang/test/Sema/warn-compare-enum-types-mismatch.c
+++ b/clang/test/Sema/warn-compare-enum-types-mismatch.c
@@ -6,7 +6,9 @@ typedef enum EnumA {
 } EnumA;
 
 enum EnumB {
-  B
+  B,
+  B1 = 1,
+  B2 = A == B1
 };
 
 enum {



More information about the cfe-commits mailing list