1 ;;; semantic/analyze/debug.el --- Debug the analyzer
3 ;;; Copyright (C) 2008, 2009 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/>.
24 ;; Provide a top-order debugging tool for figuring out what's going on with
25 ;; smart completion and analyzer mode.
28 (require 'semantic
/analyze
)
29 (require 'semantic
/analyze
/complete
)
30 (require 'semantic
/db-typecache
)
32 ;; For semantic-find-tags-by-class:
33 (eval-when-compile (require 'semantic
/find
))
35 (declare-function ede-get-locator-object
"ede/files")
39 (defun semantic-analyze-debug-assist ()
40 "Debug semantic analysis at the current point."
42 (let ((actualfcn (fetch-overload 'semantic-analyze-current-context
))
43 (ctxt (semantic-analyze-current-context))
47 (message "Mode %s does not use the default analyzer."
51 (or (semantic-analyzer-debug-test-local-context)
52 (and ctxt
(semantic-analyzer-debug-found-prefix ctxt
))
57 (defun semantic-analyzer-debug-found-prefix (ctxt)
58 "Debug the prefix found by the analyzer output CTXT."
59 (let* ((pf (oref ctxt prefix
))
60 (pft (oref ctxt prefixtypes
))
63 (comp (condition-case nil
64 (semantic-analyze-possible-completions ctxt
)
67 (while (and (nth idx pf
) (not stop
))
68 (let ((pentry (nth idx pf
))
69 (ptentry (nth idx pft
)))
70 (if (or (stringp pentry
) (not ptentry
))
71 ;; Found someting ok. stop
73 (setq idx
(1+ idx
)))))
74 ;; We found the first non-tag entry. What is the situation?
76 ((and (eq idx
0) (stringp (car pf
)))
77 ;; First part, we couldn't find it.
78 (semantic-analyzer-debug-global-symbol ctxt
(car pf
) comp
))
79 ((not (nth (1- idx
) pft
)) ;; idx can't be 0 here.
80 ;; The previous entry failed to have an identifiable data
81 ;; type, which is a global search.
82 (semantic-analyzer-debug-missing-datatype ctxt idx comp
))
83 ((and (nth (1- idx
) pft
) (stringp (nth idx pf
)))
84 ;; Non-first search, didn't find string in known data type.
85 (semantic-analyzer-debug-missing-innertype ctxt idx comp
))
88 (message "Things look ok."))
91 (defun semantic-analyzer-debug-global-symbol (ctxt prefix comp
)
92 "Debug why we can't find the first entry in the CTXT PREFIX.
93 Argument COMP are possible completions here."
94 (let ((tab semanticdb-current-table
)
96 (origbuf (current-buffer))
98 (with-output-to-temp-buffer (help-buffer)
99 (with-current-buffer standard-output
100 (princ "Unable to find prefix ")
104 ;; NOTE: This line is copied from semantic-analyze-current-context.
105 ;; You will need to update both places.
109 (let* ((position (or (cdr-safe (oref ctxt bounds
)) (point)))
110 (prefixtypes nil
) ; Used as type return
111 (scope (semantic-calculate-scope position
))
113 (semantic-analyze-find-tag-sequence
114 (list prefix
"") scope
'prefixtypes
)
117 (error (setq finderr err
)))
121 (princ "The prefix lookup code threw the following error:\n ")
123 (princ "\n\nTo debug this error you can do this:
124 M-x toggle-debug-on-error RET
125 and then re-run the debug analyzer.\n")
127 ;; No find error, just not found
128 (princ "The prefix ")
130 (princ " could not be found in the local scope,
131 nor in any search tables.\n")
135 ;; Describe local scope, and why we might not be able to
137 (semantic-analyzer-debug-describe-scope ctxt
)
139 (semantic-analyzer-debug-show-completions comp
)
141 (princ "When Semantic cannot find a symbol, it could be because the include
142 path was setup incorrectly.\n")
144 (semantic-analyzer-debug-insert-include-summary tab
)
147 (semantic-analyzer-debug-add-buttons)
150 (defun semantic-analyzer-debug-missing-datatype (ctxt idx comp
)
151 "Debug why we can't find a datatype entry for CTXT prefix at IDX.
152 Argument COMP are possible completions here."
153 (let* ((prefixitem (nth idx
(oref ctxt prefix
)))
154 (dt (nth (1- idx
) (oref ctxt prefixtypes
)))
155 (tt (semantic-tag-type prefixitem
))
156 (tab semanticdb-current-table
)
158 (when dt
(error "Missing Datatype debugger is confused"))
159 (with-output-to-temp-buffer (help-buffer)
160 (with-current-buffer standard-output
161 (princ "Unable to find datatype for: \"")
162 (princ (semantic-format-tag-prototype prefixitem
))
165 (when (semantic-tag-p tt
)
166 (semantic-analyzer-debug-insert-tag tt
)
167 (princ "\nRaw data type is: "))
168 (princ (format "%S" tt
))
171 Semantic could not find this data type in any of its global tables.
173 Semantic locates datatypes through either the local scope, or the global
177 ;; Describe local scope, and why we might not be able to
179 (semantic-analyzer-debug-describe-scope ctxt
'(type))
181 ;; Describe the typecache.
182 (princ "\nSemantic creates and maintains a type cache for each buffer.
183 If the type is a global type, then it should appear in they typecache.
184 To examine the typecache, type:
186 M-x semanticdb-typecache-dump RET
188 Current typecache Statistics:\n")
189 (princ (format " %4d types global in this file\n %4d types from includes.\n"
190 (length (semanticdb-typecache-file-tags tab
))
191 (length (semanticdb-typecache-include-tags tab
))))
193 (princ "\nIf the datatype is not in the typecache, then your include
194 path may be incorrect. ")
196 (semantic-analyzer-debug-insert-include-summary tab
)
200 (semantic-analyzer-debug-add-buttons)
203 (defun semantic-analyzer-debug-missing-innertype (ctxt idx comp
)
204 "Debug why we can't find an entry for CTXT prefix at IDX for known type.
205 We need to see if we have possible completions against the entry before
206 being too vocal about it.
207 Argument COMP are possible completions here."
208 (let* ((prefixitem (nth idx
(oref ctxt prefix
)))
209 (prevprefix (nth (1- idx
) (oref ctxt prefix
)))
210 (dt (nth (1- idx
) (oref ctxt prefixtypes
)))
211 (desired-type (semantic-analyze-type-constraint ctxt
))
212 (orig-buffer (current-buffer))
213 (ots (semantic-analyze-tag-type prevprefix
218 (when (not dt
) (error "Missing Innertype debugger is confused"))
219 (with-output-to-temp-buffer (help-buffer)
220 (with-current-buffer standard-output
221 (princ "Cannot find prefix \"")
223 (princ "\" in datatype:
225 (semantic-analyzer-debug-insert-tag dt
)
229 ;; Any language with a namespace.
230 ((string= (semantic-tag-type dt
) "namespace")
231 (princ "Semantic may not have found all possible namespaces with
233 (princ (semantic-tag-name dt
))
234 (princ ". You can debug the entire typecache, including merged namespaces
237 M-x semanticdb-typecache-dump RET")
240 ;; @todo - external declarations??
244 ;; A generic explanation
246 (princ "\nSemantic has found the datatype ")
247 (semantic-analyzer-debug-insert-tag dt
)
248 (if (or (not (semantic-equivalent-tag-p ots dt
))
250 (set-buffer orig-buffer
)
251 (car (semantic-analyze-dereference-metatype
252 ots
(oref ctxt scope
))))))
254 (nexttype (save-excursion
255 (set-buffer orig-buffer
)
256 (car (semantic-analyze-dereference-metatype
257 ots
(oref ctxt scope
))))))
258 (if (eq nexttype lasttype
)
259 (princ "\n [ Debugger error trying to help with metatypes ]")
262 (princ "\nwhich is a metatype")
263 (princ "\nwhich is derived from metatype ")
264 (semantic-analyzer-debug-insert-tag lasttype
)))
266 (princ ".\nThe Metatype stack is:\n")
268 (semantic-analyzer-debug-insert-tag lasttype
)
271 (not (eq nexttype lasttype
)))
273 (semantic-analyzer-debug-insert-tag nexttype
)
275 (setq lasttype nexttype
278 (set-buffer orig-buffer
)
279 (car (semantic-analyze-dereference-metatype
280 nexttype
(oref ctxt scope
)))))
285 "Last metatype is nil. This means that semantic cannot derive
286 the list of members because the type referred to cannot be found.\n")
289 (princ "\nand its list of members.")
293 (princ " Semantic does not know what
294 possible completions there are for \"")
296 (princ "\". Examine the known
297 members below for more."))
298 (princ " Semantic knows of some
299 possible completions for \"")
307 (semantic-analyzer-debug-show-completions comp
)
309 (princ "\nKnown members of ")
310 (princ (semantic-tag-name dt
))
312 (dolist (M (semantic-tag-type-members dt
))
314 ;;(princ (semantic-format-tag-prototype M))
315 (semantic-analyzer-debug-insert-tag M
)
318 ;; This doesn't refer to in-type completions.
319 ;;(semantic-analyzer-debug-global-miss-text prefixitem)
323 (princ "\nWhen there are known members that would make good completion
324 candidates that are not in the completion list, then the most likely
325 cause is a type constraint. Semantic has determined that there is a
326 type constraint looking for the type ")
327 (if (semantic-tag-p desired-type
)
328 (semantic-analyzer-debug-insert-tag desired-type
)
329 (princ (format "%S" desired-type
)))
332 (semantic-analyzer-debug-add-buttons)
337 (defun semantic-analyzer-debug-test-local-context ()
338 "Test the local context parsed from the file."
339 (let* ((prefixandbounds (semantic-ctxt-current-symbol-and-bounds (point)))
340 (prefix (car prefixandbounds
))
341 (bounds (nth 2 prefixandbounds
))
343 (when (and (or (not prefixandbounds
)
347 (with-output-to-temp-buffer (help-buffer)
348 (with-current-buffer standard-output
349 (princ "Local Context Parser Failed.
351 If this is unexpected, then there is likely a bug in the Semantic
352 local context parser.
354 Consider debugging the function ")
355 (let ((lcf (fetch-overload 'semantic-ctxt-current-symbol-and-bounds
)))
357 (princ (symbol-name lcf
))
358 (princ "semantic-ctxt-current-symbol-and-bounds,
359 or implementing a version specific to ")
360 (princ (symbol-name major-mode
))
363 (semantic-analyzer-debug-add-buttons)
367 ;;; General Inserters with help
369 (defun semantic-analyzer-debug-show-completions (comp)
370 "Show the completion list COMP."
372 (princ "\nNo known possible completions.\n")
374 (princ "\nPossible completions are:\n")
381 (semantic-analyzer-debug-insert-tag C
)))
385 (defvar semantic-dependency-system-include-path
)
387 (defun semantic-analyzer-debug-insert-include-summary (table)
388 "Display a summary of includes for the semanticdb TABLE."
389 (require 'semantic
/dep
)
390 (semantic-fetch-tags)
391 (let ((inc (semantic-find-tags-by-class 'include table
))
392 ;;(path (semanticdb-find-test-translate-path-no-loading))
395 (set-buffer (semanticdb-get-buffer table
))
396 semanticdb-find-lost-includes
))
399 (set-buffer (semanticdb-get-buffer table
))
400 semantic-dependency-system-include-path
))
403 (set-buffer (semanticdb-get-buffer table
))
404 (and (boundp 'ede-object
)
408 (set-buffer (semanticdb-get-buffer table
))
409 (and (boundp 'ede-object-project
)
410 ede-object-project
))))
412 (princ "\n\nInclude Path Summary:")
414 (princ "\n\nThis file's project include search is handled by the EDE object:\n")
415 (princ " Buffer Target: ")
416 (princ (object-print edeobj
))
418 (when (not (eq edeobj edeproj
))
419 (princ " Buffer Project: ")
420 (princ (object-print edeproj
))
423 (let ((loc (ede-get-locator-object edeproj
)))
424 (princ " Backup Locator: ")
425 (princ (object-print loc
))
429 (princ "\n\nThe system include path is:\n")
435 (princ "\n\nInclude Summary: ")
436 (princ (semanticdb-full-filename table
))
438 (princ (format "%s contains %d includes.\n"
439 (file-name-nondirectory
440 (semanticdb-full-filename table
))
447 (let* ((fileinner (semantic-dependency-tag-file i
))
448 (tableinner (when fileinner
449 (semanticdb-file-table-object fileinner t
))))
450 (cond ((not fileinner
)
451 (setq unknown
(1+ unknown
)))
452 ((number-or-marker-p (oref tableinner pointmax
))
455 (setq unparsed
(1+ unparsed
))))))
456 (setq all
(+ ok unknown unparsed
))
457 (when (not (= 0 all
))
458 (princ (format " Unknown Includes: %d\n" unknown
))
459 (princ (format " Unparsed Includes: %d\n" unparsed
))
460 (princ (format " Parsed Includes: %d\n" ok
)))
466 (princ "\nA likely cause of an unfound tag is missing include files.")
467 (semantic-analyzer-debug-insert-tag-list
468 "The following includes were not found" unk
)
470 (princ "\nYou can fix the include path for ")
471 (princ (symbol-name (oref table major-mode
)))
472 (princ " by using this function:
474 M-x semantic-customize-system-include-path RET
476 which customizes the mode specific variable for the mode-local
477 variable `semantic-dependency-system-include-path'.")
480 (princ "\n No unknown includes.\n"))
483 (defun semantic-analyzer-debug-describe-scope (ctxt &optional classconstraint
)
484 "Describe the scope in CTXT for finding a global symbol.
485 Optional argument CLASSCONSTRAINT says to output to tags of that class."
486 (let* ((scope (oref ctxt
:scope
))
487 (parents (oref scope parents
))
488 (cc (or classconstraint
(oref ctxt prefixclass
)))
490 (princ "\nLocal Scope Information:")
491 (princ "\n * Tag Class Constraint against SCOPE: ")
492 (princ (format "%S" classconstraint
))
495 (semantic-analyzer-debug-insert-tag-list
496 " >> Known parent types with possible in scope symbols"
498 (princ "\n * No known parents in current scope."))
500 (let ((si (semantic-analyze-tags-of-class-list
501 (oref scope scope
) cc
))
502 (lv (semantic-analyze-tags-of-class-list
503 (oref scope localvar
) cc
))
506 (semantic-analyzer-debug-insert-tag-list
507 " >> Known symbols within the current scope"
509 (princ "\n * No known symbols currently in scope."))
512 (semantic-analyzer-debug-insert-tag-list
513 " >> Known symbols that are declared locally"
515 (princ "\n * No known symbols declared locally."))
520 (defun semantic-analyzer-debug-global-miss-text (name-in)
521 "Use 'princ' to show text describing not finding symbol NAME-IN.
522 NAME is the name of the unfound symbol."
523 (let ((name (cond ((stringp name-in
)
525 ((semantic-tag-p name-in
)
526 (semantic-format-tag-name name-in
))
527 (t (format "%S" name-in
)))))
528 (when (not (string= name
""))
531 (princ " is a local variable, argument, or symbol in some
532 namespace or class exposed via scoping statements, then it should
535 Debugging the scope can be done with:
536 M-x semantic-calculate-scope RET
538 If the prefix is a global symbol, in an included file, then
539 your search path may be incomplete.
544 (defun semantic-analyzer-debug-insert-tag-list (text taglist
)
545 "Prefixing with TEXT, dump TAGLIST in a help buffer."
546 (princ "\n") (princ text
) (princ ":\n")
550 ;;(princ (semantic-format-tag-prototype M))
551 (semantic-analyzer-debug-insert-tag M
)
555 (defun semantic-analyzer-debug-insert-tag (tag &optional parent
)
556 "Display a TAG by name, with possible jumpitude.
557 PARENT is a possible parent (by nesting) tag."
558 (let ((str (semantic-format-tag-prototype tag parent
)))
559 (if (and (semantic-tag-with-position-p tag
)
560 (semantic-tag-file-name tag
))
562 'mouse-face
'custom-button-pressed-face
570 (button-get button
'tag
))
571 (setq buff
(current-buffer))
573 (if (get-buffer-window buff
)
574 (select-window (get-buffer-window buff
))
575 (pop-to-buffer buff t
))
577 (pulse-line-hook-function)))
584 (defvar semantic-analyzer-debug-orig nil
585 "The originating buffer for a help button.")
587 (defun semantic-analyzer-debug-add-buttons ()
588 "Add push-buttons to the *Help* buffer.
589 Look for key expressions, and add push-buttons near them."
590 (let ((orig-buffer (make-marker)))
591 (set-marker orig-buffer
(point) (current-buffer))
593 ;; Get a buffer ready.
594 (set-buffer "*Help*")
595 (toggle-read-only -
1)
596 (goto-char (point-min))
597 (set (make-local-variable 'semantic-analyzer-debug-orig
) orig-buffer
)
598 ;; First, add do-in buttons to recommendations.
599 (while (re-search-forward "^\\s-*M-x \\(\\(\\w\\|\\s_\\)+\\) " nil t
)
600 (let ((fcn (match-string 1)))
601 (when (not (fboundp (intern-soft fcn
)))
602 (error "Help Err: Can't find %s" fcn
))
605 (insert-button "[ Do It ]"
606 'mouse-face
'custom-button-pressed-face
608 'action
`(lambda (arg)
609 (let ((M semantic-analyzer-debug-orig
))
610 (set-buffer (marker-buffer M
))
612 (call-interactively (quote ,(intern-soft fcn
))))
615 ;; Do something else?
619 (set-buffer-modified-p nil
)
622 (provide 'semantic
/analyze
/debug
)
624 ;;; semantic/analyze/debug.el ends here