0

即使在我认为缺少此检查之后,我现在也突然clang-analyzer-alpha.unix.PthreadLock从 clang-tidy 4.0 工具中获取检查的输出。这是我正在尝试使用 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与锁定升压接头无关。这是一个不正确的警告吗?如果没有,那我该怎么办?

4

0 回答 0