Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 3e45634

Browse files
committed
P0426: Make the library implementation of constexpr char_traits a little easier
by providing a memchr builtin that returns char* instead of void*. Also add a __has_feature flag to indicate the presence of constexpr forms of the relevant <string> functions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@292555 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 8ae50eb commit 3e45634

File tree

7 files changed

+44
-0
lines changed

7 files changed

+44
-0
lines changed

include/clang/Basic/Builtins.def

+1
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,7 @@ BUILTIN(__builtin_smulll_overflow, "bSLLiCSLLiCSLLi*", "n")
13391339
BUILTIN(__builtin_addressof, "v*v&", "nct")
13401340
BUILTIN(__builtin_operator_new, "v*z", "c")
13411341
BUILTIN(__builtin_operator_delete, "vv*", "n")
1342+
BUILTIN(__builtin_char_memchr, "c*cC*iz", "n")
13421343

13431344
// Safestack builtins
13441345
BUILTIN(__builtin___get_unsafe_stack_start, "v*", "Fn")

lib/AST/ExprConstant.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -5683,6 +5683,7 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
56835683
case Builtin::BI__builtin_strchr:
56845684
case Builtin::BI__builtin_wcschr:
56855685
case Builtin::BI__builtin_memchr:
5686+
case Builtin::BI__builtin_char_memchr:
56865687
case Builtin::BI__builtin_wmemchr: {
56875688
if (!Visit(E->getArg(0)))
56885689
return false;
@@ -5720,6 +5721,7 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
57205721
// Fall through.
57215722
case Builtin::BImemchr:
57225723
case Builtin::BI__builtin_memchr:
5724+
case Builtin::BI__builtin_char_memchr:
57235725
// memchr compares by converting both sides to unsigned char. That's also
57245726
// correct for strchr if we get this far (to cope with plain char being
57255727
// unsigned in the strchr case).

lib/CodeGen/CGBuiltin.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,10 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
11891189
return RValue::get(Dest.getPointer());
11901190
}
11911191

1192+
case Builtin::BI__builtin_char_memchr:
1193+
BuiltinID = Builtin::BI__builtin_memchr;
1194+
break;
1195+
11921196
case Builtin::BI__builtin___memcpy_chk: {
11931197
// fold __builtin_memcpy_chk(x, y, cst1, cst2) to memcpy iff cst1<=cst2.
11941198
llvm::APSInt Size, DstSize;

lib/Lex/PPMacroExpansion.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@ static bool HasFeature(const Preprocessor &PP, StringRef Feature) {
11831183
.Case("cxx_attributes", LangOpts.CPlusPlus11)
11841184
.Case("cxx_auto_type", LangOpts.CPlusPlus11)
11851185
.Case("cxx_constexpr", LangOpts.CPlusPlus11)
1186+
.Case("cxx_constexpr_string_builtins", LangOpts.CPlusPlus11)
11861187
.Case("cxx_decltype", LangOpts.CPlusPlus11)
11871188
.Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11)
11881189
.Case("cxx_default_function_template_args", LangOpts.CPlusPlus11)

test/CodeGenCXX/builtins.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ int x = __builtin_abs(-2);
2626
long y = __builtin_abs(-2l);
2727
// CHECK: [[Y:%.+]] = call i64 @_Z13__builtin_absl(i64 -2)
2828
// CHECK: store i64 [[Y]], i64* @y, align 8
29+
30+
extern const char char_memchr_arg[32];
31+
char *memchr_result = __builtin_char_memchr(char_memchr_arg, 123, 32);
32+
// CHECK: call i8* @memchr(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @char_memchr_arg, i32 0, i32 0), i32 123, i64 32)

test/Lexer/has_feature_cxx0x.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ int no_constexpr();
301301
// CHECK-11: has_constexpr
302302
// CHECK-NO-11: no_constexpr
303303

304+
#if __has_feature(cxx_constexpr_string_builtins)
305+
int has_constexpr_string_builtins();
306+
#else
307+
int no_constexpr_string_builtins();
308+
#endif
309+
310+
// CHECK-1Z: has_constexpr_string_builtins
311+
// CHECK-14: has_constexpr_string_builtins
312+
// CHECK-11: has_constexpr_string_builtins
313+
// CHECK-NO-11: no_constexpr_string_builtins
314+
304315
#if __has_feature(cxx_generalized_initializers)
305316
int has_generalized_initializers();
306317
#else

test/SemaCXX/constexpr-string.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ namespace StrchrEtc {
166166
static_assert(__builtin_memchr(nullptr, 'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
167167
static_assert(__builtin_memchr(nullptr, 'x', 0) == nullptr); // FIXME: Should we reject this?
168168

169+
static_assert(__builtin_char_memchr(kStr, 'a', 0) == nullptr);
170+
static_assert(__builtin_char_memchr(kStr, 'a', 1) == kStr);
171+
static_assert(__builtin_char_memchr(kStr, '\0', 5) == nullptr);
172+
static_assert(__builtin_char_memchr(kStr, '\0', 6) == kStr + 5);
173+
static_assert(__builtin_char_memchr(kStr, '\xff', 8) == kStr + 4);
174+
static_assert(__builtin_char_memchr(kStr, '\xff' + 256, 8) == kStr + 4);
175+
static_assert(__builtin_char_memchr(kStr, '\xff' - 256, 8) == kStr + 4);
176+
static_assert(__builtin_char_memchr(kFoo, 'x', 3) == nullptr);
177+
static_assert(__builtin_char_memchr(kFoo, 'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
178+
static_assert(__builtin_char_memchr(nullptr, 'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
179+
static_assert(__builtin_char_memchr(nullptr, 'x', 0) == nullptr); // FIXME: Should we reject this?
180+
181+
static_assert(*__builtin_char_memchr(kStr, '\xff', 8) == '\xff');
182+
constexpr bool char_memchr_mutable() {
183+
char buffer[] = "mutable";
184+
*__builtin_char_memchr(buffer, 't', 8) = 'r';
185+
*__builtin_char_memchr(buffer, 'm', 8) = 'd';
186+
return __builtin_strcmp(buffer, "durable") == 0;
187+
}
188+
static_assert(char_memchr_mutable());
189+
169190
constexpr bool a = !strchr("hello", 'h'); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strchr' cannot be used in a constant expression}}
170191
constexpr bool b = !memchr("hello", 'h', 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'memchr' cannot be used in a constant expression}}
171192
}

0 commit comments

Comments
 (0)