[clang] [clang][bytecode] Handle __builtin_wcslen (PR #118446)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 3 00:56:19 PST 2024
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/118446
... just like strlen.
>From 652b93f0487d18fc9703c4bba623149c853f9540 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 3 Dec 2024 09:39:54 +0100
Subject: [PATCH] [clang][bytecode] Handle __builtin_wcslen
... just like strlen.
---
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 4 +++-
clang/test/AST/ByteCode/builtin-functions.cpp | 12 ++++++++++++
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 8ff0fad0aa5a79..2da16608e26c43 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -243,7 +243,7 @@ static bool interp__builtin_strlen(InterpState &S, CodePtr OpPC,
unsigned ID = Func->getBuiltinID();
const Pointer &StrPtr = getParam<Pointer>(Frame, 0);
- if (ID == Builtin::BIstrlen)
+ if (ID == Builtin::BIstrlen || ID == Builtin::BIwcslen)
diagnoseNonConstexprBuiltin(S, OpPC, ID);
if (!CheckArray(S, OpPC, StrPtr))
@@ -1857,6 +1857,8 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
break;
case Builtin::BI__builtin_strlen:
case Builtin::BIstrlen:
+ case Builtin::BI__builtin_wcslen:
+ case Builtin::BIwcslen:
if (!interp__builtin_strlen(S, OpPC, Frame, F, Call))
return false;
break;
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index b94adfa3ab36bd..d8c8d207fbc45f 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -7,6 +7,10 @@
// RUN: %clang_cc1 -triple avr -std=c++20 -Wno-string-plus-int -fexperimental-new-constant-interpreter %s -verify=expected,both
// RUN: %clang_cc1 -triple avr -std=c++20 -Wno-string-plus-int -verify=ref,both %s -Wno-constant-evaluated
+extern "C" {
+ typedef decltype(sizeof(int)) size_t;
+ extern size_t wcslen(const wchar_t *p);
+}
namespace strcmp {
constexpr char kFoobar[6] = {'f','o','o','b','a','r'};
@@ -85,6 +89,14 @@ constexpr const char *a = "foo\0quux";
constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator.
constexpr int bad = __builtin_strlen(d); // both-error {{constant expression}} \
// both-note {{one-past-the-end}}
+
+ constexpr int wn = __builtin_wcslen(L"hello");
+ static_assert(wn == 5);
+ constexpr int wm = wcslen(L"hello"); // both-error {{constant expression}} \
+ // both-note {{non-constexpr function 'wcslen' cannot be used in a constant expression}}
+
+ int arr[3]; // both-note {{here}}
+ int wk = arr[wcslen(L"hello")]; // both-warning {{array index 5}}
}
namespace nan {
More information about the cfe-commits
mailing list