I am trying to get Negative Capabilities working, as described in the link. I have the following code, which is based on the example in the link provided. (I changed their example mutex, Mutex, which they never define, to std::mutex, which has all the required annotations for the other threading safety checks to work.)
#include <mutex>
#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
#define EXCLUDES(...) \
THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
#define REQUIRES(...) \
THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
class Foo {
std::mutex mu;
void foo() {
mu.lock();
bar();
baz();
mu.unlock();
}
void bar() {
mu.lock();
// ...
mu.unlock();
}
void baz() {
bif();
}
void bif() REQUIRES(!mu);
};
I expect this to warn me of not using REQUIRES(!mu) in all the places required. Instead, I get a syntax error:
<source>:30:23: error: invalid argument type 'std::mutex' to unary expression
30 | void bif() REQUIRES(!mu);
| ^~~
1 error generated.
Compiler returned: 1
Which seems to suggest the compiler is not even able to parse this correctly. I have tried other (larger) examples, from a real project, and they correctly warn that I should use negative capabilities, but display the same syntax error as here when I actually try to write REQUIRES(!mu).
I have tried with multiple versions of clang, using the -Wthread-safety-negative flag as described in the documentation. (Notably, 20.1.0 and trunk, from compiler explorer.)
mutex.h. You need an annotated interface for mutexes too.Mutexwhile you usestd::mutex