[clang] [BoundsSafety] unify ParseLexedAttribute (PR #186033)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 12 02:54:42 PDT 2026


================
@@ -751,38 +753,56 @@ void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
 
   if (LA.Decls.size() > 0) {
     Decl *D = LA.Decls[0];
-    NamedDecl *ND  = dyn_cast<NamedDecl>(D);
-    RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
 
-    // Allow 'this' within late-parsed attributes.
-    Sema::CXXThisScopeRAII ThisScope(Actions, RD, Qualifiers(),
-                                     ND && ND->isCXXInstanceMember());
+    if (getLangOpts().CPlusPlus) {
+      NamedDecl *ND = dyn_cast<NamedDecl>(D);
+      RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
 
-    if (LA.Decls.size() == 1) {
-      // If the Decl is templatized, add template parameters to scope.
-      ReenterTemplateScopeRAII InDeclScope(*this, D, EnterScope);
+      // Allow 'this' within late-parsed attributes.
+      Sema::CXXThisScopeRAII ThisScope(Actions, RD, Qualifiers(),
+                                       ND && ND->isCXXInstanceMember());
 
-      // If the Decl is on a function, add function parameters to the scope.
+      if (LA.Decls.size() == 1) {
+        // If the Decl is templatized, add template parameters to scope.
+        ReenterTemplateScopeRAII InDeclScope(*this, D, EnterScope);
+
+        // If the Decl is on a function, add function parameters to the scope.
+        bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
+        if (HasFunScope) {
+          InDeclScope.Scopes.Enter(Scope::FnScope | Scope::DeclScope |
+                                   Scope::CompoundStmtScope);
+          Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
+        }
+
+        ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, nullptr,
+                              nullptr, SourceLocation(),
+                              ParsedAttr::Form::GNU(), nullptr);
+
+        if (HasFunScope)
+          Actions.ActOnExitFunctionContext();
+      } else {
+        // If there are multiple decls, then the decl cannot be within the
+        // function scope.
+        ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, nullptr,
+                              nullptr, SourceLocation(),
+                              ParsedAttr::Form::GNU(), nullptr);
+      }
+    } else {
----------------
cor3ntin wrote:

I think you could merge the else statement with the `if` - ie I'm not sure gating on `LA.Decls.size() == 1` make sense, you could probably just define `HasFunScope` as `LA.Decls.size() == 1 && EnterScope && D->isFunctionOrFunctionTemplate();`

https://github.com/llvm/llvm-project/pull/186033


More information about the cfe-commits mailing list