Skip to content

Commit 326d152

Browse files
simartinPKEuS
authored andcommitted
Ticket danmar#7914: Properly recognize template parameters that contains operators. (danmar#870)
1 parent 7de0553 commit 326d152

4 files changed

Lines changed: 35 additions & 41 deletions

File tree

lib/templatesimplifier.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
269269
return 0;
270270

271271
// num/type ..
272-
if (!tok->isNumber() && tok->tokType() != Token::eChar && !tok->isName())
272+
if (!tok->isNumber() && tok->tokType() != Token::eChar && !tok->isName() && !tok->isOp())
273273
return 0;
274274
tok = tok->next();
275275
if (!tok)
@@ -509,6 +509,12 @@ std::list<Token *> TemplateSimplifier::getTemplateInstantiations(Token *tokens)
509509
tok = tok->next()->findClosingBracket();
510510
if (!tok)
511511
break;
512+
// #7914
513+
// Ignore template instantiations within template definitions: they will only be
514+
// handled if the definition is actually instantiated
515+
const Token *tok2 = Token::findmatch(tok, "{|;");
516+
if (tok2 && tok2->str() == "{")
517+
tok = tok2->link();
512518
} else if (Token::Match(tok->previous(), "[({};=] %name% <") ||
513519
Token::Match(tok->previous(), "%type% %name% <") ||
514520
Token::Match(tok->tokAt(-2), "[,:] private|protected|public %name% <")) {

test/testgarbage.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,7 @@ class TestGarbage : public TestFixture {
915915
}
916916

917917
void garbageCode125() {
918-
ASSERT_THROW(checkCode("{ T struct B : T valueA_AA ; } T : [ T > ( ) { B } template < T > struct A < > : ] { ( ) { return valueA_AC struct { : } } b A < int > AC ( ) a_aa.M ; ( ) ( ) }"),
919-
InternalError);
918+
checkCode("{ T struct B : T valueA_AA ; } T : [ T > ( ) { B } template < T > struct A < > : ] { ( ) { return valueA_AC struct { : } } b A < int > AC ( ) a_aa.M ; ( ) ( ) }");
920919
ASSERT_THROW(checkCode("template < Types > struct S :{ ( S < ) S >} { ( ) { } } ( ) { return S < void > ( ) }"),
921920
InternalError);
922921
}

test/testsimplifytemplate.cpp

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ class TestSimplifyTemplate : public TestFixture {
310310
// The expected result..
311311
const char expected[] = "void f ( ) { A < int > a ; } "
312312
"template < typename T > class B { void g ( ) { A < T > b ; b = A < T > :: h ( ) ; } } ; "
313-
"class A < int > { } ; class A < T > { } ;";
313+
"class A < int > { } ;";
314314

315315
ASSERT_EQUALS(expected, tok(code));
316316
}
@@ -473,7 +473,6 @@ class TestSimplifyTemplate : public TestFixture {
473473

474474
const char expected[] = "int main ( ) { b < 2 > ( ) ; return 0 ; } "
475475
"void b < 2 > ( ) { a < 2 > ( ) ; } "
476-
"void a < i > ( ) { } "
477476
"void a < 2 > ( ) { }";
478477

479478
ASSERT_EQUALS(expected, tok(code));
@@ -722,7 +721,6 @@ class TestSimplifyTemplate : public TestFixture {
722721
ASSERT_EQUALS("C < int > c ; "
723722
"struct C < int > { A < B < X < int > > > ab ; } ; "
724723
"struct B < X < int > > { } ; " // <- redundant.. but nevermind
725-
"struct A < B < X < T > > > { } ; " // <- redundant.. but nevermind
726724
"struct A < B < X < int > > > { } ;", tok(code));
727725
}
728726

@@ -912,11 +910,12 @@ class TestSimplifyTemplate : public TestFixture {
912910
"template < class T > void Fred < T > :: g ( ) { } "
913911
"template void Fred < float > :: f ( ) ; "
914912
"template void Fred < int > :: g ( ) ; "
915-
"class Fred < T > { void f ( ) ; void g ( ) ; } ; "
916-
"Fred < T > :: f ( ) { } "
917-
"Fred < T > :: g ( ) { } "
918913
"class Fred < float > { void f ( ) ; void g ( ) ; } ; "
919-
"class Fred < int > { void f ( ) ; void g ( ) ; } ;";
914+
"Fred < float > :: f ( ) { } "
915+
"Fred < float > :: g ( ) { } "
916+
"class Fred < int > { void f ( ) ; void g ( ) ; } ; "
917+
"Fred < int > :: f ( ) { } "
918+
"Fred < int > :: g ( ) { }";
920919

921920
ASSERT_EQUALS(expected, tok(code));
922921
}
@@ -927,13 +926,10 @@ class TestSimplifyTemplate : public TestFixture {
927926
"template<> void Fred<float>::f() { }\n"
928927
"template<> void Fred<int>::g() { }\n";
929928

930-
const char expected[] = "template < class T > void Fred < T > :: f ( ) { } "
929+
const char expected[] = "template < class T > class Fred { void f ( ) ; } ; "
930+
"template < class T > void Fred < T > :: f ( ) { } "
931931
"template < > void Fred < float > :: f ( ) { } "
932-
"template < > void Fred < int > :: g ( ) { } "
933-
"class Fred < T > { void f ( ) ; } ; "
934-
"Fred < T > :: f ( ) { } "
935-
"class Fred < float > { void f ( ) ; } ; "
936-
"class Fred < int > { void f ( ) ; } ;";
932+
"template < > void Fred < int > :: g ( ) { }";
937933

938934
ASSERT_EQUALS(expected, tok(code));
939935
}
@@ -982,13 +978,10 @@ class TestSimplifyTemplate : public TestFixture {
982978
void template55() { // #6604
983979
// Avoid constconstconst in macro instantiations
984980
ASSERT_EQUALS(
985-
"class AtSmartPtr < T > : public ConstCastHelper < AtSmartPtr < const T > , T > { "
981+
"template < class T > class AtSmartPtr : public ConstCastHelper < AtSmartPtr < const T > , T > { "
986982
"friend struct ConstCastHelper < AtSmartPtr < const T > , T > ; "
987-
"AtSmartPtr < T > ( const AtSmartPtr < T > & r ) ; "
988-
"} ; "
989-
"class AtSmartPtr < const T > : public ConstCastHelper < AtSmartPtr < const const T > , const T > { "
990-
"friend struct ConstCastHelper < AtSmartPtr < const const T > , const T > ; "
991-
"AtSmartPtr < const T > ( const AtSmartPtr < T > & r ) ; } ;",
983+
"AtSmartPtr ( const AtSmartPtr < T > & r ) ; "
984+
"} ;",
992985
tok("template<class T> class AtSmartPtr : public ConstCastHelper<AtSmartPtr<const T>, T>\n"
993986
"{\n"
994987
" friend struct ConstCastHelper<AtSmartPtr<const T>, T>;\n"
@@ -1062,17 +1055,12 @@ class TestSimplifyTemplate : public TestFixture {
10621055
" enum {value = !type_equal<T, typename Unconst<T>::type>::value };\n"
10631056
"};";
10641057
const char exp1[] = "template < class T > struct Unconst { } ; "
1058+
"template < class T > struct Unconst < const T > { } ; "
1059+
"template < class T > struct Unconst < const T & > { } ; "
1060+
"template < class T > struct Unconst < T * const > { } ; "
1061+
"template < class T1 , class T2 > struct type_equal { enum Anonymous0 { value = 0 } ; } ; "
10651062
"template < class T > struct type_equal < T , T > { enum Anonymous1 { value = 1 } ; } ; "
1066-
"template < class T > struct template_is_const { enum Anonymous2 { value = ! type_equal < T , Unconst < T > :: type > :: value } ; } ; "
1067-
"struct type_equal < T , T > { enum Anonymous0 { value = 0 } ; } ; "
1068-
"struct Unconst < const T * const > { } ; "
1069-
"struct Unconst < const T & * const > { } ; "
1070-
"struct Unconst < T * const * const > { } ; "
1071-
"struct Unconst < T * const > { } ; "
1072-
"struct Unconst < T * const > { } ; "
1073-
"struct Unconst < T * const > { } ; "
1074-
"struct Unconst < const T & > { } ; "
1075-
"struct Unconst < const T > { } ;";
1063+
"template < class T > struct template_is_const { enum Anonymous2 { value = ! type_equal < T , Unconst < T > :: type > :: value } ; } ;";
10761064
ASSERT_EQUALS(exp1, tok(code1));
10771065
}
10781066

@@ -1346,6 +1334,10 @@ class TestSimplifyTemplate : public TestFixture {
13461334
ASSERT_EQUALS(3U, templateParameters("X<char, int(*)(), bool> x;"));
13471335
TODO_ASSERT_EQUALS(1U, 0U, templateParameters("X<int...> x;")); // Mishandled valid syntax
13481336
TODO_ASSERT_EQUALS(2U, 0U, templateParameters("X<class, typename...> x;")); // Mishandled valid syntax
1337+
ASSERT_EQUALS(2U, templateParameters("X<1, T> x;"));
1338+
ASSERT_EQUALS(1U, templateParameters("X<i == 0> x;"));
1339+
ASSERT_EQUALS(2U, templateParameters("X<int, i>=0> x;"));
1340+
ASSERT_EQUALS(3U, templateParameters("X<int, i>=0, i - 2> x;"));
13491341
}
13501342

13511343
// Helper function to unit test TemplateSimplifier::getTemplateNamePosition

test/testsimplifytokens.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,16 +2500,13 @@ class TestSimplifyTokens : public TestFixture {
25002500

25012501
void simplifyOperator2() {
25022502
// #6576
2503-
ASSERT_EQUALS("class TClass { "
2504-
"public: "
2505-
"TClass & operator= ( const TClass & rhs ) ; "
2503+
ASSERT_EQUALS("template < class T > class SharedPtr { "
2504+
"SharedPtr & operator= ( SharedPtr < Y > const & r ) ; "
25062505
"} ; "
2507-
"TClass :: TClass ( const TClass & other ) "
2508-
"{ "
2509-
"operator= ( other ) ; "
2510-
"} class SharedPtr < Y > { "
2511-
"SharedPtr < Y > & operator= ( SharedPtr < Y > const & r ) ; "
2512-
"} ;",
2506+
"class TClass { "
2507+
"public: TClass & operator= ( const TClass & rhs ) ; "
2508+
"} ; "
2509+
"TClass :: TClass ( const TClass & other ) { operator= ( other ) ; }",
25132510
tok("template<class T>\n"
25142511
" class SharedPtr {\n"
25152512
" SharedPtr& operator=(SharedPtr<Y> const & r);\n"

0 commit comments

Comments
 (0)