Skip to content

Commit e785885

Browse files
authored
fix #9975 (Template method not simplified) (cppcheck-opensource#2894)
1 parent d7473ca commit e785885

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/templatesimplifier.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,13 @@ void TemplateSimplifier::getTemplateInstantiations()
850850
// look for function instantiation with type deduction
851851
// fixme: only single argument functions supported
852852
if (tok->strAt(1) == "(") {
853-
std::string fullName = qualification + (qualification.empty() ? "" : " :: ") + tok->str();
853+
std::string fullName;
854+
if (!qualification.empty())
855+
fullName = qualification + " :: " + tok->str();
856+
else if (!scopeName.empty())
857+
fullName = scopeName + " :: " + tok->str();
858+
else
859+
fullName = tok->str();
854860
// get all declarations with this name
855861
for (auto pos = functionNameMap.lower_bound(tok->str());
856862
pos != functionNameMap.upper_bound(tok->str()); ++pos) {

test/testsimplifytemplate.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ class TestSimplifyTemplate : public TestFixture {
247247

248248
TEST_CASE(templateTypeDeduction1); // #8962
249249
TEST_CASE(templateTypeDeduction2);
250+
TEST_CASE(templateTypeDeduction3);
250251

251252
TEST_CASE(simplifyTemplateArgs1);
252253
TEST_CASE(simplifyTemplateArgs2);
@@ -5258,6 +5259,28 @@ class TestSimplifyTemplate : public TestFixture {
52585259
TODO_ASSERT_EQUALS(expected, actual, tok(code));
52595260
}
52605261

5262+
void templateTypeDeduction3() { // #9975
5263+
const char code[] = "struct A {\n"
5264+
" int a = 1;\n"
5265+
" void f() { g(1); }\n"
5266+
" template <typename T> void g(T x) { a = 2; }\n"
5267+
"};\n"
5268+
"int main() {\n"
5269+
" A a;\n"
5270+
" a.f();\n"
5271+
"}";
5272+
const char exp[] = "struct A { "
5273+
"int a ; a = 1 ; "
5274+
"void f ( ) { g<int> ( 1 ) ; } "
5275+
"void g<int> ( int x ) ; "
5276+
"} ; "
5277+
"int main ( ) { "
5278+
"A a ; "
5279+
"a . f ( ) ; "
5280+
"} void A :: g<int> ( int x ) { a = 2 ; }";
5281+
ASSERT_EQUALS(exp, tok(code));
5282+
}
5283+
52615284
void simplifyTemplateArgs1() {
52625285
ASSERT_EQUALS("foo<2> = 2 ; foo<2> ;", tok("template<int N> foo = N; foo < ( 2 ) >;"));
52635286
ASSERT_EQUALS("foo<2> = 2 ; foo<2> ;", tok("template<int N> foo = N; foo < 1 + 1 >;"));

0 commit comments

Comments
 (0)