Skip to content

Commit 86d4513

Browse files
committedSep 19, 2018
Thread safety analysis: Handle ObjCIvarRefExpr in SExprBuilder::translate
Summary: This imitates the code for MemberExpr. Fixes PR38896. Reviewers: aaron.ballman, delesley, lukasza, rjmccall Reviewed By: delesley Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D52200 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@342600 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent bd0e6da commit 86d4513

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed
 

‎include/clang/Analysis/Analyses/ThreadSafetyCommon.h

+2
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ class SExprBuilder {
397397
CallingContext *Ctx) ;
398398
til::SExpr *translateCXXThisExpr(const CXXThisExpr *TE, CallingContext *Ctx);
399399
til::SExpr *translateMemberExpr(const MemberExpr *ME, CallingContext *Ctx);
400+
til::SExpr *translateObjCIVarRefExpr(const ObjCIvarRefExpr *IVRE,
401+
CallingContext *Ctx);
400402
til::SExpr *translateCallExpr(const CallExpr *CE, CallingContext *Ctx,
401403
const Expr *SelfE = nullptr);
402404
til::SExpr *translateCXXMemberCallExpr(const CXXMemberCallExpr *ME,

‎lib/Analysis/ThreadSafetyCommon.cpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ til::SExpr *SExprBuilder::translate(const Stmt *S, CallingContext *Ctx) {
211211
return translateCXXThisExpr(cast<CXXThisExpr>(S), Ctx);
212212
case Stmt::MemberExprClass:
213213
return translateMemberExpr(cast<MemberExpr>(S), Ctx);
214+
case Stmt::ObjCIvarRefExprClass:
215+
return translateObjCIVarRefExpr(cast<ObjCIvarRefExpr>(S), Ctx);
214216
case Stmt::CallExprClass:
215217
return translateCallExpr(cast<CallExpr>(S), Ctx);
216218
case Stmt::CXXMemberCallExprClass:
@@ -311,9 +313,9 @@ static const ValueDecl *getValueDeclFromSExpr(const til::SExpr *E) {
311313
return nullptr;
312314
}
313315

314-
static bool hasCppPointerType(const til::SExpr *E) {
316+
static bool hasAnyPointerType(const til::SExpr *E) {
315317
auto *VD = getValueDeclFromSExpr(E);
316-
if (VD && VD->getType()->isPointerType())
318+
if (VD && VD->getType()->isAnyPointerType())
317319
return true;
318320
if (const auto *C = dyn_cast<til::Cast>(E))
319321
return C->castOpcode() == til::CAST_objToPtr;
@@ -344,7 +346,20 @@ til::SExpr *SExprBuilder::translateMemberExpr(const MemberExpr *ME,
344346
D = getFirstVirtualDecl(VD);
345347

346348
til::Project *P = new (Arena) til::Project(E, D);
347-
if (hasCppPointerType(BE))
349+
if (hasAnyPointerType(BE))
350+
P->setArrow(true);
351+
return P;
352+
}
353+
354+
til::SExpr *SExprBuilder::translateObjCIVarRefExpr(const ObjCIvarRefExpr *IVRE,
355+
CallingContext *Ctx) {
356+
til::SExpr *BE = translate(IVRE->getBase(), Ctx);
357+
til::SExpr *E = new (Arena) til::SApply(BE);
358+
359+
const auto *D = cast<ObjCIvarDecl>(IVRE->getDecl()->getCanonicalDecl());
360+
361+
til::Project *P = new (Arena) til::Project(E, D);
362+
if (hasAnyPointerType(BE))
348363
P->setArrow(true);
349364
return P;
350365
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -Wthread-safety-beta -Wno-objc-root-class %s
2+
3+
class __attribute__((lockable)) Lock {
4+
public:
5+
void Acquire() __attribute__((exclusive_lock_function())) {}
6+
void Release() __attribute__((unlock_function())) {}
7+
};
8+
9+
class __attribute__((scoped_lockable)) AutoLock {
10+
public:
11+
AutoLock(Lock &lock) __attribute__((exclusive_lock_function(lock)))
12+
: lock_(lock) {
13+
lock.Acquire();
14+
}
15+
~AutoLock() __attribute__((unlock_function())) { lock_.Release(); }
16+
17+
private:
18+
Lock &lock_;
19+
};
20+
21+
@interface MyInterface {
22+
@private
23+
Lock lock_;
24+
int value_;
25+
}
26+
27+
- (void)incrementValue;
28+
- (void)decrementValue;
29+
30+
@end
31+
32+
@implementation MyInterface
33+
34+
- (void)incrementValue {
35+
AutoLock lock(lock_);
36+
value_ += 1;
37+
}
38+
39+
- (void)decrementValue {
40+
lock_.Acquire(); // expected-note{{mutex acquired here}}
41+
value_ -= 1;
42+
} // expected-warning{{mutex 'self->lock_' is still held at the end of function}}
43+
44+
@end

0 commit comments

Comments
 (0)
Please sign in to comment.