Skip to content

Commit 781277c

Browse files
Fix #14477 internalAstError with fold expression in template (#8203)
Co-authored-by: Daniel Marjamäki <daniel.marjamaki@gmail.com>
1 parent 65c94a6 commit 781277c

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

lib/templatesimplifier.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,8 +2104,6 @@ void TemplateSimplifier::expandTemplate(
21042104
Token * const beforeTypeToken = mTokenList.back();
21052105
bool pointerType = false;
21062106
const bool isVariadicTemplateArg = templateDeclaration.isVariadic() && itype + 1 == typeParametersInDeclaration.size();
2107-
if (isVariadicTemplateArg && mTypesUsedInTemplateInstantiation.size() > 1 && !Token::Match(tok3->next(), "...|<"))
2108-
continue;
21092107
if (isVariadicTemplateArg && Token::Match(tok3, "%name% ... %name%"))
21102108
tok3 = tok3->tokAt(2);
21112109
if (!isVariadicTemplateArg && copy && Token::Match(mTypesUsedInTemplateInstantiation[itype].token(), "%num% ,|>|>>") &&
@@ -2130,6 +2128,7 @@ void TemplateSimplifier::expandTemplate(
21302128
}
21312129
}
21322130
const std::string endStr(isVariadicTemplateArg ? ">" : ",>");
2131+
Token* begPar = nullptr;
21332132
for (Token *typetok = mTypesUsedInTemplateInstantiation[itype].token();
21342133
typetok && (typeindentlevel > 0 || endStr.find(typetok->str()[0]) == std::string::npos);
21352134
typetok = typetok->next()) {
@@ -2153,6 +2152,10 @@ void TemplateSimplifier::expandTemplate(
21532152
--typeindentlevel;
21542153
Token *back;
21552154
if (copy) {
2155+
if (isVariadicTemplateArg && typetok == mTypesUsedInTemplateInstantiation[itype].token() && typetok->isLiteral()) {
2156+
mTokenList.addtoken("(", mTokenList.back());
2157+
begPar = mTokenList.back();
2158+
}
21562159
mTokenList.addtoken(typetok, tok3);
21572160
back = mTokenList.back();
21582161
} else
@@ -2181,6 +2184,10 @@ void TemplateSimplifier::expandTemplate(
21812184
if (copy)
21822185
back->templateArgFrom(typetok);
21832186
}
2187+
if (begPar) {
2188+
mTokenList.addtoken(")", mTokenList.back());
2189+
Token::createMutualLinks(begPar, mTokenList.back());
2190+
}
21842191
if (pointerType && Token::simpleMatch(beforeTypeToken, "const")) {
21852192
mTokenList.addtoken(beforeTypeToken);
21862193
beforeTypeToken->deleteThis();

test/testsimplifytemplate.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6372,7 +6372,7 @@ class TestSimplifyTemplate : public TestFixture {
63726372
"class E<1,3> { "
63736373
"template < int ... I > "
63746374
"int f ( int n , std :: integer_sequence < int , I ... > ) { "
6375-
"return ( ( ( I == n ) ? : 0 ) + ... ) ; "
6375+
"return ( ( ( I == n ) ? ( 1 , 3 ) : 0 ) + ... ) ; " // TODO the simplification is not quite correct
63766376
"} "
63776377
"} ;";
63786378
ASSERT_EQUALS(expected, tok(code));
@@ -6390,6 +6390,18 @@ class TestSimplifyTemplate : public TestFixture {
63906390
"A<int,S> a ; "
63916391
"struct A<int,S> { } ;";
63926392
ASSERT_EQUALS(expected2, tok(code2));
6393+
6394+
const char code3[] = "template <int... N>\n" // #14477
6395+
" int f() {\n"
6396+
" return (0 | ... | (1, 2, 4));\n"
6397+
"}\n"
6398+
"int main() {\n"
6399+
" return f<1, 2, 4>();\n"
6400+
"}\n";
6401+
const char expected3[] = "int f<1,2,4> ( ) ; "
6402+
"int main ( ) { return f<1,2,4> ( ) ; } "
6403+
"int f<1,2,4> ( ) { return ( 0 | ... | ( 1 , 2 , 4 ) ) ; }";
6404+
ASSERT_EQUALS(expected3, tok(code3));
63936405
}
63946406

63956407
void template_variable_1() {

0 commit comments

Comments
 (0)