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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (Kupa-Martin)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/84068.diff


2 Files Affected:

- (modified) clang/lib/AST/Expr.cpp (+6-3) 
- (modified) clang/test/Sema/warn-compare-enum-types-mismatch.c (+3-1) 


``````````diff
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index b4de2155adcebd..9a28e6f750069b 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -264,10 +264,13 @@ namespace {
 }
 
 QualType Expr::getEnumCoercedType(const ASTContext &Ctx) const {
-  if (isa<EnumType>(this->getType()))
+  if (isa<EnumType>(this->getType())) {
     return this->getType();
-  else if (const auto *ECD = this->getEnumConstantDecl())
-    return Ctx.getTypeDeclType(cast<EnumDecl>(ECD->getDeclContext()));
+  } else if (const auto *ECD = this->getEnumConstantDecl()) {
+    const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
+    if (ED->isCompleteDefinition())
+      return Ctx.getTypeDeclType(ED);
+  }
   return this->getType();
 }
 
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 {

``````````

</details>


https://github.com/llvm/llvm-project/pull/84068


More information about the cfe-commits mailing list