[clang] [Clang][WIP] Constant Expressions inside of gcc'asm strings (PR #131003)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 12 11:43:51 PDT 2025


================
@@ -17928,43 +17928,70 @@ std::optional<std::string> Expr::tryEvaluateString(ASTContext &Ctx) const {
   return {};
 }
 
-bool Expr::EvaluateCharRangeAsString(std::string &Result,
-                                     const Expr *SizeExpression,
-                                     const Expr *PtrExpression, ASTContext &Ctx,
-                                     EvalResult &Status) const {
-  LValue String;
-  EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression);
-  Info.InConstantContext = true;
+template <typename T>
+static bool EvaluateCharRangeAsStringImpl(const Expr*, T& Result,
+                                          const Expr *SizeExpression,
+                                          const Expr *PtrExpression, ASTContext &Ctx,
+                                          Expr::EvalResult &Status) {
+    LValue String;
+    EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression);
+    Info.InConstantContext = true;
 
-  FullExpressionRAII Scope(Info);
-  APSInt SizeValue;
-  if (!::EvaluateInteger(SizeExpression, SizeValue, Info))
-    return false;
+    FullExpressionRAII Scope(Info);
+    APSInt SizeValue;
+    if (!::EvaluateInteger(SizeExpression, SizeValue, Info))
+      return false;
 
-  uint64_t Size = SizeValue.getZExtValue();
+    uint64_t Size = SizeValue.getZExtValue();
 
-  if (!::EvaluatePointer(PtrExpression, String, Info))
-    return false;
+    if constexpr(std::is_same_v<APValue, T>)
+        Result = APValue(APValue::UninitArray{}, Size, Size);
+    //else
+    //    Result.reserve(Size);
 
-  QualType CharTy = PtrExpression->getType()->getPointeeType();
-  for (uint64_t I = 0; I < Size; ++I) {
-    APValue Char;
-    if (!handleLValueToRValueConversion(Info, PtrExpression, CharTy, String,
-                                        Char))
+    if (!::EvaluatePointer(PtrExpression, String, Info))
+      return false;
+
+    QualType CharTy = PtrExpression->getType()->getPointeeType();
+    for (uint64_t I = 0; I < Size; ++I) {
+      APValue Char;
+      if (!handleLValueToRValueConversion(Info, PtrExpression, CharTy, String,
+                                          Char))
+        return false;
+
+      if constexpr(std::is_same_v<APValue, T>) {
+          Result.getArrayInitializedElt(I) = std::move(Char);
+      }
+      else {
+          APSInt C = Char.getInt();
+          Result.push_back(static_cast<char>(C.getExtValue()));
+      }
+
+      if (!HandleLValueArrayAdjustment(Info, PtrExpression, String, CharTy, 1))
+        return false;
+    }
+    if (!Scope.destroy())
       return false;
 
-    APSInt C = Char.getInt();
-    Result.push_back(static_cast<char>(C.getExtValue()));
-    if (!HandleLValueArrayAdjustment(Info, PtrExpression, String, CharTy, 1))
+    if (!CheckMemoryLeaks(Info))
----------------
erichkeane wrote:

This looks like a `if (!X) return false; return true;` here :) 

https://github.com/llvm/llvm-project/pull/131003


More information about the cfe-commits mailing list