Skip to content

Commit 1e9bb71

Browse files
dougsonosDoug Wyatt
authored andcommitted
[Clang] Diagnose additional ObjC statements as function effect violations (llvm#112148)
Bug fix: `@autoreleasepool`, `@synchronized`, and `@finally` were not being noticed and treated as function effect violations. --------- Co-authored-by: Doug Wyatt <[email protected]>
1 parent 8aa7881 commit 1e9bb71

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

clang/lib/Sema/SemaFunctionEffects.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,13 +1133,40 @@ class Analyzer {
11331133
return true;
11341134
}
11351135

1136+
bool VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Finally) {
1137+
diagnoseLanguageConstruct(FunctionEffect::FE_ExcludeCatch,
1138+
ViolationID::ThrowsOrCatchesExceptions,
1139+
Finally->getAtFinallyLoc());
1140+
return true;
1141+
}
1142+
11361143
bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
11371144
diagnoseLanguageConstruct(FunctionEffect::FE_ExcludeObjCMessageSend,
11381145
ViolationID::AccessesObjCMethodOrProperty,
11391146
Msg->getBeginLoc());
11401147
return true;
11411148
}
11421149

1150+
bool VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *ARP) {
1151+
// Under the hood, @autorelease (potentially?) allocates memory and
1152+
// invokes ObjC methods. We don't currently have memory allocation as
1153+
// a "language construct" but we do have ObjC messaging, so diagnose that.
1154+
diagnoseLanguageConstruct(FunctionEffect::FE_ExcludeObjCMessageSend,
1155+
ViolationID::AccessesObjCMethodOrProperty,
1156+
ARP->getBeginLoc());
1157+
return true;
1158+
}
1159+
1160+
bool VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Sync) {
1161+
// Under the hood, this calls objc_sync_enter and objc_sync_exit, wrapped
1162+
// in a @try/@finally block. Diagnose this generically as "ObjC
1163+
// messaging".
1164+
diagnoseLanguageConstruct(FunctionEffect::FE_ExcludeObjCMessageSend,
1165+
ViolationID::AccessesObjCMethodOrProperty,
1166+
Sync->getBeginLoc());
1167+
return true;
1168+
}
1169+
11431170
bool VisitSEHExceptStmt(SEHExceptStmt *Exc) {
11441171
diagnoseLanguageConstruct(FunctionEffect::FE_ExcludeCatch,
11451172
ViolationID::ThrowsOrCatchesExceptions,

clang/test/SemaObjCXX/attr-nonblocking-constraints.mm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,17 @@ void nb4() [[clang::nonblocking]] {
2323
}
2424
@catch (...) { // expected-warning {{function with 'nonblocking' attribute must not throw or catch exceptions}}
2525
}
26+
@finally { // expected-warning {{function with 'nonblocking' attribute must not throw or catch exceptions}}
27+
}
28+
}
29+
30+
@class Lock;
31+
extern Lock *someLock;
32+
33+
void nb5() [[clang::nonblocking]] {
34+
@autoreleasepool { // expected-warning {{function with 'nonblocking' attribute must not access ObjC methods or properties}}
35+
}
36+
37+
@synchronized(someLock) { // expected-warning {{function with 'nonblocking' attribute must not access ObjC methods or properties}}
38+
}
2639
}

0 commit comments

Comments
 (0)