[clang] [clang] WIP: Warn on mismatched RequiresCapability attributes (PR #67520)

Aaron Puchert via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 30 16:56:15 PDT 2024


================
@@ -2260,6 +2260,37 @@ static bool neverReturns(const CFGBlock *B) {
   return false;
 }
 
+template <typename AttrT>
+static SmallVector<const Expr *> collectAttrArgs(const FunctionDecl *FD) {
+  SmallVector<const Expr *> Args;
+  for (const AttrT *A : FD->specific_attrs<AttrT>()) {
+    for (const Expr *E : A->args())
+      Args.push_back(E);
+  }
+
+  return Args;
+}
+
+static void diagnoseMismatchedFunctionAttrs(const FunctionDecl *FD,
+                                            ThreadSafetyHandler &Handler) {
+  assert(FD);
+  FD = FD->getDefinition();
+  assert(FD);
+  auto FDArgs = collectAttrArgs<RequiresCapabilityAttr>(FD);
+
+  for (const FunctionDecl *D = FD->getPreviousDecl(); D;
+       D = D->getPreviousDecl()) {
+    auto DArgs = collectAttrArgs<RequiresCapabilityAttr>(D);
----------------
aaronpuchert wrote:

I don't see why we should restrict this to `requires_capability`. I think pretty much all attributes should be visible to callers except `no_thread_safety_analysis`. This is also [documented](https://clang.llvm.org/docs/ThreadSafetyAnalysis.html):

> Q. Should I put attributes in the header file, or in the .cc/.cpp/.cxx file?
>
> (A) Attributes are part of the formal interface of a function, and should always go in the header, where they are visible to anything that includes the header. Attributes in the .cpp file are not visible outside of the immediate translation unit, which leads to false negatives and false positives.

And later:

> Unlike the other attributes, `NO_THREAD_SAFETY_ANALYSIS` is not part of the interface of a function, and should thus be placed on the function definition (in the `.cc` or `.cpp` file) rather than on the function declaration (in the header).

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


More information about the cfe-commits mailing list