]> code.delx.au - gnu-emacs-elpa/blob - packages/ada-mode/ada-gnat-compile.el
ada-mode 5.1.3, wisi 1.0.4
[gnu-emacs-elpa] / packages / ada-mode / ada-gnat-compile.el
1 ;; Ada mode compiling functionality provided by the 'gnat'
2 ;; tool.
3 ;;
4 ;; These tools are all Ada-specific; use Makefiles for multi-language
5 ;; GNAT compilation tools.
6 ;;
7 ;; GNAT is provided by AdaCore; see http://libre.adacore.com/
8 ;;
9 ;;; Copyright (C) 2012 - 2014 Free Software Foundation, Inc.
10 ;;
11 ;; Author: Stephen Leake <stephen_leake@member.fsf.org>
12 ;; Maintainer: Stephen Leake <stephen_leake@member.fsf.org>
13 ;;
14 ;; This file is part of GNU Emacs.
15 ;;
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
20 ;;
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
25 ;;
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;;
29 ;;; Usage:
30 ;;
31 ;; Emacs should enter Ada mode automatically when you load an Ada
32 ;; file, based on the file extension.
33 ;;
34 ;; By default, ada-mode is configured to load this file, so nothing
35 ;; special needs to done to use it.
36
37 (require 'compile)
38 (require 'gnat-core)
39
40 ;;;;; code
41
42 ;;;; compiler message handling
43
44 (defun ada-gnat-compilation-filter ()
45 "Filter to add text properties to secondary file references.
46 For `compilation-filter-hook'."
47 (save-excursion
48 (goto-char compilation-filter-start)
49
50 ;; primary references are handled by font-lock functions; see
51 ;; `compilation-mode-font-lock-keywords'.
52 ;;
53 ;; compilation-filter might insert partial lines, or it might insert multiple lines
54 (goto-char (line-beginning-position))
55 (while (not (eobp))
56 ;; We don't want 'next-error' to always go to secondary
57 ;; references, so we _don't_ set 'compilation-message text
58 ;; property. Instead, we set 'ada-secondary-error, so
59 ;; `ada-goto-secondary-error' will handle it. We also set
60 ;; fonts, so the user can see the reference.
61
62 ;; typical secondary references look like:
63 ;;
64 ;; trivial_productions_test.adb:57:77: ==> in call to "Get" at \
65 ;; opentoken-token-enumerated-analyzer.ads:88, instance at line 41
66 ;;
67 ;; c:/foo/bar/lookahead_test.adb:379:14: found type access to "Standard.String" defined at line 379
68 ;;
69 ;; lookahead_test.ads:23:09: "Name" has been inherited from subprogram at aunit-simple_test_cases.ads:47
70 ;;
71 ;; lalr.adb:668:37: non-visible declaration at analyzer.ads:60, instance at parser.ads:38
72 ;;
73 ;; save the file from the primary reference, look for "*.ad?:nn", "at line nnn"
74
75 (let (file)
76 (when (looking-at "^\\(\\(.:\\)?[^ :\n]+\\):")
77 (setq file (match-string-no-properties 1)))
78
79 (skip-syntax-forward "^-"); space following primary reference
80
81 (while (search-forward-regexp "\\s-\\(\\([^[:blank:]]+\\.[[:alpha:]]+\\):\\([0-9]+\\)\\)"
82 (line-end-position) t)
83
84 (goto-char (match-end 0))
85 (with-silent-modifications
86 (compilation--put-prop 2 'font-lock-face compilation-info-face); file
87 (compilation--put-prop 3 'font-lock-face compilation-line-face); line
88 (put-text-property
89 (match-beginning 0) (match-end 0)
90 'ada-secondary-error
91 (list
92 (match-string-no-properties 2); file
93 (string-to-number (match-string-no-properties 3)); line
94 1)); column
95 ))
96
97 (when (search-forward-regexp "\\(at line \\)\\([0-9]+\\)" (line-end-position) t)
98 (with-silent-modifications
99 (compilation--put-prop 1 'font-lock-face compilation-info-face); "at line" instead of file
100 (compilation--put-prop 2 'font-lock-face compilation-line-face); line
101 (put-text-property
102 (match-beginning 1) (match-end 1)
103 'ada-secondary-error
104 (list
105 file
106 (string-to-number (match-string-no-properties 2)); line
107 1)); column
108 ))
109 (forward-line 1))
110 )
111 ))
112
113 (defun ada-gnat-debug-filter ()
114 ;; call ada-gnat-compilation-filter with `compilation-filter-start' bound
115 (interactive)
116 (beginning-of-line)
117 (let ((compilation-filter-start (point)))
118 (ada-gnat-compilation-filter)))
119
120 ;;;;; auto fix compilation errors
121
122 (defconst ada-gnat-quoted-name-regexp
123 "\"\\([a-zA-Z0-9_.']+\\)\""
124 "regexp to extract the quoted names in error messages")
125
126 (defconst ada-gnat-quoted-punctuation-regexp
127 "\"\\([,:;=()|]+\\)\""
128 "regexp to extract quoted punctuation in error messages")
129
130 (defvar ada-gnat-fix-error-hook nil
131 "For `ada-fix-error-alist'.")
132
133 (defun ada-gnat-misspelling ()
134 "Return correct spelling from current compiler error, if there are corrections offered.
135 Prompt user if more than one."
136 ;; wisi-output.adb:115:41: no selector "Productions" for type "RHS_Type" defined at wisi.ads:77
137 ;; wisi-output.adb:115:41: invalid expression in loop iterator
138 ;; wisi-output.adb:115:42: possible misspelling of "Production"
139 ;; wisi-output.adb:115:42: possible misspelling of "Production"
140 ;;
141 ;; column number can vary, so only check the line number
142
143 (let ((line (progn (beginning-of-line) (nth 1 (compilation--message->loc (ada-get-compilation-message)))))
144 done choices)
145 (while (not done)
146 (forward-line 1)
147 (setq done (or (not (ada-get-compilation-message))
148 (not (equal line (nth 1 (compilation--message->loc (ada-get-compilation-message)))))))
149 (when (and (not done)
150 (progn
151 (skip-syntax-forward "^-")
152 (forward-char 1)
153 (looking-at (concat "possible misspelling of " ada-gnat-quoted-name-regexp))))
154 (push (match-string 1) choices)))
155
156 ;; return correct spelling
157 (cond
158 ((= 0 (length choices))
159 nil)
160
161 ((= 1 (length choices))
162 (car choices))
163
164 (t ;; multiple choices
165 (completing-read "correct spelling: " choices))
166 )))
167
168 (defun ada-gnat-fix-error (msg source-buffer source-window)
169 "For `ada-gnat-fix-error-hook'."
170 (let ((start-pos (point))
171 message-column
172 result)
173 ;; Move to start of error message text
174 (skip-syntax-forward "^-")
175 (forward-char 1)
176 (setq message-column (current-column))
177
178 ;; recognize it, handle it
179 (setq
180 result
181 (unwind-protect
182 (cond
183 ;; It is tempting to define an alist of (MATCH . ACTION), but
184 ;; that is too hard to debug
185 ;;
186 ;; This list will get long, so let's impose some order.
187 ;;
188 ;; First expressions that start with a named regexp, alphabetical by variable name.
189 ;;
190 ;; Then expressions that start with a string, alphabetical by string.
191 ;;
192 ;; Then style errors.
193
194 ((looking-at (concat ada-gnat-quoted-name-regexp " is not visible"))
195 (let ((ident (match-string 1))
196 (done nil)
197 (file-line-struct (progn (beginning-of-line) (ada-get-compilation-message)))
198 pos choices unit-name)
199 ;; next line may contain a reference to where ident is
200 ;; defined; if present, it will have been marked by
201 ;; ada-gnat-compilation-filter:
202 ;;
203 ;; gnatquery.adb:255:13: "Has_Element" is not visible
204 ;; gnatquery.adb:255:13: non-visible declaration at a-convec.ads:68, instance at gnatcoll-arg_lists.ads:157
205 ;; gnatquery.adb:255:13: non-visible declaration at a-coorse.ads:62, instance at gnatcoll-xref.ads:912
206 ;; gnatquery.adb:255:13: non-visible declaration at a-coorse.ads:62, instance at gnatcoll-xref.ads:799
207 ;; gnatquery.adb:255:13: non-visible declaration at gnatcoll-xref.ads:314
208 ;;
209 ;; or the next line may contain "multiple use clauses cause hiding"
210 ;;
211 ;; the lines after that may contain alternate matches;
212 ;; collect all, let user choose.
213 (forward-line 1)
214 (unless (looking-at ".* multiple use clauses cause hiding")
215 (while (not done)
216 (let ((limit (1- (line-end-position))))
217 ;; 1- because next compilation error is at next line beginning
218 (setq done (not
219 (and
220 (equal file-line-struct (ada-get-compilation-message))
221 (setq pos (next-single-property-change (point) 'ada-secondary-error nil limit))
222 (< pos limit))))
223 (when (not done)
224 (let* ((item (get-text-property pos 'ada-secondary-error))
225 (unit-file (nth 0 item)))
226 (add-to-list 'choices (ada-ada-name-from-file-name unit-file))
227 (goto-char (1+ pos))
228 (goto-char (1+ (next-single-property-change (point) 'ada-secondary-error nil limit)))
229 (when (eolp) (forward-line 1))
230 ))
231 )));; unless while let
232
233 (cond
234 ((= 0 (length choices))
235 (setq unit-name nil))
236
237 ((= 1 (length choices))
238 (setq unit-name (car choices)))
239
240 (t ;; multiple choices
241 (setq unit-name
242 (completing-read "package name: " choices)))
243 );; cond
244
245 (when unit-name
246 (pop-to-buffer source-buffer)
247 ;; We either need to add a with_clause for a package, or
248 ;; prepend the package name here (or add a use clause, but I
249 ;; don't want to do that automatically).
250 ;;
251 ;; If we need to add a with_clause, unit-name may be only
252 ;; the prefix of the real package name, but in that case
253 ;; we'll be back after the next compile; no way to get the
254 ;; full package name (without the function/type name) now.
255 ;; Note that we can't use gnat find, because the code
256 ;; doesn't compile.
257 (cond
258 ((looking-at (concat unit-name "\\."))
259 (ada-fix-add-with-clause unit-name))
260 (t
261 (ada-fix-insert-unit-name unit-name)
262 (insert ".")))
263 t) ;; success, else nil => fail
264 ))
265
266 ((or (looking-at (concat ada-gnat-quoted-name-regexp " is undefined"))
267 (looking-at (concat ada-gnat-quoted-name-regexp " is not a predefined library unit")))
268 ;; We either need to add a with_clause for a package, or
269 ;; something is spelled wrong.
270 (save-excursion
271 (let ((unit-name (match-string 1))
272 (correct-spelling (ada-gnat-misspelling)))
273 (if correct-spelling
274 (progn
275 (pop-to-buffer source-buffer)
276 (search-forward unit-name)
277 (replace-match correct-spelling))
278
279 ;; else assume missing with
280 (pop-to-buffer source-buffer)
281 (ada-fix-add-with-clause unit-name))))
282 t)
283
284 ((looking-at (concat ada-gnat-quoted-name-regexp " not declared in " ada-gnat-quoted-name-regexp))
285 (save-excursion
286 (let ((child-name (match-string 1))
287 (correct-spelling (ada-gnat-misspelling)))
288 (if correct-spelling
289 (progn
290 (setq correct-spelling (match-string 1))
291 (pop-to-buffer source-buffer)
292 (search-forward child-name)
293 (replace-match correct-spelling))
294
295 ;; else guess that "child" is a child package, and extend the with_clause
296 (pop-to-buffer source-buffer)
297 (ada-fix-extend-with-clause child-name))))
298 t)
299
300 ((looking-at (concat ada-gnat-quoted-punctuation-regexp
301 " should be "
302 ada-gnat-quoted-punctuation-regexp))
303 (let ((bad (match-string-no-properties 1))
304 (good (match-string-no-properties 2)))
305 (pop-to-buffer source-buffer)
306 (looking-at bad)
307 (delete-region (match-beginning 0) (match-end 0))
308 (insert good))
309 t)
310
311 ;;;; strings
312 ((looking-at (concat "misspelling of " ada-gnat-quoted-name-regexp))
313 (let ((expected-name (match-string 1)))
314 (pop-to-buffer source-buffer)
315 (looking-at ada-name-regexp)
316 (delete-region (match-beginning 1) (match-end 1))
317 (insert expected-name))
318 t)
319
320 ((looking-at (concat "\"end " ada-name-regexp ";\" expected"))
321 (let ((expected-name (match-string 1)))
322 (pop-to-buffer source-buffer)
323 (if (looking-at (concat "end " ada-name-regexp ";"))
324 (progn
325 (goto-char (match-end 1)) ; just before ';'
326 (delete-region (match-beginning 1) (match-end 1)))
327 ;; else we have just 'end;'
328 (forward-word 1)
329 (insert " "))
330 (insert expected-name))
331 t)
332
333 ((looking-at "expected an access type")
334 (progn
335 (set-buffer source-buffer)
336 (backward-char 1)
337 (when (looking-at "\\.all")
338 (delete-char 4)
339 t)))
340
341 ((looking-at (concat "expected \\(private \\)?type " ada-gnat-quoted-name-regexp))
342 (let ((type (match-string 2)))
343 (forward-line 1)
344 (move-to-column message-column)
345 (cond
346 ((looking-at "found type access")
347 (pop-to-buffer source-buffer)
348 (if (looking-at "'Access")
349 (kill-word 1)
350 (forward-word 1)
351 (insert ".all"))
352 t)
353 ((looking-at "found type .*_Access_Type")
354 ;; assume just need '.all'
355 (pop-to-buffer source-buffer)
356 (forward-word 1)
357 (insert ".all")
358 t)
359 )))
360
361 ((looking-at "extra \".\" ignored")
362 (set-buffer source-buffer)
363 (delete-char 1)
364 t)
365
366 ((looking-at (concat "keyword " ada-gnat-quoted-name-regexp " expected here"))
367 (let ((expected-keyword (match-string 1)))
368 (pop-to-buffer source-buffer)
369 (insert " " expected-keyword))
370 t)
371
372 ((looking-at "\\(?:possible \\)?missing \"with \\([a-zA-Z0-9_.]+\\);")
373 ;; also 'possible missing "with Ada.Text_IO; use Ada.Text_IO"' - ignoring the 'use'
374 (let ((package-name (match-string-no-properties 1)))
375 (pop-to-buffer source-buffer)
376 ;; FIXME (later): should check if prefix is already with'd, extend it
377 (ada-fix-add-with-clause package-name))
378 t)
379
380 ;; must be after above
381 ((looking-at "missing \"\\(.+\\)\"")
382 (let ((stuff (match-string-no-properties 1)))
383 (set-buffer source-buffer)
384 (insert (concat stuff)));; if missing ")", don't need space; otherwise do?
385 t)
386
387 ((looking-at "No legal interpretation for operator")
388 (forward-line 1)
389 (move-to-column message-column)
390 (looking-at (concat "use clause on " ada-gnat-quoted-name-regexp))
391 (let ((package (match-string 1)))
392 (pop-to-buffer source-buffer)
393 (ada-fix-add-use package))
394 t)
395
396 ((looking-at (concat "no selector " ada-gnat-quoted-name-regexp))
397 ;; Check next line for spelling error.
398 (save-excursion
399 (let ((unit-name (match-string 1))
400 (correct-spelling (ada-gnat-misspelling)))
401 (when correct-spelling
402 (pop-to-buffer source-buffer)
403 (search-forward unit-name)
404 (replace-match correct-spelling)
405 t))))
406
407 ((looking-at (concat "operator for \\(private \\)?type " ada-gnat-quoted-name-regexp))
408 (let ((type (match-string 2)))
409 (pop-to-buffer source-buffer)
410 (ada-goto-declarative-region-start)
411 (newline-and-indent)
412 (insert "use type " type ";"))
413 t)
414
415 ((looking-at "parentheses required for unary minus")
416 (set-buffer source-buffer)
417 (insert "(")
418 (forward-word 1)
419 (insert ")")
420 t)
421
422 ((looking-at "prefix of dereference must be an access type")
423 (pop-to-buffer source-buffer)
424 ;; point is after '.' in '.all'
425 (delete-region (- (point) 1) (+ (point) 3))
426 t)
427
428 ;;;; warnings
429 ((looking-at (concat "warning: " ada-gnat-quoted-name-regexp " is already use-visible"))
430 ;; just delete the 'use'; assume it's on a line by itself.
431 (pop-to-buffer source-buffer)
432 (beginning-of-line)
433 (delete-region (point) (progn (forward-line 1) (point)))
434 t)
435
436 ((looking-at (concat "warning: " ada-gnat-quoted-name-regexp " is not modified, could be declared constant"))
437 (pop-to-buffer source-buffer)
438 (search-forward ":")
439 (forward-comment (- (point-max) (point)))
440 ;; "aliased" must be before "constant", so check for it
441 (when (looking-at "aliased")
442 (forward-word 1)
443 (forward-char 1))
444 (insert "constant ")
445 t)
446
447 ((looking-at (concat "warning: constant " ada-gnat-quoted-name-regexp " is not referenced"))
448 (let ((constant (match-string 1)))
449 (pop-to-buffer source-buffer)
450 (end-of-line)
451 (newline-and-indent)
452 (insert "pragma Unreferenced (" constant ");"))
453 t)
454
455 ((looking-at (concat "warning: formal parameter " ada-gnat-quoted-name-regexp " is not referenced"))
456 (let ((param (match-string 1)))
457 (pop-to-buffer source-buffer)
458 (ada-goto-declarative-region-start)
459 (newline-and-indent)
460 (insert "pragma Unreferenced (" param ");"))
461 t)
462
463 ((looking-at (concat "warning: formal parameter " ada-gnat-quoted-name-regexp " is not modified"))
464 (let ((param (match-string 1))
465 (mode-regexp "\"\\([in out]+\\)\"")
466 new-mode
467 old-mode)
468 (forward-line 1)
469 (search-forward-regexp
470 (concat "mode could be " mode-regexp " instead of " mode-regexp))
471 (setq new-mode (match-string 1))
472 (setq old-mode (match-string 2))
473 (pop-to-buffer source-buffer)
474 (search-forward old-mode)
475 (replace-match new-mode)
476 (ada-align)
477 )
478 t)
479
480 ((or
481 (looking-at (concat "warning: no entities of " ada-gnat-quoted-name-regexp " are referenced$"))
482 (looking-at (concat "warning: unit " ada-gnat-quoted-name-regexp " is never instantiated$"))
483 (looking-at "warning: redundant with clause"))
484 ;; just delete the 'with'; assume it's on a line by itself.
485 (pop-to-buffer source-buffer)
486 (beginning-of-line)
487 (delete-region (point) (progn (forward-line 1) (point)))
488 t)
489
490 ((looking-at (concat "warning: variable " ada-gnat-quoted-name-regexp " is assigned but never read"))
491 (let ((param (match-string 1)))
492 (pop-to-buffer source-buffer)
493 (ada-goto-end)
494 (newline-and-indent)
495 (insert "pragma Unreferenced (" param ");"))
496 t)
497
498 ((looking-at (concat "warning: unit " ada-gnat-quoted-name-regexp " is not referenced$"))
499 ;; just delete the 'with'; assume it's on a line by itself.
500 (pop-to-buffer source-buffer)
501 (beginning-of-line)
502 (delete-region (point) (progn (forward-line 1) (point)))
503 t)
504
505 ;;;; style errors
506 ((looking-at "(style) \".*\" in wrong column")
507 (set-buffer source-buffer)
508 (funcall indent-line-function)
509 t)
510
511 ((looking-at "(style) bad capitalization, mixed case required")
512 (set-buffer source-buffer)
513 (forward-word)
514 (ada-case-adjust-identifier)
515 t)
516
517 ((looking-at (concat "(style) bad casing of " ada-gnat-quoted-name-regexp))
518 (let ((correct (match-string-no-properties 1))
519 end)
520 ;; gnat leaves point on first bad character, but we need to replace the whole word
521 (set-buffer source-buffer)
522 (skip-syntax-backward "w_")
523 (setq end (point))
524 (skip-syntax-forward "w_")
525 (delete-region (point) end)
526 (insert correct))
527 t)
528
529 ((or
530 (looking-at "(style) bad column")
531 (looking-at "(style) bad indentation")
532 (looking-at "(style) incorrect layout"))
533 (set-buffer source-buffer)
534 (funcall indent-line-function)
535 t)
536
537 ((looking-at "(style) misplaced \"then\"")
538 (set-buffer source-buffer)
539 (delete-indentation)
540 t)
541
542 ((looking-at "(style) missing \"overriding\" indicator")
543 (set-buffer source-buffer)
544 (cond
545 ((looking-at "\\(procedure\\)\\|\\(function\\)")
546 (insert "overriding ")
547 t)
548 (t
549 nil)))
550
551 ((looking-at "(style) space not allowed")
552 (set-buffer source-buffer)
553 ;; Error places point on space. More than one trailing space
554 ;; should be fixed by delete-trailing-whitespace in
555 ;; before-save-hook, once the file is modified.
556 (delete-char 1)
557 t)
558
559 ((looking-at "(style) space required")
560 (set-buffer source-buffer)
561 (insert " ")
562 t)
563 )));; end of setq unwind-protect cond
564 (if result
565 t
566 (goto-char start-pos)
567 nil)
568 ))
569
570 ;;;;; setup
571
572 (defun ada-gnat-compile-select-prj ()
573 (setq ada-fix-error-hook 'ada-gnat-fix-error-hook)
574 (add-to-list 'completion-ignored-extensions ".ali") ;; gnat library files
575
576 (add-hook 'compilation-filter-hook 'ada-gnat-compilation-filter)
577
578 ;; ada-mode.el project file parser sets this to other compilers used
579 ;; in the project, so we only add here.
580 (add-to-list 'compilation-error-regexp-alist 'gnat)
581 )
582
583 (defun ada-gnat-compile-deselect-prj ()
584 (setq ada-fix-error-hook nil)
585 (setq completion-ignored-extensions (delete ".ali" completion-ignored-extensions))
586 (setq compilation-filter-hook (delete 'ada-gnat-compilation-filter compilation-filter-hook))
587 (setq compilation-error-regexp-alist (delete 'gnat compilation-error-regexp-alist))
588 )
589
590 (defun ada-gnat-compile ()
591 "Set Ada mode global vars to use 'gnat' for compiling."
592 (add-to-list 'ada-prj-file-ext-extra "gpr")
593 (add-to-list 'ada-prj-parser-alist '("gpr" . gnat-parse-gpr))
594 (add-to-list 'ada-select-prj-compiler '(gnat . ada-gnat-compile-select-prj))
595 (add-to-list 'ada-deselect-prj-compiler '(gnat . ada-gnat-compile-deselect-prj))
596
597 (add-to-list 'ada-prj-parse-one-compiler (cons 'gnat 'gnat-prj-parse-emacs-one))
598 (add-to-list 'ada-prj-parse-final-compiler (cons 'gnat 'gnat-prj-parse-emacs-final))
599
600 (add-hook 'ada-gnat-fix-error-hook 'ada-gnat-fix-error))
601
602 (provide 'ada-gnat-compile)
603 (provide 'ada-compiler)
604
605 (ada-gnat-compile)
606
607 (add-to-list
608 'compilation-error-regexp-alist-alist
609 '(gnat
610 ;; typical:
611 ;; cards_package.adb:45:32: expected private type "System.Address"
612 ;;
613 ;; with full path Source_Reference pragma :
614 ;; d:/maphds/version_x/1773/sbs-abi-dll_lib.ads.gp:39:06: file "interfaces_c.ads" not found
615 ;;
616 ;; gnu cc1: (gnatmake can invoke the C compiler)
617 ;; foo.c:2: `TRUE' undeclared here (not in a function)
618 ;; foo.c:2 : `TRUE' undeclared here (not in a function)
619 "^\\(\\(.:\\)?[^ :\n]+\\):\\([0-9]+\\)\\s-?:?\\([0-9]+\\)?" 1 3 4))
620
621 (unless (default-value ada-compiler)
622 (set-default 'ada-compiler 'gnat))
623
624 ;; end of file