Fixed #8957 (Tokenizer::setVarId: varid not set when lambda function is used)

This commit is contained in:
Daniel Marjamäki 2019-02-03 08:57:04 +01:00
parent 4457faa26b
commit ae001d4336
3 changed files with 20 additions and 2 deletions

View File

@ -2663,7 +2663,11 @@ void Tokenizer::setVarIdPass1()
if (tok->str() == "{") {
bool isExecutable;
if (tok->strAt(-1) == ")" || Token::Match(tok->tokAt(-2), ") %type%") ||
const Token *prev = tok->previous();
while (Token::Match(prev, "%name%|."))
prev = prev->previous();
const bool isLambda = prev && prev->str() == ")" && Token::simpleMatch(prev->link()->previous(), "] (");
if ((!isLambda && (tok->strAt(-1) == ")" || Token::Match(tok->tokAt(-2), ") %type%"))) ||
(initlist && tok->strAt(-1) == "}")) {
isExecutable = true;
} else {

View File

@ -510,7 +510,7 @@ private:
" g(std::move(x)).foo([=](int value) mutable {;});\n"
" X y=x;\n"
"}";
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 11U, ValueFlow::Value::MovedVariable));
ASSERT_EQUALS(true, testValueOfX(code, 11U, ValueFlow::Value::MovedVariable));
}
void valueFlowCalculations() {

View File

@ -160,6 +160,7 @@ private:
TEST_CASE(varid_structinit); // #6406
TEST_CASE(varid_arrayinit); // #7579
TEST_CASE(varid_lambda_arg);
TEST_CASE(varid_lambda_mutable);
TEST_CASE(varidclass1);
TEST_CASE(varidclass2);
@ -2462,6 +2463,19 @@ private:
ASSERT_EQUALS(exp2, tokenize(code2));
}
void varid_lambda_mutable() {
// #8957
const char code1[] = "static void func() {\n"
" auto x = []() mutable {};\n"
" dostuff(x);\n"
"}";
const char exp1[] = "1: static void func ( ) {\n"
"2: auto x@1 ; x@1 = [ ] ( ) mutable { } ;\n"
"3: dostuff ( x@1 ) ;\n"
"4: }\n";
ASSERT_EQUALS(exp1, tokenize(code1));
}
void varidclass1() {
const std::string actual = tokenize(
"class Fred\n"