[clang] Thread Safety Analysis: Handle parenthesis (PR #140656)
Prabhu Rajasekaran via cfe-commits
cfe-commits at lists.llvm.org
Mon May 19 19:07:39 PDT 2025
https://github.com/Prabhuk updated https://github.com/llvm/llvm-project/pull/140656
>From 8570d7a7c2c10e94db53fa23074f46706c8cb32a Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Tue, 20 May 2025 01:57:36 +0000
Subject: [PATCH 1/2] Thread Safety Analysis: Handle parenthesis
When the variable guared by a lock was enclosed in parenthesis, access
violation warnings were not emitted. This patch fixes it and adds a
regression test.
---
clang/lib/Analysis/ThreadSafety.cpp | 2 +-
.../thread-safety-handle-parenthesis.cpp | 19 +++++++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Analysis/thread-safety-handle-parenthesis.cpp
diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index 7e86af6b4a317..156df51a71012 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -1671,7 +1671,7 @@ void ThreadSafetyAnalyzer::checkAccess(const FactSet &FSet, const Expr *Exp,
// Guard against self-initialization. e.g., int &i = i;
if (E == Exp)
break;
- Exp = E;
+ Exp = E->IgnoreImplicit()->IgnoreParenCasts();
continue;
}
}
diff --git a/clang/test/Analysis/thread-safety-handle-parenthesis.cpp b/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
new file mode 100644
index 0000000000000..910ce597b5033
--- /dev/null
+++ b/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -verify -fsyntax-only -std=c++20 -Wthread-safety %s
+
+class __attribute__((lockable)) Lock {};
+
+void sink_protected(int) {}
+
+class Baz {
+public:
+ Lock lock_;
+ int protected_num_ __attribute__((guarded_by(lock_))) = 1;
+};
+
+void baz_paran_test() {
+ Baz baz;
+ int& n = baz.protected_num_;
+ sink_protected(n); // expected-warning{{reading variable 'protected_num_' requires holding mutex 'baz.lock_'}}
+ int& n2 = (baz.protected_num_);
+ sink_protected(n2); // expected-warning{{reading variable 'protected_num_' requires holding mutex 'baz.lock_'}}
+}
>From a97f15dc1b070d5f66d987df98e9a8834bf2722b Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Tue, 20 May 2025 02:07:25 +0000
Subject: [PATCH 2/2] simplify test file
---
clang/test/Analysis/thread-safety-handle-parenthesis.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/test/Analysis/thread-safety-handle-parenthesis.cpp b/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
index 910ce597b5033..0b6b8ce61658b 100644
--- a/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
+++ b/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
@@ -1,10 +1,10 @@
-// RUN: %clang_cc1 -verify -fsyntax-only -std=c++20 -Wthread-safety %s
+// RUN: %clang_cc1 -verify -fsyntax-only -Wthread-safety %s
-class __attribute__((lockable)) Lock {};
+struct __attribute__((lockable)) Lock {};
-void sink_protected(int) {}
+void sink_protected(int);
-class Baz {
+struct Baz {
public:
Lock lock_;
int protected_num_ __attribute__((guarded_by(lock_))) = 1;
More information about the cfe-commits
mailing list