[clang] Warning for incorrect useof 'pure' attribute (PR #78200)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 17 08:39:41 PST 2024


================
@@ -11792,6 +11792,26 @@ static bool CheckMultiVersionFunction(Sema &S, FunctionDecl *NewFD,
                                          OldDecl, Previous);
 }
 
+static void CheckFunctionDeclarationAttributesUsage(Sema &S,
+                                                    FunctionDecl *NewFD) {
+  bool IsPure = NewFD->hasAttr<PureAttr>();
+  bool IsConst = NewFD->hasAttr<ConstAttr>();
+
+  if (IsPure && IsConst) {
+    S.Diag(NewFD->getLocation(), diag::warn_const_attr_with_pure_attr);
+    NewFD->dropAttr<PureAttr>();
+  }
+  if (IsPure || IsConst) {
+    // constructors and destructors also functions which returns void
+    if (NewFD->getReturnType()->isVoidType()) {
+      S.Diag(NewFD->getLocation(), diag::warn_pure_function_returns_void)
+          << IsConst;
+      NewFD->dropAttr<PureAttr>();
+      NewFD->dropAttr<ConstAttr>();
----------------
erichkeane wrote:

There is perhaps use in changing `dropAttr` to be a variadic template, and dropping a bunch at once.  I realize 'N' is small here, but turns from O(2N) to O(N).  So in DeclBase.h:

```
template <typename ...Ts>
  void dropAttr() {
    if (!HasAttrs) return;
 
    AttrVec &Vec = getAttrs();
    llvm::erase_if(Vec, [](Attr *A) { return isa<Ts...>(A)  ; });
 
    if (Vec.empty())
      HasAttrs = false;
  }
};
```

WDYT Aaron?  Is this something we'd use?

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


More information about the cfe-commits mailing list