即使在我认为这个check was missing之后,我现在突然从铿锵的4.0工具中获得clang-analyzer-alpha.unix.PthreadLock
的输出。这是我的代码的一个调低用例,我试图通过使用clang-tidy工具进行现代化。
我使用-checks=*
参数启用了所有检查。
#include <boost/thread.hpp>
#include <boost/thread/once.hpp>
void foo2()
{
boost:mutex mymutex;
boost::mutex::scoped_lock lock(mymutex);
int* x = NULL; // This is intentional. This triggers the clang-tidy checks. If I remove this lines, I wont get the clang-tidy warnings/errors/recommendations.
}
int main() {
foo2();
return 0;
}
当对此代码运行clang-tidy时,会产生以下警告。
path/boost/include/boost/thread/pthread/mutex.hpp:149:23: warning: This lock has already been acquired [clang-analyzer-alpha.unix.PthreadLock]
int res = posix::pthread_mutex_lock(&m);
^
path/Source.cpp:40:5: note: Calling 'foo2'
foo2();
^
path/Source.cpp:32:31: note: Calling constructor for 'unique_lock'
boost::mutex::scoped_lock lock(getMutex());
^
path/boost/include/boost/thread/lock_types.hpp:157:7: note: Calling 'unique_lock::lock'
lock();
^
path/boost/include/boost/thread/lock_types.hpp:369:7: note: Taking false branch
if (m == 0)
^
path/boost/include/boost/thread/lock_types.hpp:374:7: note: Taking false branch
if (owns_lock())
^
path/boost/include/boost/thread/lock_types.hpp:379:7: note: Calling 'mutex::lock'
m->lock();
^
path/boost/thread/pthread/mutex.hpp:149:23: note: This lock has already been acquired
int res = posix::pthread_mutex_lock(&m);
^
path/Source.cpp:34:10: warning: unused variable 'x' [clang-diagnostic-unused-variable]
int* x = NULL;
^
path/Source.cpp:34:14: warning: use nullptr [modernize-use-nullptr]
int* x = NULL;
^
nullptr
Suppressed 33125 warnings (33125 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
为什么clang-tidy认为这个锁已被占用?两个函数完全分开,foo2
内的锁与升级头中的锁无关。这是不正确的警告吗?如果没有,那我该怎么办呢?