fix #9975 (Template method not simplified) (#2894)

This commit is contained in:
IOBYTE 2020-11-10 15:19:04 -05:00 committed by GitHub
parent d7473cab8c
commit e785885b4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -850,7 +850,13 @@ void TemplateSimplifier::getTemplateInstantiations()
// look for function instantiation with type deduction
// fixme: only single argument functions supported
if (tok->strAt(1) == "(") {
std::string fullName = qualification + (qualification.empty() ? "" : " :: ") + tok->str();
std::string fullName;
if (!qualification.empty())
fullName = qualification + " :: " + tok->str();
else if (!scopeName.empty())
fullName = scopeName + " :: " + tok->str();
else
fullName = tok->str();
// get all declarations with this name
for (auto pos = functionNameMap.lower_bound(tok->str());
pos != functionNameMap.upper_bound(tok->str()); ++pos) {

View File

@ -247,6 +247,7 @@ private:
TEST_CASE(templateTypeDeduction1); // #8962
TEST_CASE(templateTypeDeduction2);
TEST_CASE(templateTypeDeduction3);
TEST_CASE(simplifyTemplateArgs1);
TEST_CASE(simplifyTemplateArgs2);
@ -5258,6 +5259,28 @@ private:
TODO_ASSERT_EQUALS(expected, actual, tok(code));
}
void templateTypeDeduction3() { // #9975
const char code[] = "struct A {\n"
" int a = 1;\n"
" void f() { g(1); }\n"
" template <typename T> void g(T x) { a = 2; }\n"
"};\n"
"int main() {\n"
" A a;\n"
" a.f();\n"
"}";
const char exp[] = "struct A { "
"int a ; a = 1 ; "
"void f ( ) { g<int> ( 1 ) ; } "
"void g<int> ( int x ) ; "
"} ; "
"int main ( ) { "
"A a ; "
"a . f ( ) ; "
"} void A :: g<int> ( int x ) { a = 2 ; }";
ASSERT_EQUALS(exp, tok(code));
}
void simplifyTemplateArgs1() {
ASSERT_EQUALS("foo<2> = 2 ; foo<2> ;", tok("template<int N> foo = N; foo < ( 2 ) >;"));
ASSERT_EQUALS("foo<2> = 2 ; foo<2> ;", tok("template<int N> foo = N; foo < 1 + 1 >;"));