cwarn.el 11 KB

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