[clang] [Clang] [Sema] Improve support for `__restrict`-qualified member functions (PR #83855)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 9 05:34:33 PDT 2024


================
@@ -5052,6 +5052,21 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
   Function->setInnerLocStart(PatternDecl->getInnerLocStart());
   Function->setRangeEnd(PatternDecl->getEndLoc());
 
+  // Propagate '__restrict' properly.
+  if (auto MD = dyn_cast<CXXMethodDecl>(Function)) {
+    bool Restrict = cast<CXXMethodDecl>(PatternDecl)->isEffectivelyRestrict();
+    if (Restrict != MD->getMethodQualifiers().hasRestrict()) {
+      const auto *FPT = MD->getType()->getAs<FunctionProtoType>();
+      FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
+      if (Restrict)
+        EPI.TypeQuals.addRestrict();
+      else
+        EPI.TypeQuals.removeRestrict();
+      MD->setType(Context.getFunctionType(FPT->getReturnType(),
+                                          FPT->getParamTypes(), EPI));
+    }
+  }
+
----------------
Sirraide wrote:

Actually, I think that’s not it. I got confused by this too just now: this here is about propagating `__restrict`-ness (or lack thereof since we may have to remove it again if the declaration is `__restrict`, but the definition isn’t, and we’re in GCC mode) to the definition of the function only.

Specifically, we can’t just make the declaration `__restrict` when we first create it; the declaration needs to be `__restrict` iff the template declaration is `__restrict`, and the definition needs to be `__restrict` iff the template defintion is `__restrict`; the former should happen automatically as part of template instantiation; this here is only about the latter.

I’m going to expand this comment a bit more to elaborate on this.

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


More information about the cfe-commits mailing list