================
@@ -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) &&
----------------
melver wrote:

In C, a field's attributes are processed while its record is still being 
parsed. A capability attribute written *after* the closing brace only reaches 
the record later, in `ActOnFields`. Many C codebases use that form:

```c
struct Node {
  void (*lock)(void) __attribute__((acquire_capability()));   // warns: 'Node' 
isn't annotated ...
} __attribute__((capability("mutex")));
```

So `checkRecordDeclForAttr(RD)` sees no attribute and warns even though `Node` 
is a capability. C is the main motivation for this change, so could you add 
this case to `attr-capabilities.c`? Possible fixes:
- skip the check while `!RD->isCompleteDefinition()`, as 
`checkRecordTypeForCapability` already does;
- or defer it until the record is finished.

C++ is probably unaffected, because class member attributes are late-parsed 
after the trailing class attributes are applied. A matching C++ test would 
confirm that.

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