Skip to content

Commit 89a0ee8

Browse files
authored
[clang][bytecode] Handle __builtin_wcslen (llvm#118446)
... just like strlen.
1 parent dd2b2b8 commit 89a0ee8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static bool interp__builtin_strlen(InterpState &S, CodePtr OpPC,
243243
unsigned ID = Func->getBuiltinID();
244244
const Pointer &StrPtr = getParam<Pointer>(Frame, 0);
245245

246-
if (ID == Builtin::BIstrlen)
246+
if (ID == Builtin::BIstrlen || ID == Builtin::BIwcslen)
247247
diagnoseNonConstexprBuiltin(S, OpPC, ID);
248248

249249
if (!CheckArray(S, OpPC, StrPtr))
@@ -1857,6 +1857,8 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
18571857
break;
18581858
case Builtin::BI__builtin_strlen:
18591859
case Builtin::BIstrlen:
1860+
case Builtin::BI__builtin_wcslen:
1861+
case Builtin::BIwcslen:
18601862
if (!interp__builtin_strlen(S, OpPC, Frame, F, Call))
18611863
return false;
18621864
break;

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
// RUN: %clang_cc1 -triple avr -std=c++20 -Wno-string-plus-int -fexperimental-new-constant-interpreter %s -verify=expected,both
88
// RUN: %clang_cc1 -triple avr -std=c++20 -Wno-string-plus-int -verify=ref,both %s -Wno-constant-evaluated
99

10+
extern "C" {
11+
typedef decltype(sizeof(int)) size_t;
12+
extern size_t wcslen(const wchar_t *p);
13+
}
1014

1115
namespace strcmp {
1216
constexpr char kFoobar[6] = {'f','o','o','b','a','r'};
@@ -85,6 +89,14 @@ constexpr const char *a = "foo\0quux";
8589
constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator.
8690
constexpr int bad = __builtin_strlen(d); // both-error {{constant expression}} \
8791
// both-note {{one-past-the-end}}
92+
93+
constexpr int wn = __builtin_wcslen(L"hello");
94+
static_assert(wn == 5);
95+
constexpr int wm = wcslen(L"hello"); // both-error {{constant expression}} \
96+
// both-note {{non-constexpr function 'wcslen' cannot be used in a constant expression}}
97+
98+
int arr[3]; // both-note {{here}}
99+
int wk = arr[wcslen(L"hello")]; // both-warning {{array index 5}}
88100
}
89101

90102
namespace nan {

0 commit comments

Comments
 (0)