From be174d6266d2317362e1bae7d731e15f94772c43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 14 Oct 2012 19:48:53 +0200 Subject: [PATCH] TemplateSimplifier: Fixed bad pattern that was detected by CheckInternal and pointed out by edward-san. The handling of recursive templates is better now. --- lib/templatesimplifier.cpp | 33 ++++++++++++++++++++------------- test/testsimplifytokens.cpp | 8 ++++---- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 1ab94fb75..76f965511 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1039,7 +1039,7 @@ bool TemplateSimplifier::simplifyTemplateInstantions( } if (tok3->str() == "<" && templateParameters(tok3) > 0) ++indentlevel; - else if (indentlevel > 0 && Token::simpleMatch(tok3, "> [,>]")) + else if (indentlevel > 0 && Token::Match(tok3, "> [,>]")) --indentlevel; templateMatchPattern += tok3->str(); templateMatchPattern += " "; @@ -1084,19 +1084,26 @@ bool TemplateSimplifier::simplifyTemplateInstantions( Token * tok5 = tok4->tokAt(2); unsigned int typeCountInInstantion = 1U; // There is always atleast one type const Token *typetok = (!typesUsedInTemplateInstantion.empty()) ? typesUsedInTemplateInstantion[0] : 0; - while (tok5 && tok5->str() != ">") { - if (tok5->str() != ",") { - if (!typetok || - tok5->isUnsigned() != typetok->isUnsigned() || - tok5->isSigned() != typetok->isSigned() || - tok5->isLong() != typetok->isLong()) { - break; - } + unsigned int indentlevel5 = 0; // indentlevel for tok5 + while (tok5 && (indentlevel5 > 0 || tok5->str() != ">")) { + if (tok5->str() == "<" && templateParameters(tok5) > 0) + ++indentlevel5; + else if (indentlevel5 > 0 && Token::Match(tok5, "> [,>]")) + --indentlevel5; + else if (indentlevel5 == 0) { + if (tok5->str() != ",") { + if (!typetok || + tok5->isUnsigned() != typetok->isUnsigned() || + tok5->isSigned() != typetok->isSigned() || + tok5->isLong() != typetok->isLong()) { + break; + } - typetok = typetok ? typetok->next() : 0; - } else { - typetok = (typeCountInInstantion < typesUsedInTemplateInstantion.size()) ? typesUsedInTemplateInstantion[typeCountInInstantion] : 0; - ++typeCountInInstantion; + typetok = typetok ? typetok->next() : 0; + } else { + typetok = (typeCountInInstantion < typesUsedInTemplateInstantion.size()) ? typesUsedInTemplateInstantion[typeCountInInstantion] : 0; + ++typeCountInInstantion; + } } tok5 = tok5->next(); } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 45a13a706..4c716ef7b 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -2201,10 +2201,10 @@ private: "template struct B { };\n" "template struct C { A > > ab; };\n" "C c;"; - ASSERT_EQUALS("template < class T > struct A { } ; " - "template < class T > struct B { } ; " - "C c ; " - "struct C { A < B < X < int > > > ab ; }", tok(code)); + ASSERT_EQUALS("C c ; " + "struct C { A>> ab ; } " + "struct B> { } " // <- redundant.. but nevermind + "struct A>> { }", tok(code)); } void template34() {