[cfe-commits] r150700 - in /cfe/trunk: lib/Analysis/ThreadSafety.cpp test/SemaCXX/warn-thread-safety-analysis.cpp
DeLesley Hutchins
delesley at google.com
Thu Feb 16 09:03:24 PST 2012
Author: delesley
Date: Thu Feb 16 11:03:24 2012
New Revision: 150700
URL: http://llvm.org/viewvc/llvm-project?rev=150700&view=rev
Log:
Thread-Safety: added support for 'this' as a lock expression.
Modified:
cfe/trunk/lib/Analysis/ThreadSafety.cpp
cfe/trunk/test/SemaCXX/warn-thread-safety-analysis.cpp
Modified: cfe/trunk/lib/Analysis/ThreadSafety.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ThreadSafety.cpp?rev=150700&r1=150699&r2=150700&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ThreadSafety.cpp (original)
+++ cfe/trunk/lib/Analysis/ThreadSafety.cpp Thu Feb 16 11:03:24 2012
@@ -62,11 +62,11 @@
///
/// Clang introduces an additional wrinkle, which is that it is difficult to
/// derive canonical expressions, or compare expressions directly for equality.
-/// Thus, we identify a mutex not by an Expr, but by the set of named
+/// Thus, we identify a mutex not by an Expr, but by the list of named
/// declarations that are referenced by the Expr. In other words,
/// x->foo->bar.mu will be a four element vector with the Decls for
/// mu, bar, and foo, and x. The vector will uniquely identify the expression
-/// for all practical purposes.
+/// for all practical purposes. Null is used to denote 'this'.
///
/// Note we will need to perform substitution on "this" and function parameter
/// names when constructing a lock expression.
@@ -122,8 +122,10 @@
} else if (isa<CXXThisExpr>(Exp)) {
if (Parent)
buildMutexID(Parent, D, 0, 0, 0);
- else
+ else {
+ DeclSeq.push_back(0); // Use 0 to represent 'this'.
return; // mutexID is still valid in this case
+ }
} else if (UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp))
buildMutexID(UOE->getSubExpr(), D, Parent, NumArgs, FunArgs);
else if (CastExpr *CE = dyn_cast<CastExpr>(Exp))
@@ -233,6 +235,8 @@
/// We do not want to output the entire expression text for security reasons.
StringRef getName() const {
assert(isValid());
+ if (!DeclSeq.front())
+ return "this"; // Use 0 to represent 'this'.
return DeclSeq.front()->getName();
}
Modified: cfe/trunk/test/SemaCXX/warn-thread-safety-analysis.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-thread-safety-analysis.cpp?rev=150700&r1=150699&r2=150700&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-thread-safety-analysis.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-thread-safety-analysis.cpp Thu Feb 16 11:03:24 2012
@@ -2049,5 +2049,35 @@
// expected-warning {{calling function 'foo' requires exclusive lock on 'mu_'}}
}
+} // end namespace FunctionDefinitionTest
+
+
+namespace SelfLockingTest {
+
+class LOCKABLE MyLock {
+public:
+ int foo GUARDED_BY(this);
+
+ void lock() EXCLUSIVE_LOCK_FUNCTION();
+ void unlock() UNLOCK_FUNCTION();
+
+ void doSomething() {
+ this->lock(); // allow 'this' as a lock expression
+ foo = 0;
+ doSomethingElse();
+ this->unlock();
+ }
+
+ void doSomethingElse() EXCLUSIVE_LOCKS_REQUIRED(this) {
+ foo = 1;
+ };
+
+ void test() {
+ foo = 2; // \
+ // expected-warning {{writing variable 'foo' requires locking 'this' exclusively}}
+ }
};
+} // end namespace SelfLockingTest
+
+
More information about the cfe-commits
mailing list