[clang] [Clang] Restrict CWG1504 devirtualization to pointer arithmetic on record types. (PR #209593)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 14 14:48:06 PDT 2026


================
@@ -2606,30 +2606,46 @@ CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
   // objects accessed through array subscripts or pointer arithmetic with
   // non-zero offsets, since the dynamic type must match the static type.
   //
-  // A single object is considered to be an array of one element, so p[0]
-  // could still be a derived object, but p[N] for N != 0 cannot.
-  const Expr *Inner = Base->IgnoreParenImpCasts();
-  if (const auto *UO = dyn_cast<UnaryOperator>(Inner))
+  // Since a single object is treated as an array of one element for pointer
+  // arithmetic, p[0] could still designate a derived object, but p[N] for
+  // N != 0 cannot.
+  
+  auto CheckPointerArithmetic = [&](const Expr *Ptr) -> CXXMethodDecl * {
+    Ptr = Ptr->IgnoreParenBaseCasts();
----------------
AZero13 wrote:

To solve this, I've entirely rewritten the logic to ensure we always recompute the devirtualized method from the proven dynamic type.

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


More information about the cfe-commits mailing list