Skip to content

Commit 74dbb15

Browse files
committed
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.
1 parent 07e2ba4 commit 74dbb15

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

clang/lib/Analysis/ThreadSafety.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,7 @@ void ThreadSafetyAnalyzer::checkAccess(const FactSet &FSet, const Expr *Exp,
16711671
// Guard against self-initialization. e.g., int &i = i;
16721672
if (E == Exp)
16731673
break;
1674-
Exp = E;
1674+
Exp = E->IgnoreImplicit()->IgnoreParenCasts();
16751675
continue;
16761676
}
16771677
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
// RUN: %clang_cc1 -verify -fsyntax-only -std=c++20 -Wthread-safety %s
3+
4+
class __attribute__((lockable)) Lock {};
5+
6+
void sink_protected(int) {}
7+
8+
class Baz {
9+
public:
10+
Lock lock_;
11+
int protected_num_ __attribute__((guarded_by(lock_))) = 1;
12+
};
13+
14+
void baz_paran_test() {
15+
Baz baz;
16+
int& n = baz.protected_num_;
17+
sink_protected(n); // expected-warning{{reading variable 'protected_num_' requires holding mutex 'baz.lock_'}}
18+
int& n2 = (baz.protected_num_);
19+
sink_protected(n2); // expected-warning{{reading variable 'protected_num_' requires holding mutex 'baz.lock_'}}
20+
}

0 commit comments

Comments
 (0)