Fixed #7988 (variable in condition gets wrong varid)

This commit is contained in:
Daniel Marjamäki 2018-11-11 07:50:25 +01:00
parent 148f257c6b
commit 93cde415a7
2 changed files with 26 additions and 0 deletions

View File

@ -2343,6 +2343,9 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
}
} else if (!c && ((TemplateSimplifier::templateParameters(tok2) > 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::map<std::stri
if (tok2->str() != ">")
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(), "(|[")) {

View File

@ -143,6 +143,7 @@ private:
TEST_CASE(varid_templateArray);
TEST_CASE(varid_templateParameter); // #7046 set varid for "X": std::array<int,X> 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 (x<a||x>b); }";
ASSERT_EQUALS("1: void f ( ) { if ( x < a || x > b ) { ; } }\n", tokenize(code1));
const char code2[] = "void f() { if (1+x<a||x>b); }";
ASSERT_EQUALS("1: void f ( ) { if ( 1 + x < a || x > b ) { ; } }\n", tokenize(code2));
const char code3[] = "void f() { if (x<a||x>b+1); }";
ASSERT_EQUALS("1: void f ( ) { if ( x < a || x > b + 1 ) { ; } }\n", tokenize(code3));
const char code4[] = "void f() { if ((x==13) && (x<a||x>b)); }";
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<int *>(code)[0] = 0;"));