Fixed #9665 (Tokenizer::setVarId: for loop variables)

This commit is contained in:
Daniel Marjamäki 2020-04-18 12:08:53 +02:00
parent 13d88d7132
commit de53f63f76
2 changed files with 31 additions and 0 deletions

View File

@ -3503,6 +3503,15 @@ void Tokenizer::setVarIdPass1()
if (decl) {
variableMap.addVariable(prev2->str());
if (Token::simpleMatch(tok->previous(), "for (") && Token::Match(prev2, "%name% [=,]")) {
for (const Token *tok3 = prev2->next(); tok3 && tok3->str() != ";"; tok3 = tok3->next()) {
if (Token::Match(tok3, "[([]"))
tok3 = tok3->link();
if (Token::Match(tok3, ", %name% [,=;]"))
variableMap.addVariable(tok3->next()->str());
}
}
// set varid for template parameters..
tok = tok->next();
while (Token::Match(tok, "%name%|::"))

View File

@ -94,6 +94,8 @@ private:
TEST_CASE(varid61); // #4988 inline function
TEST_CASE(varid62);
TEST_CASE(varid63);
TEST_CASE(varid_for_1);
TEST_CASE(varid_for_2);
TEST_CASE(varid_cpp_keywords_in_c_code);
TEST_CASE(varid_cpp_keywords_in_c_code2); // #5373: varid=0 for argument called "delete"
TEST_CASE(varidFunctionCall1);
@ -1125,6 +1127,26 @@ private:
ASSERT_EQUALS(expected, tokenize(code, false));
}
void varid_for_1() {
const char code[] = "void foo(int a, int b) {\n"
" for (int a=1,b=2;;) {}\n"
"}";
const char expected[] = "1: void foo ( int a@1 , int b@2 ) {\n"
"2: for ( int a@3 = 1 , b@4 = 2 ; ; ) { }\n"
"3: }\n";
ASSERT_EQUALS(expected, tokenize(code, false));
}
void varid_for_2() {
const char code[] = "void foo(int a, int b) {\n"
" for (int a=f(x,y,z),b=2;;) {}\n"
"}";
const char expected[] = "1: void foo ( int a@1 , int b@2 ) {\n"
"2: for ( int a@3 = f ( x , y , z ) , b@4 = 2 ; ; ) { }\n"
"3: }\n";
ASSERT_EQUALS(expected, tokenize(code, false));
}
void varid_cpp_keywords_in_c_code() {
const char code[] = "void f() {\n"
" delete d;\n"