Ticket #7942: Don't generate invalid syntax for out of line template class constructors during instantiation.

This commit is contained in:
Simon Martin 2017-04-09 16:01:07 +02:00
parent f5f141ff42
commit ee05fe7d77
3 changed files with 18 additions and 3 deletions

View File

@ -818,8 +818,12 @@ void TemplateSimplifier::expandTemplate(
// replace name..
if (Token::Match(tok3, (name + " !!<").c_str())) {
tokenlist.addtoken(newName, tok3->linenr(), tok3->fileIndex());
continue;
if (Token::Match(tok3->tokAt(-2), "> :: %name% ( )")) {
; // Ticket #7942: Replacing for out-of-line constructors generates invalid syntax
} else {
tokenlist.addtoken(newName, tok3->linenr(), tok3->fileIndex());
continue;
}
}
// copy

View File

@ -183,6 +183,7 @@ private:
TEST_CASE(uninitConstVar);
TEST_CASE(constructors_crash1); // ticket #5641
TEST_CASE(classWithOperatorInName);// ticket #2827
TEST_CASE(templateConstructor); // ticket #7942
}
@ -3348,6 +3349,16 @@ private:
"};");
ASSERT_EQUALS("", errout.str());
}
void templateConstructor() { // ticket #7942
check("template <class T> struct Containter {\n"
" Containter();\n"
" T* mElements;\n"
"};\n"
"template <class T> Containter<T>::Containter() : mElements(nullptr) {}\n"
"Containter<int> intContainer;");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestConstructors)

View File

@ -184,7 +184,7 @@ private:
const char expected[] = "template < class T > Fred < T > :: Fred ( ) { } " // <- TODO: this should be removed
"Fred < float > fred ; "
"class Fred < float > { } ; "
"Fred < float > :: Fred < float > ( ) { }";
"Fred < float > :: Fred ( ) { }";
ASSERT_EQUALS(expected, tok(code));
}