Generate a defmacro form that transforms code like this:
(with-wrap-attempt ("function ~S" name-or-function) name-or-function (with-slots (name type c-symbol fields) fun (let* ((fun-name (intern (symbol-name name) package)) (param-names (mapcar #'foreign-type-name fields)) (param-syms (mapcar (lambda (x) (gensym (symbol-name x))) param-names)) (make-function-p (not (foreign-function-variadic-p fun))) (maybe-cbv-return (when (cbv-return-p fun) (list 'return-value)))) (with-gensyms (!fun !fields rest) (let ((form (if make-function-p `(defun ,fun-name (,@maybe-cbv-return ,@param-names ,@(when (foreign-function-variadic-p fun) `(&rest ,rest))) ,@(when maybe-cbv-return `((declare (ignorable ,@maybe-cbv-return)))) ,(let ((!fun (find-function name-or-function))) (with-slots ((!fields fields)) !fun (foreign-to-ffi (and (car !fields) (foreign-type (car !fields))) param-syms param-names !fields (make-foreign-funcall !fun (and maybe-cbv-return 'return-value) param-syms (when (foreign-function-variadic-p fun) rest)))))) `(defmacro ,fun-name (,@maybe-cbv-return ,@param-names ,@(when (foreign-function-variadic-p fun) `(&rest ,rest))) ,@(when maybe-cbv-return `((declare (ignorable ,@maybe-cbv-return)))) (let ((,!fun (find-function ',name-or-function))) (with-slots ((,!fields fields)) ,!fun (foreign-to-ffi (and (car ,!fields) (foreign-type (car ,!fields))) ',param-syms (list ,@param-names) ,!fields (make-foreign-funcall ,!fun ,(and maybe-cbv-return 'return-value) ',param-syms ,(when (foreign-function-variadic-p fun) rest))))))))) `(progn ,@(when (foreign-function-cbv-p fun) (list (funcall *build-libffi-definition* fun))) ,form))))))

Pattern to recognize: Macro call that should be transformed by with-wrap-attempt

Macro definition:
(defmacro with-wrap-attempt ((&optional format-control &rest format-args) wrappable-name &body body)
  `(call-with-wrap-attempt ,wrappable-name (lambda () ,@body) ,format-control (list ,@format-args)))

Macroexpansion:
(call-with-wrap-attempt name-or-function (lambda nil (with-slots (name type c-symbol fields) fun (let* ((fun-name (intern (symbol-name name) package)) (param-names (mapcar (function foreign-type-name) fields)) (param-syms (mapcar (lambda (x) (gensym (symbol-name x))) param-names)) (make-function-p (not (foreign-function-variadic-p fun))) (maybe-cbv-return (when (cbv-return-p fun) (list (quote return-value))))) (with-gensyms (!fun !fields rest) (let ((form (if make-function-p (sb-int:quasiquote (defun #S(sb-impl::comma :expr fun-name :kind 0) (#S(sb-impl::comma :expr maybe-cbv-return :kind 2) #S(sb-impl::comma :expr param-names :kind 2) #S(sb-impl::comma :expr (when (foreign-function-variadic-p fun) (sb-int:quasiquote (&rest #S(sb-impl::comma :expr rest :kind 0)))) :kind 2)) #S(sb-impl::comma :expr (when maybe-cbv-return (sb-int:quasiquote ((declare (ignorable #S(sb-impl::comma :expr maybe-cbv-return :kind 2)))))) :kind 2) #S(sb-impl::comma :expr (let ((!fun (find-function name-or-function))) (with-slots ((!fields fields)) !fun (foreign-to-ffi (and (car !fields) (foreign-type (car !fields))) param-syms param-names !fields (make-foreign-funcall !fun (and maybe-cbv-return (quote return-value)) param-syms (when (foreign-function-variadic-p fun) rest))))) :kind 0))) (sb-int:quasiquote (defmacro #S(sb-impl::comma :expr fun-name :kind 0) (#S(sb-impl::comma :expr maybe-cbv-return :kind 2) #S(sb-impl::comma :expr param-names :kind 2) #S(sb-impl::comma :expr (when (foreign-function-variadic-p fun) (sb-int:quasiquote (&rest #S(sb-impl::comma :expr rest :kind 0)))) :kind 2)) #S(sb-impl::comma :expr (when maybe-cbv-return (sb-int:quasiquote ((declare (ignorable #S(sb-impl::comma :expr maybe-cbv-return :kind 2)))))) :kind 2) (let ((#S(sb-impl::comma :expr !fun :kind 0) (find-function (quote #S(sb-impl::comma :expr name-or-function :kind 0))))) (with-slots ((#S(sb-impl::comma :expr !fields :kind 0) fields)) #S(sb-impl::comma :expr !fun :kind 0) (foreign-to-ffi (and (car #S(sb-impl::comma :expr !fields :kind 0)) (foreign-type (car #S(sb-impl::comma :expr !fields :kind 0)))) (quote #S(sb-impl::comma :expr param-syms :kind 0)) (list #S(sb-impl::comma :expr param-names :kind 2)) #S(sb-impl::comma :expr !fields :kind 0) (make-foreign-funcall #S(sb-impl::comma :expr !fun :kind 0) #S(sb-impl::comma :expr (and maybe-cbv-return (quote return-value)) :kind 0) (quote #S(sb-impl::comma :expr param-syms :kind 0)) #S(sb-impl::comma :expr (when (foreign-function-variadic-p fun) rest) :kind 0)))))))))) (sb-int:quasiquote (progn #S(sb-impl::comma :expr (when (foreign-function-cbv-p fun) (list (funcall *build-libffi-definition* fun))) :kind 2) #S(sb-impl::comma :expr form :kind 0)))))))) "function ~S" (list name-or-function))