Skip to content

-Wthread-safety-negative reported with seemingly correctly annotated code #117919

@firewave

Description

@firewave
#include <mutex>
#include <set>
#include <string>

class C
{
    void f(std::string s)
    {
        std::lock_guard<std::mutex> l(mSync);
        mObject.emplace(std::move(s));
    }

private:
    std::mutex mSync;
    std::set<std::string> mObject __attribute__((guarded_by(mSync)));
};

Building with -D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS -stdlib=libc++ -Weverything gives the following:

<source>:9:37: warning: acquiring mutex 'mSync' requires negative capability '!mSync' [-Wthread-safety-negative]
    9 |         std::lock_guard<std::mutex> l(mSync);
      |                                     ^
<source>:8:5: note: thread warning in function 'f'
    8 |     {
      |     ^

https://godbolt.org/z/d7q7s9jK7

There is some discussion about this in the patch which introduced this: https://reviews.llvm.org/D84604.

CC @aaronpuchert @AaronBallman @LebedevRI

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions