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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 19 06:01:39 PST 2024


================
@@ -11801,6 +11801,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->dropAttrs<PureAttr>();
+  }
+  if (!IsPure && !IsConst)
+    return;
----------------
AaronBallman wrote:

```suggestion
  // If there are no pure or const attributes, there's nothing to check.
  if (!IsPure || !IsConst)
    return;

  // If the function is marked both pure and const, we retain the const attribute
  // because it makes stronger guarantees than the pure attribute, and we drop
  // the pure attribute explicitly to prevent later confusion about semantics.
  if (IsPure && IsConst) {
    S.Diag(NewFD->getLocation(), diag::warn_const_attr_with_pure_attr);
    NewFD->dropAttrs<PureAttr>();
  }
```
Rearranging the logic and adding some comments.

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


More information about the cfe-commits mailing list