From 78011164ebc62b0746c9c742c2546a02effdb281 Mon Sep 17 00:00:00 2001 From: DeLesley Hutchins Date: Tue, 29 Sep 2015 16:24:18 +0000 Subject: [PATCH] Thread Safety Analysis: allow capability attribute on unions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@248805 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/Attr.td | 4 ++-- lib/Sema/SemaDeclAttr.cpp | 11 ++++------ test/Sema/attr-capabilities.c | 8 +++++-- test/SemaCXX/warn-thread-safety-analysis.cpp | 23 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/include/clang/Basic/Attr.td b/include/clang/Basic/Attr.td index 4b8a7b71f9..a758a4c35e 100644 --- a/include/clang/Basic/Attr.td +++ b/include/clang/Basic/Attr.td @@ -1543,8 +1543,8 @@ def Capability : InheritableAttr { let Spellings = [GNU<"capability">, CXX11<"clang", "capability">, GNU<"shared_capability">, CXX11<"clang", "shared_capability">]; - let Subjects = SubjectList<[Struct, TypedefName], ErrorDiag, - "ExpectedStructOrTypedef">; + let Subjects = SubjectList<[Record, TypedefName], ErrorDiag, + "ExpectedStructOrUnionOrTypedef">; let Args = [StringArgument<"Name">]; let Accessors = [Accessor<"isShared", [GNU<"shared_capability">, diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 9cc082152b..8218c20696 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -628,13 +628,10 @@ static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, // Check that this attribute only applies to lockable types. QualType QT = cast(D)->getType(); - if (!QT->isDependentType()) { - const RecordType *RT = getRecordType(QT); - if (!RT || !RT->getDecl()->hasAttr()) { - S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) - << Attr.getName(); - return false; - } + if (!QT->isDependentType() && !typeHasCapability(S, QT)) { + S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) + << Attr.getName(); + return false; } // Check that all arguments are lockable objects. diff --git a/test/Sema/attr-capabilities.c b/test/Sema/attr-capabilities.c index 5bfbdacf50..8996770486 100644 --- a/test/Sema/attr-capabilities.c +++ b/test/Sema/attr-capabilities.c @@ -4,11 +4,15 @@ typedef int __attribute__((capability("role"))) ThreadRole; struct __attribute__((shared_capability("mutex"))) Mutex {}; struct NotACapability {}; +// Put capability attributes on unions +union __attribute__((capability("mutex"))) MutexUnion { int a; char* b; }; +typedef union { int a; char* b; } __attribute__((capability("mutex"))) MutexUnion2; + // Test an invalid capability name struct __attribute__((capability("wrong"))) IncorrectName {}; // expected-warning {{invalid capability name 'wrong'; capability name must be 'mutex' or 'role'}} -int Test1 __attribute__((capability("test1"))); // expected-error {{'capability' attribute only applies to structs and typedefs}} -int Test2 __attribute__((shared_capability("test2"))); // expected-error {{'shared_capability' attribute only applies to structs and typedefs}} +int Test1 __attribute__((capability("test1"))); // expected-error {{'capability' attribute only applies to structs, unions, and typedefs}} +int Test2 __attribute__((shared_capability("test2"))); // expected-error {{'shared_capability' attribute only applies to structs, unions, and typedefs}} int Test3 __attribute__((acquire_capability("test3"))); // expected-warning {{'acquire_capability' attribute only applies to functions}} int Test4 __attribute__((try_acquire_capability("test4"))); // expected-error {{'try_acquire_capability' attribute only applies to functions}} int Test5 __attribute__((release_capability("test5"))); // expected-warning {{'release_capability' attribute only applies to functions}} diff --git a/test/SemaCXX/warn-thread-safety-analysis.cpp b/test/SemaCXX/warn-thread-safety-analysis.cpp index 6daefd78d3..01e16a488c 100644 --- a/test/SemaCXX/warn-thread-safety-analysis.cpp +++ b/test/SemaCXX/warn-thread-safety-analysis.cpp @@ -5159,3 +5159,26 @@ void test3() { } // end namespace GlobalAcquiredBeforeAfterTest + +namespace LockableUnions { + +union LOCKABLE MutexUnion { + int a; + char* b; + + void Lock() EXCLUSIVE_LOCK_FUNCTION(); + void Unlock() UNLOCK_FUNCTION(); +}; + +MutexUnion muun2; +MutexUnion muun1 ACQUIRED_BEFORE(muun2); + +void test() { + muun2.Lock(); + muun1.Lock(); // expected-warning {{mutex 'muun1' must be acquired before 'muun2'}} + muun1.Unlock(); + muun2.Unlock(); +} + +} // end namespace LockableUnions + -- 2.40.0