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

via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 17 07:45:01 PST 2024


================
@@ -11792,6 +11792,32 @@ static bool CheckMultiVersionFunction(Sema &S, FunctionDecl *NewFD,
                                          OldDecl, Previous);
 }
 
+static void CheckFunctionDeclarationAttributesUsage(Sema &S,
+                                                    FunctionDecl *NewFD) {
+  const bool is_pure = NewFD->hasAttr<PureAttr>();
+  const bool is_const = NewFD->hasAttr<ConstAttr>();
+
+  if (is_pure && is_const) {
+    S.Diag(NewFD->getLocation(), diag::warn_const_attr_with_pure_attr);
+    NewFD->dropAttr<PureAttr>();
+  }
+  if (is_pure || is_const) {
+    if (isa<CXXConstructorDecl>(NewFD)) {
----------------
kelbon wrote:

> I don't agree that a constructor 'returns a value'. Both a constructor and a destructor are the same in that they are functions that modify their 1st parameter (the this parameter) or 'throw'. They are effectively the same thing.
> 
> I don't see how a constructor could ever make sense with 'pure', same as it doesn't make sense on destructors.
> 
> We should be handling them the same.

But user cannot separate the creation of an object and its construction from C++ code (not counting placement new)

```cpp
struct A {
... fields ...
  [[pure]] A(...args);
};

... 

A a = A(args...);
...
```
But on other side user can write:

```cpp
struct A { ... };

[[puer]] A A_constructor(...args);

```

And it will be correct, so, why i was hoping we let user create it less verbose in C++ interface


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


More information about the cfe-commits mailing list