[clang] f9305c9 - [clang][bytecode] Use in `Expr::tryEvaluateString` (#160118)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 24 22:50:42 PDT 2025
Author: Timm Baeder
Date: 2025-09-25T07:50:38+02:00
New Revision: f9305c900ba23de7a9088ed00912cc6113eba506
URL: https://github.com/llvm/llvm-project/commit/f9305c900ba23de7a9088ed00912cc6113eba506
DIFF: https://github.com/llvm/llvm-project/commit/f9305c900ba23de7a9088ed00912cc6113eba506.diff
LOG: [clang][bytecode] Use in `Expr::tryEvaluateString` (#160118)
Fixes #157492
Added:
Modified:
clang/lib/AST/ByteCode/Context.cpp
clang/lib/AST/ByteCode/Context.h
clang/lib/AST/ExprConstant.cpp
clang/test/SemaCXX/verbose-trap.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index 71d0bcf61a5ff..306f95c479d0f 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -237,6 +237,52 @@ bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
return evaluateStringRepr(Parent, SizeExpr, PtrExpr, Result);
}
+bool Context::evaluateString(State &Parent, const Expr *E,
+ std::string &Result) {
+ assert(Stk.empty());
+ Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
+
+ auto PtrRes = C.interpretAsPointer(E, [&](const Pointer &Ptr) {
+ const Descriptor *FieldDesc = Ptr.getFieldDesc();
+ if (!FieldDesc->isPrimitiveArray())
+ return false;
+
+ if (!Ptr.isConst())
+ return false;
+
+ unsigned N = Ptr.getNumElems();
+
+ if (Ptr.elemSize() == 1 /* bytes */) {
+ const char *Chars = reinterpret_cast<const char *>(Ptr.getRawAddress());
+ unsigned Length = strnlen(Chars, N);
+ // Wasn't null terminated.
+ if (N == Length)
+ return false;
+ Result.assign(Chars, Length);
+ return true;
+ }
+
+ PrimType ElemT = FieldDesc->getPrimType();
+ for (unsigned I = Ptr.getIndex(); I != N; ++I) {
+ INT_TYPE_SWITCH(ElemT, {
+ auto Elem = Ptr.elem<T>(I);
+ if (Elem.isZero())
+ return true;
+ Result.push_back(static_cast<char>(Elem));
+ });
+ }
+ // We didn't find a 0 byte.
+ return false;
+ });
+
+ if (PtrRes.isInvalid()) {
+ C.cleanup();
+ Stk.clear();
+ return false;
+ }
+ return true;
+}
+
bool Context::evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result) {
assert(Stk.empty());
Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
diff --git a/clang/lib/AST/ByteCode/Context.h b/clang/lib/AST/ByteCode/Context.h
index 280a31725555f..f5fa977cbcad8 100644
--- a/clang/lib/AST/ByteCode/Context.h
+++ b/clang/lib/AST/ByteCode/Context.h
@@ -67,6 +67,10 @@ class Context final {
bool evaluateCharRange(State &Parent, const Expr *SizeExpr,
const Expr *PtrExpr, std::string &Result);
+ /// Evaluate \param E and if it can be evaluated to a null-terminated string,
+ /// copy the result into \param Result.
+ bool evaluateString(State &Parent, const Expr *E, std::string &Result);
+
/// Evalute \param E and if it can be evaluated to a string literal,
/// run strlen() on it.
bool evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result);
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index a4fca1811c92b..b706b14945b6d 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -18905,9 +18905,15 @@ std::optional<std::string> Expr::tryEvaluateString(ASTContext &Ctx) const {
uint64_t Result;
std::string StringResult;
+ if (Info.EnableNewConstInterp) {
+ if (!Info.Ctx.getInterpContext().evaluateString(Info, this, StringResult))
+ return std::nullopt;
+ return StringResult;
+ }
+
if (EvaluateBuiltinStrLen(this, Result, Info, &StringResult))
return StringResult;
- return {};
+ return std::nullopt;
}
template <typename T>
diff --git a/clang/test/SemaCXX/verbose-trap.cpp b/clang/test/SemaCXX/verbose-trap.cpp
index 2503f9860d9c3..0dd090e57d016 100644
--- a/clang/test/SemaCXX/verbose-trap.cpp
+++ b/clang/test/SemaCXX/verbose-trap.cpp
@@ -1,6 +1,9 @@
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fcxx-exceptions -verify %s
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -fcxx-exceptions -verify %s
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fcxx-exceptions -verify %s -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -fcxx-exceptions -verify %s -fexperimental-new-constant-interpreter
+
#if !__has_builtin(__builtin_verbose_trap)
#error
#endif
@@ -45,3 +48,14 @@ void f2() {
void test() {
f<constCat3, constMsg3>(nullptr);
}
+
+/// Arguments must be null terminated.
+int foo() {
+ constexpr char K[] = {'a', 'b'};
+ __builtin_verbose_trap("hehe", K); // expected-error {{argument to __builtin_verbose_trap must be a pointer to a constant string}}
+ __builtin_verbose_trap(K, "hehe"); //expected-error {{argument to __builtin_verbose_trap must be a pointer to a constant string}}
+
+ constexpr char K2[] = {'a', 'b', '\0'};
+ __builtin_verbose_trap("hehe", K2);
+ __builtin_verbose_trap(K2, "hehe");
+}
More information about the cfe-commits
mailing list