diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 85db792fa..472c125c0 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -695,7 +695,7 @@ bool TemplateSimplifier::instantiateMatch(const Token *instance, const std::stri const Token *tok = instance; unsigned int indentlevel = 0; for (tok = instance; tok && (tok->str() != ">" || indentlevel > 0); tok = tok->next()) { - if (Token::Match(tok, "[<,] %name% <") && templateParameters(tok->tokAt(2)) > 0) + if (Token::Match(tok, "<|,|(|:: %name% <") && templateParameters(tok->tokAt(2)) > 0) ++indentlevel; if (indentlevel > 0 && tok->str() == ">") --indentlevel; diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 092dc8e6f..b51f5486b 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -113,6 +113,9 @@ private: TEST_CASE(templateNamePosition); TEST_CASE(expandSpecialized); + + // Test TemplateSimplifier::instantiateMatch + TEST_CASE(instantiateMatch); } std::string tok(const char code[], bool simplify = true, bool debugwarnings = false, Settings::PlatformType type = Settings::Native) { @@ -1473,6 +1476,31 @@ private: ASSERT_EQUALS("class A < int > { } ;", tok("template<> class A {};")); ASSERT_EQUALS("class A < int > : public B { } ;", tok("template<> class A : public B {};")); } + + unsigned int instantiateMatch(const char code[], const std::string& name, const std::size_t numberOfArguments, const char patternAfter[]) { + Tokenizer tokenizer(&settings, this); + + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp", ""); + + return TemplateSimplifier::instantiateMatch(tokenizer.tokens(), name, numberOfArguments, patternAfter); + } + + void instantiateMatch() { + // Ticket #8175 + ASSERT_EQUALS(false, + instantiateMatch("ConvertHelper < From, To > c ;", + "ConvertHelper", 2, ":: %name% (")); + ASSERT_EQUALS(true, + instantiateMatch("ConvertHelper < From, To > :: Create ( ) ;", + "ConvertHelper", 2, ":: %name% (")); + ASSERT_EQUALS(false, + instantiateMatch("integral_constant < bool, sizeof ( ConvertHelper < From, To > :: Create ( ) ) > ;", + "integral_constant", 2, ":: %name% (")); + ASSERT_EQUALS(false, + instantiateMatch("integral_constant < bool, sizeof ( ns :: ConvertHelper < From, To > :: Create ( ) ) > ;", + "integral_constant", 2, ":: %name% (")); + } }; REGISTER_TEST(TestSimplifyTemplate)