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