clang-tidy weird warning for mutex lock
| Bugzilla Link | 30408 |
| Version | unspecified |
| OS | Linux |
| Reporter | LLVM Bugzilla Contributor |
| CC | @devincoughlin,@EugeneZelenko |
Extended Description
Recently, I checked out llvm tools from following link.
http://llvm.org/docs/GettingStarted.html#getting-started-quickly-a-summary
I followed the instructions and build the tools required for clang-tidy.
At my workplace, we are modernizing our module's source code to C++11 standard and we aggressively using clang-tidy (built from the trunk as mentioned above) for this purpose.
One of the use cases where we are stuck is a weird warning for mutex locks.
Here is a smaller reproducible version of our use case
#include <boost/thread.hpp>
void foo2() { boost::mutex crapmutex; boost::mutex::scoped_lock lock(crapmutex); }
int main() {
foo2();
return 0;
}
When we run clang-tidy with all checks enabled, specify our internal compilation options followed by standard compiler flags, we get following warnings for mutex locks
33110 warnings generated. ..../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); ^ ..../Source.cpp:13:5: note: Calling 'foo2' foo2(); ^ ...../Source.cpp:8:31: note: Calling constructor for 'unique_lock' boost::mutex::scoped_lock lock(crapmutex); ^ ..../boost/include/boost/thread/lock_types.hpp:157:7: note: Calling 'unique_lock::lock' lock(); ^ ..../boost/include/boost/thread/lock_types.hpp:369:7: note: Taking false branch if (m == 0) ^ ..../boost/include/boost/thread/lock_types.hpp:374:7: note: Taking false branch if (owns_lock()) ^ ..../boost/include/boost/thread/lock_types.hpp:379:7: note: Calling 'mutex::lock' m->lock(); ^ ..../boost/include/boost/thread/pthread/mutex.hpp:149:23: note: This lock has already been acquired int res = posix::pthread_mutex_lock(&m); ^ Suppressed 33109 warnings (33109 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 is this warning coming up? These two locks are totally not related. Is this a bug? If not then why is this warning coming up?
Is it possible the checker is confusing boost::mutex and std::mutex?
Also, the alpha checkers are completely unsupported and often unfinished so I wouldn't advise running all of them on your code base. They are known to produce false positives and possibly even crash!
Also, note that when I change small use case to
#include
void foo2() { std::mutex crapmutex; std::unique_lockstd::mutex lock(crapmutex) }
int main() { foo2(); return 0; }
the warning disappears.
assigned to @tkremenek