clang-tidy 4.0 clang-analyzer-alpha.unix.PthreadLock check

234 Views Asked by At

Even after I thought that this check was missing, I am now suddenly getting the output of clang-analyzer-alpha.unix.PthreadLock check from the clang-tidy 4.0 tool. Here is a toned down use case of my code which I am trying to modernize by using clang-tidy tool. I have enabled all the checks using -checks=* argument.

#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;
}

When clang-tidy is ran over this code,it produces following warnings.

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.

Why does clang-tidy think that this lock has already been taken? Two functions are totally apart and the lock inside foo2 has no relation to lock in the boost headers. Is this an incorrect warning ? If not then what should I do about it?

0

There are 0 best solutions below