[clang] [clang][bytecode] Allow checking builtin functions... (PR #119328)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 9 21:35:37 PST 2024


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

... in checkingPotentialConstantExpression mode. This is what the current interpreter does, yet it doesn't do so for `__builtin_operator_new`.

>From 3d465f0e3798e575e5b28d871b87359e35ba3325 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 10 Dec 2024 06:27:59 +0100
Subject: [PATCH] [clang][bytecode] Allow checking builtin functions...

... in checkingPotentialConstantExpression mode. This is what the
current interpreter does, yet it doesn't do so for
`__builtin_operator_new`.
---
 clang/lib/AST/ByteCode/Interp.cpp             | 5 ++++-
 clang/test/AST/ByteCode/builtin-functions.cpp | 4 ++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 435af1201890c8..7c7752080746e9 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1360,7 +1360,10 @@ bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
 
 bool CallBI(InterpState &S, CodePtr OpPC, const Function *Func,
             const CallExpr *CE, uint32_t BuiltinID) {
-  if (S.checkingPotentialConstantExpression())
+  // A little arbitrary, but the current interpreter allows evaluation
+  // of builtin functions in this mode, with some exceptions.
+  if (BuiltinID == Builtin::BI__builtin_operator_new &&
+      S.checkingPotentialConstantExpression())
     return false;
   auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC);
 
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index a67b6b665f3790..7dd08cb5fa1c35 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1198,11 +1198,11 @@ namespace BuiltinMemcpy {
   }
   static_assert(simpleMove() == 12);
 
-  constexpr int memcpyTypeRem() { // ref-error {{never produces a constant expression}}
+  constexpr int memcpyTypeRem() { // both-error {{never produces a constant expression}}
     int a = 12;
     int b = 0;
     __builtin_memmove(&b, &a, 1); // both-note {{'memmove' not supported: size to copy (1) is not a multiple of size of element type 'int'}} \
-                                  // ref-note {{not supported}}
+                                  // both-note {{not supported}}
     return b;
   }
   static_assert(memcpyTypeRem() == 12); // both-error {{not an integral constant expression}} \



More information about the cfe-commits mailing list