@@ -38,6 +38,7 @@ class TestSimplifyTemplate : public TestFixture {
3838
3939 void run () OVERRIDE {
4040 settings.addEnabled (" portability" );
41+ settings.checkUnusedTemplates = true ;
4142
4243 TEST_CASE (template1);
4344 TEST_CASE (template2);
@@ -191,6 +192,8 @@ class TestSimplifyTemplate : public TestFixture {
191192 TEST_CASE (templateTypeDeduction2);
192193
193194 TEST_CASE (simplifyTemplateArgs);
195+
196+ TEST_CASE (removeTemplates);
194197 }
195198
196199 std::string tok (const char code[], bool debugwarnings = false , Settings::PlatformType type = Settings::Native) {
@@ -386,7 +389,8 @@ class TestSimplifyTemplate : public TestFixture {
386389
387390 // The expected result..
388391 const char expected[] = " class A<int> ; "
389- " void f ( ) { A<int> a ; } ; "
392+ " void f ( ) { A<int> a ; } "
393+ " template < typename T > class B { void g ( ) { A < T > b ; b = A < T > :: h ( ) ; } } ; "
390394 " class A<int> { } ;" ;
391395
392396 ASSERT_EQUALS (expected, tok (code));
@@ -1385,6 +1389,7 @@ class TestSimplifyTemplate : public TestFixture {
13851389 " template <class T, unsigned S> C3<T, S>::C3(const C3<T, S> &v) { C1<T *> c1; }\n "
13861390 " C3<int,6> c3;" ;
13871391 const char exp[] = " struct C1<int*> ; "
1392+ " template < class T > void f ( ) { x = y ? ( C1 < int > :: allocate ( 1 ) ) : 0 ; } "
13881393 " class C3<int,6> ; "
13891394 " C3<int,6> c3 ; "
13901395 " class C3<int,6> { } ; "
@@ -2392,6 +2397,7 @@ class TestSimplifyTemplate : public TestFixture {
23922397 " template <typename T> class Fred {};\n "
23932398 " ObjectCache<Fred> _cache;" ;
23942399 const char exp[] = " class ObjectCache<Fred> ; "
2400+ " template < typename T > class Fred { } ; "
23952401 " ObjectCache<Fred> _cache ; "
23962402 " class ObjectCache<Fred> { } ;" ;
23972403 ASSERT_EQUALS (exp, tok (code));
@@ -2499,7 +2505,8 @@ class TestSimplifyTemplate : public TestFixture {
24992505 " template < class T > struct Unconst < const T & > { } ; "
25002506 " template < class T > struct Unconst < T * const > { } ; "
25012507 " template < class T1 , class T2 > struct type_equal { enum Anonymous0 { value = 0 } ; } ; "
2502- " template < class T > struct type_equal < T , T > { enum Anonymous1 { value = 1 } ; } ;" ;
2508+ " template < class T > struct type_equal < T , T > { enum Anonymous1 { value = 1 } ; } ; "
2509+ " template < class T > struct template_is_const { enum Anonymous2 { value = ! type_equal < T , Unconst < T > :: type > :: value } ; } ;" ;
25032510 ASSERT_EQUALS (exp1, tok (code1));
25042511 }
25052512
@@ -2739,7 +2746,7 @@ class TestSimplifyTemplate : public TestFixture {
27392746 const char code[] = " class Fred {\n "
27402747 " template<class T> explicit Fred(T t) { }\n "
27412748 " }" ;
2742- ASSERT_EQUALS (" class Fred { }" , tok (code));
2749+ ASSERT_EQUALS (" class Fred { template < class T > explicit Fred ( T t ) { } }" , tok (code));
27432750
27442751 // #3532
27452752 const char code2[] = " class Fred {\n "
@@ -3562,6 +3569,26 @@ class TestSimplifyTemplate : public TestFixture {
35623569 ASSERT_EQUALS (" foo<false> = false ; foo<false> ;" , tok (" template<bool N> foo = N; foo < ( 1 - 1) ? true : false >;" ));
35633570 }
35643571
3572+ std::string removeTemplates (const char code[]) {
3573+ Settings settings2;
3574+ settings2.checkUnusedTemplates = false ;
3575+
3576+ errout.str (" " );
3577+
3578+ Tokenizer tokenizer (&settings2, this );
3579+
3580+ std::istringstream istr (code);
3581+ tokenizer.tokenize (istr, " test.cpp" );
3582+
3583+ return tokenizer.tokens ()->stringifyList (0 , true );
3584+ }
3585+
3586+ void removeTemplates () {
3587+ // #9057
3588+ ASSERT_EQUALS (" ;" , removeTemplates (" template <typename T> using foo = enable_if_t<is_same<T, int>() && sizeof(int) == 4, int>;\n "
3589+ " template <typename T, typename U = foo<T>> struct bar {};\n "
3590+ " template <typename T> using baz = bar<T>;\n " ));
3591+ }
35653592};
35663593
35673594REGISTER_TEST (TestSimplifyTemplate)
0 commit comments