all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Glenn Morris <rgm@gnu.org>
To: rms@gnu.org
Cc: emacs-devel@gnu.org
Subject: Re: suppressing byte-compiler warnings about undefined functions
Date: Mon, 12 Nov 2007 23:20:34 -0500	[thread overview]
Message-ID: <ah8x52wzcd.fsf@fencepost.gnu.org> (raw)
In-Reply-To: E1IrIZ5-0002Bq-LB@fencepost.gnu.org

Richard Stallman wrote:

>     + (defun declare-function (fn file &optional arglist)
>
> Why not &rest?

Because I want to specify the arglist as a single quoted list.

> Breaking the line after the file name would make the example more
> helpful.

OK.

> This would run a lot faster if it just reads the file into a
> temporary buffer and avoids visiting.

Yes. I based the first implementation on authors.el, which uses
visiting for some reason (maybe it needs to process file local
variables to get the right coding system for author names, or
something).

> checkdoc-scan could be far far more efficient. It should scan all
> files for `declare-function' calls, sort them by the file that is
> supposed to define them, then load each file just once to verify.

OK, here's a version that works like that.

This could be used for things other than Emacs, so I no longer think
it should go in admin.el. Where could it be installed?

I would also like to move process-lines out of admin.el and make it
more generally available (in subr.el?). It is used by this code, and
also by authors.el.

I will also install a Makefile rule such that one can use `make
checkdecs' to verify the Emacs lisp/ directory.


(defconst checkdec-warning-buffer "*Check Declarations Warnings*"
  "Name of buffer used to display any checkdec warnings.")

(defun checkdec-scan (file)
  "Scan FILE for `declare-function' calls.
Return a list with elements of the form (FNFILE FN ARGLIST), where
ARGLIST may be absent.  This claims that FNFILE defines FN, with ARGLIST."
  (let ((m (format "Scanning %s..." file))
        alist fnfile fn)
    (message "%s" m)
    (with-temp-buffer
      (insert-file-contents file)
      (while (re-search-forward
              "^[ \t]*(declare-function[ \t]+'\\(\\S-+\\)[ \t]+\
\"\\(\\S-+\\)\"" nil t)
        (setq fn (match-string 1)
              fnfile (match-string 2))
        (or (file-name-absolute-p fnfile)
            (setq fnfile (expand-file-name fnfile (file-name-directory file))))
        (setq alist (cons
                     (list fnfile fn
                           (progn
                             (skip-chars-forward " \t\n'")
                             ;; Use `t' to distinguish no arglist
                             ;; specified from an empty one.
                             (if (looking-at "(")
                                 (read (current-buffer))
                               t)))
                     alist))))
    (message "%sdone" m)
    alist))

(autoload 'byte-compile-arglist-signature "bytecomp")

(defun checkdec-verify (fnfile fnlist)
  "Check that FNFILE contains function definitions matching FNLIST.
Each element of FNLIST has the form (FILE FN ARGLIST), where
ARGLIST is optional.  This means FILE claimed FN was defined in
FNFILE with the specified ARGLIST.  Returns nil if all claims are
found to be true, otherwise a list of errors with elements of the form
\(FILE FN TYPE), where TYPE is a string giving details of the error."
  (let ((m (format "Checking %s..." fnfile))
        re fn sig siglist arglist type errlist)
    (message "%s" m)
    (if (file-exists-p fnfile)
        (with-temp-buffer
          (insert-file-contents fnfile)
          (setq re (format "^(defun[ \t]+%s\\>"
                           (regexp-opt (mapcar (lambda (e) (cadr e)) fnlist)
                                       t)))
          (while (re-search-forward re nil t)
            (skip-chars-forward " \t\n")
            (setq fn (match-string 1)
                  sig (if (looking-at "(") (byte-compile-arglist-signature
                                            (read (current-buffer))))
                  ;; alist of functions and arglist signatures.
                  siglist (cons (cons fn sig) siglist)))))
    (dolist (e fnlist)
      (setq arglist (nth 2 e)
            type
            (if re                     ; re non-nil means found a file
                (if (setq sig (assoc (cadr e) siglist))
                    ;; Recall we use t to mean no arglist specified,
                    ;; to distinguish from an empty arglist.
                    (unless (eq arglist t)
                      (unless (equal (byte-compile-arglist-signature arglist)
                                     (cdr sig))
                        "arglist mismatch"))
                  "function not found")
              "file not found"))
      (when type
        (setq errlist (cons (list (car e) (cadr e) type) errlist))))
    (message "%s%s" m (if errlist "problems found" "OK"))
    errlist))

(defun checkdec-sort (alist)
  "Sort a list with elements FILE (FNFILE ...).
Returned list has elements FNFILE (FILE ...)."
  (let (file fnfile rest sort a)
    (dolist (e alist)
      (setq file (car e))
      (dolist (f (cdr e))
        (setq fnfile (car f)
              rest (cdr f))
        (if (setq a (assoc fnfile sort))
            (setcdr a (append (cdr a) (list (cons file rest))))
          (setq sort (cons (list fnfile (cons file rest)) sort)))))
    sort))

(defun checkdec-warn (file fn fnfile type)
  "Warn that FILE made a false claim about FN in FNFILE.
TYPE is a string giving the nature of the error.  Warning is displayed in
`checkdec-warning-buffer'."
  (display-warning 'checkdec
                   (format "%s said `%s' was defined in %s: %s"
                           (file-name-nondirectory file) fn
                           (file-name-nondirectory fnfile)
                           type)
                   nil checkdec-warning-buffer))

;;;###autoload
(defun checkdec-file (file)
  "Check veracity of all `declare-function' statements in FILE.
See `checkdec-directory' for more information."
  (interactive "fFile to check: ")
  (or (file-exists-p file)
      (error "file `%s' not found" file))
  (let ((m (format "Checking %s..." file))
        err errlist)
    (message "%s" m)
    (dolist (e (checkdec-sort (list (cons file (checkdec-scan file)))))
      (if (setq err (checkdec-verify (car e) (cdr e)))
          (setq errlist (cons (cons (car e) err) errlist))))
    (if (get-buffer checkdec-warning-buffer)
        (kill-buffer checkdec-warning-buffer))
    (dolist (e (checkdec-sort errlist))
      (dolist (f (cdr e))
        (checkdec-warn (car e) (cadr f) (car f) (nth 2 f))))
    (message "%s%s" m (if errlist "problems found" "OK"))
    errlist))

;;;###autoload
(defun checkdec-directory (root)
  "Check veracity of all `declare-function' statements under directory ROOT.
Returns non-nil if any false statements are found.  For this to
work correctly, the statements must adhere to the format
described in the documentation of `declare-function'."
  (interactive "DEroot lisp directory: ")
  (setq root (expand-file-name root))
  (let ((m "Checking `declare-function' statements...")
        (m2 "Finding files with declarations...")
        alist err errlist files)
    (message "%s" m)
    (message "%s" m2)
    (setq files (process-lines "find" root "-name" "*.el"
                                 "-exec" "grep" "-l"
                                 "^[ 	]*(declare-function" "{}" ";"))
    (message "%s%d found" m2 (length files))
    (when files
      (dolist (file files)
        (setq alist (cons (cons file (checkdec-scan file)) alist)))
      ;; Sort so that things are ordered by the files supposed to
      ;; contain the defuns.
      (dolist (e (checkdec-sort alist))
        (if (setq err (checkdec-verify (car e) (cdr e)))
            (setq errlist (cons (cons (car e) err) errlist))))
      (if (get-buffer checkdec-warning-buffer)
          (kill-buffer checkdec-warning-buffer))
      ;; Sort back again so that errors are ordered by the files
      ;; containing the declare-function statements.
      (dolist (e (checkdec-sort errlist))
        (dolist (f (cdr e))
          (checkdec-warn (car e) (cadr f) (car f) (nth 2 f))))
      (message "%s%s" m (if errlist "problems found" "OK"))
      errlist)))

  reply	other threads:[~2007-11-13  4:20 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-10  0:55 suppressing byte-compiler warnings about undefined functions Glenn Morris
2007-11-10  3:13 ` Stefan Monnier
2007-11-10 17:54 ` Richard Stallman
2007-11-11  1:11   ` Glenn Morris
2007-11-11  4:00     ` Glenn Morris
2007-11-11 19:33     ` Richard Stallman
2007-11-13  4:20       ` Glenn Morris [this message]
2007-11-13  9:31         ` martin rudalics
2007-11-14  5:32           ` Glenn Morris
2007-11-14 14:53             ` Stefan Monnier
2007-11-15  3:07             ` Richard Stallman
2007-11-17  3:58               ` Glenn Morris
2007-11-17 23:30                 ` Richard Stallman
2007-11-13 20:02         ` Richard Stallman
2007-11-13 21:05           ` Stefan Monnier
2007-11-14  5:30             ` Glenn Morris
2007-11-15  3:06             ` Richard Stallman
     [not found]     ` <ur9oddrnmg0.fsf@mothra.ics.uci.edu>
2007-11-19  0:39       ` byte-compiler warnings about undefined functions can now be silenced Glenn Morris
2007-11-19  9:04         ` martin rudalics
2007-11-24  3:12           ` Glenn Morris
2007-11-19 19:02         ` Richard Stallman
2007-11-20  4:05           ` Glenn Morris
2007-11-19 19:33         ` Jay Belanger
2007-11-19 20:46           ` Glenn Morris
2007-11-20 12:12           ` Richard Stallman
2007-11-11  8:46 ` suppressing byte-compiler warnings about undefined functions Alan Mackenzie
2007-11-11 15:51   ` Dan Nicolaescu
2007-11-11 16:10     ` Alan Mackenzie
2007-11-11 16:18       ` Dan Nicolaescu
2007-11-11 18:32         ` Dan Nicolaescu
2007-11-11 23:54           ` Richard Stallman

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=ah8x52wzcd.fsf@fencepost.gnu.org \
    --to=rgm@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=rms@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.