From de53f63f76f6ecdadf492b3b619c71862a32cbfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 18 Apr 2020 12:08:53 +0200 Subject: [PATCH] Fixed #9665 (Tokenizer::setVarId: for loop variables) --- lib/tokenize.cpp | 9 +++++++++ test/testvarid.cpp | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 5f7ad136b..575f117b4 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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%|::")) diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 538d211b8..44215426b 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -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"