Skip to content

Commit 7b8e8fe

Browse files
Fix #13670 FP legacyUninitvar for variables declared in for loop (danmar#7344)
1 parent 68c129d commit 7b8e8fe

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

lib/symboldatabase.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5001,8 +5001,13 @@ const Token *Scope::checkVariable(const Token *tok, AccessControl varaccess, con
50015001
tok2 = tok2->link();
50025002
continue;
50035003
}
5004-
if (Token::Match(tok2, ", %name%"))
5004+
if (Token::Match(tok2, ", %name%")) {
5005+
if (tok2->next()->varId() == 0) {
5006+
check->debugMessage(tok2->next(), "varid0", "Scope::checkVariable found variable \'" + tok2->strAt(1) + "\' with varid 0.");
5007+
return tok;
5008+
}
50055009
addVariable(tok2->next(), typestart, vartok->previous(), varaccess, vType, this, settings);
5010+
}
50065011
}
50075012
}
50085013
}

lib/tokenize.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4727,7 +4727,7 @@ void Tokenizer::setVarIdPass1()
47274727
const Token* prev2 = tok2->previous();
47284728
if (Token::Match(prev2, "%type% [;[=,)]") && tok2->strAt(-1) != "const")
47294729
;
4730-
else if (Token::Match(prev2, "%type% :") && tok->strAt(-1) == "for")
4730+
else if (Token::Match(prev2, "%type% [:({]") && tok->strAt(-1) == "for")
47314731
;
47324732
else if (Token::Match(prev2, "%type% ( !!)") && Token::simpleMatch(tok2->link(), ") ;")) {
47334733
// In C++ , a variable can't be called operator+ or something like that.
@@ -4780,11 +4780,11 @@ void Tokenizer::setVarIdPass1()
47804780
syntaxErrorC(prev2, prev2->strAt(-2) + prev2->strAt(-1) + " " + prev2->str());
47814781
variableMap.addVariable(prev2->str(), scopeStack.size() <= 1);
47824782

4783-
if (Token::simpleMatch(tok->previous(), "for (") && Token::Match(prev2, "%name% [=,]")) {
4783+
if (Token::simpleMatch(tok->previous(), "for (") && Token::Match(prev2, "%name% [=[({,]")) {
47844784
for (const Token *tok3 = prev2->next(); tok3 && tok3->str() != ";"; tok3 = tok3->next()) {
47854785
if (Token::Match(tok3, "[([]"))
47864786
tok3 = tok3->link();
4787-
if (Token::Match(tok3, ", %name% [,=;]"))
4787+
if (Token::Match(tok3, ", %name% [=[({,;]"))
47884788
variableMap.addVariable(tok3->strAt(1), false);
47894789
}
47904790
}

test/testvarid.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class TestVarID : public TestFixture {
107107
TEST_CASE(varid71); // #12676 - wrong varid in uninstantiated templated constructor
108108
TEST_CASE(varid_for_1);
109109
TEST_CASE(varid_for_2);
110+
TEST_CASE(varid_for_3);
110111
TEST_CASE(varid_cpp_keywords_in_c_code);
111112
TEST_CASE(varid_cpp_keywords_in_c_code2); // #5373: varid=0 for argument called "delete"
112113
TEST_CASE(varid_cpp_keywords_in_c_code3);
@@ -1418,6 +1419,28 @@ class TestVarID : public TestFixture {
14181419
ASSERT_EQUALS(expected, tokenize(code));
14191420
}
14201421

1422+
void varid_for_3() {
1423+
const char code[] = "void f(int w, int h) {\n" // #13670
1424+
" for (int a[2] = { 2, w / 2 }, b = 2; a[1] && a[0] <= h; a[1] /= 2, a[0] *= 2) {}\n"
1425+
"}\n";
1426+
const char expected[] = "1: void f ( int w@1 , int h@2 ) {\n"
1427+
"2: for ( int a@3 [ 2 ] = { 2 , w@1 / 2 } , b@4 = 2 ; a@3 [ 1 ] && a@3 [ 0 ] <= h@2 ; a@3 [ 1 ] /= 2 , a@3 [ 0 ] *= 2 ) { }\n"
1428+
"3: }\n";
1429+
ASSERT_EQUALS(expected, tokenize(code));
1430+
1431+
const char code2[] = "void f() {\n"
1432+
" for (int a(1), b{ 2 }, c = 3; a < b + c; ++a) {}\n"
1433+
" for (int a{ 1 }, b(2), c = 3; a < b + c; ++a) {}\n"
1434+
" for (int a = 1, b{ 2 }, c(3); a < b + c; ++a) {}\n"
1435+
"}\n";
1436+
const char expected2[] = "1: void f ( ) {\n"
1437+
"2: for ( int a@1 ( 1 ) , b@2 { 2 } , c@3 = 3 ; a@1 < b@2 + c@3 ; ++ a@1 ) { }\n"
1438+
"3: for ( int a@4 { 1 } , b@5 ( 2 ) , c@6 = 3 ; a@4 < b@5 + c@6 ; ++ a@4 ) { }\n"
1439+
"4: for ( int a@7 = 1 , b@8 { 2 } , c@9 ( 3 ) ; a@7 < b@8 + c@9 ; ++ a@7 ) { }\n"
1440+
"5: }\n";
1441+
ASSERT_EQUALS(expected2, tokenize(code2));
1442+
}
1443+
14211444
void varid_cpp_keywords_in_c_code() {
14221445
const char code[] = "void f() {\n"
14231446
" delete d;\n"

0 commit comments

Comments
 (0)