[clang] [CIR] Add array new cookie support (PR #163649)

via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 15 15:12:05 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- clang/lib/CIR/CodeGen/CIRGenCXXABI.cpp clang/lib/CIR/CodeGen/CIRGenCXXABI.h clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp clang/lib/CIR/CodeGen/CIRGenModule.cpp clang/lib/CIR/CodeGen/CIRGenTypeCache.h clang/test/CIR/CodeGen/new.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp b/clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp
index 0b67a89ce..d32f68722 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp
@@ -348,8 +348,8 @@ static mlir::Value emitCXXNewAllocSize(CIRGenFunction &cgf, const CXXNewExpr *e,
 
     // On overflow, produce a -1 so operator new will fail
     if (hasAnyOverflow) {
-      size = cgf.getBuilder().getConstInt(
-          loc, llvm::APInt::getAllOnes(sizeWidth));
+      size =
+          cgf.getBuilder().getConstInt(loc, llvm::APInt::getAllOnes(sizeWidth));
     } else {
       size = cgf.getBuilder().getConstInt(loc, allocationSize);
     }
@@ -407,8 +407,7 @@ void CIRGenFunction::emitNewArrayInitializer(
   const Expr *init = e->getInitializer();
   const InitListExpr *ile = dyn_cast<InitListExpr>(init);
   if (ile) {
-    cgm.errorNYI(ile->getSourceRange(),
-                 "emitNewArrayInitializer: init list");
+    cgm.errorNYI(ile->getSourceRange(), "emitNewArrayInitializer: init list");
     return;
   }
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
index 06b2f7dd0..cd7ca88d2 100644
--- a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
@@ -136,8 +136,7 @@ public:
                               Address src) override;
 
   Address initializeArrayCookie(CIRGenFunction &cgf, Address newPtr,
-                                mlir::Value numElements,
-                                const CXXNewExpr *e,
+                                mlir::Value numElements, const CXXNewExpr *e,
                                 QualType elementType) override;
 
 protected:
@@ -2010,9 +2009,9 @@ mlir::Value CIRGenItaniumCXXABI::emitDynamicCast(CIRGenFunction &cgf,
 CharUnits CIRGenItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
   // The array cookie is a size_t; pad that up to the element alignment.
   // The cookie is actually right-justified in that space.
-  return std::max(CharUnits::fromQuantity(cgm.SizeSizeInBytes),
-                  cgm.getASTContext().getPreferredTypeAlignInChars(
-                      elementType));
+  return std::max(
+      CharUnits::fromQuantity(cgm.SizeSizeInBytes),
+      cgm.getASTContext().getPreferredTypeAlignInChars(elementType));
 }
 
 Address CIRGenItaniumCXXABI::initializeArrayCookie(CIRGenFunction &cgf,
@@ -2061,12 +2060,12 @@ Address CIRGenItaniumCXXABI::initializeArrayCookie(CIRGenFunction &cgf,
 
   // Finally, compute a pointer to the actual data buffer by skipping
   // over the cookie completely.
-  auto dataOffset = cgf.getBuilder().getSignedInt(
-      loc, cookieSize.getQuantity(), /*width=*/32);
+  auto dataOffset = cgf.getBuilder().getSignedInt(loc, cookieSize.getQuantity(),
+                                                  /*width=*/32);
   mlir::Value dataPtr =
       cgf.getBuilder().createPtrStride(loc, baseBytePtr, dataOffset);
-  mlir::Value finalPtr = cgf.getBuilder().createPtrBitcast(
-      dataPtr, newPtr.getElementType());
+  mlir::Value finalPtr =
+      cgf.getBuilder().createPtrBitcast(dataPtr, newPtr.getElementType());
   CharUnits finalAlignment = baseAlignment.alignmentAtOffset(cookieSize);
   return Address(finalPtr, newPtr.getElementType(), finalAlignment);
 }

``````````

</details>


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


More information about the cfe-commits mailing list