[clang] [clang] Constant-evaluate format strings as last resort (PR #135864)
via cfe-commits
cfe-commits at lists.llvm.org
Mon May 12 08:51:44 PDT 2025
================
@@ -791,10 +791,26 @@ class Expr : public ValueStmt {
const Expr *PtrExpression, ASTContext &Ctx,
EvalResult &Status) const;
- /// If the current Expr can be evaluated to a pointer to a null-terminated
- /// constant string, return the constant string (without the terminating
- /// null).
- std::optional<std::string> tryEvaluateString(ASTContext &Ctx) const;
+ class StringEvalResult {
+ std::string Storage;
+ const StringLiteral *SL;
+ uint64_t Offset;
+
+ public:
+ StringEvalResult(std::string Contents);
+ StringEvalResult(const StringLiteral *SL, uint64_t Offset);
+
+ llvm::StringRef getString() const;
+ bool getStringLiteral(const StringLiteral *&SL, uint64_t &Offset) const;
+ };
----------------
apple-fcloutier wrote:
This is a polite `union` of `std::string` and `StringLiteral`: `Storage` is empty when `SL` is set, and vice versa. `getString()` encapsulates useful logic to decide how to get a `StringRef` back, which I think justifies having a separate type instead of an aggregate. I will add a comment on top of it.
https://github.com/llvm/llvm-project/pull/135864
More information about the cfe-commits
mailing list