* net/tramp-sh.el (tramp-sh-handle-vc-registered): Apply heredoc
[emacs.git] / lisp / progmodes / cwarn.el
blob4e4fc1388773f629cc9f420d11108afd64a05399
1 ;;; cwarn.el --- highlight suspicious C and C++ constructions
3 ;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 ;; Author: Anders Lindgren <andersl@andersl.com>
6 ;; Keywords: c, languages, faces
7 ;; X-Url: http://www.andersl.com/emacs
8 ;; Version: 1.3.1
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;{{{ Documentation
29 ;; Description:
31 ;; CWarn is a package that highlights suspicious C and C++ constructions.
33 ;; For example, take a look at the following piece of C code:
35 ;; if (x = 0);
36 ;; foo();
38 ;; The code contains two, possibly fatal, bugs. The first is that the
39 ;; assignment operator "=" is used as part of the test; the user
40 ;; probably meant to use the comparison operator "==".
42 ;; The second problem is that an extra semicolon is placed after
43 ;; closing parenthesis of the test expression. This makes the body of
44 ;; the if statement to be an empty statement, not the call to the
45 ;; function "foo", as the user probably intended.
47 ;; This package is capable of highlighting the following C and C++
48 ;; constructions:
50 ;; * Assignments inside expressions, including variations like "+=".
51 ;; * Semicolon following immediately after `if', `for', and `while'
52 ;; (except, of course, after a `do .. while' statement).
53 ;; * C++ functions with reference parameters.
55 ;; Note that none of the constructions highlighted (especially not C++
56 ;; reference parameters) are considered errors by the language
57 ;; definitions.
59 ;; Usage:
61 ;; CWarn is implemented as two minor modes: `cwarn-mode' and
62 ;; `global-cwarn-mode'. The former can be applied to individual buffers
63 ;; and the latter to all buffers.
65 ;; Activate this package by Customize, or by placing the following line
66 ;; into the appropriate init file:
68 ;; (global-cwarn-mode 1)
70 ;; Also, `font-lock-mode' or `global-font-lock-mode' must be enabled.
72 ;; Afterthought:
74 ;; After using this package for several weeks it feels as though I
75 ;; find stupid typo-style bugs while editing rather than at compile-
76 ;; or run-time, if I ever find them.
78 ;; On the other hand, I find myself using assignments inside
79 ;; expressions much more often than I used to do. The reason is that
80 ;; there is no risk of interpreting an assignment operator as a
81 ;; comparison ("hey, the assignment operator is red, duh!").
83 ;; Reporting bugs:
85 ;; Out of the last ten bugs you found, how many did you report?
87 ;; When reporting a bug, please:
89 ;; * Send a mail the maintainer of the package, or to the author
90 ;; if no maintainer exists.
91 ;; * Include the name of the package in the title of the mail, to
92 ;; simplify for the recipient.
93 ;; * State exactly what you did, what happened, and what you expected
94 ;; to see when you found the bug.
95 ;; * If the bug cause an error, set the variable `debug-on-error' to t,
96 ;; repeat the operations that triggered the error and include
97 ;; the backtrace in the letter.
98 ;; * If possible, include an example that activates the bug.
99 ;; * Should you speculate about the cause of the problem, please
100 ;; state explicitly that you are guessing.
102 ;;}}}
104 ;;; Code:
106 ;;{{{ Dependencies
108 (require 'custom)
109 (require 'font-lock)
110 (require 'cc-mode)
112 ;;}}}
113 ;;{{{ Variables
115 (defgroup cwarn nil
116 "Highlight suspicious C and C++ constructions."
117 :version "21.1"
118 :group 'faces)
120 (defcustom cwarn-configuration
121 '((c-mode (not reference))
122 (c++-mode t))
123 "List of items each describing which features are enable for a mode.
124 Each item is on the form (mode featurelist), where featurelist can be
125 on one of three forms:
127 * A list of enabled features.
128 * A list starting with the atom `not' followed by the features
129 which are not enabled.
130 * The atom t, that represent that all features are enabled.
132 See variable `cwarn-font-lock-feature-keywords-alist' for available
133 features."
134 :type '(repeat sexp)
135 :group 'cwarn)
137 (defcustom cwarn-font-lock-feature-keywords-alist
138 '((assign . cwarn-font-lock-assignment-keywords)
139 (semicolon . cwarn-font-lock-semicolon-keywords)
140 (reference . cwarn-font-lock-reference-keywords))
141 "An alist mapping a CWarn feature to font-lock keywords.
142 The keywords could either a font-lock keyword list or a symbol.
143 If it is a symbol it is assumed to be a variable containing a font-lock
144 keyword list."
145 :type '(alist :key-type (choice (const assign)
146 (const semicolon)
147 (const reference))
148 :value-type (sexp :tag "Value"))
149 :group 'cwarn)
151 (defcustom cwarn-verbose t
152 "When nil, CWarn mode will not generate any messages.
154 Currently, messages are generated when the mode is activated and
155 deactivated."
156 :group 'cwarn
157 :type 'boolean)
159 (defcustom cwarn-mode-text " CWarn"
160 "String to display in the mode line when CWarn mode is active.
162 \(When the string is not empty, make sure that it has a leading space.)"
163 :tag "CWarn mode text" ; To separate it from `global-...'
164 :group 'cwarn
165 :type 'string)
167 (defcustom cwarn-load-hook nil
168 "Functions to run when CWarn mode is first loaded."
169 :tag "Load Hook"
170 :group 'cwarn
171 :type 'hook)
173 ;;}}}
174 ;;{{{ The modes
176 ;;;###autoload
177 (define-minor-mode cwarn-mode
178 "Minor mode that highlights suspicious C and C++ constructions.
180 Suspicious constructs are highlighted using `font-lock-warning-face'.
182 Note, in addition to enabling this minor mode, the major mode must
183 be included in the variable `cwarn-configuration'. By default C and
184 C++ modes are included.
186 With a prefix argument ARG, enable the mode if ARG is positive,
187 and disable it otherwise. If called from Lisp, enable the mode
188 if ARG is omitted or nil."
189 :group 'cwarn :lighter cwarn-mode-text
190 (cwarn-font-lock-keywords cwarn-mode)
191 (if font-lock-mode (font-lock-fontify-buffer)))
193 ;;;###autoload
194 (define-obsolete-function-alias 'turn-on-cwarn-mode 'cwarn-mode "24.1")
196 ;;}}}
197 ;;{{{ Help functions
199 (defun cwarn-is-enabled (mode &optional feature)
200 "Non-nil if CWarn FEATURE is enabled for MODE.
201 FEATURE is an atom representing one construction to highlight.
203 Check if any feature is enabled for MODE if no feature is specified.
205 The valid features are described by the variable
206 `cwarn-font-lock-feature-keywords-alist'."
207 (let ((mode-configuration (assq mode cwarn-configuration)))
208 (and mode-configuration
209 (or (null feature)
210 (let ((list-or-t (nth 1 mode-configuration)))
211 (or (eq list-or-t t)
212 (if (eq (car-safe list-or-t) 'not)
213 (not (memq feature (cdr list-or-t)))
214 (memq feature list-or-t))))))))
216 (defun cwarn-inside-macro ()
217 "True if point is inside a C macro definition."
218 (save-excursion
219 (beginning-of-line)
220 (while (eq (char-before (1- (point))) ?\\)
221 (forward-line -1))
222 (back-to-indentation)
223 (eq (char-after) ?#)))
225 (defun cwarn-font-lock-keywords (addp)
226 "Install/remove keywords into current buffer.
227 If ADDP is non-nil, install else remove."
228 (dolist (pair cwarn-font-lock-feature-keywords-alist)
229 (let ((feature (car pair))
230 (keywords (cdr pair)))
231 (if (not (listp keywords))
232 (setq keywords (symbol-value keywords)))
233 (if (cwarn-is-enabled major-mode feature)
234 (funcall (if addp 'font-lock-add-keywords 'font-lock-remove-keywords)
235 nil keywords)))))
237 ;;}}}
238 ;;{{{ Font-lock keywords and match functions
240 ;; This section contains font-lock keywords. A font lock keyword can
241 ;; either contain a regular expression or a match function. All
242 ;; keywords defined here use match functions since the C and C++
243 ;; constructions highlighted by CWarn are too complex to be matched by
244 ;; regular expressions.
246 ;; A match function should act like a normal forward search. They
247 ;; should return non-nil if they found a candidate and the match data
248 ;; should correspond to the highlight part of the font-lock keyword.
249 ;; The functions should not generate errors, in that case font-lock
250 ;; will fail to highlight the buffer. A match function takes one
251 ;; argument, LIMIT, that represent the end of area to be searched.
253 ;; The variable `cwarn-font-lock-feature-keywords-alist' contains a
254 ;; mapping from CWarn features to the font-lock keywords defined
255 ;; below.
257 (defmacro cwarn-font-lock-match (re &rest body)
258 "Match RE but only if BODY holds."
259 `(let ((res nil))
260 (while
261 (progn
262 (setq res (re-search-forward ,re limit t))
263 (and res
264 (save-excursion
265 (when (match-beginning 1) (goto-char (match-beginning 1)))
266 (condition-case nil ; In case something barfs.
267 (not (save-match-data
268 ,@body))
269 (error t))))))
270 res))
272 ;;{{{ Assignment in expressions
274 (defconst cwarn-font-lock-assignment-keywords
275 '((cwarn-font-lock-match-assignment-in-expression
276 (1 font-lock-warning-face))))
278 (defun cwarn-font-lock-match-assignment-in-expression (limit)
279 "Match assignments inside expressions."
280 (cwarn-font-lock-match
281 "[^!<>=]\\(\\([-+*/%&^|]\\|<<\\|>>\\)?=\\)[^=]"
282 (backward-up-list 1)
283 (and (memq (following-char) '(?\( ?\[))
284 (not (progn
285 (skip-chars-backward " ")
286 (skip-chars-backward "a-zA-Z0-9_")
288 ;; Default parameter of function.
289 (c-at-toplevel-p)
290 (looking-at "for\\>")))))))
292 ;;}}}
293 ;;{{{ Semicolon
295 (defconst cwarn-font-lock-semicolon-keywords
296 '((cwarn-font-lock-match-dangerous-semicolon (0 font-lock-warning-face))))
298 (defun cwarn-font-lock-match-dangerous-semicolon (limit)
299 "Match semicolons directly after `for', `while', and `if'.
300 The semicolon after a `do { ... } while (x);' construction is not matched."
301 (cwarn-font-lock-match
303 (backward-sexp 2) ; Expression and keyword.
304 (or (looking-at "\\(for\\|if\\)\\>")
305 (and (looking-at "while\\>")
306 (condition-case nil
307 (progn
308 (backward-sexp 2) ; Body and "do".
309 (not (looking-at "do\\>")))
310 (error t))))))
312 ;;}}}
313 ;;{{{ Reference
315 (defconst cwarn-font-lock-reference-keywords
316 '((cwarn-font-lock-match-reference (1 font-lock-warning-face))))
318 (defun cwarn-font-lock-match-reference (limit)
319 "Font-lock matcher for C++ reference parameters."
320 (cwarn-font-lock-match
321 "[^&]\\(&\\)[^&=]"
322 (backward-up-list 1)
323 (and (eq (following-char) ?\()
324 (not (cwarn-inside-macro))
325 (c-at-toplevel-p))))
327 ;;}}}
329 ;;}}}
330 ;;{{{ The end
332 (defun turn-on-cwarn-mode-if-enabled ()
333 "Turn on CWarn mode in the current buffer if applicable.
334 The mode is turned if some feature is enabled for the current
335 `major-mode' in `cwarn-configuration'."
336 (when (cwarn-is-enabled major-mode) (cwarn-mode 1)))
338 ;;;###autoload
339 (define-globalized-minor-mode global-cwarn-mode
340 cwarn-mode turn-on-cwarn-mode-if-enabled)
342 (provide 'cwarn)
344 (run-hooks 'cwarn-load-hook)
346 ;;}}}
348 ;;; cwarn.el ends here