Merge pull request #2697 from pfultz2/unique_lock

Extend mutex checking for more locking patterns
This commit is contained in:
Daniel Marjamäki 2020-06-27 07:32:26 +02:00 committed by GitHub
commit 1ad70bbeb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -2439,7 +2439,7 @@ void CheckStl::checkMutexes()
continue;
if (isLocalMutex(var, tok->scope()))
localMutexError(tok);
} else if (Token::Match(tok, "%var% (|{ %var% )|}")) {
} else if (Token::Match(tok, "%var% (|{ %var% )|}|,")) {
if (!isLockGuard(var))
continue;
const Variable* mvar = tok->tokAt(2)->variable();

View File

@ -4429,6 +4429,22 @@ private:
true);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" static std::mutex m;\n"
" static std::unique_lock<std::mutex> g(m, std::defer_lock);\n"
" static std::lock(g);\n"
"}\n",
true);
ASSERT_EQUALS("[test.cpp:3]: (warning) Lock guard is defined globally. Lock guards are intended to be local. A global lock guard could lead to a deadlock since it won't unlock until the end of the program.\n", errout.str());
check("void f() {\n"
" static std::mutex m;\n"
" std::unique_lock<std::mutex> g(m, std::defer_lock);\n"
" std::lock(g);\n"
"}\n",
true);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" std::mutex m;\n"
" std::lock_guard<std::mutex> g(m);\n"
@ -4436,6 +4452,21 @@ private:
true);
ASSERT_EQUALS("[test.cpp:3]: (warning) The lock is ineffective because the mutex is locked at the same scope as the mutex itself.\n", errout.str());
check("void f() {\n"
" std::mutex m;\n"
" std::unique_lock<std::mutex> g(m);\n"
"}\n",
true);
ASSERT_EQUALS("[test.cpp:3]: (warning) The lock is ineffective because the mutex is locked at the same scope as the mutex itself.\n", errout.str());
check("void f() {\n"
" std::mutex m;\n"
" std::unique_lock<std::mutex> g(m, std::defer_lock);\n"
" std::lock(g);\n"
"}\n",
true);
ASSERT_EQUALS("[test.cpp:3]: (warning) The lock is ineffective because the mutex is locked at the same scope as the mutex itself.\n", errout.str());
check("void g();\n"
"void f() {\n"
" static std::mutex m;\n"