all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Functionality to maintain function prototypes in C
Date: Fri, 28 Oct 2016 23:38:04 +0200	[thread overview]
Message-ID: <87wpgsduir.fsf@informatimago.com> (raw)
In-Reply-To: CADdV=MvigPr_eHR9pEkxmh0FrAxG9gRhRdwZKdBCF3W0Sr3_FQ@mail.gmail.com

Nikolai Weibull <now@disu.se> writes:

> Hi!
>
> C has the issue of being compiled in a single pass.  After many years
> of working around this issue by defining my static functions in
> reverse order, which is good for the compiler, but bad for the user,
> I’m now looking to try writing them in a more natural order.  The main
> issue with this is maintaining the static-function prototypes.  I’ve
> looked around, but I can’t find a minor mode that’ll add, update, and
> remove static-function prototypes automatically, either by command or
> at file save, so I was hoping that someone here could point me in the
> right direction.

Once upon a time, I used macros:

In a file named BcInterface.h:

#define PROCEDURE(nam,prm,res)       extern         res      nam  prm    ;


In a file named BcImplementation.h:

#define PROCEDURE(nam,prm,res)                      res      nam  prm


Then it was a simple matter of:

( cat ${source}_header_prefix
  grep PROCEDURE ${source}.c
  cat ${source}_header_suffix ) > ${source}.h

But that was before I used emacs :-)


;;; Code:

(defun c-collect-defun-signatures ()
  "Return a list of C function signatures found in the buffer."
  (let ((signatures '())
        (last-defun -1))
    (goto-char (point-min))
    (end-of-defun)
    (beginning-of-defun)
    (while (/= last-defun (point))
      (setf last-defun (point))
      (when (re-search-forward "\\(.*(.*)\\)[^(){}]*{" nil t)
        (push (match-string 1) signatures))
      (end-of-defun)
      (end-of-defun)
      (beginning-of-defun))
    signatures))

(defun c-update-forward-defun-signatures ()
  "Update the forward function signatures.
If a section doesn't already exist, creates it at point."
  (interactive)
  (let ((signatures (save-excursion (nreverse (c-collect-defun-signatures))))
        (section-begin "\n// BEGIN FORWARD FUNCTION SIGNATURES\n")
        (section-end   "\n// END FORWARD FUNCTION SIGNATURES\n"))
    (goto-char (or (save-excursion
                    (goto-char (point-min))
                    (when (re-search-forward
                           (format "%s.*%s" section-begin section-end)
                           nil t)
                      (delete-region (match-beginning 0) (match-end 0))
                      (match-beginning 0)))
                   (point)))
    (insert section-begin)
    (dolist (signature signatures)
      (insert signature ";\n"))
    (insert section-end)))

(provide 'c-defun)
;;; c-defun.el ends here


The first time, move the point where you want the forward signatures to
be inserted and M-x c-update-forward-defun-signatures RET

Then  M-x c-update-forward-defun-signatures RET again will update the
same section, wherever the point is.

Now, you will have to move all the type definition used in the function
signature before this section…

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk




  reply	other threads:[~2016-10-28 21:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-28 18:55 Functionality to maintain function prototypes in C Nikolai Weibull
2016-10-28 21:38 ` Pascal J. Bourguignon [this message]
2016-10-29  1:30   ` John Mastro
2016-10-29  8:26   ` Nikolai Weibull
2016-10-29  8:34 ` Thien-Thi Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wpgsduir.fsf@informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.