[cfe-commits] r155379 - in /cfe/trunk: lib/Sema/SemaDeclAttr.cpp test/SemaCXX/warn-thread-safety-parsing.cpp

DeLesley Hutchins delesley at google.com
Mon Apr 23 11:39:55 PDT 2012


Author: delesley
Date: Mon Apr 23 13:39:55 2012
New Revision: 155379

URL: http://llvm.org/viewvc/llvm-project?rev=155379&view=rev
Log:
Thread safety analysis:  support the use of pt_guarded_by attributes
on smart pointers.  Also adds test case for previous commit.

Modified:
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp
    cfe/trunk/test/SemaCXX/warn-thread-safety-parsing.cpp

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=155379&r1=155378&r2=155379&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Mon Apr 23 13:39:55 2012
@@ -238,6 +238,24 @@
   return QT->isBooleanType() || QT->isIntegerType();
 }
 
+
+// Check to see if the type is a smart pointer of some kind.  We assume
+// it's a smart pointer if it defines both operator-> and operator*.
+static bool threadSafetyCheckIsSmartPointer(Sema &S, const QualType QT) {
+  if (const RecordType *RT = QT->getAs<RecordType>()) {
+    DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
+      S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
+    if (Res1.first == Res1.second)
+      return false;
+
+    DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
+      S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
+    if (Res2.first != Res2.second)
+      return true;
+  }
+  return false;
+}
+
 ///
 /// \brief Check if passed in Decl is a pointer type.
 /// Note that this function may produce an error message.
@@ -249,6 +267,10 @@
     QualType QT = vd->getType();
     if (QT->isAnyPointerType())
       return true;
+
+    if (threadSafetyCheckIsSmartPointer(S, QT))
+      return true;
+
     S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
       << Attr.getName()->getName() << QT;
   } else {

Modified: cfe/trunk/test/SemaCXX/warn-thread-safety-parsing.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-thread-safety-parsing.cpp?rev=155379&r1=155378&r2=155379&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-thread-safety-parsing.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-thread-safety-parsing.cpp Mon Apr 23 13:39:55 2012
@@ -1343,6 +1343,10 @@
 
 namespace PointerToMemberTest {
 
+// Empty string should be ignored.
+int  testEmptyAttribute GUARDED_BY("");
+void testEmptyAttributeFunction() EXCLUSIVE_LOCKS_REQUIRED("");
+
 class Graph {
 public:
   Mu mu_;
@@ -1356,5 +1360,31 @@
 
 }
 
+
+namespace SmartPointerTest {
+
+template<class T>
+class smart_ptr {
+ public:
+  smart_ptr(T* p) : ptr_(p) { };
+
+  T* operator->() { return ptr_; }
+  T& operator*()  { return ptr_; }
+
+ private:
+  T* ptr_;
+};
+
+
+class MyClass {
+public:
+  Mu mu_;
+
+  smart_ptr<int> a PT_GUARDED_BY(mu_);
+};
+
+}
+
+
 } // end namespace TestMultiDecl
 





More information about the cfe-commits mailing list