Skip to content

[Clang] Diagnose additional ObjC statements as function effect violations #112148

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 3 commits into from
Oct 14, 2024
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
27 changes: 27 additions & 0 deletions clang/lib/Sema/SemaFunctionEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,13 +1133,40 @@ class Analyzer {
return true;
}

bool VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Finally) {
diagnoseLanguageConstruct(FunctionEffect::FE_ExcludeCatch,
ViolationID::ThrowsOrCatchesExceptions,
Finally->getAtFinallyLoc());
return true;
}

bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
diagnoseLanguageConstruct(FunctionEffect::FE_ExcludeObjCMessageSend,
ViolationID::AccessesObjCMethodOrProperty,
Msg->getBeginLoc());
return true;
}

bool VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *ARP) {
// Under the hood, @autorelease (potentially?) allocates memory and
// invokes ObjC methods. We don't currently have memory allocation as
// a "language construct" but we do have ObjC messaging, so diagnose that.
diagnoseLanguageConstruct(FunctionEffect::FE_ExcludeObjCMessageSend,
ViolationID::AccessesObjCMethodOrProperty,
ARP->getBeginLoc());
return true;
}

bool VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Sync) {
// Under the hood, this calls objc_sync_enter and objc_sync_exit, wrapped
// in a @try/@finally block. Diagnose this generically as "ObjC
// messaging".
diagnoseLanguageConstruct(FunctionEffect::FE_ExcludeObjCMessageSend,
ViolationID::AccessesObjCMethodOrProperty,
Sync->getBeginLoc());
return true;
}

bool VisitSEHExceptStmt(SEHExceptStmt *Exc) {
diagnoseLanguageConstruct(FunctionEffect::FE_ExcludeCatch,
ViolationID::ThrowsOrCatchesExceptions,
Expand Down
13 changes: 13 additions & 0 deletions clang/test/SemaObjCXX/attr-nonblocking-constraints.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ void nb4() [[clang::nonblocking]] {
}
@catch (...) { // expected-warning {{function with 'nonblocking' attribute must not throw or catch exceptions}}
}
@finally { // expected-warning {{function with 'nonblocking' attribute must not throw or catch exceptions}}
}
}

@class Lock;
extern Lock *someLock;

void nb5() [[clang::nonblocking]] {
@autoreleasepool { // expected-warning {{function with 'nonblocking' attribute must not access ObjC methods or properties}}
}

@synchronized(someLock) { // expected-warning {{function with 'nonblocking' attribute must not access ObjC methods or properties}}
}
}
Loading