help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Finding Unused Identifiers


From: August Karlstrom
Subject: Re: Finding Unused Identifiers
Date: Sat, 04 Mar 2006 19:19:46 GMT
User-agent: Mozilla Thunderbird 1.0.7 (X11/20051013)

Peter Tury wrote:
As the first easiest(?) step it might help if you generate a list of all
words what has only one instance in that file (and that instance is in an
outermost defvar...)? If the list would have a form what is usable in
compilation mode, then one could go throught it easily and check what
should really be removed...

I wrote the following, which basically does what I want:

(defconst my-elisp-global-decl-re
  (concat "^[ \t]*("
          (regexp-opt '("defconst" "defimage" "defmacro" "defsubst"
                        "defun" "defvar"))
          "[ \t]+\\<\\(\\w+\\)\\>"))


(defun my-elisp-unused-globals ()
  "Return a list of unused global identifiers.  Returns a list of
global identifiers in the current buffer declared with defconst,
defimage, defmacro, defsubst, defun or defvar that are never
accessed in the same buffer.  Requires that no local identifier
uses the same name as a global identifier."
  (let ((pos nil)
        (res nil)
        (id nil))
    (save-excursion
      (beginning-of-buffer)
      (setq pos (re-search-forward my-elisp-global-decl-re nil t))
      (while (not (null pos))
        (save-excursion
          (setq id (match-string-no-properties 1))
          (setq pos (re-search-forward
                     (concat "\\<" id "\\>") nil t))
          (when (null pos)
            (setq pos (re-search-backward
                       (concat "\\<" id "\\>") nil t 2)))
          (when (null pos)
            (setq res (cons id res))))
        (setq pos (re-search-forward
                   my-elisp-global-decl-re nil t))))
    res))


(defun my-elisp-list-unused-globals ()
  "Display the list returned by `my-elisp-unused-globals'. Output
is written to a separate buffer."
  (interactive)
  (let ((unused-globals (my-elisp-unused-globals))
        (buffer nil))
    (setq buffer (get-buffer-create "*Elisp Unused Globals*"))
    (when (= (count-windows) 1) (split-window))
    (other-window 1)
    (switch-to-buffer buffer)
    (erase-buffer)
    (if (null unused-globals)
        (insert "No unused global identifiers found.")
      (insert "Found unused global identifier(s):\n\n")
      (mapc '(lambda (x) (insert x) (newline)) unused-globals)
      (switch-to-buffer buffer))))


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature.  This email was infected under the terms of the GNU
General Public License.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]