emacs/lisp/progmodes/cwarn.el

349 lines
11 KiB
EmacsLisp
Raw Normal View History

;;; cwarn.el --- highlight suspicious C and C++ constructions
1999-12-21 09:08:41 +00:00
2013-01-01 09:11:05 +00:00
;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
1999-12-21 09:08:41 +00:00
;; Author: Anders Lindgren <andersl@andersl.com>
;; Keywords: c, languages, faces
;; X-Url: http://www.andersl.com/emacs
;; Version: 1.3.1
1999-12-21 09:08:41 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
1999-12-21 09:08:41 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
1999-12-21 09:08:41 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1999-12-21 09:08:41 +00:00
;;; Commentary:
;;{{{ Documentation
;; Description:
;;
;; CWarn is a package that highlights suspicious C and C++ constructions.
;;
;; For example, take a look at the following piece of C code:
;;
;; if (x = 0);
;; foo();
;;
;; The code contains two, possibly fatal, bugs. The first is that the
;; assignment operator "=" is used as part of the test; the user
;; probably meant to use the comparison operator "==".
1999-12-21 09:08:41 +00:00
;;
;; The second problem is that an extra semicolon is placed after
;; closing parenthesis of the test expression. This makes the body of
;; the if statement to be an empty statement, not the call to the
;; function "foo", as the user probably intended.
;;
;; This package is capable of highlighting the following C and C++
;; constructions:
;;
;; * Assignments inside expressions, including variations like "+=".
;; * Semicolon following immediately after `if', `for', and `while'
;; (except, of course, after a `do .. while' statement).
;; * C++ functions with reference parameters.
;;
;; Note that none of the constructions highlighted (especially not C++
;; reference parameters) are considered errors by the language
1999-12-21 09:08:41 +00:00
;; definitions.
;; Usage:
;;
;; CWarn is implemented as two minor modes: `cwarn-mode' and
;; `global-cwarn-mode'. The former can be applied to individual buffers
;; and the latter to all buffers.
;;
;; Activate this package by Customize, or by placing the following line
;; into the appropriate init file:
;;
;; (global-cwarn-mode 1)
;;
;; Also, `font-lock-mode' or `global-font-lock-mode' must be enabled.
;; Afterthought:
;;
;; After using this package for several weeks it feels as though I
;; find stupid typo-style bugs while editing rather than at compile-
;; or run-time, if I ever find them.
;;
;; On the other hand, I find myself using assignments inside
;; expressions much more often than I used to do. The reason is that
;; there is no risk of interpreting an assignment operator as a
;; comparison ("hey, the assignment operator is red, duh!").
;; Reporting bugs:
;;
;; Out of the last ten bugs you found, how many did you report?
;;
;; When reporting a bug, please:
;;
;; * Send a mail the maintainer of the package, or to the author
;; if no maintainer exists.
;; * Include the name of the package in the title of the mail, to
;; simplify for the recipient.
;; * State exactly what you did, what happened, and what you expected
;; to see when you found the bug.
;; * If the bug cause an error, set the variable `debug-on-error' to t,
;; repeat the operations that triggered the error and include
1999-12-21 09:08:41 +00:00
;; the backtrace in the letter.
;; * If possible, include an example that activates the bug.
;; * Should you speculate about the cause of the problem, please
;; state explicitly that you are guessing.
;;}}}
;;; Code:
;;{{{ Dependencies
(require 'custom)
(require 'font-lock)
(require 'cc-mode)
;;}}}
;;{{{ Variables
(defgroup cwarn nil
"Highlight suspicious C and C++ constructions."
:version "21.1"
1999-12-21 09:08:41 +00:00
:group 'faces)
(defcustom cwarn-configuration
'((c-mode (not reference))
(c++-mode t))
"List of items each describing which features are enable for a mode.
1999-12-21 09:08:41 +00:00
Each item is on the form (mode featurelist), where featurelist can be
on one of three forms:
* A list of enabled features.
* A list starting with the atom `not' followed by the features
which are not enabled.
* The atom t, that represent that all features are enabled.
See variable `cwarn-font-lock-feature-keywords-alist' for available
features."
:type '(repeat sexp)
1999-12-21 09:08:41 +00:00
:group 'cwarn)
(defcustom cwarn-font-lock-feature-keywords-alist
'((assign . cwarn-font-lock-assignment-keywords)
(semicolon . cwarn-font-lock-semicolon-keywords)
(reference . cwarn-font-lock-reference-keywords))
"An alist mapping a CWarn feature to font-lock keywords.
1999-12-21 09:08:41 +00:00
The keywords could either a font-lock keyword list or a symbol.
If it is a symbol it is assumed to be a variable containing a font-lock
keyword list."
:type '(alist :key-type (choice (const assign)
(const semicolon)
(const reference))
:value-type (sexp :tag "Value"))
1999-12-21 09:08:41 +00:00
:group 'cwarn)
(defcustom cwarn-verbose t
"When nil, CWarn mode will not generate any messages.
1999-12-21 09:08:41 +00:00
Currently, messages are generated when the mode is activated and
deactivated."
:group 'cwarn
:type 'boolean)
(defcustom cwarn-mode-text " CWarn"
"String to display in the mode line when CWarn mode is active.
1999-12-21 09:08:41 +00:00
\(When the string is not empty, make sure that it has a leading space.)"
:tag "CWarn mode text" ; To separate it from `global-...'
:group 'cwarn
:type 'string)
(defcustom cwarn-load-hook nil
"Functions to run when CWarn mode is first loaded."
1999-12-21 09:08:41 +00:00
:tag "Load Hook"
:group 'cwarn
:type 'hook)
;;}}}
;;{{{ The modes
;;;###autoload
(define-minor-mode cwarn-mode
"Minor mode that highlights suspicious C and C++ constructions.
1999-12-21 09:08:41 +00:00
2009-10-05 11:34:15 +00:00
Suspicious constructs are highlighted using `font-lock-warning-face'.
1999-12-21 09:08:41 +00:00
Note, in addition to enabling this minor mode, the major mode must
be included in the variable `cwarn-configuration'. By default C and
C++ modes are included.
With a prefix argument ARG, enable the mode if ARG is positive,
and disable it otherwise. If called from Lisp, enable the mode
if ARG is omitted or nil."
2005-04-04 09:21:06 +00:00
:group 'cwarn :lighter cwarn-mode-text
(cwarn-font-lock-keywords cwarn-mode)
(if font-lock-mode (font-lock-fontify-buffer)))
1999-12-21 09:08:41 +00:00
;;;###autoload
Use declare forms, where possible, to mark obsolete functions. * lisp/allout.el (allout-passphrase-hint-string): Likewise. (allout-init): Use a declare form to mark obsolete. * lisp/calendar/calendar.el (calendar-version): * lisp/calendar/icalendar.el (icalendar-extract-ical-from-buffer) (icalendar-convert-diary-to-ical): * lisp/cus-edit.el (custom-mode): * lisp/ansi-color.el (ansi-color-unfontify-region): * lisp/international/latin1-disp.el (latin1-char-displayable-p): * lisp/progmodes/cwarn.el (turn-on-cwarn-mode): * lisp/progmodes/which-func.el (which-func-update-1): Use define-obsolete-function-alias. * lisp/bookmark.el (bookmark-jump-noselect): Use a declare form to mark this function obsolete. * lisp/calendar/cal-x.el (calendar-two-frame-setup) (calendar-only-one-frame-setup, calendar-one-frame-setup): * lisp/calendar/calendar.el (american-calendar, european-calendar) (calendar-for-loop): * lisp/comint.el (comint-dynamic-simple-complete) (comint-dynamic-complete-as-filename, comint-unquote-filename): * lisp/desktop.el (desktop-load-default): * lisp/dired-x.el (dired-omit-here-always) (dired-hack-local-variables, dired-default-directory): * lisp/emacs-lisp/derived.el (derived-mode-class): * lisp/emacs-lisp/timer.el (timer-set-time-with-usecs): * lisp/emacs-lock.el (toggle-emacs-lock): * lisp/epa.el (epa-display-verify-result): * lisp/epg.el (epg-sign-keys, epg-start-sign-keys) (epg-passphrase-callback-function): * lisp/eshell/esh-util.el (eshell-for): * lisp/eshell/eshell.el (eshell-remove-from-window-buffer-names) (eshell-add-to-window-buffer-names): * lisp/files.el (locate-file-completion): * lisp/imenu.el (imenu-example--create-c-index) (imenu-example--create-lisp-index) (imenu-example--lisp-extract-index-name) (imenu-example--name-and-position): * lisp/international/mule-cmds.el (princ-list): * lisp/international/mule-diag.el (decode-codepage-char): * lisp/international/mule-util.el (detect-coding-with-priority): * lisp/iswitchb.el (iswitchb-read-buffer): * lisp/mail/mailalias.el (mail-complete): * lisp/mail/sendmail.el (mail-sent-via): * lisp/mouse.el (mouse-popup-menubar-stuff, mouse-popup-menubar) (mouse-major-mode-menu): * lisp/password-cache.el (password-read-and-add): * lisp/pcomplete.el (pcomplete-parse-comint-arguments): * lisp/progmodes/sh-script.el (sh-maybe-here-document): * lisp/replace.el (query-replace-regexp-eval): * lisp/savehist.el (savehist-load): * lisp/simple.el (choose-completion-delete-max-match): * lisp/term.el (term-dynamic-simple-complete): * lisp/vc/ediff-init.el (ediff-check-version): * lisp/vc/ediff-wind.el (ediff-choose-window-setup-function-automatically): * lisp/vc/vc.el (vc-diff-switches-list): * lisp/view.el (view-return-to-alist-update): Likewise. * lisp/iswitchb.el (iswitchb-read-buffer): Move code of iswitchb-define-mode-map here, and delete that obsolete function. * lisp/subr.el (eval-next-after-load, makehash, insert-string) (assoc-ignore-representation, assoc-ignore-case): Use declare to mark obsolete. (mode-line-inverse-video): Variable deleted. * lisp/emacs-lisp/byte-run.el (make-obsolete): Doc fix; emphasize that this applies to functions. * lisp/erc/erc.el (erc-send-command): Use define-obsolete-function-alias. * lisp/international/mule-util.el (string-to-sequence): Remove. * lisp/net/newst-backend.el (newsticker-cache-filename): * lisp/net/newst-treeview.el (newsticker-groups-filename): Fix incorrect obsolescence declaration. * lisp/net/snmp-mode.el (snmp-font-lock-keywords-3): Don't use obsolete font-lock-reference-face. * lisp/url/url-parse.el (url-recreate-url-attributes): * lisp/url/url-util.el (url-generate-unique-filename): Use declare to mark obsolete. * src/xdisp.c (mode_line_inverse_video): Delete obsolete variable.
2012-09-25 12:13:02 +08:00
(define-obsolete-function-alias 'turn-on-cwarn-mode 'cwarn-mode "24.1")
1999-12-21 09:08:41 +00:00
;;}}}
;;{{{ Help functions
(defun cwarn-is-enabled (mode &optional feature)
"Non-nil if CWarn FEATURE is enabled for MODE.
2011-11-20 04:48:53 +01:00
FEATURE is an atom representing one construction to highlight.
1999-12-21 09:08:41 +00:00
Check if any feature is enabled for MODE if no feature is specified.
The valid features are described by the variable
`cwarn-font-lock-feature-keywords-alist'."
2011-11-20 04:48:53 +01:00
(let ((mode-configuration (assq mode cwarn-configuration)))
(and mode-configuration
1999-12-21 09:08:41 +00:00
(or (null feature)
2011-11-20 04:48:53 +01:00
(let ((list-or-t (nth 1 mode-configuration)))
1999-12-21 09:08:41 +00:00
(or (eq list-or-t t)
(if (eq (car-safe list-or-t) 'not)
(not (memq feature (cdr list-or-t)))
(memq feature list-or-t))))))))
(defun cwarn-inside-macro ()
"True if point is inside a C macro definition."
(save-excursion
(beginning-of-line)
(while (eq (char-before (1- (point))) ?\\)
(forward-line -1))
(back-to-indentation)
(eq (char-after) ?#)))
(defun cwarn-font-lock-keywords (addp)
2011-11-20 04:48:53 +01:00
"Install/remove keywords into current buffer.
If ADDP is non-nil, install else remove."
1999-12-21 09:08:41 +00:00
(dolist (pair cwarn-font-lock-feature-keywords-alist)
(let ((feature (car pair))
(keywords (cdr pair)))
(if (not (listp keywords))
(setq keywords (symbol-value keywords)))
(if (cwarn-is-enabled major-mode feature)
(funcall (if addp 'font-lock-add-keywords 'font-lock-remove-keywords)
nil keywords)))))
1999-12-21 09:08:41 +00:00
;;}}}
;;{{{ Font-lock keywords and match functions
;; This section contains font-lock keywords. A font lock keyword can
;; either contain a regular expression or a match function. All
;; keywords defined here use match functions since the C and C++
;; constructions highlighted by CWarn are too complex to be matched by
;; regular expressions.
;;
;; A match function should act like a normal forward search. They
;; should return non-nil if they found a candidate and the match data
;; should correspond to the highlight part of the font-lock keyword.
;; The functions should not generate errors, in that case font-lock
1999-12-21 09:08:41 +00:00
;; will fail to highlight the buffer. A match function takes one
;; argument, LIMIT, that represent the end of area to be searched.
;;
;; The variable `cwarn-font-lock-feature-keywords-alist' contains a
;; mapping from CWarn features to the font-lock keywords defined
;; below.
(defmacro cwarn-font-lock-match (re &rest body)
"Match RE but only if BODY holds."
`(let ((res nil))
(while
(progn
(setq res (re-search-forward ,re limit t))
(and res
(save-excursion
(when (match-beginning 1) (goto-char (match-beginning 1)))
(condition-case nil ; In case something barfs.
(not (save-match-data
,@body))
(error t))))))
res))
1999-12-21 09:08:41 +00:00
;;{{{ Assignment in expressions
(defconst cwarn-font-lock-assignment-keywords
'((cwarn-font-lock-match-assignment-in-expression
(1 font-lock-warning-face))))
(defun cwarn-font-lock-match-assignment-in-expression (limit)
"Match assignments inside expressions."
(cwarn-font-lock-match
"[^!<>=]\\(\\([-+*/%&^|]\\|<<\\|>>\\)?=\\)[^=]"
(backward-up-list 1)
(and (memq (following-char) '(?\( ?\[))
(not (progn
(skip-chars-backward " ")
(skip-chars-backward "a-zA-Z0-9_")
(or
;; Default parameter of function.
(c-at-toplevel-p)
(looking-at "for\\>")))))))
1999-12-21 09:08:41 +00:00
;;}}}
;;{{{ Semicolon
(defconst cwarn-font-lock-semicolon-keywords
'((cwarn-font-lock-match-dangerous-semicolon (0 font-lock-warning-face))))
(defun cwarn-font-lock-match-dangerous-semicolon (limit)
"Match semicolons directly after `for', `while', and `if'.
The semicolon after a `do { ... } while (x);' construction is not matched."
(cwarn-font-lock-match
";"
(backward-sexp 2) ; Expression and keyword.
(or (looking-at "\\(for\\|if\\)\\>")
(and (looking-at "while\\>")
(condition-case nil
(progn
(backward-sexp 2) ; Body and "do".
(not (looking-at "do\\>")))
(error t))))))
1999-12-21 09:08:41 +00:00
;;}}}
;;{{{ Reference
(defconst cwarn-font-lock-reference-keywords
'((cwarn-font-lock-match-reference (1 font-lock-warning-face))))
(defun cwarn-font-lock-match-reference (limit)
"Font-lock matcher for C++ reference parameters."
(cwarn-font-lock-match
"[^&]\\(&\\)[^&=]"
(backward-up-list 1)
(and (eq (following-char) ?\()
(not (cwarn-inside-macro))
(c-at-toplevel-p))))
1999-12-21 09:08:41 +00:00
;;}}}
;;}}}
;;{{{ The end
(defun turn-on-cwarn-mode-if-enabled ()
"Turn on CWarn mode in the current buffer if applicable.
The mode is turned if some feature is enabled for the current
`major-mode' in `cwarn-configuration'."
(when (cwarn-is-enabled major-mode) (cwarn-mode 1)))
;;;###autoload
(define-globalized-minor-mode global-cwarn-mode
cwarn-mode turn-on-cwarn-mode-if-enabled)
1999-12-21 09:08:41 +00:00
(provide 'cwarn)
(run-hooks 'cwarn-load-hook)
;;}}}
;;; cwarn.el ends here