Skip to content

Commit ccf6699

Browse files
committed
Support [[guarded_by(mutex)]] attribute inside C struct
Today, it's only supported inside C++ classes or top level C/C++ declaration. I mostly copied and adapted over the code from the [[counted_by(value)]] lookup.
1 parent 50d837e commit ccf6699

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

clang/include/clang/Parse/Parser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,6 +3128,12 @@ class Parser : public CodeCompletionHandler {
31283128
IdentifierInfo *ScopeName, SourceLocation ScopeLoc,
31293129
ParsedAttr::Form Form);
31303130

3131+
void ParseGuardedByAttribute(IdentifierInfo &AttrName,
3132+
SourceLocation AttrNameLoc,
3133+
ParsedAttributes &Attrs,
3134+
IdentifierInfo *ScopeName,
3135+
SourceLocation ScopeLoc, ParsedAttr::Form Form);
3136+
31313137
void ParseTypeofSpecifier(DeclSpec &DS);
31323138
SourceLocation ParseDecltypeSpecifier(DeclSpec &DS);
31333139
void AnnotateExistingDecltypeSpecifier(const DeclSpec &DS,

clang/lib/Parse/ParseDecl.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,14 @@ void Parser::ParseGNUAttributeArgs(
671671
ParseBoundsAttribute(*AttrName, AttrNameLoc, Attrs, ScopeName, ScopeLoc,
672672
Form);
673673
return;
674+
} else if (AttrKind == ParsedAttr::AT_GuardedBy) {
675+
ParseGuardedByAttribute(*AttrName, AttrNameLoc, Attrs, ScopeName, ScopeLoc,
676+
Form);
677+
return;
678+
} else if (AttrKind == ParsedAttr::AT_PtGuardedBy) {
679+
ParseGuardedByAttribute(*AttrName, AttrNameLoc, Attrs, ScopeName, ScopeLoc,
680+
Form);
681+
return;
674682
} else if (AttrKind == ParsedAttr::AT_CXXAssume) {
675683
ParseCXXAssumeAttributeArg(Attrs, AttrName, AttrNameLoc, EndLoc, Form);
676684
return;
@@ -3330,6 +3338,57 @@ void Parser::DistributeCLateParsedAttrs(Decl *Dcl,
33303338
}
33313339
}
33323340

3341+
/// GuardedBy attributes (e.g., guarded_by):
3342+
/// AttrName '(' expression ')'
3343+
void Parser::ParseGuardedByAttribute(IdentifierInfo &AttrName,
3344+
SourceLocation AttrNameLoc,
3345+
ParsedAttributes &Attrs,
3346+
IdentifierInfo *ScopeName,
3347+
SourceLocation ScopeLoc,
3348+
ParsedAttr::Form Form) {
3349+
assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
3350+
3351+
BalancedDelimiterTracker Parens(*this, tok::l_paren);
3352+
Parens.consumeOpen();
3353+
3354+
if (Tok.is(tok::r_paren)) {
3355+
Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
3356+
Parens.consumeClose();
3357+
return;
3358+
}
3359+
3360+
ArgsVector ArgExprs;
3361+
// Don't evaluate argument when the attribute is ignored.
3362+
using ExpressionKind =
3363+
Sema::ExpressionEvaluationContextRecord::ExpressionKind;
3364+
EnterExpressionEvaluationContext EC(
3365+
Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, nullptr,
3366+
ExpressionKind::EK_BoundsAttrArgument);
3367+
3368+
ExprResult ArgExpr(
3369+
Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()));
3370+
3371+
if (ArgExpr.isInvalid()) {
3372+
Parens.skipToEnd();
3373+
return;
3374+
}
3375+
3376+
ArgExprs.push_back(ArgExpr.get());
3377+
3378+
auto &AL = *Attrs.addNew(
3379+
&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()), ScopeName,
3380+
ScopeLoc, ArgExprs.data(), ArgExprs.size(), Form);
3381+
3382+
if (!Tok.is(tok::r_paren)) {
3383+
Diag(Tok.getLocation(), diag::err_attribute_wrong_number_arguments)
3384+
<< AL << 1;
3385+
Parens.skipToEnd();
3386+
return;
3387+
}
3388+
3389+
Parens.consumeClose();
3390+
}
3391+
33333392
/// Bounds attributes (e.g., counted_by):
33343393
/// AttrName '(' expression ')'
33353394
void Parser::ParseBoundsAttribute(IdentifierInfo &AttrName,

clang/test/Sema/warn-thread-safety-analysis.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
struct LOCKABLE Mutex {};
2929

3030
struct Foo {
31-
struct Mutex *mu_;
31+
struct Mutex *mu_;
32+
struct Bar {
33+
struct Mutex *other_mu;
34+
} bar;
35+
int a_value GUARDED_BY(mu_);
36+
int* a_ptr PT_GUARDED_BY(bar.other_mu);
3237
};
3338

3439
// Declare mutex lock/unlock functions.
@@ -136,6 +141,9 @@ int main(void) {
136141
// Cleanup happens automatically -> no warning.
137142
}
138143

144+
foo_.a_value = 0; // expected-warning {{writing variable 'a_value' requires holding mutex 'mu_' exclusively}}
145+
*foo_.a_ptr = 1; // expected-warning {{writing the value pointed to by 'a_ptr' requires holding mutex 'bar.other_mu' exclusively}}
146+
139147
return 0;
140148
}
141149

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,11 +1516,7 @@ class Foo {
15161516
mutable Mutex mu;
15171517
int a GUARDED_BY(mu);
15181518

1519-
static int si GUARDED_BY(mu);
1520-
//FIXME: Bug 32066 - Error should be emitted irrespective of C++ dialect
1521-
#if __cplusplus <= 199711L
1522-
// expected-error@-3 {{invalid use of non-static data member 'mu'}}
1523-
#endif
1519+
static int si GUARDED_BY(mu); // expected-error {{invalid use of non-static data member 'mu'}}
15241520

15251521
static void foo() EXCLUSIVE_LOCKS_REQUIRED(mu);
15261522
//FIXME: Bug 32066 - Error should be emitted irrespective of C++ dialect

0 commit comments

Comments
 (0)