TemplateSimplifier: Fix crash seen in Travis

This commit is contained in:
Daniel Marjamäki 2017-12-23 17:29:28 +01:00
parent 2d7fedbb49
commit d237d36d46
2 changed files with 24 additions and 6 deletions

View File

@ -1431,11 +1431,9 @@ void TemplateSimplifier::replaceTemplateUsage(Token * const instantiationToken,
if (!Token::simpleMatch(nameTok->next(), templateParametersMatchPattern.c_str())) if (!Token::simpleMatch(nameTok->next(), templateParametersMatchPattern.c_str()))
continue; continue;
std::string fullName(nameTok->str()); // FIXME Proper name matching
for (const Token *tok = nameTok->tokAt(-2); Token::Match(tok, "%name% ::"); tok = tok->tokAt(-2)) const std::string lastName(templateName.find(" ") == std::string::npos ? templateName : templateName.substr(templateName.rfind(" ") + 1));
fullName = tok->str() + "::" + fullName; if (lastName != nameTok->str())
fullName = getFullName(scopeInfo, fullName);
if (fullName != templateName)
continue; continue;
// match parameters // match parameters

View File

@ -108,6 +108,7 @@ private:
TEST_CASE(template_namespace_1); TEST_CASE(template_namespace_1);
TEST_CASE(template_namespace_2); TEST_CASE(template_namespace_2);
TEST_CASE(template_namespace_3); TEST_CASE(template_namespace_3);
TEST_CASE(template_namespace_4);
// Test TemplateSimplifier::templateParameters // Test TemplateSimplifier::templateParameters
TEST_CASE(templateParameters); TEST_CASE(templateParameters);
@ -1410,7 +1411,7 @@ private:
"}"; "}";
ASSERT_EQUALS("namespace test16 {" ASSERT_EQUALS("namespace test16 {"
" void * test ( ) {" " void * test ( ) {"
" return foo < int > :: bar ( ) ;" " return test16 :: foo < int > :: bar ( ) ;"
" } " " } "
"} " "} "
"struct test16 :: foo < int > {" "struct test16 :: foo < int > {"
@ -1418,6 +1419,25 @@ private:
"} ;", tok(code)); "} ;", tok(code));
} }
void template_namespace_4() {
const char code[] = "namespace foo {\n"
" template<class T> class A { void dostuff() {} };\n"
" struct S : public A<int> {\n"
" void f() {\n"
" A<int>::dostuff();\n"
" }\n"
" };\n"
"}";
ASSERT_EQUALS("namespace foo {"
" struct S : public foo :: A < int > {"
" void f ( ) {"
" foo :: A < int > :: dostuff ( ) ;"
" }"
" } ; "
"} "
"class foo :: A < int > { void dostuff ( ) { } } ;", tok(code));
}
unsigned int templateParameters(const char code[]) { unsigned int templateParameters(const char code[]) {
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);