Skip to content

Commit 98a7510

Browse files
committed
[clang][bytecode] Fix __builtin_strchr with unsigned chars
Get the zero-extended truncated desired value in that case. Add one RUN line to the constexpr-string.cpp test case, to not increase the runtime of that test too much.
1 parent 7b91bb2 commit 98a7510

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,10 +2042,16 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC,
20422042
}
20432043

20442044
if (ID == Builtin::BIstrchr || ID == Builtin::BI__builtin_strchr) {
2045+
int64_t DesiredTrunc;
2046+
if (S.getASTContext().CharTy->isSignedIntegerType())
2047+
DesiredTrunc =
2048+
Desired.trunc(S.getASTContext().getCharWidth()).getSExtValue();
2049+
else
2050+
DesiredTrunc =
2051+
Desired.trunc(S.getASTContext().getCharWidth()).getZExtValue();
20452052
// strchr compares directly to the passed integer, and therefore
20462053
// always fails if given an int that is not a char.
2047-
if (Desired !=
2048-
Desired.trunc(S.getASTContext().getCharWidth()).getSExtValue()) {
2054+
if (Desired != DesiredTrunc) {
20492055
S.Stk.push<Pointer>();
20502056
return true;
20512057
}

clang/test/SemaCXX/constexpr-string.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-signed-char
88
// RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-wchar -DNO_PREDEFINED_WCHAR_T
99

10+
// RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-signed-char -fexperimental-new-constant-interpreter
11+
1012
# 9 "/usr/include/string.h" 1 3 4 // expected-warning {{this style of line directive is a GNU extension}}
1113
extern "C" {
1214
typedef decltype(sizeof(int)) size_t;

0 commit comments

Comments
 (0)