Print test timings unconditionally
[emacs.git] / lisp / cedet / semantic / analyze / refs.el
blob6268da80650cd262e67e3e1f5518c416dca146f6
1 ;;; semantic/analyze/refs.el --- Analysis of the references between tags.
3 ;; Copyright (C) 2008-2018 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Analyze the references between tags.
26 ;; The original purpose of these analysis is to provide a way to jump
27 ;; between a prototype and implementation.
29 ;; Finding all prototype/impl matches is hard because you have to search
30 ;; through the entire set of allowed databases to capture all possible
31 ;; refs. The core analysis class stores basic starting point, and then
32 ;; entire raw search data, which is expensive to calculate.
34 ;; Once the raw data is available, queries for impl, prototype, or
35 ;; perhaps other things become cheap.
37 (require 'semantic)
38 (require 'semantic/analyze)
39 (require 'semantic/db-find)
40 (eval-when-compile (require 'semantic/find))
42 (declare-function data-debug-new-buffer "data-debug")
43 (declare-function data-debug-insert-object-slots "eieio-datadebug")
44 (declare-function semantic-momentary-highlight-tag "semantic/decorate")
46 ;;; Code:
47 (defclass semantic-analyze-references ()
48 ((tag :initarg :tag
49 :type semantic-tag
50 :documentation
51 "The starting TAG we are providing references analysis for.")
52 (tagdb :initarg :tagdb
53 :documentation
54 "The database that tag can be found in.")
55 (scope :initarg :scope
56 :documentation "A Scope object.")
57 (rawsearchdata :initarg :rawsearchdata
58 :documentation
59 "The raw search data for TAG's name across all databases.")
60 ;; Note: Should I cache queried data here? I expect that searching
61 ;; through rawsearchdata will be super-fast, so why bother?
63 "Class containing data from a semantic analysis.")
65 (define-overloadable-function semantic-analyze-tag-references (tag &optional db)
66 "Analyze the references for TAG.
67 Returns a class with information about TAG.
69 Optional argument DB is a database. It will be used to help
70 locate TAG.
72 Use `semantic-analyze-current-tag' to debug this fcn.")
74 (defun semantic-analyze-tag-references-default (tag &optional db)
75 "Analyze the references for TAG.
76 Returns a class with information about TAG.
78 Optional argument DB is a database. It will be used to help
79 locate TAG.
81 Use `semantic-analyze-current-tag' to debug this fcn."
82 (when (not (semantic-tag-p tag)) (signal 'wrong-type-argument (list 'semantic-tag-p tag)))
83 (let ((allhits nil)
84 (scope nil)
86 (save-excursion
87 (semantic-go-to-tag tag db)
88 (setq scope (semantic-calculate-scope))
90 (setq allhits (semantic--analyze-refs-full-lookup tag scope t))
92 (semantic-analyze-references (semantic-tag-name tag)
93 :tag tag
94 :tagdb db
95 :scope scope
96 :rawsearchdata allhits)
97 )))
99 ;;; METHODS
101 ;; These accessor methods will calculate the useful bits from the context, and cache values
102 ;; into the context.
103 (cl-defmethod semantic-analyze-refs-impl ((refs semantic-analyze-references) &optional in-buffer)
104 "Return the implementations derived in the reference analyzer REFS.
105 Optional argument IN-BUFFER indicates that the returned tag should be in an active buffer."
106 (let ((allhits (oref refs rawsearchdata))
107 (tag (oref refs :tag))
108 (impl nil)
110 (semanticdb-find-result-mapc
111 (lambda (T DB)
112 "Examine T in the database DB, and sort it."
113 (let* ((ans (semanticdb-normalize-one-tag DB T))
114 (aT (cdr ans))
115 (aDB (car ans))
117 (when (and (not (semantic-tag-prototype-p aT))
118 (semantic-tag-similar-p tag aT
119 :prototype-flag
120 :parent
121 :typemodifiers
122 :default-value))
123 (when in-buffer (save-excursion (semantic-go-to-tag aT aDB)))
124 (push aT impl))))
125 allhits)
126 impl))
128 (cl-defmethod semantic-analyze-refs-proto ((refs semantic-analyze-references) &optional in-buffer)
129 "Return the prototypes derived in the reference analyzer REFS.
130 Optional argument IN-BUFFER indicates that the returned tag should be in an active buffer."
131 (let ((allhits (oref refs rawsearchdata))
132 (tag (oref refs :tag))
133 (proto nil))
134 (semanticdb-find-result-mapc
135 (lambda (T DB)
136 "Examine T in the database DB, and sort it."
137 (let* ((ans (semanticdb-normalize-one-tag DB T))
138 (aT (cdr ans))
139 (aDB (car ans))
141 (when (and (semantic-tag-prototype-p aT)
142 (semantic-tag-similar-p tag aT
143 :prototype-flag
144 :parent
145 :typemodifiers
146 :default-value))
147 (when in-buffer (save-excursion (semantic-go-to-tag aT aDB)))
148 (push aT proto))))
149 allhits)
150 proto))
152 ;;; LOOKUP
154 (defun semantic--analyze-refs-full-lookup (tag scope &optional noerror)
155 "Perform a full lookup for all occurrences of TAG in the current project.
156 TAG should be the tag currently under point.
157 SCOPE is the scope the cursor is in. From this a list of parents is
158 derived. If SCOPE does not have parents, then only a simple lookup is done.
159 Optional argument NOERROR means don't error if the lookup fails."
160 (if (not (oref scope parents))
161 ;; If this tag has some named parent, but is not
162 (semantic--analyze-refs-full-lookup-simple tag noerror)
164 ;; We have some sort of lineage we need to consider when we do
165 ;; our side lookup of tags.
166 (semantic--analyze-refs-full-lookup-with-parents tag scope)
169 (defun semantic--analyze-refs-find-child-in-find-results (find-results name class)
170 "Find in FIND-RESULT a tag NAME which is a child of a tag in FIND-RESULTS.
171 CLASS is the class of the tag that ought to be returned."
172 (let ((ans nil)
173 (subans nil))
174 ;; Loop over each segment of the find results.
175 (dolist (FDB find-results)
176 (setq subans nil)
177 ;; Loop over each tag in the find results.
178 (dolist (T (cdr FDB))
179 ;; For each tag, get the children.
180 (let* ((chil (semantic-tag-type-members T))
181 (match (semantic-find-tags-by-name name chil)))
182 ;; Go over the matches, looking for matching tag class.
183 (dolist (M match)
184 (when (semantic-tag-of-class-p M class)
185 (push M subans)))))
186 ;; Store current matches into a new find results.
187 (when subans
188 (push (cons (car FDB) subans) ans))
190 ans))
192 (defun semantic--analyze-refs-find-tags-with-parent (find-results parents)
193 "Find in FIND-RESULTS all tags with PARENTS.
194 NAME is the name of the tag needing finding.
195 PARENTS is a list of names."
196 (let ((ans nil) (usingnames nil))
197 ;; Loop over the find-results passed in.
198 (semanticdb-find-result-mapc
199 (lambda (tag db)
200 (let* ((p (semantic-tag-named-parent tag))
201 (ps (when (stringp p) (semantic-analyze-split-name p))))
202 (when (stringp ps) (setq ps (list ps)))
203 (when ps
204 ;; If there is a perfect match, then use it.
205 (if (equal ps parents)
206 (push (list db tag) ans))
207 ;; No match, find something from our list of using names.
208 ;; Do we need to split UN?
209 (save-excursion
210 (semantic-go-to-tag tag db)
211 (setq usingnames nil)
212 (let ((imports (semantic-ctxt-imported-packages)))
213 ;; Derive the names from all the using statements.
214 (mapc (lambda (T)
215 (setq usingnames
216 (cons (semantic-format-tag-name-from-anything T) usingnames)))
217 imports))
218 (dolist (UN usingnames)
219 (when (equal (cons UN ps) parents)
220 (push (list db tag) ans)
221 (setq usingnames (cdr usingnames))))
222 ))))
223 find-results)
224 ans))
226 (defun semantic--analyze-refs-full-lookup-with-parents (tag scope)
227 "Perform a lookup for all occurrences of TAG based on TAG's SCOPE.
228 TAG should be the tag currently under point."
229 (let* ((classmatch (semantic-tag-class tag))
230 (plist (mapcar (lambda (T) (semantic-tag-name T)) (oref scope parents)))
231 ;; The first item in the parent list
232 (name (car plist))
233 ;; Stuff from the simple list.
234 (simple (semantic--analyze-refs-full-lookup-simple tag t))
235 ;; Find all hits for the first parent name.
236 (brute (semanticdb-find-tags-collector
237 (lambda (table tags)
238 (semanticdb-deep-find-tags-by-name-method table name tags)
240 nil nil t))
241 ;; Prime the answer.
242 (answer (semantic--analyze-refs-find-tags-with-parent simple plist))
244 ;; First parent is already search to initialize "brute".
245 (setq plist (cdr plist))
247 ;; Go through the list of parents, and try to find matches.
248 ;; As we cycle through plist, for each level look for NAME,
249 ;; and compare the named-parent, and also dive into the next item of
250 ;; plist.
251 (while (and plist brute)
253 ;; Find direct matches
254 (let* ((direct (semantic--analyze-refs-find-child-in-find-results
255 brute (semantic-tag-name tag) classmatch))
256 (pdirect (semantic--analyze-refs-find-tags-with-parent
257 direct plist)))
258 (setq answer (append pdirect answer)))
260 ;; The next set of search items.
261 (setq brute (semantic--analyze-refs-find-child-in-find-results
262 brute (car plist) 'type))
264 (setq plist (cdr plist)))
266 ;; Brute now has the children from the very last match.
267 (let* ((direct (semantic--analyze-refs-find-child-in-find-results
268 brute (semantic-tag-name tag) classmatch))
270 (setq answer (append direct answer)))
272 answer))
274 (defun semantic--analyze-refs-full-lookup-simple (tag &optional noerror)
275 "Perform a simple lookup for occurrences of TAG in the current project.
276 TAG should be the tag currently under point.
277 Optional NOERROR means don't throw errors on failure to find something.
278 This only compares the tag name, and does not infer any matches in namespaces,
279 or parts of some other data structure.
280 Only works for tags in the global namespace."
281 (let* ((name (semantic-tag-name tag))
282 (brute (semanticdb-find-tags-collector
283 (lambda (table tags)
284 (semanticdb-find-tags-by-name-method table name tags)
286 nil ;; This may need to be the entire project??
287 nil t))
290 (when (and (not brute) (not noerror))
291 ;; An error, because tag under point ought to be found.
292 (error "Cannot find any references to %s in wide search" name))
294 (let* ((classmatch (semantic-tag-class tag))
295 (RES
296 (semanticdb-find-tags-collector
297 (lambda (table tags)
298 (semantic-find-tags-by-class classmatch tags)
299 ;; @todo - Add parent check also.
301 brute nil)))
303 (when (and (not RES) (not noerror))
304 (error "Cannot find any definitions for %s in wide search"
305 (semantic-tag-name tag)))
307 ;; Return the matching tags and databases.
308 RES)))
311 ;;; USER COMMANDS
313 ;;;###autoload
314 (defun semantic-analyze-current-tag ()
315 "Analyze the tag under point."
316 (interactive)
317 (let* ((tag (semantic-current-tag))
318 (start (current-time))
319 (sac (semantic-analyze-tag-references tag))
321 (message "Analysis took %.2f seconds." (semantic-elapsed-time start nil))
322 (if sac
323 (progn
324 (require 'eieio-datadebug)
325 (data-debug-new-buffer "*Analyzer Reference ADEBUG*")
326 (data-debug-insert-object-slots sac "]"))
327 (message "No Context to analyze here."))))
329 ;;;###autoload
330 (defun semantic-analyze-proto-impl-toggle ()
331 "Toggle between the implementation, and a prototype of tag under point."
332 (interactive)
333 (require 'semantic/decorate)
334 (semantic-fetch-tags)
335 (let* ((tag (semantic-current-tag))
336 (sar (if tag
337 (semantic-analyze-tag-references tag)
338 (error "Point must be in a declaration")))
339 (target (if (semantic-tag-prototype-p tag)
340 (car (semantic-analyze-refs-impl sar t))
341 (car (semantic-analyze-refs-proto sar t))))
344 (when (not target)
345 (error "Could not find suitable %s"
346 (if (semantic-tag-prototype-p tag) "implementation" "prototype")))
348 (push-mark)
349 (semantic-go-to-tag target)
350 (pop-to-buffer-same-window (current-buffer))
351 (semantic-momentary-highlight-tag target))
354 (provide 'semantic/analyze/refs)
356 ;; Local variables:
357 ;; generated-autoload-file: "../loaddefs.el"
358 ;; generated-autoload-load-name: "semantic/analyze/refs"
359 ;; End:
361 ;;; semantic/analyze/refs.el ends here