Partial fix for #9384 varid 0 with lambda (#3875)

This commit is contained in:
chrchr-github 2022-03-08 20:10:51 +01:00 committed by GitHub
parent fbdfb60809
commit c9f47dec8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 28 deletions

View File

@ -113,7 +113,7 @@ const Token * Tokenizer::isFunctionHead(const Token *tok, const std::string &end
} }
if (cpp && tok->str() == ")") { if (cpp && tok->str() == ")") {
tok = tok->next(); tok = tok->next();
while (Token::Match(tok, "const|noexcept|override|final|volatile|&|&& !!(") || while (Token::Match(tok, "const|noexcept|override|final|volatile|mutable|&|&& !!(") ||
(Token::Match(tok, "%name% !!(") && tok->isUpperCaseName())) (Token::Match(tok, "%name% !!(") && tok->isUpperCaseName()))
tok = tok->next(); tok = tok->next();
if (tok && tok->str() == ")") if (tok && tok->str() == ")")
@ -3552,7 +3552,8 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
} }
} }
if (cpp && tok3 && Token::simpleMatch(tok3->previous(), "] (") && Token::simpleMatch(tok3->link(), ") {")) if (cpp && tok3 && Token::simpleMatch(tok3->previous(), "] (") &&
(Token::simpleMatch(tok3->link(), ") {") || Token::Match(tok3->link(), ") . %name%")))
isLambdaArg = true; isLambdaArg = true;
} }
@ -3619,7 +3620,7 @@ void Tokenizer::setVarIdStructMembers(Token **tok1,
while (Token::Match(tok->next(), ")| . %name% !!(")) { while (Token::Match(tok->next(), ")| . %name% !!(")) {
// Don't set varid for trailing return type // Don't set varid for trailing return type
if (tok->strAt(1) == ")" && tok->linkAt(1)->previous()->isName() && if (tok->strAt(1) == ")" && (tok->linkAt(1)->previous()->isName() || tok->linkAt(1)->strAt(-1) == "]") &&
isFunctionHead(tok->linkAt(1), "{|;")) { isFunctionHead(tok->linkAt(1), "{|;")) {
tok = tok->tokAt(3); tok = tok->tokAt(3);
continue; continue;
@ -5780,7 +5781,7 @@ void Tokenizer::removeMacrosInGlobalScope()
if (tok->str() == "(") { if (tok->str() == "(") {
tok = tok->link(); tok = tok->link();
if (Token::Match(tok, ") %type% {") && if (Token::Match(tok, ") %type% {") &&
!Token::Match(tok->next(), "const|namespace|class|struct|union|noexcept|override|final|volatile")) !Token::Match(tok->next(), "const|namespace|class|struct|union|noexcept|override|final|volatile|mutable"))
tok->deleteNext(); tok->deleteNext();
} }

View File

@ -2609,34 +2609,56 @@ private:
void varid_lambda_arg() { void varid_lambda_arg() {
// #8664 // #8664
const char code1[] = "static void func(int ec) {\n" {
" func2([](const std::error_code& ec) { return ec; });\n" const char code[] = "static void func(int ec) {\n"
"}"; " func2([](const std::error_code& ec) { return ec; });\n"
const char exp1[] = "1: static void func ( int ec@1 ) {\n" "}";
"2: func2 ( [ ] ( const std :: error_code & ec@2 ) { return ec@2 ; } ) ;\n" const char exp[] = "1: static void func ( int ec@1 ) {\n"
"3: }\n"; "2: func2 ( [ ] ( const std :: error_code & ec@2 ) { return ec@2 ; } ) ;\n"
ASSERT_EQUALS(exp1, tokenize(code1)); "3: }\n";
ASSERT_EQUALS(exp, tokenize(code));
const char code2[] = "static void func(int ec) {\n" }
" func2([](int x, const std::error_code& ec) { return x + ec; });\n" {
"}"; const char code[] = "static void func(int ec) {\n"
const char exp2[] = "1: static void func ( int ec@1 ) {\n" " func2([](int x, const std::error_code& ec) { return x + ec; });\n"
"2: func2 ( [ ] ( int x@2 , const std :: error_code & ec@3 ) { return x@2 + ec@3 ; } ) ;\n" "}";
"3: }\n"; const char exp[] = "1: static void func ( int ec@1 ) {\n"
ASSERT_EQUALS(exp2, tokenize(code2)); "2: func2 ( [ ] ( int x@2 , const std :: error_code & ec@3 ) { return x@2 + ec@3 ; } ) ;\n"
"3: }\n";
ASSERT_EQUALS(exp, tokenize(code));
}
// #9384
{
const char code[] = "auto g = [](const std::string& s) -> std::string { return {}; };\n";
const char exp[] = "1: auto g@1 ; g@1 = [ ] ( const std :: string & s@2 ) . std :: string { return { } ; } ;\n";
ASSERT_EQUALS(exp, tokenize(code));
}
{
const char code[] = "auto g = [](std::function<void()> p) {};\n";
const char exp[] = "1: auto g@1 ; g@1 = [ ] ( std :: function < void ( ) > p@2 ) { } ;\n";
TODO_ASSERT_EQUALS(exp, "1: auto g@1 ; g@1 = [ ] ( std :: function < void ( ) > p ) { } ;\n", tokenize(code));
}
} }
void varid_lambda_mutable() { void varid_lambda_mutable() {
// #8957 // #8957
const char code1[] = "static void func() {\n" {
" auto x = []() mutable {};\n" const char code[] = "static void func() {\n"
" dostuff(x);\n" " auto x = []() mutable {};\n"
"}"; " dostuff(x);\n"
const char exp1[] = "1: static void func ( ) {\n" "}";
"2: auto x@1 ; x@1 = [ ] ( ) mutable { } ;\n" const char exp[] = "1: static void func ( ) {\n"
"3: dostuff ( x@1 ) ;\n" "2: auto x@1 ; x@1 = [ ] ( ) mutable { } ;\n"
"4: }\n"; "3: dostuff ( x@1 ) ;\n"
ASSERT_EQUALS(exp1, tokenize(code1)); "4: }\n";
ASSERT_EQUALS(exp, tokenize(code));
}
// #9384
{
const char code[] = "auto g = [](int i) mutable {};\n";
const char exp[] = "1: auto g@1 ; g@1 = [ ] ( int i@2 ) mutable { } ;\n";
ASSERT_EQUALS(exp, tokenize(code));
}
} }
void varid_trailing_return1() { // #8889 void varid_trailing_return1() { // #8889