Fix #11893 FP constStatement unused variable (#5567)

This commit is contained in:
chrchr-github 2023-10-19 10:51:16 +02:00 committed by GitHub
parent e1a120e6b0
commit 7c3ae68e4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 5 deletions

View File

@ -2246,13 +2246,13 @@ void TemplateSimplifier::expandTemplate(
assert(brackets.empty() == false);
assert(brackets.top()->str() == "{");
Token::createMutualLinks(brackets.top(), mTokenList.back());
if (tok3->strAt(1) == ";") {
const Token * tokSemicolon = tok3->next();
mTokenList.addtoken(tokSemicolon, tokSemicolon->linenr(), tokSemicolon->column(), tokSemicolon->fileIndex());
}
brackets.pop();
if (brackets.empty() && !Token::Match(tok3, "} >|,|{")) {
inTemplateDefinition = false;
if (isClass && tok3->strAt(1) == ";") {
const Token* tokSemicolon = tok3->next();
mTokenList.addtoken(tokSemicolon, tokSemicolon->linenr(), tokSemicolon->column(), tokSemicolon->fileIndex());
}
break;
}
} else if (tok3->str() == ")") {

View File

@ -216,6 +216,7 @@ private:
TEST_CASE(template175); // #10908
TEST_CASE(template176); // #11146
TEST_CASE(template177);
TEST_CASE(template178);
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@ -4518,6 +4519,34 @@ private:
ASSERT_EQUALS(exp, tok(code));
}
void template178() {
const char code[] = "template<typename T>\n" // #11893
"void g() {\n"
" for (T i = 0; i < T{ 3 }; ++i) {}\n"
"}\n"
"void f() {\n"
" g<int>();\n"
"}";
const char exp[] = "void g<int> ( ) ; void f ( ) { g<int> ( ) ; } void g<int> ( ) { for ( int i = 0 ; i < int { 3 } ; ++ i ) { } }";
ASSERT_EQUALS(exp, tok(code));
const char code2[] = "template<typename T>\n"
"struct S {\n"
" T c;\n"
" template<typename U>\n"
" void g() {\n"
" for (U i = 0; i < U{ 3 }; ++i) {}\n"
" }\n"
"};\n"
"void f() {\n"
" S<char> s{};\n"
" s.g<int>();\n"
"}";
const char exp2[] = "struct S<char> ; void f ( ) { S<char> s { } ; s . g<int> ( ) ; } struct S<char> { char c ; void g<int> ( ) ; } ; "
"void S<char> :: g<int> ( ) { for ( int i = 0 ; i < int { 3 } ; ++ i ) { } }";
ASSERT_EQUALS(exp2, tok(code2));
}
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
const char code[] = "template <typename T> struct C {};\n"
"template <typename T> struct S {a};\n"
@ -5229,7 +5258,7 @@ private:
void template_array_type() {
ASSERT_EQUALS("void foo<int[]> ( int [ ] x ) ; "
"void bar ( ) { int [ 3 ] y ; foo<int[]> ( y ) ; } "
"void foo<int[]> ( int [ ] x ) { } ;",
"void foo<int[]> ( int [ ] x ) { }",
tok("template <class T> void foo(T x) {};\n"
"void bar() {\n"
" int[3] y;\n"