jeudi 1 septembre 2016

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

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>

class SingletonMutex
{
    public:

        static boost::mutex& getInstance()
        {
            boost::call_once(&Init::Do,flag);
            return *m_mutex;
        }

    private:
        SingletonMutex() = default;
        static boost::mutex* m_mutex;
        struct Init { static void Do() { m_mutex = new boost::mutex; } };
        static boost::once_flag flag;
};

boost::once_flag SingletonMutex::flag = BOOST_ONCE_INIT;

boost::mutex* SingletonMutex::m_mutex = nullptr;

boost::mutex& getMutex()
{
    return SingletonMutex::getInstance();
}

void foo2()
{
    boost::mutex::scoped_lock lock(getMutex());       
    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;
}

Aucun commentaire:

Enregistrer un commentaire