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.
107 (with-current-buffer origbuf
108 (let* ((position (or (cdr-safe (oref ctxt bounds
)) (point)))
109 (prefixtypes nil
) ; Used as type return
110 (scope (semantic-calculate-scope position
))
112 (semantic-analyze-find-tag-sequence
113 (list prefix
"") scope
'prefixtypes
)
116 (error (setq finderr err
)))
120 (princ "The prefix lookup code threw the following error:\n ")
122 (princ "\n\nTo debug this error you can do this:
123 M-x toggle-debug-on-error RET
124 and then re-run the debug analyzer.\n")
126 ;; No find error, just not found
127 (princ "The prefix ")
129 (princ " could not be found in the local scope,
130 nor in any search tables.\n")
134 ;; Describe local scope, and why we might not be able to
136 (semantic-analyzer-debug-describe-scope ctxt
)
138 (semantic-analyzer-debug-show-completions comp
)
140 (princ "When Semantic cannot find a symbol, it could be because the include
141 path was setup incorrectly.\n")
143 (semantic-analyzer-debug-insert-include-summary tab
)
146 (semantic-analyzer-debug-add-buttons)
149 (defun semantic-analyzer-debug-missing-datatype (ctxt idx comp
)
150 "Debug why we can't find a datatype entry for CTXT prefix at IDX.
151 Argument COMP are possible completions here."
152 (let* ((prefixitem (nth idx
(oref ctxt prefix
)))
153 (dt (nth (1- idx
) (oref ctxt prefixtypes
)))
154 (tt (semantic-tag-type prefixitem
))
155 (tab semanticdb-current-table
)
157 (when dt
(error "Missing Datatype debugger is confused"))
158 (with-output-to-temp-buffer (help-buffer)
159 (with-current-buffer standard-output
160 (princ "Unable to find datatype for: \"")
161 (princ (semantic-format-tag-prototype prefixitem
))
164 (when (semantic-tag-p tt
)
165 (semantic-analyzer-debug-insert-tag tt
)
166 (princ "\nRaw data type is: "))
167 (princ (format "%S" tt
))
170 Semantic could not find this data type in any of its global tables.
172 Semantic locates datatypes through either the local scope, or the global
176 ;; Describe local scope, and why we might not be able to
178 (semantic-analyzer-debug-describe-scope ctxt
'(type))
180 ;; Describe the typecache.
181 (princ "\nSemantic creates and maintains a type cache for each buffer.
182 If the type is a global type, then it should appear in they typecache.
183 To examine the typecache, type:
185 M-x semanticdb-typecache-dump RET
187 Current typecache Statistics:\n")
188 (princ (format " %4d types global in this file\n %4d types from includes.\n"
189 (length (semanticdb-typecache-file-tags tab
))
190 (length (semanticdb-typecache-include-tags tab
))))
192 (princ "\nIf the datatype is not in the typecache, then your include
193 path may be incorrect. ")
195 (semantic-analyzer-debug-insert-include-summary tab
)
199 (semantic-analyzer-debug-add-buttons)
202 (defun semantic-analyzer-debug-missing-innertype (ctxt idx comp
)
203 "Debug why we can't find an entry for CTXT prefix at IDX for known type.
204 We need to see if we have possible completions against the entry before
205 being too vocal about it.
206 Argument COMP are possible completions here."
207 (let* ((prefixitem (nth idx
(oref ctxt prefix
)))
208 (prevprefix (nth (1- idx
) (oref ctxt prefix
)))
209 (dt (nth (1- idx
) (oref ctxt prefixtypes
)))
210 (desired-type (semantic-analyze-type-constraint ctxt
))
211 (orig-buffer (current-buffer))
212 (ots (semantic-analyze-tag-type prevprefix
217 (when (not dt
) (error "Missing Innertype debugger is confused"))
218 (with-output-to-temp-buffer (help-buffer)
219 (with-current-buffer standard-output
220 (princ "Cannot find prefix \"")
222 (princ "\" in datatype:
224 (semantic-analyzer-debug-insert-tag dt
)
228 ;; Any language with a namespace.
229 ((string= (semantic-tag-type dt
) "namespace")
230 (princ "Semantic may not have found all possible namespaces with
232 (princ (semantic-tag-name dt
))
233 (princ ". You can debug the entire typecache, including merged namespaces
236 M-x semanticdb-typecache-dump RET")
239 ;; @todo - external declarations??
243 ;; A generic explanation
245 (princ "\nSemantic has found the datatype ")
246 (semantic-analyzer-debug-insert-tag dt
)
247 (if (or (not (semantic-equivalent-tag-p ots dt
))
248 (not (with-current-buffer orig-buffer
249 (car (semantic-analyze-dereference-metatype
250 ots
(oref ctxt scope
))))))
252 (nexttype (with-current-buffer orig-buffer
253 (car (semantic-analyze-dereference-metatype
254 ots
(oref ctxt scope
))))))
255 (if (eq nexttype lasttype
)
256 (princ "\n [ Debugger error trying to help with metatypes ]")
259 (princ "\nwhich is a metatype")
260 (princ "\nwhich is derived from metatype ")
261 (semantic-analyzer-debug-insert-tag lasttype
)))
263 (princ ".\nThe Metatype stack is:\n")
265 (semantic-analyzer-debug-insert-tag lasttype
)
268 (not (eq nexttype lasttype
)))
270 (semantic-analyzer-debug-insert-tag nexttype
)
272 (setq lasttype nexttype
274 (with-current-buffer orig-buffer
275 (car (semantic-analyze-dereference-metatype
276 nexttype
(oref ctxt scope
)))))
281 "Last metatype is nil. This means that semantic cannot derive
282 the list of members because the type referred to cannot be found.\n")
285 (princ "\nand its list of members.")
289 (princ " Semantic does not know what
290 possible completions there are for \"")
292 (princ "\". Examine the known
293 members below for more."))
294 (princ " Semantic knows of some
295 possible completions for \"")
303 (semantic-analyzer-debug-show-completions comp
)
305 (princ "\nKnown members of ")
306 (princ (semantic-tag-name dt
))
308 (dolist (M (semantic-tag-type-members dt
))
310 ;;(princ (semantic-format-tag-prototype M))
311 (semantic-analyzer-debug-insert-tag M
)
314 ;; This doesn't refer to in-type completions.
315 ;;(semantic-analyzer-debug-global-miss-text prefixitem)
319 (princ "\nWhen there are known members that would make good completion
320 candidates that are not in the completion list, then the most likely
321 cause is a type constraint. Semantic has determined that there is a
322 type constraint looking for the type ")
323 (if (semantic-tag-p desired-type
)
324 (semantic-analyzer-debug-insert-tag desired-type
)
325 (princ (format "%S" desired-type
)))
328 (semantic-analyzer-debug-add-buttons)
333 (defun semantic-analyzer-debug-test-local-context ()
334 "Test the local context parsed from the file."
335 (let* ((prefixandbounds (semantic-ctxt-current-symbol-and-bounds (point)))
336 (prefix (car prefixandbounds
))
337 (bounds (nth 2 prefixandbounds
))
339 (when (and (or (not prefixandbounds
)
343 (with-output-to-temp-buffer (help-buffer)
344 (with-current-buffer standard-output
345 (princ "Local Context Parser Failed.
347 If this is unexpected, then there is likely a bug in the Semantic
348 local context parser.
350 Consider debugging the function ")
351 (let ((lcf (fetch-overload 'semantic-ctxt-current-symbol-and-bounds
)))
353 (princ (symbol-name lcf
))
354 (princ "semantic-ctxt-current-symbol-and-bounds,
355 or implementing a version specific to ")
356 (princ (symbol-name major-mode
))
359 (semantic-analyzer-debug-add-buttons)
363 ;;; General Inserters with help
365 (defun semantic-analyzer-debug-show-completions (comp)
366 "Show the completion list COMP."
368 (princ "\nNo known possible completions.\n")
370 (princ "\nPossible completions are:\n")
377 (semantic-analyzer-debug-insert-tag C
)))
381 (defvar semantic-dependency-system-include-path
)
383 (defun semantic-analyzer-debug-insert-include-summary (table)
384 "Display a summary of includes for the semanticdb TABLE."
385 (require 'semantic
/dep
)
386 (semantic-fetch-tags)
387 (let ((inc (semantic-find-tags-by-class 'include table
))
388 ;;(path (semanticdb-find-test-translate-path-no-loading))
390 (with-current-buffer (semanticdb-get-buffer table
)
391 semanticdb-find-lost-includes
))
393 (with-current-buffer (semanticdb-get-buffer table
)
394 semantic-dependency-system-include-path
))
396 (with-current-buffer (semanticdb-get-buffer table
)
397 (and (boundp 'ede-object
)
400 (with-current-buffer (semanticdb-get-buffer table
)
401 (and (boundp 'ede-object-project
)
402 ede-object-project
))))
404 (princ "\n\nInclude Path Summary:")
406 (princ "\n\nThis file's project include search is handled by the EDE object:\n")
407 (princ " Buffer Target: ")
408 (princ (object-print edeobj
))
410 (when (not (eq edeobj edeproj
))
411 (princ " Buffer Project: ")
412 (princ (object-print edeproj
))
415 (let ((loc (ede-get-locator-object edeproj
)))
416 (princ " Backup Locator: ")
417 (princ (object-print loc
))
421 (princ "\n\nThe system include path is:\n")
427 (princ "\n\nInclude Summary: ")
428 (princ (semanticdb-full-filename table
))
430 (princ (format "%s contains %d includes.\n"
431 (file-name-nondirectory
432 (semanticdb-full-filename table
))
439 (let* ((fileinner (semantic-dependency-tag-file i
))
440 (tableinner (when fileinner
441 (semanticdb-file-table-object fileinner t
))))
442 (cond ((not fileinner
)
443 (setq unknown
(1+ unknown
)))
444 ((number-or-marker-p (oref tableinner pointmax
))
447 (setq unparsed
(1+ unparsed
))))))
448 (setq all
(+ ok unknown unparsed
))
449 (when (not (= 0 all
))
450 (princ (format " Unknown Includes: %d\n" unknown
))
451 (princ (format " Unparsed Includes: %d\n" unparsed
))
452 (princ (format " Parsed Includes: %d\n" ok
)))
458 (princ "\nA likely cause of an unfound tag is missing include files.")
459 (semantic-analyzer-debug-insert-tag-list
460 "The following includes were not found" unk
)
462 (princ "\nYou can fix the include path for ")
463 (princ (symbol-name (oref table major-mode
)))
464 (princ " by using this function:
466 M-x semantic-customize-system-include-path RET
468 which customizes the mode specific variable for the mode-local
469 variable `semantic-dependency-system-include-path'.")
472 (princ "\n No unknown includes.\n"))
475 (defun semantic-analyzer-debug-describe-scope (ctxt &optional classconstraint
)
476 "Describe the scope in CTXT for finding a global symbol.
477 Optional argument CLASSCONSTRAINT says to output to tags of that class."
478 (let* ((scope (oref ctxt
:scope
))
479 (parents (oref scope parents
))
480 (cc (or classconstraint
(oref ctxt prefixclass
)))
482 (princ "\nLocal Scope Information:")
483 (princ "\n * Tag Class Constraint against SCOPE: ")
484 (princ (format "%S" classconstraint
))
487 (semantic-analyzer-debug-insert-tag-list
488 " >> Known parent types with possible in scope symbols"
490 (princ "\n * No known parents in current scope."))
492 (let ((si (semantic-analyze-tags-of-class-list
493 (oref scope scope
) cc
))
494 (lv (semantic-analyze-tags-of-class-list
495 (oref scope localvar
) cc
))
498 (semantic-analyzer-debug-insert-tag-list
499 " >> Known symbols within the current scope"
501 (princ "\n * No known symbols currently in scope."))
504 (semantic-analyzer-debug-insert-tag-list
505 " >> Known symbols that are declared locally"
507 (princ "\n * No known symbols declared locally."))
512 (defun semantic-analyzer-debug-global-miss-text (name-in)
513 "Use 'princ' to show text describing not finding symbol NAME-IN.
514 NAME is the name of the unfound symbol."
515 (let ((name (cond ((stringp name-in
)
517 ((semantic-tag-p name-in
)
518 (semantic-format-tag-name name-in
))
519 (t (format "%S" name-in
)))))
520 (when (not (string= name
""))
523 (princ " is a local variable, argument, or symbol in some
524 namespace or class exposed via scoping statements, then it should
527 Debugging the scope can be done with:
528 M-x semantic-calculate-scope RET
530 If the prefix is a global symbol, in an included file, then
531 your search path may be incomplete.
536 (defun semantic-analyzer-debug-insert-tag-list (text taglist
)
537 "Prefixing with TEXT, dump TAGLIST in a help buffer."
538 (princ "\n") (princ text
) (princ ":\n")
542 ;;(princ (semantic-format-tag-prototype M))
543 (semantic-analyzer-debug-insert-tag M
)
547 (defun semantic-analyzer-debug-insert-tag (tag &optional parent
)
548 "Display a TAG by name, with possible jumpitude.
549 PARENT is a possible parent (by nesting) tag."
550 (let ((str (semantic-format-tag-prototype tag parent
)))
551 (if (and (semantic-tag-with-position-p tag
)
552 (semantic-tag-file-name tag
))
554 'mouse-face
'custom-button-pressed-face
562 (button-get button
'tag
))
563 (setq buff
(current-buffer))
565 (if (get-buffer-window buff
)
566 (select-window (get-buffer-window buff
))
567 (pop-to-buffer buff t
))
569 (pulse-line-hook-function)))
576 (defvar semantic-analyzer-debug-orig nil
577 "The originating buffer for a help button.")
579 (defun semantic-analyzer-debug-add-buttons ()
580 "Add push-buttons to the *Help* buffer.
581 Look for key expressions, and add push-buttons near them."
582 (let ((orig-buffer (make-marker)))
583 (set-marker orig-buffer
(point) (current-buffer))
584 ;; Get a buffer ready.
585 (with-current-buffer "*Help*"
586 (toggle-read-only -
1)
587 (goto-char (point-min))
588 (set (make-local-variable 'semantic-analyzer-debug-orig
) orig-buffer
)
589 ;; First, add do-in buttons to recommendations.
590 (while (re-search-forward "^\\s-*M-x \\(\\(\\w\\|\\s_\\)+\\) " nil t
)
591 (let ((fcn (match-string 1)))
592 (when (not (fboundp (intern-soft fcn
)))
593 (error "Help Err: Can't find %s" fcn
))
596 (insert-button "[ Do It ]"
597 'mouse-face
'custom-button-pressed-face
599 'action
`(lambda (arg)
600 (let ((M semantic-analyzer-debug-orig
))
601 (set-buffer (marker-buffer M
))
603 (call-interactively (quote ,(intern-soft fcn
))))
606 ;; Do something else?
610 (set-buffer-modified-p nil
)
613 (provide 'semantic
/analyze
/debug
)
615 ;; arch-tag: 943db1e5-47e6-4bec-9989-78ebfadf0358
616 ;;; semantic/analyze/debug.el ends here