Skip to content

Commit

Permalink
Merge pull request #653 from andreasfertig/fixIssue595
Browse files Browse the repository at this point in the history
Fixed #595: Pack expansion in primary template.
  • Loading branch information
andreasfertig authored Jun 28, 2024
2 parents f865d81 + faadfbc commit e473634
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
35 changes: 24 additions & 11 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,14 @@ void CodeGenerator::EndLifetimeScope()
}
//-----------------------------------------------------------------------------

// In a primary template we can see a ParenListExpr with a PackExpansionExpr. With the equal sign we need a type.
static bool IsPrimaryTemplatePackExpansionExpr(const ParenListExpr* stmt)
{
return stmt and stmt->getNumExprs() and isa_and_nonnull<PackExpansionExpr>(stmt->getExpr(0)) and
stmt->getType().isNull();
}
//-----------------------------------------------------------------------------

void CodeGenerator::InsertArg(const LinkageSpecDecl* stmt)
{
mOutputFormatHelper.Append("extern \"",
Expand Down Expand Up @@ -1316,22 +1324,27 @@ void CodeGenerator::InsertArg(const VarDecl* stmt)
if(not(ctorExpr and ctorExpr->getConstructor()->isDefaultConstructor() and
ctorExpr->getConstructor()->getParent()->hasTrivialDefaultConstructor())) {

if(not isa<CXXParenListInitExpr>(init)) {
const bool isPrimaryTemplatePackExpansionExpr{
IsPrimaryTemplatePackExpansionExpr(dyn_cast_or_null<ParenListExpr>(init))};

if(not isa<CXXParenListInitExpr>(init) and not isPrimaryTemplatePackExpansionExpr) {
mOutputFormatHelper.Append(hlpAssing);
}

if(GetInsightsOptions().ShowLifetime and init->isXValue() and
stmt->getType()->isRValueReferenceType()) {
WrapInParensIfNeeded(isPrimaryTemplatePackExpansionExpr, [&] {
if(GetInsightsOptions().ShowLifetime and init->isXValue() and
stmt->getType()->isRValueReferenceType()) {

if(GetInsightsOptions().UseShow2C) {
mOutputFormatHelper.Append("&");
}
if(GetInsightsOptions().UseShow2C) {
mOutputFormatHelper.Append("&");
}

InsertArg(StaticCast(stmt->getType(), init, false));
InsertArg(StaticCast(stmt->getType(), init, false));

} else {
InsertArg(init);
}
} else {
InsertArg(init);
}
});
}
}
}
Expand Down Expand Up @@ -2583,7 +2596,7 @@ void CodeGenerator::InsertArg(const MaterializeTemporaryExpr* stmt)
{
// At least in case of a ternary operator wrapped inside a MaterializeTemporaryExpr parens are necessary
const auto* temporary = stmt->getSubExpr();
WrapInParensIfNeeded(isa<ConditionalOperator>(temporary), [&] { InsertArg(temporary); });
WrapInParensIfNeeded(isa_and_nonnull<ConditionalOperator>(temporary), [&] { InsertArg(temporary); });
}
//-----------------------------------------------------------------------------

Expand Down
13 changes: 13 additions & 0 deletions tests/Issue595.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <tuple>

template<typename... C>
void
test(C...c)
{
std::tuple<C...> tpl(c...);
}

int main()
{
test(3, 5.5, 'a');
}
23 changes: 23 additions & 0 deletions tests/Issue595.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <tuple>

template<typename ... C>
void test(C... c)
{
std::tuple<C...> tpl(c... );
}

/* First instantiated from: Issue595.cpp:12 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
void test<int, double, char>(int __c0, double __c1, char __c2)
{
std::tuple<int, double, char> tpl = std::tuple<int, double, char>(__c0, __c1, __c2);
}
#endif


int main()
{
test(3, 5.5, 'a');
return 0;
}

0 comments on commit e473634

Please sign in to comment.