[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Functionality to maintain function prototypes in C
From: |
Pascal J. Bourguignon |
Subject: |
Re: Functionality to maintain function prototypes in C |
Date: |
Fri, 28 Oct 2016 23:38:04 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) |
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