Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cedet / semantic / analyze.el
blobffc2c327c5ab52fd50859b66dbef98e5e47c218a
1 ;;; semantic/analyze.el --- Analyze semantic tags against local context
3 ;; Copyright (C) 2000-2005, 2007-2014 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 <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Semantic, as a tool, provides a nice list of searchable tags.
25 ;; That information can provide some very accurate answers if the current
26 ;; context of a position is known.
28 ;; Semantic-ctxt provides ways of analyzing, and manipulating the
29 ;; semantic context of a language in code.
31 ;; This library provides routines for finding intelligent answers to
32 ;; tough problems, such as if an argument to a function has the correct
33 ;; return type, or all possible tags that fit in a given local context.
36 ;;; Vocabulary:
38 ;; Here are some words used to describe different things in the analyzer:
40 ;; tag - A single entity
41 ;; prefix - The beginning of a symbol, usually used to look up something
42 ;; incomplete.
43 ;; type - The name of a datatype in the language.
44 ;; metatype - If a type is named in a declaration like:
45 ;; struct moose somevariable;
46 ;; that name "moose" can be turned into a concrete type.
47 ;; tag sequence - In C code, a list of dereferences, such as:
48 ;; this.that.theother();
49 ;; parent - For a datatype in an OO language, another datatype
50 ;; inherited from. This excludes interfaces.
51 ;; scope - A list of tags that can be dereferenced that cannot
52 ;; be found from the global namespace.
53 ;; scopetypes - A list of tags which are datatype that contain
54 ;; the scope. The scopetypes need to have the scope extracted
55 ;; in a way that honors the type of inheritance.
56 ;; nest/nested - When one tag is contained entirely in another.
58 ;; context - A semantic datatype representing a point in a buffer.
60 ;; constraint - If a context specifies a specific datatype is needed,
61 ;; that is a constraint.
62 ;; constants - Some datatypes define elements of themselves as a
63 ;; constant. These need to be returned as there would be no
64 ;; other possible completions.
66 (eval-when-compile (require 'cl))
67 (require 'semantic)
68 (require 'semantic/format)
69 (require 'semantic/ctxt)
70 (require 'semantic/scope)
71 (require 'semantic/sort)
72 (require 'semantic/analyze/fcn)
74 (eval-when-compile (require 'semantic/find))
76 (declare-function data-debug-new-buffer "data-debug")
77 (declare-function data-debug-insert-object-slots "eieio-datadebug")
79 ;;; Code:
80 (defvar semantic-analyze-error-stack nil
81 "Collection of any errors thrown during analysis.")
83 (defun semantic-analyze-push-error (err)
84 "Push the error in ERR-DATA onto the error stack.
85 Argument ERR."
86 (push err semantic-analyze-error-stack))
88 ;;; Analysis Classes
90 ;; These classes represent what a context is. Different types
91 ;; of contexts provide differing amounts of information to help
92 ;; provide completions.
94 (defclass semantic-analyze-context ()
95 ((bounds :initarg :bounds
96 :type list
97 :documentation "The bounds of this context.
98 Usually bound to the dimension of a single symbol or command.")
99 (prefix :initarg :prefix
100 :type list
101 :documentation "List of tags defining local text.
102 This can be nil, or a list where the last element can be a string
103 representing text that may be incomplete. Preceding elements
104 must be semantic tags representing variables or functions
105 called in a dereference sequence.")
106 (prefixclass :initarg :prefixclass
107 :type list
108 :documentation "Tag classes expected at this context.
109 These are classes for tags, such as 'function, or 'variable.")
110 (prefixtypes :initarg :prefixtypes
111 :type list
112 :documentation "List of tags defining types for :prefix.
113 This list is one shorter than :prefix. Each element is a semantic
114 tag representing a type matching the semantic tag in the same
115 position in PREFIX.")
116 (scope :initarg :scope
117 :type (or null semantic-scope-cache)
118 :documentation "List of tags available in scopetype.
119 See `semantic-analyze-scoped-tags' for details.")
120 (buffer :initarg :buffer
121 :type buffer
122 :documentation "The buffer this context is derived from.")
123 (errors :initarg :errors
124 :documentation "Any errors thrown an caught during analysis.")
126 "Base analysis data for any context.")
128 (defclass semantic-analyze-context-assignment (semantic-analyze-context)
129 ((assignee :initarg :assignee
130 :type list
131 :documentation "A sequence of tags for an assignee.
132 This is a variable into which some value is being placed. The last
133 item in the list is the variable accepting the value. Earlier
134 tags represent the variables being dereferenced to get to the
135 assignee."))
136 "Analysis class for a value in an assignment.")
138 (defclass semantic-analyze-context-functionarg (semantic-analyze-context)
139 ((function :initarg :function
140 :type list
141 :documentation "A sequence of tags for a function.
142 This is a function being called. The cursor will be in the position
143 of an argument.
144 The last tag in :function is the function being called. Earlier
145 tags represent the variables being dereferenced to get to the
146 function.")
147 (index :initarg :index
148 :type integer
149 :documentation "The index of the argument for this context.
150 If a function takes 4 arguments, this value should be bound to
151 the values 1 through 4.")
152 (argument :initarg :argument
153 :type list
154 :documentation "A sequence of tags for the :index argument.
155 The argument can accept a value of some type, and this contains the
156 tag for that definition. It should be a tag, but might
157 be just a string in some circumstances.")
159 "Analysis class for a value as a function argument.")
161 (defclass semantic-analyze-context-return (semantic-analyze-context)
162 () ; No extra data.
163 "Analysis class for return data.
164 Return data methods identify the required type by the return value
165 of the parent function.")
167 ;;; METHODS
169 ;; Simple methods against the context classes.
171 (defmethod semantic-analyze-type-constraint
172 ((context semantic-analyze-context) &optional desired-type)
173 "Return a type constraint for completing :prefix in CONTEXT.
174 Optional argument DESIRED-TYPE may be a non-type tag to analyze."
175 (when (semantic-tag-p desired-type)
176 ;; Convert the desired type if needed.
177 (if (not (eq (semantic-tag-class desired-type) 'type))
178 (setq desired-type (semantic-tag-type desired-type)))
179 ;; Protect against plain strings
180 (cond ((stringp desired-type)
181 (setq desired-type (list desired-type 'type)))
182 ((and (stringp (car desired-type))
183 (not (semantic-tag-p desired-type)))
184 (setq desired-type (list (car desired-type) 'type)))
185 ((semantic-tag-p desired-type)
186 ;; We have a tag of some sort. Yay!
187 nil)
188 (t (setq desired-type nil))
190 desired-type))
192 (defmethod semantic-analyze-type-constraint
193 ((context semantic-analyze-context-functionarg))
194 "Return a type constraint for completing :prefix in CONTEXT."
195 (call-next-method context (car (oref context argument))))
197 (defmethod semantic-analyze-type-constraint
198 ((context semantic-analyze-context-assignment))
199 "Return a type constraint for completing :prefix in CONTEXT."
200 (call-next-method context (car (reverse (oref context assignee)))))
202 (defmethod semantic-analyze-interesting-tag
203 ((context semantic-analyze-context))
204 "Return a tag from CONTEXT that would be most interesting to a user."
205 (let ((prefix (reverse (oref context :prefix))))
206 ;; Go back through the prefix until we find a tag we can return.
207 (while (and prefix (not (semantic-tag-p (car prefix))))
208 (setq prefix (cdr prefix)))
209 ;; Return the found tag, or nil.
210 (car prefix)))
212 (defmethod semantic-analyze-interesting-tag
213 ((context semantic-analyze-context-functionarg))
214 "Try the base, and if that fails, return what we are assigning into."
215 (or (call-next-method) (car-safe (oref context :function))))
217 (defmethod semantic-analyze-interesting-tag
218 ((context semantic-analyze-context-assignment))
219 "Try the base, and if that fails, return what we are assigning into."
220 (or (call-next-method) (car-safe (oref context :assignee))))
222 ;;; ANALYSIS
224 ;; Start out with routines that will calculate useful parts of
225 ;; the general analyzer function. These could be used directly
226 ;; by an application that doesn't need to calculate the full
227 ;; context.
229 (define-overloadable-function semantic-analyze-find-tag-sequence (sequence &optional
230 scope typereturn throwsym)
231 "Attempt to find all tags in SEQUENCE.
232 Optional argument LOCALVAR is the list of local variables to use when
233 finding the details on the first element of SEQUENCE in case
234 it is not found in the global set of tables.
235 Optional argument SCOPE are additional terminals to search which are currently
236 scoped. These are not local variables, but symbols available in a structure
237 which doesn't need to be dereferenced.
238 Optional argument TYPERETURN is a symbol in which the types of all found
239 will be stored. If nil, that data is thrown away.
240 Optional argument THROWSYM specifies a symbol the throw on non-recoverable error.")
242 (defun semantic-analyze-find-tag-sequence-default (sequence &optional
243 scope typereturn
244 throwsym)
245 "Attempt to find all tags in SEQUENCE.
246 SCOPE are extra tags which are in scope.
247 TYPERETURN is a symbol in which to place a list of tag classes that
248 are found in SEQUENCE.
249 Optional argument THROWSYM specifies a symbol the throw on non-recoverable error."
250 (let ((s sequence) ; copy of the sequence
251 (tmp nil) ; tmp find variable
252 (tag nil) ; tag return list
253 (tagtype nil) ; tag types return list
254 (fname nil)
255 (miniscope (when scope (clone scope)))
257 ;; First order check. Is this wholly contained in the typecache?
258 (setq tmp (semanticdb-typecache-find sequence))
260 (if tmp
261 (progn
262 ;; We are effectively done...
263 (setq s nil)
264 (setq tag (list tmp)))
266 ;; For the first entry, it better be a variable, but it might
267 ;; be in the local context too.
268 ;; NOTE: Don't forget c++ namespace foo::bar.
269 (setq tmp (or
270 ;; Is this tag within our scope. Scopes can sometimes
271 ;; shadow other things, so it goes first.
272 (and scope (semantic-scope-find (car s) nil scope))
273 ;; Find the tag out there... somewhere, but not in scope
274 (semantic-analyze-find-tag (car s))
277 (if (and (listp tmp) (semantic-tag-p (car tmp)))
278 (setq tmp (semantic-analyze-select-best-tag tmp)))
279 (if (not (semantic-tag-p tmp))
280 (if throwsym
281 (throw throwsym "Cannot find definition")
282 (error "Cannot find definition for \"%s\"" (car s))))
283 (setq s (cdr s))
284 (setq tag (cons tmp tag)) ; tag is nil here...
285 (setq fname (semantic-tag-file-name tmp))
288 ;; For the middle entries
289 (while s
290 ;; Using the tag found in TMP, let's find the tag
291 ;; representing the full typeographic information of its
292 ;; type, and use that to determine the search context for
293 ;; (car s)
294 (let* ((tmptype
295 ;; In some cases the found TMP is a type,
296 ;; and we can use it directly.
297 (cond ((semantic-tag-of-class-p tmp 'type)
298 (or (semantic-analyze-type tmp miniscope)
299 tmp))
301 (semantic-analyze-tag-type tmp miniscope))))
302 (typefile
303 (when tmptype
304 (semantic-tag-file-name tmptype)))
305 (slots nil))
307 ;; Get the children
308 (setq slots (semantic-analyze-scoped-type-parts tmptype scope))
310 ;; find (car s) in the list o slots
311 (setq tmp (semantic-find-tags-by-name (car s) slots))
313 ;; If we have lots
314 (if (and (listp tmp) (semantic-tag-p (car tmp)))
315 (setq tmp (semantic-analyze-select-best-tag tmp)))
317 ;; Make sure we have a tag.
318 (if (not (semantic-tag-p tmp))
319 (if (cdr s)
320 ;; In the middle, we need to keep seeking our types out.
321 (error "Cannot find definition for \"%s\"" (car s))
322 ;; Else, it's ok to end with a non-tag
323 (setq tmp (car s))))
325 (setq fname (or typefile fname))
326 (when (and fname (semantic-tag-p tmp)
327 (not (semantic-tag-in-buffer-p tmp)))
328 (semantic--tag-put-property tmp :filename fname))
329 (setq tag (cons tmp tag))
330 (setq tagtype (cons tmptype tagtype))
331 (when miniscope
332 (let ((rawscope
333 (apply 'append
334 (mapcar 'semantic-tag-type-members tagtype))))
335 (oset miniscope fullscope rawscope)))
337 (setq s (cdr s)))
339 (if typereturn (set typereturn (nreverse tagtype)))
340 ;; Return the mess
341 (nreverse tag)))
343 (defun semantic-analyze-find-tag (name &optional tagclass scope)
344 "Return the first tag found with NAME or nil if not found.
345 Optional argument TAGCLASS specifies the class of tag to return,
346 such as 'function or 'variable.
347 Optional argument SCOPE specifies a scope object which has
348 additional tags which are in SCOPE and do not need prefixing to
349 find.
351 This is a wrapper on top of semanticdb, semanticdb typecache,
352 semantic-scope, and semantic search functions. Almost all
353 searches use the same arguments."
354 (let ((namelst (if (consp name) name ;; test if pre-split.
355 (semantic-analyze-split-name name))))
356 (cond
357 ;; If the splitter gives us a list, use the sequence finder
358 ;; to get the list. Since this routine is expected to return
359 ;; only one tag, return the LAST tag found from the sequence
360 ;; which is supposedly the nested reference.
362 ;; Of note, the SEQUENCE function below calls this function
363 ;; (recursively now) so the names that we get from the above
364 ;; fcn better not, in turn, be splittable.
365 ((listp namelst)
366 ;; If we had a split, then this is likely a c++ style namespace::name sequence,
367 ;; so take a short-cut through the typecache.
368 (or (semanticdb-typecache-find namelst)
369 ;; Ok, not there, try the usual...
370 (let ((seq (semantic-analyze-find-tag-sequence
371 namelst scope nil)))
372 (semantic-analyze-select-best-tag seq tagclass)
374 ;; If NAME is solo, then do our searches for it here.
375 ((stringp namelst)
376 (let ((retlist (and scope (semantic-scope-find name tagclass scope))))
377 (if retlist
378 (semantic-analyze-select-best-tag
379 retlist tagclass)
380 (if (eq tagclass 'type)
381 (semanticdb-typecache-find name)
382 ;; Search in the typecache. First entries in a sequence are
383 ;; often there.
384 (setq retlist (semanticdb-typecache-find name))
385 (if retlist
386 retlist
387 (semantic-analyze-select-best-tag
388 (semanticdb-strip-find-results
389 (semanticdb-find-tags-by-name name)
390 'name)
391 tagclass)
392 )))))
395 ;;; SHORT ANALYSIS
397 ;; Create a mini-analysis of just the symbol under point.
399 (define-overloadable-function semantic-analyze-current-symbol
400 (analyzehookfcn &optional position)
401 "Call ANALYZEHOOKFCN after analyzing the symbol under POSITION.
402 The ANALYZEHOOKFCN is called with the current symbol bounds, and the
403 analyzed prefix. It should take the arguments (START END PREFIX).
404 The ANALYZEHOOKFCN is only called if some sort of prefix with bounds was
405 found under POSITION.
407 The results of ANALYZEHOOKFCN is returned, or nil if there was nothing to
408 call it with.
410 For regular analysis, you should call `semantic-analyze-current-context'
411 to calculate the context information. The purpose for this function is
412 to provide a large number of non-cached analysis for filtering symbols."
413 ;; Only do this in a Semantic enabled buffer.
414 (when (not (semantic-active-p))
415 (error "Cannot analyze buffers not supported by Semantic"))
416 ;; Always refresh out tags in a safe way before doing the
417 ;; context.
418 (semantic-refresh-tags-safe)
419 ;; Do the rest of the analysis.
420 (save-match-data
421 (save-excursion
422 (:override)))
425 (defun semantic-analyze-current-symbol-default (analyzehookfcn position)
426 "Call ANALYZEHOOKFCN on the analyzed symbol at POSITION."
427 (let* ((semantic-analyze-error-stack nil)
428 (LLstart (current-time))
429 (prefixandbounds (semantic-ctxt-current-symbol-and-bounds (or position (point))))
430 (prefix (car prefixandbounds))
431 (bounds (nth 2 prefixandbounds))
432 (scope (semantic-calculate-scope position))
433 (end nil)
435 ;; Only do work if we have bounds (meaning a prefix to complete)
436 (when bounds
438 (if debug-on-error
439 (catch 'unfindable
440 ;; If debug on error is on, allow debugging in this fcn.
441 (setq prefix (semantic-analyze-find-tag-sequence
442 prefix scope 'prefixtypes 'unfindable)))
443 ;; Debug on error is off. Capture errors and move on
444 (condition-case err
445 ;; NOTE: This line is duplicated in
446 ;; semantic-analyzer-debug-global-symbol
447 ;; You will need to update both places.
448 (setq prefix (semantic-analyze-find-tag-sequence
449 prefix scope 'prefixtypes))
450 (error (semantic-analyze-push-error err))))
452 (setq end (current-time))
453 ;;(message "Analysis took %.2f sec" (semantic-elapsed-time LLstart end))
456 (when prefix
457 (prog1
458 (funcall analyzehookfcn (car bounds) (cdr bounds) prefix)
459 ;;(setq end (current-time))
460 ;;(message "hookfcn took %.5f sec" (semantic-elapsed-time LLstart end))
465 ;;; MAIN ANALYSIS
467 ;; Create a full-up context analysis.
469 ;;;###autoload
470 (define-overloadable-function semantic-analyze-current-context (&optional position)
471 "Analyze the current context at optional POSITION.
472 If called interactively, display interesting information about POSITION
473 in a separate buffer.
474 Returns an object based on symbol `semantic-analyze-context'.
476 This function can be overridden with the symbol `analyze-context'.
477 When overriding this function, your override will be called while
478 cursor is at POSITION. In addition, your function will not be called
479 if a cached copy of the return object is found."
480 (interactive "d")
481 ;; Only do this in a Semantic enabled buffer.
482 (when (not (semantic-active-p))
483 (error "Cannot analyze buffers not supported by Semantic"))
484 ;; Always refresh out tags in a safe way before doing the
485 ;; context.
486 (semantic-refresh-tags-safe)
487 ;; Do the rest of the analysis.
488 (if (not position) (setq position (point)))
489 (save-excursion
490 (goto-char position)
491 (let* ((answer (semantic-get-cache-data 'current-context)))
492 (with-syntax-table semantic-lex-syntax-table
493 (when (not answer)
494 (setq answer (:override))
495 (when (and answer (oref answer bounds))
496 (with-slots (bounds) answer
497 (semantic-cache-data-to-buffer (current-buffer)
498 (car bounds)
499 (cdr bounds)
500 answer
501 'current-context
502 'exit-cache-zone)))
503 ;; Check for interactivity
504 (when (called-interactively-p 'any)
505 (if answer
506 (semantic-analyze-pop-to-context answer)
507 (message "No Context."))
510 answer))))
512 (defun semantic-analyze-current-context-default (position)
513 "Analyze the current context at POSITION.
514 Returns an object based on symbol `semantic-analyze-context'."
515 (let* ((semantic-analyze-error-stack nil)
516 (context-return nil)
517 (prefixandbounds (semantic-ctxt-current-symbol-and-bounds (or position (point))))
518 (prefix (car prefixandbounds))
519 (bounds (nth 2 prefixandbounds))
520 ;; @todo - vv too early to really know this answer! vv
521 (prefixclass (semantic-ctxt-current-class-list))
522 (prefixtypes nil)
523 (scope (semantic-calculate-scope position))
524 (function nil)
525 (fntag nil)
526 arg fntagend argtag
527 assign asstag newseq
530 ;; Pattern for Analysis:
532 ;; Step 1: Calculate DataTypes in Scope:
534 ;; a) Calculate the scope (above)
536 ;; Step 2: Parse context
538 ;; a) Identify function being called, or variable assignment,
539 ;; and find source tags for those references
540 ;; b) Identify the prefix (text cursor is on) and find the source
541 ;; tags for those references.
543 ;; Step 3: Assemble an object
546 ;; Step 2 a:
548 (setq function (semantic-ctxt-current-function))
550 (when function
551 ;; Calculate the argument for the function if there is one.
552 (setq arg (semantic-ctxt-current-argument))
554 ;; Find a tag related to the function name.
555 (condition-case err
556 (setq fntag
557 (semantic-analyze-find-tag-sequence function scope))
558 (error (semantic-analyze-push-error err)))
560 ;; fntag can have the last entry as just a string, meaning we
561 ;; could not find the core datatype. In this case, the searches
562 ;; below will not work.
563 (when (stringp (car (last fntag)))
564 ;; Take a wild guess!
565 (setcar (last fntag) (semantic-tag (car (last fntag)) 'function))
568 (when fntag
569 (let ((fcn (semantic-find-tags-by-class 'function fntag)))
570 (when (not fcn)
571 (let ((ty (semantic-find-tags-by-class 'type fntag)))
572 (when ty
573 ;; We might have a constructor with the same name as
574 ;; the found datatype.
575 (setq fcn (semantic-find-tags-by-name
576 (semantic-tag-name (car ty))
577 (semantic-tag-type-members (car ty))))
578 (if fcn
579 (let ((lp fcn))
580 (while lp
581 (when (semantic-tag-get-attribute (car lp)
582 :constructor)
583 (setq fcn (cons (car lp) fcn)))
584 (setq lp (cdr lp))))
585 ;; Give up, go old school
586 (setq fcn fntag))
588 (setq fntagend (car (reverse fcn))
589 argtag
590 (when (semantic-tag-p fntagend)
591 (nth (1- arg) (semantic-tag-function-arguments fntagend)))
592 fntag fcn))))
594 ;; Step 2 b:
596 ;; Only do work if we have bounds (meaning a prefix to complete)
597 (when bounds
599 (if debug-on-error
600 (catch 'unfindable
601 (setq prefix (semantic-analyze-find-tag-sequence
602 prefix scope 'prefixtypes 'unfindable))
603 ;; If there's an alias, dereference it and analyze
604 ;; sequence again.
605 (when (setq newseq
606 (semantic-analyze-dereference-alias prefix))
607 (setq prefix (semantic-analyze-find-tag-sequence
608 newseq scope 'prefixtypes 'unfindable))))
609 ;; Debug on error is off. Capture errors and move on
610 (condition-case err
611 ;; NOTE: This line is duplicated in
612 ;; semantic-analyzer-debug-global-symbol
613 ;; You will need to update both places.
614 (progn
615 (setq prefix (semantic-analyze-find-tag-sequence
616 prefix scope 'prefixtypes))
617 (when (setq newseq
618 (semantic-analyze-dereference-alias prefix))
619 (setq prefix (semantic-analyze-find-tag-sequence
620 newseq scope 'prefixtypes))))
621 (error (semantic-analyze-push-error err))))
624 ;; Step 3:
626 (cond
627 (fntag
628 ;; If we found a tag for our function, we can go into
629 ;; functional context analysis mode, meaning we have a type
630 ;; for the argument.
631 (setq context-return
632 (semantic-analyze-context-functionarg
633 "functionargument"
634 :buffer (current-buffer)
635 :function fntag
636 :index arg
637 :argument (list argtag)
638 :scope scope
639 :prefix prefix
640 :prefixclass prefixclass
641 :bounds bounds
642 :prefixtypes prefixtypes
643 :errors semantic-analyze-error-stack)))
645 ;; No function, try assignment
646 ((and (setq assign (semantic-ctxt-current-assignment))
647 ;; We have some sort of an assignment
648 (condition-case err
649 (setq asstag (semantic-analyze-find-tag-sequence
650 assign scope))
651 (error (semantic-analyze-push-error err)
652 nil)))
654 (setq context-return
655 (semantic-analyze-context-assignment
656 "assignment"
657 :buffer (current-buffer)
658 :assignee asstag
659 :scope scope
660 :bounds bounds
661 :prefix prefix
662 :prefixclass prefixclass
663 :prefixtypes prefixtypes
664 :errors semantic-analyze-error-stack)))
666 ;; TODO: Identify return value condition.
667 ;;((setq return .... what to do?)
668 ;; ...)
670 (bounds
671 ;; Nothing in particular
672 (setq context-return
673 (semantic-analyze-context
674 "context"
675 :buffer (current-buffer)
676 :scope scope
677 :bounds bounds
678 :prefix prefix
679 :prefixclass prefixclass
680 :prefixtypes prefixtypes
681 :errors semantic-analyze-error-stack)))
683 (t (setq context-return nil))
686 ;; Return our context.
687 context-return))
689 (defun semantic-analyze-dereference-alias (taglist)
690 "Dereference first tag in TAGLIST if it is an alias.
691 Returns a sequence of names which can then be fed again into
692 `semantic-analyze-find-tag-sequence'.
693 Returns nil if no alias was found."
694 (when (eq (semantic-tag-get-attribute (car taglist) :kind) 'alias)
695 (let ((tagname
696 (semantic-analyze-split-name
697 (semantic-tag-name
698 (car (semantic-tag-get-attribute (car taglist) :members))))))
699 (append (if (listp tagname)
700 tagname
701 (list tagname))
702 (cdr taglist)))))
704 (defun semantic-adebug-analyze (&optional ctxt)
705 "Perform `semantic-analyze-current-context'.
706 Display the results as a debug list.
707 Optional argument CTXT is the context to show."
708 (interactive)
709 (require 'data-debug)
710 (let ((start (current-time))
711 (ctxt (or ctxt (semantic-analyze-current-context)))
712 (end (current-time)))
713 (if (not ctxt)
714 (message "No Analyzer Results")
715 (message "Analysis took %.2f seconds."
716 (semantic-elapsed-time start end))
717 (semantic-analyze-pulse ctxt)
718 (if ctxt
719 (progn
720 (data-debug-new-buffer "*Analyzer ADEBUG*")
721 (data-debug-insert-object-slots ctxt "]"))
722 (message "No Context to analyze here.")))))
725 ;;; DEBUG OUTPUT
727 ;; Friendly output of a context analysis.
729 (declare-function pulse-momentary-highlight-region "pulse")
731 (defmethod semantic-analyze-pulse ((context semantic-analyze-context))
732 "Pulse the region that CONTEXT affects."
733 (require 'pulse)
734 (with-current-buffer (oref context :buffer)
735 (let ((bounds (oref context :bounds)))
736 (when bounds
737 (pulse-momentary-highlight-region (car bounds) (cdr bounds))))))
739 (defcustom semantic-analyze-summary-function 'semantic-format-tag-prototype
740 "Function to use when creating items in Imenu.
741 Some useful functions are found in `semantic-format-tag-functions'."
742 :group 'semantic
743 :type semantic-format-tag-custom-list)
745 (defun semantic-analyze-princ-sequence (sequence &optional prefix buff)
746 "Send the tag SEQUENCE to standard out.
747 Use PREFIX as a label.
748 Use BUFF as a source of override methods."
749 (while sequence
750 (princ prefix)
751 (cond
752 ((semantic-tag-p (car sequence))
753 (princ (funcall semantic-analyze-summary-function
754 (car sequence))))
755 ((stringp (car sequence))
756 (princ "\"")
757 (princ (semantic--format-colorize-text (car sequence) 'variable))
758 (princ "\""))
760 (princ (format "'%S" (car sequence)))))
761 (princ "\n")
762 (setq sequence (cdr sequence))
763 (setq prefix (make-string (length prefix) ? ))
766 (defmethod semantic-analyze-show ((context semantic-analyze-context))
767 "Insert CONTEXT into the current buffer in a nice way."
768 (semantic-analyze-princ-sequence (oref context prefix) "Prefix: " )
769 (semantic-analyze-princ-sequence (oref context prefixclass) "Prefix Classes: ")
770 (semantic-analyze-princ-sequence (oref context prefixtypes) "Prefix Types: ")
771 (semantic-analyze-princ-sequence (oref context errors) "Encountered Errors: ")
772 (princ "--------\n")
773 ;(semantic-analyze-princ-sequence (oref context scopetypes) "Scope Types: ")
774 ;(semantic-analyze-princ-sequence (oref context scope) "Scope: ")
775 ;(semantic-analyze-princ-sequence (oref context localvariables) "LocalVars: ")
776 (when (oref context scope)
777 (semantic-analyze-show (oref context scope)))
780 (defmethod semantic-analyze-show ((context semantic-analyze-context-assignment))
781 "Insert CONTEXT into the current buffer in a nice way."
782 (semantic-analyze-princ-sequence (oref context assignee) "Assignee: ")
783 (call-next-method))
785 (defmethod semantic-analyze-show ((context semantic-analyze-context-functionarg))
786 "Insert CONTEXT into the current buffer in a nice way."
787 (semantic-analyze-princ-sequence (oref context function) "Function: ")
788 (princ "Argument Index: ")
789 (princ (oref context index))
790 (princ "\n")
791 (semantic-analyze-princ-sequence (oref context argument) "Argument: ")
792 (call-next-method))
794 (defun semantic-analyze-pop-to-context (context)
795 "Display CONTEXT in a temporary buffer.
796 CONTEXT's content is described in `semantic-analyze-current-context'."
797 (semantic-analyze-pulse context)
798 (with-output-to-temp-buffer "*Semantic Context Analysis*"
799 (princ "Context Type: ")
800 (princ (eieio-object-name context))
801 (princ "\n")
802 (princ "Bounds: ")
803 (princ (oref context bounds))
804 (princ "\n")
805 (semantic-analyze-show context)
807 (shrink-window-if-larger-than-buffer
808 (get-buffer-window "*Semantic Context Analysis*"))
811 (provide 'semantic/analyze)
813 ;; Local variables:
814 ;; generated-autoload-file: "loaddefs.el"
815 ;; generated-autoload-load-name: "semantic/analyze"
816 ;; End:
818 ;;; semantic/analyze.el ends here