[clang] Support `guarded_by` attribute and related attributes inside C structs and support late parsing them (PR #95455)
Pierre d'Herbemont via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 14 08:13:22 PDT 2024
================
@@ -3330,6 +3340,118 @@ void Parser::DistributeCLateParsedAttrs(Decl *Dcl,
}
}
+/// GuardedBy attributes (e.g., guarded_by):
+/// AttrName '(' expression ')'
+void Parser::ParseGuardedByAttribute(
+ IdentifierInfo &AttrName, SourceLocation AttrNameLoc,
+ ParsedAttributes &Attrs, IdentifierInfo *ScopeName, SourceLocation ScopeLoc,
+ SourceLocation *EndLoc, ParsedAttr::Form Form) {
+ assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
+
+ BalancedDelimiterTracker Parens(*this, tok::l_paren);
+ Parens.consumeOpen();
+
+ if (Tok.is(tok::r_paren)) {
+ Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
+ Parens.consumeClose();
+ return;
+ }
+
+ ArgsVector ArgExprs;
+ // Don't evaluate argument when the attribute is ignored.
+ using ExpressionKind =
+ Sema::ExpressionEvaluationContextRecord::ExpressionKind;
+ EnterExpressionEvaluationContext EC(
+ Actions,
+ getLangOpts().CPlusPlus
+ ? Sema::ExpressionEvaluationContext::Unevaluated
+ : Sema::ExpressionEvaluationContext::PotentiallyEvaluated,
----------------
pdherbemont wrote:
I thought it wasn't working properly with `Unevaluated` but it seems like it actually does!
I have iterated on the patch further and simplified a bit by directly passing `EK_AttrArgument` to `EnterExpressionEvaluationContext` from within `ParseAttributeArgsCommon`. This allows me to remove `ParseGuardedAttribute()` and `ParseAcquiredAttribute()` and only rely on `ParseAttributeArgsCommon`.
This may have unforeseen side effects on some GNU/Clang/Microsoft attributes, but those are not caught by the test suite... So I am tempted to propose the simpler patch.
https://github.com/llvm/llvm-project/pull/95455
More information about the cfe-commits
mailing list