From 5408153e9140f34bbca9059638c61cc12bd539cf Mon Sep 17 00:00:00 2001 From: DeLesley Hutchins Date: Fri, 31 Aug 2012 22:09:53 +0000 Subject: [PATCH] Thread-safety analysis: fix handling of LOCK_RETURNED attribute so that the latest definition of a function is always used when computing lock expressions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@163028 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ThreadSafety.cpp | 10 +++-- test/SemaCXX/warn-thread-safety-analysis.cpp | 45 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/lib/Analysis/ThreadSafety.cpp b/lib/Analysis/ThreadSafety.cpp index 77785d3e29..b89a83c881 100644 --- a/lib/Analysis/ThreadSafety.cpp +++ b/lib/Analysis/ThreadSafety.cpp @@ -300,8 +300,9 @@ private: } else if (CXXMemberCallExpr *CMCE = dyn_cast(Exp)) { // When calling a function with a lock_returned attribute, replace // the function call with the expression in lock_returned. - if (LockReturnedAttr* At = - CMCE->getMethodDecl()->getAttr()) { + CXXMethodDecl* MD = + cast(CMCE->getMethodDecl()->getMostRecentDecl()); + if (LockReturnedAttr* At = MD->getAttr()) { CallingContext LRCallCtx(CMCE->getMethodDecl()); LRCallCtx.SelfArg = CMCE->getImplicitObjectArgument(); LRCallCtx.SelfArrow = @@ -330,8 +331,9 @@ private: NodeVec[Root].setSize(Sz + 1); return Sz + 1; } else if (CallExpr *CE = dyn_cast(Exp)) { - if (LockReturnedAttr* At = - CE->getDirectCallee()->getAttr()) { + FunctionDecl* FD = + cast(CE->getDirectCallee()->getMostRecentDecl()); + if (LockReturnedAttr* At = FD->getAttr()) { CallingContext LRCallCtx(CE->getDirectCallee()); LRCallCtx.NumArgs = CE->getNumArgs(); LRCallCtx.FunArgs = CE->getArgs(); diff --git a/test/SemaCXX/warn-thread-safety-analysis.cpp b/test/SemaCXX/warn-thread-safety-analysis.cpp index 2a362064ab..7d0d6430d9 100644 --- a/test/SemaCXX/warn-thread-safety-analysis.cpp +++ b/test/SemaCXX/warn-thread-safety-analysis.cpp @@ -3145,3 +3145,48 @@ public: } // end namespace StringIgnoreTest +namespace LockReturnedScopeFix { + +class Base { +protected: + struct Inner; + bool c; + + const Mutex& getLock(const Inner* i); + + void lockInner (Inner* i) EXCLUSIVE_LOCK_FUNCTION(getLock(i)); + void unlockInner(Inner* i) UNLOCK_FUNCTION(getLock(i)); + void foo(Inner* i) EXCLUSIVE_LOCKS_REQUIRED(getLock(i)); + + void bar(Inner* i); +}; + + +struct Base::Inner { + Mutex lock_; + void doSomething() EXCLUSIVE_LOCKS_REQUIRED(lock_); +}; + + +const Mutex& Base::getLock(const Inner* i) LOCK_RETURNED(i->lock_) { + return i->lock_; +} + + +void Base::foo(Inner* i) { + i->doSomething(); +} + +void Base::bar(Inner* i) { + if (c) { + i->lock_.Lock(); + unlockInner(i); + } + else { + lockInner(i); + i->lock_.Unlock(); + } +} + +} // end namespace LockReturnedScopeFix + -- 2.40.0