From 93cde415a78741fa7f804342534c34bbd3c8e50e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 11 Nov 2018 07:50:25 +0100 Subject: [PATCH] Fixed #7988 (variable in condition gets wrong varid) --- lib/tokenize.cpp | 11 +++++++++++ test/testvarid.cpp | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 1e2c31ba8..0b92554a5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2343,6 +2343,9 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map 0) || Token::simpleMatch(tok2, "< >") /* Ticket #4764 */)) { + const Token *start = *tok; + if (Token::Match(start->previous(), "%or%|%oror%|&&|&|^|+|-|*|/")) + return false; const Token * tok3 = tok2->findClosingBracket(); if (tok3 == nullptr) { /* Ticket #8151 */ throw tok2; @@ -2351,6 +2354,14 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::mapstr() != ">") break; singleNameCount = 1; + if (Token::Match(tok2, "> %name% %or%|%oror%|&&|&|^|+|-|*|/")) + return false; + if (Token::Match(tok2, "> %name% )")) { + if (Token::Match(tok2->linkAt(2)->previous(), "if|for|while (")) + return false; + if (!Token::Match(tok2->linkAt(2)->previous(), "%name% (")) + return false; + } } else if (Token::Match(tok2, "&|&&")) { ref = !bracket; } else if (singleNameCount == 1 && Token::Match(tok2, "( [*&]") && Token::Match(tok2->link()->next(), "(|[")) { diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 698ae153f..3d35b94a0 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -143,6 +143,7 @@ private: TEST_CASE(varid_templateArray); TEST_CASE(varid_templateParameter); // #7046 set varid for "X": std::array Y; TEST_CASE(varid_templateUsing); // #5781 #7273 + TEST_CASE(varid_not_template_in_condition); // #7988 TEST_CASE(varid_cppcast); // #6190 TEST_CASE(varid_variadicFunc); TEST_CASE(varid_typename); // #4644 @@ -2121,6 +2122,20 @@ private: tokenize(code)); } + void varid_not_template_in_condition() { + const char code1[] = "void f() { if (xb); }"; + ASSERT_EQUALS("1: void f ( ) { if ( x < a || x > b ) { ; } }\n", tokenize(code1)); + + const char code2[] = "void f() { if (1+xb); }"; + ASSERT_EQUALS("1: void f ( ) { if ( 1 + x < a || x > b ) { ; } }\n", tokenize(code2)); + + const char code3[] = "void f() { if (xb+1); }"; + ASSERT_EQUALS("1: void f ( ) { if ( x < a || x > b + 1 ) { ; } }\n", tokenize(code3)); + + const char code4[] = "void f() { if ((x==13) && (xb)); }"; + ASSERT_EQUALS("1: void f ( ) { if ( ( x == 13 ) && ( x < a || x > b ) ) { ; } }\n", tokenize(code4)); + } + void varid_cppcast() { ASSERT_EQUALS("1: const_cast < int * > ( code ) [ 0 ] = 0 ;\n", tokenize("const_cast(code)[0] = 0;"));