[clang] [clang][bytecode] Classify 1-bit unsigned integers as bool (PR #104662)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 16 21:12:45 PDT 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/104662

This happens for enum types with bool parent types. isBooleanType() returns false for them however.

The previous version did the same thing by re-classifying the enum integer type, but that breaks with forward-declared enums.

>From c4ecc9a5cac6ba21b2f7f83b511442ea1fcf307a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 17 Aug 2024 05:59:53 +0200
Subject: [PATCH] [clang][bytecode] Classify 1-bit unsigned integers as bool

This happens for enum types with bool parent types. isBooleanType()
returns false for them however.

The previous version did the same thing by re-classifying the enum
integer type, but that breaks with forward-declared enums.
---
 clang/lib/AST/ByteCode/Context.cpp | 6 +++---
 clang/test/AST/ByteCode/c.c        | 3 ++-
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index e9c1fd1b8dc9f9..48ae363f651f5a 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -135,9 +135,6 @@ std::optional<PrimType> Context::classify(QualType T) const {
   if (T->isAnyComplexType() || T->isVectorType())
     return std::nullopt;
 
-  if (const auto *ET = T->getAs<EnumType>())
-    return classify(ET->getDecl()->getIntegerType());
-
   if (T->isSignedIntegerOrEnumerationType()) {
     switch (Ctx.getIntWidth(T)) {
     case 64:
@@ -163,6 +160,9 @@ std::optional<PrimType> Context::classify(QualType T) const {
       return PT_Uint16;
     case 8:
       return PT_Uint8;
+    case 1:
+      // Might happen for enum types.
+      return PT_Bool;
     default:
       return PT_IntAP;
     }
diff --git a/clang/test/AST/ByteCode/c.c b/clang/test/AST/ByteCode/c.c
index 13a5e082a125f2..b38259d41130eb 100644
--- a/clang/test/AST/ByteCode/c.c
+++ b/clang/test/AST/ByteCode/c.c
@@ -295,4 +295,5 @@ void T1(void) {
                                                            // pedantic-warning {{use of GNU statement expression extension}}
 }
 
-
+enum teste1 test1f(void), (*test1)(void) = test1f; // pedantic-warning {{ISO C forbids forward references to 'enum' types}}
+enum teste1 { TEST1 };



More information about the cfe-commits mailing list