[PATCH] D99877: [Clang] Allow processing of attributes on statements by plugins

Josh Junon via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 5 01:56:46 PDT 2021


Qix- created this revision.
Qix- added a reviewer: aaron.ballman.
Qix- added a project: clang.
Qix- requested review of this revision.
Herald added a subscriber: cfe-commits.

Pretty cut and dry; currently plugins can create attributes for declarations but any that are not recognized produce a diagnostic, leaving no hook for plugins to react to them.

This adds a quick check to the attribute to see if the implementation would like to handle it first, akin to how declarations are handled.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99877

Files:
  clang/include/clang/Sema/ParsedAttr.h
  clang/lib/Sema/SemaStmtAttr.cpp


Index: clang/lib/Sema/SemaStmtAttr.cpp
===================================================================
--- clang/lib/Sema/SemaStmtAttr.cpp
+++ clang/lib/Sema/SemaStmtAttr.cpp
@@ -430,11 +430,12 @@
   case ParsedAttr::AT_Unlikely:
     return handleUnlikely(S, St, A, Range);
   default:
-    // N.B., ClangAttrEmitter.cpp emits a diagnostic helper that ensures a
-    // declaration attribute is not written on a statement, but this code is
-    // needed for attributes in Attr.td that do not list any subjects.
-    S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt)
-        << A << St->getBeginLoc();
+    if (A.getInfo().handleStmtAttribute(S, St, A) == ParsedAttrInfo::NotHandled)
+      // N.B., ClangAttrEmitter.cpp emits a diagnostic helper that ensures a
+      // declaration attribute is not written on a statement, but this code is
+      // needed for attributes in Attr.td that do not list any subjects.
+      S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt)
+          << A << St->getBeginLoc();
     return nullptr;
   }
 }
Index: clang/include/clang/Sema/ParsedAttr.h
===================================================================
--- clang/include/clang/Sema/ParsedAttr.h
+++ clang/include/clang/Sema/ParsedAttr.h
@@ -117,6 +117,13 @@
                                            const ParsedAttr &Attr) const {
     return NotHandled;
   }
+  /// If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this
+  /// Stmt then do so and return either AttributeApplied if it was applied or
+  /// AttributeNotApplied if it wasn't. Otherwise return NotHandled.
+  virtual AttrHandling handleStmtAttribute(Sema &S, Stmt *St,
+                                           const ParsedAttr &Attr) const {
+    return NotHandled;
+  }

   static const ParsedAttrInfo &get(const AttributeCommonInfo &A);
 };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99877.335219.patch
Type: text/x-patch
Size: 1888 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210405/4da6f3e4/attachment.bin>


More information about the cfe-commits mailing list