fix template namespace bug (#2780)

Co-authored-by: Robert Reif <reif@FX6840>
This commit is contained in:
IOBYTE 2020-09-07 11:45:19 -04:00 committed by GitHub
parent bb46083a8c
commit 2106e30304
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 3 deletions

View File

@ -843,9 +843,7 @@ void TemplateSimplifier::getTemplateInstantiations()
std::string qualification;
Token * qualificationTok = tok;
while (Token::Match(tok, "%name% :: %name%")) {
// ignore redundant namespaces
if (scopeName.find(tok->str()) == std::string::npos)
qualification += (qualification.empty() ? "" : " :: ") + tok->str();
qualification += (qualification.empty() ? "" : " :: ") + tok->str();
tok = tok->tokAt(2);
}

View File

@ -2090,24 +2090,66 @@ private:
" template<typename T> T foo(T t) { return t; }\n"
" template<> char foo<char>(char a) { return a; }\n"
" template<> int foo<int>(int a) { return a; }\n"
" template short NS2::foo<short>(short);\n"
" template long NS1::NS2::foo<long>(long);\n"
" }\n"
" template float NS2::foo<float>(float);\n"
" template bool NS1::NS2::foo<bool>(bool);\n"
"}\n"
"template double NS1::NS2::foo<double>(double);";
const char exp[] = "namespace NS1 { "
"namespace NS2 { "
"int foo<int> ( int a ) ; "
"char foo<char> ( char a ) ; "
"short foo<short> ( short t ) ; "
"long foo<long> ( long t ) ; "
"float foo<float> ( float t ) ; "
"bool foo<bool> ( bool t ) ; "
"double foo<double> ( double t ) ; "
"char foo<char> ( char a ) { return a ; } "
"int foo<int> ( int a ) { return a ; } "
"} "
"} "
"short NS1 :: NS2 :: foo<short> ( short t ) { return t ; } "
"long NS1 :: NS2 :: foo<long> ( long t ) { return t ; } "
"float NS1 :: NS2 :: foo<float> ( float t ) { return t ; } "
"bool NS1 :: NS2 :: foo<bool> ( bool t ) { return t ; } "
"double NS1 :: NS2 :: foo<double> ( double t ) { return t ; }";
ASSERT_EQUALS(exp, tok(code));
}
{
const char code[] = "namespace NS1 {\n"
" namespace NS {\n"
" template<typename T> T foo(T t) { return t; }\n"
" template<> char foo<char>(char a) { return a; }\n"
" template<> int foo<int>(int a) { return a; }\n"
" template short NS::foo<short>(short);\n"
" template long NS1::NS::foo<long>(long);\n"
" }\n"
" template float NS::foo<float>(float);\n"
" template bool NS1::NS::foo<bool>(bool);\n"
"}\n"
"template double NS1::NS::foo<double>(double);";
const char exp[] = "namespace NS1 { "
"namespace NS { "
"int foo<int> ( int a ) ; "
"char foo<char> ( char a ) ; "
"short foo<short> ( short t ) ; "
"long foo<long> ( long t ) ; "
"float foo<float> ( float t ) ; "
"bool foo<bool> ( bool t ) ; "
"double foo<double> ( double t ) ; "
"char foo<char> ( char a ) { return a ; } "
"int foo<int> ( int a ) { return a ; } "
"} "
"} "
"short NS1 :: NS :: foo<short> ( short t ) { return t ; } "
"long NS1 :: NS :: foo<long> ( long t ) { return t ; } "
"float NS1 :: NS :: foo<float> ( float t ) { return t ; } "
"bool NS1 :: NS :: foo<bool> ( bool t ) { return t ; } "
"double NS1 :: NS :: foo<double> ( double t ) { return t ; }";
ASSERT_EQUALS(exp, tok(code));
}
}
void template92() {