aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Analysis/ThreadSafety.cpp
diff options
context:
space:
mode:
authorMarco Elver <elver@google.com>2025-02-16 14:53:41 +0100
committerMarco Elver <elver@google.com>2025-02-26 16:34:33 +0100
commit3c8c0d4d8d9bbc160d160e683f7a74fd28574dc6 (patch)
tree590858e82edbebf498f0f77d13a165d6e8d77385 /clang/lib/Analysis/ThreadSafety.cpp
parent5c8e22bb2653b5229cb90b9e28c4a19692a2445b (diff)
downloadllvm-3c8c0d4d8d9bbc160d160e683f7a74fd28574dc6.zip
llvm-3c8c0d4d8d9bbc160d160e683f7a74fd28574dc6.tar.gz
llvm-3c8c0d4d8d9bbc160d160e683f7a74fd28574dc6.tar.bz2
Thread Safety Analysis: Handle address-of followed by dereference
Correctly analyze expressions where the address of a guarded variable is taken and immediately dereferenced, such as (*(type-specifier *)&x). Previously, such patterns would result in false negatives. Pull Request: https://github.com/llvm/llvm-project/pull/127396
Diffstat (limited to 'clang/lib/Analysis/ThreadSafety.cpp')
-rw-r--r--clang/lib/Analysis/ThreadSafety.cpp11
1 files changed, 11 insertions, 0 deletions
diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index bfaf1a0..1bad4eac 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -1765,6 +1765,8 @@ void ThreadSafetyAnalyzer::checkAccess(const FactSet &FSet, const Expr *Exp,
void ThreadSafetyAnalyzer::checkPtAccess(const FactSet &FSet, const Expr *Exp,
AccessKind AK,
ProtectedOperationKind POK) {
+ // Strip off paren- and cast-expressions, checking if we encounter any other
+ // operator that should be delegated to checkAccess() instead.
while (true) {
if (const auto *PE = dyn_cast<ParenExpr>(Exp)) {
Exp = PE->getSubExpr();
@@ -1783,6 +1785,15 @@ void ThreadSafetyAnalyzer::checkPtAccess(const FactSet &FSet, const Expr *Exp,
break;
}
+ if (const auto *UO = dyn_cast<UnaryOperator>(Exp)) {
+ if (UO->getOpcode() == UO_AddrOf) {
+ // Pointer access via pointer taken of variable, so the dereferenced
+ // variable is not actually a pointer.
+ checkAccess(FSet, UO->getSubExpr(), AK, POK);
+ return;
+ }
+ }
+
// Pass by reference warnings are under a different flag.
ProtectedOperationKind PtPOK = POK_VarDereference;
if (POK == POK_PassByRef) PtPOK = POK_PtPassByRef;