Skip to content

Commit 9a9bf80

Browse files
committed
[clang][ThreadSafety] Warn when constructor is trylock
With this change, Clang will generate a warning when a constructor is annotated as a trylock function. Issue #92408
1 parent abedb3b commit 9a9bf80

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,12 @@ static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
605605

606606
static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
607607
SmallVectorImpl<Expr *> &Args) {
608+
if (isa<CXXConstructorDecl>(D)) {
609+
S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
610+
<< AL << AL.isRegularKeywordAttribute() << ExpectedFunction;
611+
return false;
612+
}
613+
608614
if (!AL.checkAtLeastNumArgs(S, 1))
609615
return false;
610616

clang/test/SemaCXX/warn-thread-safety-parsing.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,13 @@ class EtfFoo {
747747
// expected-warning {{'exclusive_trylock_function' attribute without capability arguments refers to 'this', but 'EtfFoo' isn't annotated with 'capability' or 'scoped_lockable' attribute}}
748748
};
749749

750+
// It does not make sense to annotate a constructor as an exclusive trylock
751+
// function. See <https://github.com/llvm/llvm-project/issues/92408>.
752+
class EtfConstructor {
753+
EtfConstructor() EXCLUSIVE_TRYLOCK_FUNCTION(1); // \
754+
// expected-warning {{'exclusive_trylock_function' attribute only applies to functions}}
755+
};
756+
750757
class EXCLUSIVE_TRYLOCK_FUNCTION(1) EtfTestClass { // \
751758
// expected-warning {{'exclusive_trylock_function' attribute only applies to functions}}
752759
};

0 commit comments

Comments
 (0)