================
@@ -352,16 +352,23 @@ static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl 
*D,
   if (Sidx == AL.getNumArgs()) {
     // If we don't have any capability arguments, the attribute implicitly
     // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
-    // a non-static method, and that the class is a (scoped) capability.
-    const auto *MD = dyn_cast<const CXXMethodDecl>(D);
-    if (MD && !MD->isStatic()) {
-      const CXXRecordDecl *RD = MD->getParent();
+    // a non-static method, and that the class is a (scoped) capability. A
+    // function pointer field is likewise called through an object, which the
+    // attribute then refers to.
+    const RecordDecl *RD = nullptr;
+    if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
+      if (!MD->isStatic())
+        RD = MD->getParent();
+    } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
+      RD = FD->getParent();
+    }
+    if (RD) {
       // FIXME -- need to check this again on template instantiation
       if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
----------------
vtjnash wrote:

Done. There appears to be two related places we can make basically the same fix 
in a followup PR:

1. **C++11-spelled method attribute with a trailing capability.** This warns 
falsely that `Node` isn't a capability:
   ```cpp
   struct Node {
     [[clang::acquire_capability]] void lock();
   } __attribute__((capability("mutex")));
   ```
   C++11 attributes aren't late-parsed, so the check runs before the trailing 
attribute is applied. It's the same ordering this PR fixes for fields. The fix 
is to have the `ActOnFields` walk visit methods too.

2. **No check on template instantiation** (the `FIXME -- need to check this 
again on template instantiation` from 2012). With a dependent base, the pattern 
can't tell, and the instantiation is never checked:
   ```cpp
   template <class T> struct Lockable : T {
     void lock() __attribute__((acquire_capability()));
   };
   Lockable<NotCap> bad; // no warning; a non-template `struct Direct : NotCap` 
warns
   ```
   The fix is to recheck in `checkInstantiatedThreadSafetyAttrs` once the 
record is known, for methods and fields.


https://github.com/llvm/llvm-project/pull/242937
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to