Skip to content

Thread Safety Analysis: Fix pointer handling of variables with deprecated attributes #148974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions clang/lib/Sema/AnalysisBasedWarnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2112,11 +2112,26 @@ class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler {

void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK,
AccessKind AK, SourceLocation Loc) override {
assert((POK == POK_VarAccess || POK == POK_VarDereference) &&
"Only works for variables");
unsigned DiagID = POK == POK_VarAccess?
diag::warn_variable_requires_any_lock:
diag::warn_var_deref_requires_any_lock;
unsigned DiagID = 0;
switch (POK) {
case POK_VarAccess:
case POK_PassByRef:
case POK_ReturnByRef:
case POK_PassPointer:
case POK_ReturnPointer:
DiagID = diag::warn_variable_requires_any_lock;
break;
case POK_VarDereference:
case POK_PtPassByRef:
case POK_PtReturnByRef:
case POK_PtPassPointer:
case POK_PtReturnPointer:
DiagID = diag::warn_var_deref_requires_any_lock;
break;
case POK_FunctionCall:
llvm_unreachable("Only works for variables");
break;
}
PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID)
<< D << getLockKindFromAccessKind(AK));
Warnings.emplace_back(std::move(Warning), getNotes());
Expand Down
14 changes: 14 additions & 0 deletions clang/test/SemaCXX/warn-thread-safety-analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6196,6 +6196,8 @@ class Return {
Mutex mu;
Foo foo GUARDED_BY(mu);
Foo* foo_ptr PT_GUARDED_BY(mu);
Foo foo_depr GUARDED_VAR; // test deprecated attribute
Foo* foo_ptr_depr PT_GUARDED_VAR; // test deprecated attribute

Foo returns_value_locked() {
MutexLock lock(&mu);
Expand Down Expand Up @@ -6297,6 +6299,18 @@ class Return {
return *foo_ptr; // expected-warning {{returning the value that 'foo_ptr' points to by reference requires holding mutex 'mu' exclusively}}
}

Foo *returns_ptr_deprecated() {
return &foo_depr; // expected-warning {{writing variable 'foo_depr' requires holding any mutex exclusively}}
}

Foo *returns_pt_ptr_deprecated() {
return foo_ptr_depr; // expected-warning {{writing the value pointed to by 'foo_ptr_depr' requires holding any mutex exclusively}}
}

Foo &returns_ref_deprecated() {
return *foo_ptr_depr; // expected-warning {{writing the value pointed to by 'foo_ptr_depr' requires holding any mutex exclusively}}
}

// FIXME: Basic alias analysis would help catch cases like below.
Foo *returns_ptr_alias() {
mu.Lock();
Expand Down
Loading