Skip to content

Commit f88ae21

Browse files
IOBYTEamai2012
authored andcommitted
Fix #9467 (False positive on local variable when template specialization is used) (cppcheck-opensource#2357)
1 parent da163ae commit f88ae21

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

lib/templatesimplifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,7 @@ void TemplateSimplifier::expandTemplate(
20432043
addNamespace(templateDeclaration, tok3);
20442044
}
20452045
mTokenList.addtoken(newName, tok3);
2046-
} else if (!Token::Match(tok3->next(), ":|{|="))
2046+
} else if (!Token::Match(tok3->next(), ":|{|=|;"))
20472047
tok3->str(newName);
20482048
continue;
20492049
}

test/testsimplifytemplate.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class TestSimplifyTemplate : public TestFixture {
189189
TEST_CASE(template149); // unknown macro
190190
TEST_CASE(template150); // syntax error
191191
TEST_CASE(template151); // crash
192+
TEST_CASE(template152); // #9467
192193
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
193194
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
194195
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@@ -3619,6 +3620,38 @@ class TestSimplifyTemplate : public TestFixture {
36193620
}
36203621
}
36213622

3623+
void template152() { // #9467
3624+
const char code[] = "class Foo {\n"
3625+
" template <unsigned int i>\n"
3626+
" bool bar() {\n"
3627+
" return true;\n"
3628+
" }\n"
3629+
"};\n"
3630+
"template <>\n"
3631+
"bool Foo::bar<9>() {\n"
3632+
" return true;\n"
3633+
"}\n"
3634+
"int global() {\n"
3635+
" int bar = 1;\n"
3636+
" return bar;\n"
3637+
"}";
3638+
const char exp[] = "class Foo { "
3639+
"bool bar<9> ( ) ; "
3640+
"template < unsigned int i > "
3641+
"bool bar ( ) { "
3642+
"return true ; "
3643+
"} "
3644+
"} ; "
3645+
"bool Foo :: bar<9> ( ) { "
3646+
"return true ; "
3647+
"} "
3648+
"int global ( ) { "
3649+
"int bar ; bar = 1 ; "
3650+
"return bar ; "
3651+
"}";
3652+
ASSERT_EQUALS(exp, tok(code));
3653+
}
3654+
36223655
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
36233656
const char code[] = "template <typename T> struct C {};\n"
36243657
"template <typename T> struct S {a};\n"

test/testuninitvar.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class TestUninitVar : public TestFixture {
6969
TEST_CASE(uninitvar2_malloc); // malloc returns uninitialized data
7070
TEST_CASE(uninitvar8); // ticket #6230
7171
TEST_CASE(uninitvar9); // ticket #6424
72+
TEST_CASE(uninitvar10); // ticket #9467
7273
TEST_CASE(uninitvar_unconditionalTry);
7374
TEST_CASE(uninitvar_funcptr); // #6404
7475
TEST_CASE(uninitvar_operator); // #6680
@@ -2664,6 +2665,25 @@ class TestUninitVar : public TestFixture {
26642665
"[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
26652666
}
26662667

2668+
void uninitvar10() { // 9467
2669+
const char code[] = "class Foo {\n"
2670+
" template <unsigned int i>\n"
2671+
" bool bar() {\n"
2672+
" return true;\n"
2673+
" }\n"
2674+
"};\n"
2675+
"template <>\n"
2676+
"bool Foo::bar<9>() {\n"
2677+
" return true;\n"
2678+
"}\n"
2679+
"int global() {\n"
2680+
" int bar = 1;\n"
2681+
" return bar;\n"
2682+
"}";
2683+
checkUninitVar(code, "test.cpp");
2684+
ASSERT_EQUALS("", errout.str());
2685+
}
2686+
26672687
void uninitvar_unconditionalTry() {
26682688
// Unconditional scopes and try{} scopes
26692689
checkUninitVar("int f() {\n"

0 commit comments

Comments
 (0)