Partial fix for #12302 internalAstError using lambda (#5816)

This commit is contained in:
chrchr-github 2024-01-02 13:33:22 +01:00 committed by GitHub
parent 0c8ee7895d
commit d9d23d979d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 4 deletions

View File

@ -573,8 +573,12 @@ static bool iscpp11init_impl(const Token * const tok)
} }
auto isCaseStmt = [](const Token* colonTok) { auto isCaseStmt = [](const Token* colonTok) {
if (!Token::Match(colonTok->tokAt(-1), "%name%|%num%|%char% :")) if (!Token::Match(colonTok->tokAt(-1), "%name%|%num%|%char%|) :"))
return false; return false;
if (const Token* castTok = colonTok->linkAt(-1)) {
if (Token::simpleMatch(castTok->astParent(), "case"))
return true;
}
const Token* caseTok = colonTok->tokAt(-2); const Token* caseTok = colonTok->tokAt(-2);
while (caseTok && Token::Match(caseTok->tokAt(-1), "::|%name%")) while (caseTok && Token::Match(caseTok->tokAt(-1), "::|%name%"))
caseTok = caseTok->tokAt(-1); caseTok = caseTok->tokAt(-1);

View File

@ -168,8 +168,8 @@ private:
ASSERT_EQUALS(1, cppcheck.check(test_file_a.path())); ASSERT_EQUALS(1, cppcheck.check(test_file_a.path()));
ASSERT_EQUALS(1, cppcheck.check(test_file_b.path())); ASSERT_EQUALS(1, cppcheck.check(test_file_b.path()));
// TODO: how to properly disable these warnings? // TODO: how to properly disable these warnings?
errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& errmsg) { errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
return errmsg.id == "logChecker"; return msg.id == "logChecker";
}), errorLogger.errmsgs.end()); }), errorLogger.errmsgs.end());
// the internal errorlist is cleared after each check() call // the internal errorlist is cleared after each check() call
ASSERT_EQUALS(2, errorLogger.errmsgs.size()); ASSERT_EQUALS(2, errorLogger.errmsgs.size());

View File

@ -6788,6 +6788,19 @@ private:
" });\n" " });\n"
"}\n")); "}\n"));
ASSERT_NO_THROW(tokenizeAndStringify("struct A { A(int) {} };\n"
"void g(void (*)(int));\n"
"void f() {\n"
" g([](int i) {\n"
" switch (i) {\n"
" case static_cast<int>(1): {\n"
" A a(i);\n"
" if (1) {}\n"
" }\n"
" }\n"
" });\n"
"}\n"));
// #11378 // #11378
ASSERT_EQUALS("gT{(&[{= 0return", testAst("auto g = T{ [&]() noexcept -> int { return 0; } };")); ASSERT_EQUALS("gT{(&[{= 0return", testAst("auto g = T{ [&]() noexcept -> int { return 0; } };"));

View File

@ -3735,7 +3735,7 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:8]: (warning) Uninitialized variable: x\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:8]: (warning) Uninitialized variable: x\n", errout.str());
valueFlowUninit("int do_something();\n" valueFlowUninit("int do_something();\n" // #11983
"int set_st(int *x);\n" "int set_st(int *x);\n"
"int bar();\n" "int bar();\n"
"void foo() {\n" "void foo() {\n"
@ -3755,6 +3755,22 @@ private:
" if(status == 1 && x > 0){}\n" " if(status == 1 && x > 0){}\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
valueFlowUninit("int h(bool, bool*);\n" // #11760
"int f(bool t) {\n"
" int i = 0;\n"
" bool b;\n"
" if (t)\n"
" g();\n"
" if (i == 0)\n"
" i = h(t, &b);\n"
" if (i == 0 && b)\n"
" i = h(t, &b);\n"
" if (i == 0 && b)\n"
" i = h(t, &b);\n"
" return i;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void valueFlowUninit_uninitvar2() void valueFlowUninit_uninitvar2()