Set VarId in range-based for-loops

This commit is contained in:
PKEuS 2015-10-26 19:03:23 +01:00
parent a5f577d179
commit 5add1d8901
2 changed files with 25 additions and 2 deletions

View File

@ -2470,7 +2470,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
// In executable scopes, references must be assigned
// Catching by reference is an exception
if (executableScope && ref) {
if (Token::Match(tok2, "(|=|{"))
if (Token::Match(tok2, "(|=|{|:"))
; // reference is assigned => ok
else if (tok2->str() != ")" || tok2->link()->strAt(-1) != "catch")
return false; // not catching by reference => not declaration
@ -2749,7 +2749,7 @@ void Tokenizer::setVarId()
bool decl = setVarIdParseDeclaration(&tok2, variableId, scopeStack.top().isExecutable, isCPP(), isC());
if (decl) {
const Token* prev2 = tok2->previous();
if (Token::Match(prev2, "%type% [;[=,)]") && tok2->previous()->str() != "const")
if (Token::Match(prev2, "%type% [;[=,):]") && tok2->previous()->str() != "const")
;
else if (Token::Match(prev2, "%type% ( !!)") && Token::simpleMatch(tok2->link(), ") ;")) {
// In C++ , a variable can't be called operator+ or something like that.

View File

@ -139,6 +139,7 @@ private:
TEST_CASE(varid_cpp11initialization); // #4344
TEST_CASE(varid_inheritedMembers); // #4101
TEST_CASE(varid_header); // #6386
TEST_CASE(varid_rangeBasedFor);
TEST_CASE(varidclass1);
TEST_CASE(varidclass2);
@ -2154,6 +2155,28 @@ private:
"}; ", false, "test.h"));
}
void varid_rangeBasedFor() {
ASSERT_EQUALS("\n\n##file 0\n"
"1: void reset ( Foo array@1 ) {\n"
"2: for ( auto & e@2 : array@1 ) {\n"
"3: foo ( e@2 ) ; }\n"
"4: } ;\n",
tokenize("void reset(Foo array) {\n"
" for (auto& e : array)\n"
" foo(e);\n"
"};"));
ASSERT_EQUALS("\n\n##file 0\n"
"1: void reset ( Foo array@1 ) {\n"
"2: for ( auto e@2 : array@1 ) {\n"
"3: foo ( e@2 ) ; }\n"
"4: } ;\n",
tokenize("void reset(Foo array) {\n"
" for (auto e : array)\n"
" foo(e);\n"
"};"));
}
void varidclass1() {
const std::string actual = tokenize(
"class Fred\n"