From 5801fb26f09de631012878179a8293b758022093 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 5 Jul 2019 05:26:01 -0500 Subject: [PATCH] Fix syntax error with lambda captures (#1954) * Fix syntax error with lambda captures * Fix issue when using initializer in lambdas --- lib/tokenize.cpp | 3 ++- test/testtokenize.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index b7e6be0b0..f77cf0025 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9250,7 +9250,8 @@ void Tokenizer::findGarbageCode() const syntaxError(tok); if (Token::Match(tok, "[+-] [;,)]}]") && !(isCPP() && Token::Match(tok->previous(), "operator [+-] ;"))) syntaxError(tok); - if (Token::simpleMatch(tok, ",")) { + if (Token::simpleMatch(tok, ",") && + !Token::Match(tok->tokAt(-2), "[ = , &|%name%")) { if (Token::Match(tok->previous(), "(|[|{|<|%assign%|%or%|%oror%|==|!=|+|-|/|!|>=|<=|~|^|::|sizeof|throw|decltype|typeof")) syntaxError(tok); if (Token::Match(tok->next(), ")|]|>|%assign%|%or%|%oror%|==|!=|/|>=|<=|&&")) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 75bc88dad..af416693f 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -459,6 +459,7 @@ private: TEST_CASE(checkEnableIf); TEST_CASE(checkTemplates); TEST_CASE(checkNamespaces); + TEST_CASE(checkLambdas); // #9052 TEST_CASE(noCrash1); @@ -7834,6 +7835,13 @@ private: ASSERT_NO_THROW(tokenizeAndStringify("namespace x { namespace y { namespace z {}}}")) } + void checkLambdas() { + ASSERT_NO_THROW(tokenizeAndStringify("auto f(int& i) { return [=, &i] {}; }")) + ASSERT_NO_THROW(tokenizeAndStringify("auto f(int& i) { return [&, i] {}; }")) + ASSERT_NO_THROW(tokenizeAndStringify("auto f(int& i) { return [&, i = std::move(i)] {}; }")) + ASSERT_NO_THROW(tokenizeAndStringify("auto f(int& i) { return [=, i = std::move(i)] {}; }")) + } + void noCrash1() { ASSERT_NO_THROW(tokenizeAndStringify( "struct A {\n"