From 59034e17f137611232fd0a1c38107faf7047e249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 29 Aug 2017 22:35:55 +0200 Subject: [PATCH] Refactoring, reuse code to skip lambda functions --- lib/astutils.cpp | 19 +++++++++++++++++++ lib/astutils.h | 8 ++++++++ lib/checkautovariables.cpp | 34 ++++------------------------------ lib/valueflow.cpp | 15 ++++++++------- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 6bca06e95..71a4517e6 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -487,3 +487,22 @@ std::vector getArguments(const Token *ftok) getArgumentsRecursive(ftok->next()->astOperand2(), &arguments); return arguments; } + +const Token *findLambdaEndToken(const Token *first) +{ + if (!first || first->str() != "[") + return nullptr; + const Token* tok = first->link(); + if (Token::simpleMatch(tok, "] {")) + return tok->linkAt(1); + if (!Token::simpleMatch(tok, "] (")) + return nullptr; + tok = tok->linkAt(1)->next(); + if (tok && tok->str() == "constexpr") + tok = tok->next(); + if (tok && tok->str() == "mutable") + tok = tok->next(); + if (tok && tok->str() == "{") + return tok->link(); + return nullptr; +} diff --git a/lib/astutils.h b/lib/astutils.h index df7f54e5f..f360d7a0b 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -98,4 +98,12 @@ int numberOfArguments(const Token *start); */ std::vector getArguments(const Token *ftok); +/** + * find lambda function end token + * \todo handle explicit return type + * \param first The [ token + * \return nullptr or the } + */ +const Token *findLambdaEndToken(const Token *first); + #endif // astutilsH diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 2d9392d11..2745447b1 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -22,6 +22,7 @@ #include "checkautovariables.h" +#include "astutils.h" #include "errorlogger.h" #include "library.h" #include "settings.h" @@ -438,33 +439,6 @@ static bool astHasAutoResult(const Token *tok) return false; } -/*! - * Skip over a lambda expression - * \return next token - or next token beyond lambda - * \todo handle explicit return type - */ -static const Token* skipLambda(const Token* first) -{ - if (!first) - return nullptr; - if (first->str() != "[") - return first; - const Token* tok = first->link()->next(); - if (!tok) - return nullptr; - if (tok->str() == "(") { - tok = tok->link()->next(); - } - if (tok->str() == "constexpr") - tok = tok->next(); - if (tok->str() == "mutable") - tok = tok->next(); - if (tok->str() == "{") { - tok = tok->link()->next(); - } - return tok; -} - void CheckAutoVariables::returnReference() { if (_tokenizer->isC()) @@ -489,9 +463,9 @@ void CheckAutoVariables::returnReference() } // Skip over lambdas - tok2 = skipLambda(tok2); - if (!tok2) - break; + const Token *lambdaEndToken = findLambdaEndToken(tok2); + if (lambdaEndToken) + tok2 = lambdaEndToken->next(); if (tok2->str() == "(") tok2 = tok2->link(); diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 3e454aa3f..eec4c0566 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1348,13 +1348,14 @@ static bool valueFlowForward(Token * const startToken, } } - // jump over lambda function - if (Token::simpleMatch(tok2, "= [") && Token::simpleMatch(tok2->linkAt(1), "] (") && Token::simpleMatch(tok2->linkAt(1)->linkAt(1), ") {")) { - // TODO: handle lambda functions - tok2 = tok2->linkAt(1); // Goto ] - tok2 = tok2->linkAt(1); // Goto ) - tok2 = tok2->linkAt(1); // Goto } - continue; + // skip lambda functions + // TODO: handle lambda functions + if (Token::simpleMatch(tok2, "= [")) { + Token *lambdaEndToken = const_cast(findLambdaEndToken(tok2->next())); + if (lambdaEndToken) { + tok2 = lambdaEndToken; + continue; + } } if (Token::Match(tok2, "[;{}] %name% :") || tok2->str() == "case") {