ThreadSafetyAnalysis in C code

Hello,

We are interested in applying ThreadSafetyAnalysis to C code
(specifically the Linux kernel).

However, the very common usecase of declaring a struct member to be
protected by another lockable struct member inside the same struct does
not work (see addition to test below). This results in an error:

  clang/test/Sema/warn-thread-safety-analysis.c:36:24: error: use of undeclared identifier 'mu'
    int data1 GUARDED_BY(mu);
             ^
  clang/test/Sema/warn-thread-safety-analysis.c:38:24: error: use of undeclared identifier 'mu'
    int data2 GUARDED_BY(mu);

AFAIK, this is the main limitation preventing us to use it on the Linux
kernel.

Does anyone know what's going on here?

Thanks,
-- Marco

------ >8 ------

diff --git a/clang/test/Sema/warn-thread-safety-analysis.c b/clang/test/Sema/warn-thread-safety-analysis.c
index a45fb8e0f382..6b75db4c246e 100644
--- a/clang/test/Sema/warn-thread-safety-analysis.c
+++ b/clang/test/Sema/warn-thread-safety-analysis.c
@@ -31,6 +31,13 @@ struct Foo {
   struct Mutex *mu_;
};

+// Struct with mutex protecting data within.
+struct Bar {
+ int data1 GUARDED_BY(mu);
+ struct Mutex mu;
+ int data2 GUARDED_BY(mu);
+};

Hello,

We are interested in applying ThreadSafetyAnalysis to C code
(specifically the Linux kernel).

That's fantastic to hear!

However, the very common usecase of declaring a struct member to be
protected by another lockable struct member inside the same struct does
not work (see addition to test below). This results in an error:

        clang/test/Sema/warn-thread-safety-analysis.c:36:24: error: use of undeclared identifier 'mu'
          int data1 GUARDED_BY(mu);
                               ^
        clang/test/Sema/warn-thread-safety-analysis.c:38:24: error: use of undeclared identifier 'mu'
          int data2 GUARDED_BY(mu);

AFAIK, this is the main limitation preventing us to use it on the Linux
kernel.

Does anyone know what's going on here?

This is https://github.com/llvm/llvm-project/issues/20777

The crux of the issue is there are some lookup difficulties because C
doesn't have the notion of a "this" pointer for the structure object
itself.

~Aaron

Thanks, I see.

I'll respond to that issue to keep things in one place.

Thanks,
-- Marco