]> code.delx.au - gnu-emacs-elpa/blob - packages/ada-mode/gnat-inspect.el
Merge commit 'aecfbcbc10cf03ec94062ac3e590f8118e5a0434'
[gnu-emacs-elpa] / packages / ada-mode / gnat-inspect.el
1 ;;; gnat-inspect.el --- minor-mode for navigating sources using the
2 ;;; AdaCore cross reference tool gnatinspect.
3 ;;;
4 ;;; gnatinspect supports Ada and any gcc language that supports the
5 ;;; -fdump-xref switch (which includes C, C++).
6 ;;
7 ;;; Copyright (C) 2013, 2014 Free Software Foundation, Inc.
8
9 ;; Author: Stephen Leake <stephen_leake@member.fsf.org>
10 ;; Maintainer: Stephen Leake <stephen_leake@member.fsf.org>
11 ;; Version: 1.0
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Usage:
29 ;;
30 ;; M-x gnat-inspect
31
32 (require 'ada-mode) ;; for ada-prj-*, some other things
33 (require 'cl-lib)
34 (require 'compile)
35
36 ;;;;; sessions
37
38 ;; gnatinspect reads the project files and the database at startup,
39 ;; which is noticeably slow for a reasonably sized project. But
40 ;; running queries after startup is fast. So we leave gnatinspect
41 ;; running, and send it new queries via stdin, getting responses via
42 ;; stdout.
43 ;;
44 ;; We maintain a cache of active sessions, one per gnat project.
45
46 (cl-defstruct (gnat-inspect--session)
47 (process nil) ;; running gnatinspect
48 (buffer nil) ;; receives output of gnatinspect
49 (sent-kill-p nil)
50 (closed-p nil))
51
52 (defconst gnat-inspect-buffer-name-prefix " *gnatinspect-")
53
54 (defun gnat-inspect--start-process (session)
55 "Start the session process running gnatinspect."
56 (unless (buffer-live-p (gnat-inspect--session-buffer session))
57 ;; user may have killed buffer
58 (setf (gnat-inspect--session-buffer session) (gnat-run-buffer gnat-inspect-buffer-name-prefix)))
59
60 (with-current-buffer (gnat-inspect--session-buffer session)
61 (let ((process-environment (ada-prj-get 'proc_env)) ;; for GPR_PROJECT_PATH
62
63 ;; WORKAROUND: gnatinspect from gnatcoll-1.6w-20130902 can't handle aggregate projects; M910-032
64 (project-file (file-name-nondirectory
65 (ada-prj-get 'gpr_file))))
66 (erase-buffer); delete any previous messages, prompt
67 (setf (gnat-inspect--session-process session)
68 ;; FIXME: need good error message on bad project file:
69 ;; "can't handle aggregate projects?")
70 (start-process (concat "gnatinspect " (buffer-name))
71 (gnat-inspect--session-buffer session)
72 "gnatinspect"
73 (concat "--project=" project-file)))
74 (set-process-query-on-exit-flag (gnat-inspect--session-process session) nil)
75 (gnat-inspect-session-wait session)
76 )))
77
78 (defun gnat-inspect--make-session ()
79 "Create and return a session for the current project file."
80 (let ((session
81 (make-gnat-inspect--session
82 :buffer (gnat-run-buffer gnat-inspect-buffer-name-prefix))))
83 (gnat-inspect--start-process session)
84 session))
85
86 (defvar gnat-inspect--sessions '()
87 "Assoc list of sessions, indexed by absolute GNAT project file name.")
88
89 (defun gnat-inspect-cached-session ()
90 "Return a session for the current project file, creating it if necessary."
91 (gnat-inspect-ensure-gpr)
92
93 (let* ((session (cdr (assoc ada-prj-current-file gnat-inspect--sessions))))
94 (if session
95 (progn
96 (unless (process-live-p (gnat-inspect--session-process session))
97 (gnat-inspect--start-process session))
98 session)
99 ;; else
100 (prog1
101 (setq session (gnat-inspect--make-session))
102 (setq gnat-inspect--sessions
103 (cl-acons ada-prj-current-file session gnat-inspect--sessions))))
104 ))
105
106 (defconst gnat-inspect-prompt "^>>> $"
107 ;; gnatinspect output ends with this
108 "Regexp matching gnatinspect prompt; indicates previous command is complete.")
109
110 (defun gnat-inspect-session-wait (session)
111 "Wait for the current command to complete."
112 (with-current-buffer (gnat-inspect--session-buffer session)
113 (let ((process (gnat-inspect--session-process session))
114 (search-start (point-min))
115 (wait-count 0))
116 (while (progn
117 ;; process output is inserted before point, so move back over it to search it
118 (goto-char search-start)
119 (not (re-search-forward gnat-inspect-prompt (point-max) 1)));; don't search same text again
120 (setq search-start (point))
121 (message (concat "running gnatinspect ..." (make-string wait-count ?.)))
122 (accept-process-output process 1.0)
123 (setq wait-count (1+ wait-count)))
124 (message (concat "running gnatinspect ... done"))
125 )))
126
127 (defun gnat-inspect-session-send (cmd wait)
128 "Send CMD to gnatinspect session for current project.
129 If WAIT is non-nil, wait for command to complete.
130 Return buffer that holds output."
131 (let ((session (gnat-inspect-cached-session)))
132 (with-current-buffer (gnat-inspect--session-buffer session)
133 (erase-buffer)
134 (process-send-string (gnat-inspect--session-process session)
135 (concat cmd "\n"))
136 (when wait
137 (gnat-inspect-session-wait session))
138 (current-buffer)
139 )))
140
141 (defun gnat-inspect-kill-all-sessions ()
142 (interactive)
143 (let ((count 0))
144 (mapc (lambda (assoc)
145 (let ((session (cdr assoc)))
146 (when (process-live-p (gnat-inspect--session-process session))
147 (setq count (1+ count))
148 (process-send-string (gnat-inspect--session-process session) "exit\n")
149 )))
150 gnat-inspect--sessions)
151 (message "Killed %d sessions" count)
152 ))
153
154 ;;;;; utils
155
156 (defun gnat-inspect-ensure-gpr ()
157 (unless (ada-prj-get 'gpr_file)
158 (error "no gpr file specified")))
159
160 (defconst gnat-inspect-ident-file-regexp
161 ;; Write_Message:C:\Projects\GDS\work_dscovr_release\common\1553\gds-mil_std_1553-utf.ads:252:25
162 ;; Write_Message:/Projects/GDS/work_dscovr_release/common/1553/gds-mil_std_1553-utf.ads:252:25
163 "\\([^:]*\\):\\(\\(?:.:\\\|/\\)[^:]*\\):\\([0123456789]+\\):\\([0123456789]+\\)"
164 "Regexp matching <identifier>:<file>:<line>:<column>")
165
166 (defconst gnat-inspect-ident-file-regexp-alist
167 (list (concat "^" gnat-inspect-ident-file-regexp) 2 3 4)
168 "For compilation-error-regexp-alist, matching `gnatinspect overriding_recursive' output")
169
170 (defconst gnat-inspect-ident-file-type-regexp
171 (concat gnat-inspect-ident-file-regexp " (\\(.*\\))")
172 "Regexp matching <identifier>:<file>:<line>:<column> (<type>)")
173
174 (defconst gnat-inspect-ident-file-scope-regexp-alist
175 ;; RX_Enable:C:\common\1553\gds-hardware-bus_1553-raw_read_write.adb:163:13 (write reference) scope=New_Packet_TX:C:\common\1553\gds-hardware-bus_1553-raw_read_write.adb:97:14
176
177 (list (concat
178 gnat-inspect-ident-file-regexp
179 " (.*) "
180 "scope="
181 gnat-inspect-ident-file-regexp
182 )
183 2 3 4;; file line column
184 ;; 2 ;; type = error
185 ;; nil ;; hyperlink
186 ;; (list 4 'gnat-inspect-scope-secondary-error)
187 )
188 "For compilation-error-regexp-alist, matching `gnatinspect refs' output")
189
190 ;; debugging:
191 ;; in *compilation-gnatinspect-refs*, run
192 ;; (progn (set-text-properties (point-min)(point-max) nil)(compilation-parse-errors (point-min)(point-max) gnat-inspect-ident-file-scope-regexp-alist))
193
194 (defun gnat-inspect-compilation (identifier file line col cmd comp-err)
195 "Run gnatinspect IDENTIFIER:FILE:LINE:COL CMD,
196 set compilation-mode with compilation-error-regexp-alist set to COMP-ERR."
197 (gnat-inspect-ensure-gpr)
198
199 (let ((cmd-1 (format "%s %s:%s:%d:%d" cmd identifier file line col))
200 (result-count 0)
201 file line column)
202 (with-current-buffer (gnat-inspect--session-buffer (gnat-inspect-cached-session))
203 (compilation-mode)
204 (setq buffer-read-only nil)
205 (set (make-local-variable 'compilation-error-regexp-alist) (list comp-err))
206 (gnat-inspect-session-send cmd-1 t)
207 ;; at EOB. gnatinspect returns one line per result
208 (setq result-count (- (line-number-at-pos) 1))
209 (font-lock-fontify-buffer)
210 ;; font-lock-fontify-buffer applies compilation-message text properties
211 ;; IMPROVEME: for some reason, next-error works, but the font
212 ;; colors are not right (no koolaid!)
213 (goto-char (point-min))
214
215 (cl-case result-count
216 (0
217 (error "gnatinspect returned no results"))
218 (1
219 ;; just go there, don't display session-buffer. We have to
220 ;; fetch the compilation-message while in the session-buffer.
221 (let* ((msg (compilation-next-error 0 nil (point-min)))
222 (loc (compilation--message->loc msg)))
223 (setq file (caar (compilation--loc->file-struct loc))
224 line (caar (cddr (compilation--loc->file-struct loc)))
225 column (1- (compilation--loc->col loc)))
226 ))
227
228 ));; case, with-currrent-buffer
229
230 ;; compilation-next-error-function assumes there is not at error
231 ;; at point-min; work around that by moving forward 0 errors for
232 ;; the first one.
233 (if (> result-count 1)
234 ;; more than one result; display session buffer
235 (next-error 0 t)
236 ;; else don't display
237 (ada-goto-source file line column nil))
238 ))
239
240 (defun gnat-inspect-dist (found-line line found-col col)
241 "Return non-nil if found-line, -col is closer to line, col than min-distance."
242 (+ (abs (- found-line line))
243 (* (abs (- found-col col)) 250)))
244
245 ;;;;; user interface functions
246
247 (defun gnat-inspect-refresh ()
248 "For `ada-xref-refresh-function', using gnatinspect."
249 (interactive)
250 (gnat-inspect-session-send "refresh" t))
251
252 (defun gnat-inspect-other (identifier file line col)
253 "For `ada-xref-other-function', using gnatinspect."
254 (when (eq ?\" (aref identifier 0))
255 ;; gnatinspect wants the quotes stripped
256 (setq col (+ 1 col))
257 (setq identifier (substring identifier 1 (1- (length identifier))))
258 )
259
260 (let ((cmd (format "refs %s:%s:%d:%d" identifier (file-name-nondirectory file) line col))
261 (decl-loc nil)
262 (body-loc nil)
263 (search-type nil)
264 (min-distance (1- (expt 2 29)))
265 (result nil))
266
267 (with-current-buffer (gnat-inspect-session-send cmd t)
268 ;; 'gnatinspect refs' returns a list containing the declaration,
269 ;; the body, and all the references, in no particular order.
270 ;;
271 ;; We search the list, looking for the input location,
272 ;; declaration and body, then return the declaration or body as
273 ;; appropriate.
274 ;;
275 ;; the format of each line is name:file:line:column (type) scope=name:file:line:column
276 ;; 1 2 3 4 5
277 ;;
278 ;; 'type' can be:
279 ;; body
280 ;; declaration
281 ;; full declaration (for a private type)
282 ;; implicit reference
283 ;; reference
284 ;; static call
285 ;;
286 ;; Module_Type:/home/Projects/GDS/work_stephe_2/common/1553/gds-hardware-bus_1553-wrapper.ads:171:9 (full declaration) scope=Wrapper:/home/Projects/GDS/work_stephe_2/common/1553/gds-hardware-bus_1553-wrapper.ads:49:31
287 ;;
288 ;; itc_assert:/home/Projects/GDS/work_stephe_2/common/itc/opsim/itc_dscovr_gdsi/Gds1553/src/Gds1553.cpp:830:9 (reference) scope=Gds1553WriteSubaddress:/home/Projects/GDS/work_stephe_2/common/itc/opsim/itc_dscovr_gdsi/Gds1553/inc/Gds1553.hpp:173:24
289
290 (message "parsing result ...")
291
292 (goto-char (point-min))
293
294 (while (not (eobp))
295 (cond
296 ((looking-at gnat-inspect-ident-file-type-regexp)
297 ;; process line
298 (let* ((found-file (expand-file-name (match-string 2)));; converts Windows to normal
299 (found-line (string-to-number (match-string 3)))
300 (found-col (string-to-number (match-string 4)))
301 (found-type (match-string 5))
302 (dist (gnat-inspect-dist found-line line found-col col))
303 )
304
305 (when (string-equal found-type "declaration")
306 (setq decl-loc (list found-file found-line (1- found-col))))
307
308 (when (or
309 (string-equal found-type "body")
310 (string-equal found-type "full declaration"))
311 (setq body-loc (list found-file found-line (1- found-col))))
312
313 (when
314 ;; In general, we don't know where in the gnatinspect
315 ;; output the search item occurs, so we search for it.
316 ;;
317 ;; We use the same distance algorithm as gnatinspect
318 ;; to allow a fuzzy match on edited code.
319 (and (equal found-file file)
320 (< dist min-distance))
321 (setq min-distance dist)
322 (setq search-type found-type))
323 ))
324
325 (t ;; ignore line
326 ;;
327 ;; This skips GPR_PROJECT_PATH and echoed command at start of buffer.
328 ;;
329 ;; It also skips warning lines. For example,
330 ;; gnatcoll-1.6w-20130902 can't handle the Auto_Text_IO
331 ;; language, because it doesn't use the gprconfig
332 ;; configuration project. That gives lines like:
333 ;;
334 ;; common_text_io.gpr:15:07: language unknown for "gds-hardware-bus_1553-time_tone.ads"
335 ;;
336 ;; There are probably other warnings that might be reported as well.
337 )
338 )
339 (forward-line 1)
340 )
341
342 (cond
343 ((null search-type)
344 (pop-to-buffer (current-buffer))
345 (error "gnatinspect did not return other item; refresh?"))
346
347 ((and
348 (string-equal search-type "declaration")
349 body-loc)
350 (setq result body-loc))
351
352 (decl-loc
353 (setq result decl-loc))
354 )
355
356 (when (null result)
357 (pop-to-buffer (current-buffer))
358 (error "gnatinspect did not return other item; refresh?"))
359
360 (message "parsing result ... done")
361 result)))
362
363 (defun gnat-inspect-all (identifier file line col)
364 "For `ada-xref-all-function', using gnatinspect."
365 ;; This will in general return a list of references, so we use
366 ;; `compilation-start' to run gnatinspect, so the user can navigate
367 ;; to each result in turn via `next-error'.
368 (gnat-inspect-compilation identifier file line col "refs" 'gnat-inspect-ident-file))
369
370 (defun gnat-inspect-parents (identifier file line col)
371 "For `ada-xref-parent-function', using gnatinspect."
372 (gnat-inspect-compilation identifier file line col "parent_types" 'gnat-inspect-ident-file))
373
374 (defun gnat-inspect-overriding (identifier file line col)
375 "For `ada-xref-overriding-function', using gnatinspect."
376 (gnat-inspect-compilation identifier file line col "overridden_recursive" 'gnat-inspect-ident-file))
377
378 (defun gnat-inspect-overridden-1 (identifier file line col)
379 "For `ada-xref-overridden-function', using gnatinspect."
380 (unless (ada-prj-get 'gpr_file)
381 (error "no gnat project file defined."))
382
383 (when (eq ?\" (aref identifier 0))
384 ;; gnatinspect wants the quotes stripped
385 (setq col (+ 1 col))
386 (setq identifier (substring identifier 1 (1- (length identifier))))
387 )
388
389 (let ((cmd (format "overrides %s:%s:%d:%d" identifier (file-name-nondirectory file) line col))
390 result)
391 (with-current-buffer (gnat-inspect-session-send cmd t)
392
393 (goto-char (point-min))
394 (when (looking-at gnat-inspect-ident-file-regexp)
395 (setq result
396 (list
397 (match-string 2)
398 (string-to-number (match-string 3))
399 (string-to-number (match-string 4)))))
400
401 (when (null result)
402 (pop-to-buffer (current-buffer))
403 (error "gnatinspect did not return other item; refresh?"))
404
405 (message "parsing result ... done")
406 result)))
407
408 (defun gnat-inspect-overridden (other-window)
409 "Move to the overridden declaration of the identifier around point.
410 If OTHER-WINDOW (set by interactive prefix) is non-nil, show the
411 buffer in another window."
412 (interactive "P")
413
414 (let ((target
415 (gnat-inspect-overridden-1
416 (thing-at-point 'symbol)
417 (buffer-file-name)
418 (line-number-at-pos)
419 (save-excursion
420 (goto-char (car (bounds-of-thing-at-point 'symbol)))
421 (1+ (current-column)))
422 )))
423
424 (ada-goto-source (nth 0 target)
425 (nth 1 target)
426 (nth 2 target)
427 other-window)
428 ))
429
430 (defun gnat-inspect-goto-declaration (other-window)
431 "Move to the declaration or body of the identifier around point.
432 If at the declaration, go to the body, and vice versa. If at a
433 reference, goto the declaration.
434
435 If OTHER-WINDOW (set by interactive prefix) is non-nil, show the
436 buffer in another window."
437 (interactive "P")
438
439 (let ((target
440 (gnat-inspect-other
441 (thing-at-point 'symbol)
442 (buffer-file-name)
443 (line-number-at-pos)
444 (save-excursion
445 (goto-char (car (bounds-of-thing-at-point 'symbol)))
446 (1+ (current-column)))
447 )))
448
449 (ada-goto-source (nth 0 target)
450 (nth 1 target)
451 (nth 2 target)
452 other-window)
453 ))
454
455 (defvar gnat-inspect-map
456 (let ((map (make-sparse-keymap)))
457 ;; C-c C-i prefix for gnat-inspect minor mode
458
459 (define-key map "\C-c\C-i\C-d" 'gnat-inspect-goto-declaration)
460 (define-key map "\C-c\C-i\C-p" 'ada-build-prompt-select-prj-file)
461 (define-key map "\C-c\C-i\C-q" 'gnat-inspect-refresh)
462 (define-key map "\C-c\C-i\C-r" 'gnat-inspect-all)
463 map
464 ) "Local keymap used for GNAT inspect minor mode.")
465
466 (defvar gnat-inspect-menu (make-sparse-keymap "gnat-inspect"))
467 (easy-menu-define gnat-inspect-menu gnat-inspect-map "Menu keymap for gnat-inspect minor mode"
468 '("gnat-inspect"
469 ["Find and select project ..." ada-build-prompt-select-prj-file t]
470 ["Select project ..." ada-prj-select t]
471 ["Show current project" ada-prj-show t]
472 ["Next compilation error" next-error t]
473 ["Show secondary error" ada-show-secondary-error t]
474 ["Refresh cross reference cache" gnat-inspect-refresh t]
475 ))
476
477 (define-minor-mode gnat-inspect
478 "Minor mode for navigating sources using GNAT cross reference tool.
479 Enable mode if ARG is positive"
480 :initial-value t
481 :lighter " gnat-inspect" ;; mode line
482
483 ;; just enable the menu and keymap
484 )
485
486 ;;;;; support for Ada mode
487
488 (defun ada-gnat-inspect-select-prj ()
489 (setq ada-file-name-from-ada-name 'ada-gnat-file-name-from-ada-name)
490 (setq ada-ada-name-from-file-name 'ada-gnat-ada-name-from-file-name)
491 (setq ada-make-package-body 'ada-gnat-make-package-body)
492
493 (add-hook 'ada-syntax-propertize-hook 'gnatprep-syntax-propertize)
494 (add-hook 'ada-syntax-propertize-hook 'ada-gnat-syntax-propertize)
495
496 ;; must be after indentation engine setup, because that resets the
497 ;; indent function list.
498 (add-hook 'ada-mode-hook 'ada-gnat-inspect-setup t)
499
500 (setq ada-xref-refresh-function 'gnat-inspect-refresh)
501 (setq ada-xref-all-function 'gnat-inspect-all)
502 (setq ada-xref-other-function 'gnat-inspect-other)
503 (setq ada-xref-parent-function 'gnat-inspect-parents)
504 (setq ada-xref-all-function 'gnat-inspect-all)
505 (setq ada-xref-overriding-function 'gnat-inspect-overriding)
506 (setq ada-xref-overridden-function 'gnat-inspect-overridden-1)
507
508 (add-to-list 'completion-ignored-extensions ".ali") ;; gnat library files, used for cross reference
509 )
510
511 (defun ada-gnat-inspect-deselect-prj ()
512 (setq ada-file-name-from-ada-name nil)
513 (setq ada-ada-name-from-file-name nil)
514 (setq ada-make-package-body nil)
515
516 (setq ada-syntax-propertize-hook (delq 'gnatprep-syntax-propertize ada-syntax-propertize-hook))
517 (setq ada-mode-hook (delq 'ada-gnat-inspect-setup ada-mode-hook))
518
519 (setq ada-xref-other-function nil)
520 (setq ada-xref-parent-function nil)
521 (setq ada-xref-all-function nil)
522 (setq ada-xref-overriding-function nil)
523 (setq ada-xref-overridden-function nil)
524
525 (setq completion-ignored-extensions (delete ".ali" completion-ignored-extensions))
526 )
527
528 (defun ada-gnat-inspect-setup ()
529 (when (boundp 'wisi-indent-calculate-functions)
530 (add-to-list 'wisi-indent-calculate-functions 'gnatprep-indent))
531 )
532
533 (defun ada-gnat-inspect ()
534 "Set Ada mode global vars to use gnatinspect."
535 (add-to-list 'ada-prj-parser-alist '("gpr" . gnat-parse-gpr))
536 (add-to-list 'ada-select-prj-xref-tool '(gnat_inspect . ada-gnat-inspect-select-prj))
537 (add-to-list 'ada-deselect-prj-xref-tool '(gnat_inspect . ada-gnat-inspect-deselect-prj))
538
539 ;; no parse-*-xref
540
541 (font-lock-add-keywords 'ada-mode
542 ;; gnatprep preprocessor line
543 (list (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))))
544
545 (add-hook 'ada-gnat-fix-error-hook 'ada-gnat-fix-error)
546 )
547
548 (provide 'gnat-inspect)
549 (provide 'ada-xref-tool)
550
551 (add-to-list 'compilation-error-regexp-alist-alist
552 (cons 'gnat-inspect-ident-file gnat-inspect-ident-file-regexp-alist))
553 (add-to-list 'compilation-error-regexp-alist-alist
554 (cons 'gnat-inspect-ident-file-scope gnat-inspect-ident-file-scope-regexp-alist))
555
556 (unless (and (boundp 'ada-xref-tool)
557 (default-value 'ada-xref-tool))
558 (setq ada-xref-tool 'gnat_inspect))
559
560 (ada-gnat-inspect)
561
562 ;;; end of file