[clang] [Clang] Disallow explicit object parameters in more contexts (PR #89078)

via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 17 07:47:55 PDT 2024


================
@@ -5287,6 +5287,36 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
       // Check for auto functions and trailing return type and adjust the
       // return type accordingly.
       if (!D.isInvalidType()) {
+        // [dcl.fct]p6:
+        //
+        // An explicit-object-parameter-declaration is a parameter-declaration
+        // with a this specifier. An explicit-object-parameter-declaration shall
+        // appear only as the first parameter-declaration of a
+        // parameter-declaration-list of either:
+        //
+        // - a member-declarator that declares a member function [class.mem], or
+        // - a lambda-declarator [expr.prim.lambda].
+        DeclaratorContext C = D.getContext();
+        ParmVarDecl *First =
+            FTI.NumParams
+                ? dyn_cast_if_present<ParmVarDecl>(FTI.Params[0].Param)
+                : nullptr;
+        if (First && First->isExplicitObjectParameter() &&
+            C != DeclaratorContext::LambdaExpr &&
+
+            // Either not a member or nested declarator in a member.
+            (C != DeclaratorContext::Member ||
+             D.getInnermostNonParenChunk() != &DeclType) &&
+
+            // Allow out-of-line definitions if we have a scope spec.
+            D.getCXXScopeSpec().isEmpty()) {
----------------
Sirraide wrote:

Oh yeah, good point, I didn’t think of that. 

I think I should be able to borrow some code from the `MemberPointer` case below since that performs essentially the check that we need here on the scope spec (I was going to put this entire check in that case first, but then I noticed that this is also broken for e.g. regular function pointers, which is why I put this here instead).

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


More information about the cfe-commits mailing list