Skip to content

Commit ea21481

Browse files
authored
[Clang] Fix APSInt width of template argument in FinishCXXExpansionStmt() (#208859)
After merging #169680, we started getting assertions in IntegerLiteral::Create() on some ARM systems: ``` Assertion `V.getBitWidth() == C.getIntWidth(type) && "Integer type is not the correct size for constant."' ``` The expansion statements patch doesn't ever create an IntegerLiteral directly, but there is one place where we create a TemplateArgument of type 'ptrdiff_t', but we unconditionally set the bit width of its APSInt to 64 bits. Presumably, the assertion is due to 'ptrdiff_t' not being a 64-bit type on these systems, so make sure that we query its bit width and use that as the width of the APSInt. I don't have a way of verifying if this patch fixes the CI issue since pre-commit CI doesn't run on the affected system and I don't have an ARM machine, but I'm merging this anyway since this issue is breaking CI and querying the width of 'ptrdiff_t' is something we should be doing here regardless.
1 parent 8e955ee commit ea21481

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

clang/lib/Sema/SemaExpand.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,13 @@ StmtResult Sema::FinishCXXExpansionStmt(Stmt *Exp, Stmt *Body) {
558558
// Expand the body for each instantiation.
559559
SmallVector<Stmt *, 4> Instantiations;
560560
CXXExpansionStmtDecl *ESD = Expansion->getDecl();
561+
QualType PtrDiffT = Context.getPointerDiffType();
562+
unsigned PtrDiffTWidth = Context.getIntWidth(PtrDiffT);
563+
bool PtrDiffTIsUnsigned = PtrDiffT->isUnsignedIntegerType();
561564
for (uint64_t I = 0; I < *NumInstantiations; ++I) {
562-
TemplateArgument Arg{Context, llvm::APSInt::get(I),
563-
Context.getPointerDiffType()};
565+
llvm::APInt IVal{PtrDiffTWidth, I};
566+
TemplateArgument Arg{
567+
Context, llvm::APSInt{std::move(IVal), PtrDiffTIsUnsigned}, PtrDiffT};
564568
MultiLevelTemplateArgumentList MTArgList(ESD, Arg, true);
565569
MTArgList.addOuterRetainedLevels(
566570
Expansion->getDecl()->getIndexTemplateParm()->getDepth());

0 commit comments

Comments
 (0)