================
@@ -2075,6 +2070,61 @@ void CGDebugInfo::CollectRecordLambdaFields(
   }
 }
 
+/// Try to create an llvm::Constant for a constexpr array of integer elements.
+/// Handles arrays of char, short, int, long with element width up to 64 bits.
+/// Returns nullptr if the array cannot be represented.
+static llvm::Constant *tryEmitConstexprArrayAsConstant(CodeGenModule &CGM,
+                                                       const VarDecl *Var,
+                                                       const APValue *Value) {
+  const auto *ArrayTy = 
CGM.getContext().getAsConstantArrayType(Var->getType());
+  if (!ArrayTy)
+    return nullptr;
+
+  const QualType ElemQTy = ArrayTy->getElementType();
+  if (ElemQTy.isNull() || !ElemQTy->isIntegerType())
+    return nullptr;
+
+  const unsigned ElemBitWidth = CGM.getContext().getTypeSize(ElemQTy);
+  // ConstantDataArray only supports 8/16/32/64-bit elements, and
+  // getZExtValue() asserts on wider types (e.g. __int128).
+  if (ElemBitWidth > 64)
+    return nullptr;
+
+  const unsigned NumElts = Value->getArraySize();
+  const unsigned NumInits = Value->getArrayInitializedElts();
+
+  // Preallocate with filler value, then overwrite initialized elements.
+  uint64_t FillVal = 0;
+  if (Value->hasArrayFiller()) {
+    const APValue &Filler = Value->getArrayFiller();
+    if (!Filler.isInt())
+      return nullptr;
+    FillVal = Filler.getInt().getZExtValue();
+  }
+
+  SmallVector<uint64_t, 64> Vals(NumElts, FillVal);
+  for (unsigned I = 0; I < NumInits; ++I) {
+    const APValue &Elt = Value->getArrayInitializedElt(I);
+    if (!Elt.isInt())
+      return nullptr;
----------------
dzhidzhoev wrote:

> definitely you can't have a float sitting inside a `constexpr int[]`

If such a scenario is detected during previous compilation stages (resulting in 
a compilation error and preventing the emission of debug information), I would 
prefer to either eliminate this check or convert it into an assertion (and we 
already have an assertion inside the `getInt()` function).
This way, if an incorrect AST with mismatched array and element types is 
generated, the issue will not be silently ignored.

https://github.com/llvm/llvm-project/pull/182442
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to