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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 17 07:22:18 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)) {
----------------
AaronBallman wrote:

> My point is that semantically a constructor has a return value, the user can think of a constructor as a function that returns a value. And in the future we may change the behavior so that marking the constructor 'pure' will make sense.

I don't know if it's common for people to think of a constructor as a function that returns a value, but I can squint and see that line of thinking. CC @erichkeane for more opinions.

> This leads to just ignoring 'pure'/'const' on constructors without warning now to make possible change it in future (but gcc thinks it is UB)

I think we can change it in the future either way, this is about diagnostic wording.

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


More information about the cfe-commits mailing list