diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp --- a/clang/lib/AST/ItaniumMangle.cpp +++ b/clang/lib/AST/ItaniumMangle.cpp @@ -3204,7 +3204,11 @@ } void CXXNameMangler::mangleType(const DependentSizedArrayType *T) { Out << 'A'; - mangleExpression(T->getSizeExpr()); + // A DependentSizedArrayType might not have size expression as below + // + // template int arr[] = {N...}; + if (T->getSizeExpr()) + mangleExpression(T->getSizeExpr()); Out << '_'; mangleType(T->getElementType()); } diff --git a/clang/unittests/AST/DeclTest.cpp b/clang/unittests/AST/DeclTest.cpp --- a/clang/unittests/AST/DeclTest.cpp +++ b/clang/unittests/AST/DeclTest.cpp @@ -104,3 +104,37 @@ ASSERT_TRUE(0 == MangleF.compare("\x01" "foo")); ASSERT_TRUE(0 == MangleG.compare("goo")); } + +TEST(Decl, MangleDependentSizedArray) { + StringRef Code = R"( + template + int A[] = {N...}; + + template + struct S { + T B[N]; + }; + )"; + auto AST = + tooling::buildASTFromCodeWithArgs(Code, {"-target", "i386-apple-darwin"}); + ASTContext &Ctx = AST->getASTContext(); + assert(Ctx.getTargetInfo().getDataLayout().getGlobalPrefix() && + "Expected target to have a global prefix"); + DiagnosticsEngine &Diags = AST->getDiagnostics(); + + const auto *DeclA = + selectFirst("A", match(varDecl().bind("A"), Ctx)); + const auto *DeclB = + selectFirst("B", match(fieldDecl().bind("B"), Ctx)); + + std::string MangleA, MangleB; + llvm::raw_string_ostream OS_A(MangleA), OS_B(MangleB); + std::unique_ptr MC( + ItaniumMangleContext::create(Ctx, Diags)); + + MC->mangleTypeName(DeclA->getType(), OS_A); + MC->mangleTypeName(DeclB->getType(), OS_B); + + ASSERT_TRUE(0 == MangleA.compare("_ZTSA_i")); + ASSERT_TRUE(0 == MangleB.compare("_ZTSAT0__T_")); +}