[clang] [clang][Interp] Bail out on missing ComparisonCategoryInfo (PR #80131)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 6 10:45:01 PST 2024


Timm =?utf-8?q?Bäder?= <tbaeder at redhat.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/80131 at github.com>


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/80131

>From 3548cdb9907a44c25da61887b2f8e83268e3c2d9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 31 Jan 2024 12:53:39 +0100
Subject: [PATCH 1/2] [clang][Interp] Bail out on missing
 ComparisonCategoryInfo

Instead of asserting. This can happen in real-world code.
---
 clang/lib/AST/Interp/ByteCodeExprGen.cpp | 3 ++-
 clang/test/Sema/struct-cast.c            | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 8188d6f7f5c24..a1c458a09e5ae 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -379,7 +379,8 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
       return true;
     const ComparisonCategoryInfo *CmpInfo =
         Ctx.getASTContext().CompCategories.lookupInfoForType(BO->getType());
-    assert(CmpInfo);
+    if (!CmpInfo)
+      return false;
 
     // We need a temporary variable holding our return value.
     if (!Initializing) {
diff --git a/clang/test/Sema/struct-cast.c b/clang/test/Sema/struct-cast.c
index 74d00c42c295e..05e5fa4f92ca7 100644
--- a/clang/test/Sema/struct-cast.c
+++ b/clang/test/Sema/struct-cast.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only %s -verify
+// RUN: %clang_cc1 -fsyntax-only %s -fexperimental-new-constant-interpreter -verify
 // expected-no-diagnostics
 
 struct S {

>From b2a76d1d91c01ab559a707af8ab638ba49a8ac1b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 6 Feb 2024 19:44:47 +0100
Subject: [PATCH 2/2] Check for C++ instead

---
 clang/lib/AST/Interp/ByteCodeExprGen.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index a1c458a09e5ae..3e2c6be602903 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -374,13 +374,12 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
   // Special case for C++'s three-way/spaceship operator <=>, which
   // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
   // have a PrimType).
-  if (!T) {
+  if (!T && Ctx.getLangOpts().CPlusPlus) {
     if (DiscardResult)
       return true;
     const ComparisonCategoryInfo *CmpInfo =
         Ctx.getASTContext().CompCategories.lookupInfoForType(BO->getType());
-    if (!CmpInfo)
-      return false;
+    assert(CmpInfo);
 
     // We need a temporary variable holding our return value.
     if (!Initializing) {



More information about the cfe-commits mailing list