Visitar URL original
TemplateSimplifier: One more fix for #3130. Restored max recursion co… · ProgramMax/cppcheck@f3f9ea2 · GitHub
Skip to content

Commit f3f9ea2

Browse files
committed
TemplateSimplifier: One more fix for cppcheck-opensource#3130. Restored max recursion count to 100 and improve pattern for reference template instantiations instead.
1 parent f116de6 commit f3f9ea2

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

‎lib/templatesimplifier.cpp‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -815,9 +815,7 @@ void TemplateSimplifier::expandTemplate(
815815

816816
// copy
817817
tokenlist.addtoken(tok3, tok3->linenr(), tok3->fileIndex());
818-
if (Token::Match(tok3, "%type% <")) {
819-
//if (!Token::simpleMatch(tok3, (name + " <").c_str()))
820-
//done = false;
818+
if (Token::Match(tok3, "%type% <") && Token::Match(tok3->next()->findClosingBracket(), ">|>> !!&")) {
821819
templateInstantiations.push_back(tokenlist.back());
822820
}
823821

@@ -1268,7 +1266,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
12681266
amountOftemplateInstantiations = templateInstantiations.size();
12691267
simplifyCalculations(tokenlist.front());
12701268
++recursiveCount;
1271-
if (recursiveCount > 1) {
1269+
if (recursiveCount > 100) {
12721270
// bail out..
12731271
break;
12741272
}

‎test/testgarbage.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ class TestGarbage : public TestFixture {
875875
}
876876

877877
void garbageCode104() { // #6847
878-
ASSERT_THROW(checkCode("template < Types > struct S {> ( S < ) S >} { ( ) { } } ( ) { return S < void > ( ) } { ( )> >} { ( ) { } } ( ) { ( ) }"), InternalError);
878+
checkCode("template < Types > struct S {> ( S < ) S >} { ( ) { } } ( ) { return S < void > ( ) } { ( )> >} { ( ) { } } ( ) { ( ) }");
879879
}
880880

881881
void garbageCode105() { // #6859

‎test/testsimplifytemplate.cpp‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TestSimplifyTemplate : public TestFixture {
4848
TEST_CASE(template12);
4949
TEST_CASE(template13);
5050
TEST_CASE(template14);
51-
TEST_CASE(template15);
51+
TEST_CASE(template15); // recursive templates
5252
TEST_CASE(template16);
5353
TEST_CASE(template17);
5454
TEST_CASE(template18);
@@ -416,7 +416,7 @@ class TestSimplifyTemplate : public TestFixture {
416416
ASSERT_EQUALS(expected, tok(code));
417417
}
418418

419-
void template15() {
419+
void template15() { // recursive templates #3130 etc
420420
const char code[] = "template <unsigned int i> void a()\n"
421421
"{\n"
422422
" a<i-1>();\n"
@@ -436,9 +436,24 @@ class TestSimplifyTemplate : public TestFixture {
436436
"int main ( ) "
437437
"{ a<2> ( ) ; return 0 ; } "
438438
"void a<2> ( ) { a<1> ( ) ; } "
439-
"void a<1> ( ) { a < 0 > ( ) ; }";
439+
"void a<1> ( ) { a<0> ( ) ; }";
440440

441441
ASSERT_EQUALS(expected, tok(code));
442+
443+
// #3130
444+
const char code2[] = "template <int n> struct vec {\n"
445+
" vec() {}\n"
446+
" vec(const vec<n-1>& v) {}\n" // <- never used dont instantiate
447+
"};\n"
448+
"\n"
449+
"vec<4> v;";
450+
const char expected2[] = "vec<4> v ; "
451+
"struct vec<4> { "
452+
"vec<4> ( ) { } "
453+
"vec<4> ( const vec < 4 - 1 > & v ) { } "
454+
"} ;";
455+
456+
ASSERT_EQUALS(expected2, tok(code2));
442457
}
443458

444459
void template16() {
@@ -1022,7 +1037,7 @@ class TestSimplifyTemplate : public TestFixture {
10221037
"{\n"
10231038
" enum {value = !type_equal<T, typename Unconst<T>::type>::value };\n"
10241039
"};";
1025-
const char expected1[]="template < class T > struct type_equal<T,T> { } ; template < class T > struct template_is_const { } ; struct type_equal<T,T> { } ; struct Unconst<constT*const> { } ; struct Unconst<constT&*const> { } ; struct Unconst<constT&><};template<T> { } ; struct Unconst<constT><};template<T> { } ; struct Unconst<T*const> { } ; struct Unconst<constT*const> { } ; struct Unconst<constT&*const> { } ;";
1040+
const char expected1[]="template < class T > struct Unconst { } ; template < class T > struct type_equal<T,T> { } ; template < class T > struct template_is_const { } ; struct type_equal<T,T> { } ; struct Unconst<constT*const> { } ; struct Unconst<constT&*const> { } ; struct Unconst<T*const*const> { } ; struct Unconst<T*const> { } ; struct Unconst<T*const> { } ; struct Unconst<T*const> { } ; struct Unconst<constT&><};template<T> { } ; struct Unconst<constT><};template<T> { } ;";
10261041
ASSERT_EQUALS(expected1, tok(code1));
10271042
}
10281043

0 commit comments

Comments
 (0)