← Back to Pull requests

[Clang] Defer access checks in leading attributes on external declarations #244030

Open
nigham opened this pull request with 169 lines diff across 4 files
View on GitHub
nigham nigham commented on Nov 18, 2026 22:37 UTC
Author

Leading C++11 and GNU attributes in global, namespace, and linkage-block scopes are parsed before ParseExternalDeclaration enters ParseDeclarationOrFunctionDefinition (where ParsingDeclSpec pushes a DelayedDiagnosticPool). As a result, Sema evaluated access and availability checks on attribute arguments immediately in the enclosing file or namespace scope rather than deferring them until the declarator is parsed (rejecting valid out-of-line member definitions such as __attribute__((locks_excluded(C::mu_))) void C::f() {}). Commit b78d5380da11 introduced this for leading GNU attributes at global scope, and commit 4e9d349ab86d (#240734) extended the same loop to namespace scope.

Factor the leading-attribute parsing loop into
ParseExternalDeclarationWithAttrs() and wrap only the attribute loop in a ParsingDeclRAIIObject (abortAndRemember()), so non-declarator constructs parsed afterward (static_assert, nested namespaces, etc.) do not run with an active delayed-diagnostic pool. When ParseExternalDeclaration reaches
ParseDeclarationOrFunctionDefinition, transfer those delayed diagnostics into ParsingDeclSpec so they are checked per declarator; otherwise, flush any remaining diagnostics against the resulting Decl so invalid accesses on non-declarator constructs are still diagnosed.

Assited by: Antigravity w/ Gemini, reviewed by me.

Diff: 169 lines (+134 / -35) across 4 files Reviewer: daniel-petrovic Review duration: 21252m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.38) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.38 · 169 lines diff across 4 files
0.38
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.93 · 2 substantive comment(s)
0.93
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.307
Real PR Conversation Timeline
3 items
daniel-petrovic daniel-petrovic commented on code on Nov 19, 2026 07:32 UTC
Code Review
clang/lib/Parse/Parser.cpp:1
@@ -736,10 +726,38 @@ bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result,
   return false;
 }
 
-Parser::DeclGroupPtrTy
-Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
-                                 ParsedAttributes &DeclSpecAttrs,
-                                 ParsingDeclSpec *DS) {
+Parser::DeclGroupPtrTy Parser::ParseExternalDeclarationWithAttrs() {
+  ParsedAttributes DeclAttrs(AttrFactory);
+  ParsedAttributes DeclSpecAttrs(AttrFactory);

Maybe it might be worth mentioning [class.access.general]/6 somewhere, as it states this: https://eel.is/c++draft/class.access.general#6

daniel-petrovic daniel-petrovic commented on code on Nov 19, 2026 09:08 UTC
Code Review
clang/test/SemaCXX/attr-access-out-of-line.cpp:1
@@ -0,0 +1,82 @@
+// RUN: %clang_cc1 -std=c++20 -Wthread-safety -fsyntax-only -verify %s
+
+struct __attribute__((capability("mutex"))) Mutex {};
+
+namespace ns {
+class C {
+private:
+  Mutex mu_;                        // expected-note 2 {{declared private here}}
+  static constexpr int kAlign = 16; // expected-note 5 {{declared private here}}
+
+public:
+  C() __attribute__((locks_excluded(mu_)));
+  [[clang::requires_capability(mu_)]] ~C();
+  void f1() __attribute__((locks_excluded(mu_)));
+  [[clang::requires_capability(mu_)]] void f2();
+  void f3() __attribute__((locks_excluded(mu_)));
+  void f4() __attribute__((locks_excluded(mu_)));
+  static int s1;
+  static int s2;
+  static int s3;
+  friend void friend_fn();
+};
+
+// Valid out-of-line definitions in namespace scope:
+__attribute__((locks_excluded(C::mu_))) void C::f1() {}
+
+[[clang::requires_capability(C::mu_)]] void C::f2() {}
+
+__attribute__((unused)) [[clang::requires_capability(C::mu_)]]
+__attribute__((locks_excluded(C::mu_))) [[maybe_unused]]
+void C::f3() {}
+
+__attribute__((locks_excluded(C::mu_))) inline void C::f4() {}
+
+__attribute__((locks_excluded(C::mu_))) C::C() {}
+[[clang::requires_capability(C::mu_)]] C::~C() {}
+
+__attribute__((aligned(C::kAlign))) int C::s1 = 0;
+[[gnu::aligned(C::kAlign)]] int C::s2 = 0;
+static_assert(alignof(decltype(C::s1)) <= alignof(int));
+
+__attribute__((locks_excluded(C::mu_))) void friend_fn() {}
+
+// Invalid private accesses on unrelated declarations must still be diagnosed:
+__attribute__((locks_excluded(C::mu_))) // expected-error {{'mu_' is a private member of 'ns::C'}}
+void unrelated_fn() {}
+
+[[clang::requires_capability(C::mu_)]] // expected-error {{'mu_' is a private member of 'ns::C'}}
+void unrelated_fn2() {}
+
+// Multi-declarator group: C::s3 is allowed, unrelated_var is diagnosed.
+__attribute__((aligned(C::kAlign))) int C::s3 = 0, unrelated_var = 0; // expected-error {{'kAlign' is a private member of 'ns::C'}}

what I dont quite understand in __attribute__((aligned(C::kAlign))) int C::s3 = 0, unrelated_var = 0;

above we have:

if (!LeadingAttrDiags.getDelayedDiagnosticPool().pool_empty() && Result &&
      !Result.get().isNull()) {
    ParsingDeclRAIIObject FlushDiags(*this, &LeadingAttrDiags);
    FlushDiags.complete(*Result.get().begin());
  }

would mean the pool is consumed on C::s3, right ? So the whole line actually would pass, but the test expects error on that line. Maybe it's fine, but I'd just like to understand what actually happens and why

Assignees
nigham nigham
Labels
risk: low confidence: 90.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 38.1% (MEDIUM) · Higher = larger blast radius
38.1%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 90.5% · Lower = rushed or shallow review
90.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 3.6% · Safe to merge
3.6%