Commit a6de3d1a authored by Chong Yidong's avatar Chong Yidong
Browse files

cedet/semantic/debug.el,

cedet/semantic/doc.el,
cedet/semantic/tag-write.el,
cedet/semantic/analyze/complete.el,
cedet/semantic/analyze/debug.el,
cedet/semantic/analyze/fcn.el,
cedet/semantic/analyze/refs.el: New files.
parent f273dfc6
;;; semantic/analyze/complete.el --- Smart Completions
;;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
;; Author: Eric M. Ludlam <zappo@gnu.org>
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Caclulate smart completions.
;;
;; Uses the analyzer context routine to determine the best possible
;; list of completions.
;;
;;; History:
;;
;; Code was moved here from semantic-analyze.el
(require 'semantic/analyze)
;;; Code:
;;; Helper Fcns
;;
;;
(define-overloadable-function semantic-analyze-type-constants (type)
"For the tag TYPE, return any constant symbols of TYPE.
Used as options when completing.")
(defun semantic-analyze-type-constants-default (type)
"Do nothing with TYPE."
nil)
;; Old impl of the above. I'm not sure what the issue is
; (let ((ans
; (:override-with-args
; ((semantic-analyze-find-tag (semantic-tag-name type)))
; ;; Be default, we don't know.
; nil))
; (out nil))
; (dolist (elt ans)
; (cond
; ((stringp elt)
; (push (semantic-tag-new-variable
; elt (semantic-tag-name type) nil)
; out))
; ((semantic-tag-p elt)
; (push elt out))
; (t nil)))
; (nreverse out)))
(defun semantic-analyze-tags-of-class-list (tags classlist)
"Return the tags in TAGS that are of classes in CLASSLIST."
(let ((origc tags))
;; Accept only tags that are of the datatype specified by
;; the desired classes.
(setq tags (apply 'nconc ;; All input lists are permutable.
(mapcar (lambda (class)
(semantic-find-tags-by-class class origc))
classlist)))
tags))
;;; MAIN completion calculator
;;
;;
(define-overloadable-function semantic-analyze-possible-completions (context)
"Return a list of semantic tags which are possible completions.
CONTEXT is either a position (such as point), or a precalculated
context. Passing in a context is useful if the caller also needs
to access parts of the analysis.
Completions run through the following filters:
* Elements currently in scope
* Constants currently in scope
* Elements match the :prefix in the CONTEXT.
* Type of the completion matches the type of the context.
Context type matching can identify the following:
* No specific type
* Assignment into a variable of some type.
* Argument to a function with type constraints.
When called interactively, displays the list of possible completions
in a buffer."
(interactive "d")
;; In theory, we don't need the below since the context will
;; do it for us.
;;(semantic-refresh-tags-safe)
(with-syntax-table semantic-lex-syntax-table
(let* ((context (if (semantic-analyze-context-child-p context)
context
(semantic-analyze-current-context context)))
(ans (if (not context)
(error "Nothing to Complete.")
(:override))))
;; If interactive, display them.
(when (interactive-p)
(with-output-to-temp-buffer "*Possible Completions*"
(semantic-analyze-princ-sequence ans "" (current-buffer)))
(shrink-window-if-larger-than-buffer
(get-buffer-window "*Possible Completions*")))
ans)))
(defun semantic-analyze-possible-completions-default (context)
"Default method for producing smart completions.
Argument CONTEXT is an object specifying the locally derived context."
(let* ((a context)
(desired-type (semantic-analyze-type-constraint a))
(desired-class (oref a prefixclass))
(prefix (oref a prefix))
(prefixtypes (oref a prefixtypes))
(completetext nil)
(completetexttype nil)
(scope (oref a scope))
(localvar (oref scope localvar))
(c nil))
;; Calculate what our prefix string is so that we can
;; find all our matching text.
(setq completetext (car (reverse prefix)))
(if (semantic-tag-p completetext)
(setq completetext (semantic-tag-name completetext)))
(if (and (not completetext) (not desired-type))
(error "Nothing to complete"))
(if (not completetext) (setq completetext ""))
;; This better be a reasonable type, or we should fry it.
;; The prefixtypes should always be at least 1 less than
;; the prefix since the type is never looked up for the last
;; item when calculating a sequence.
(setq completetexttype (car (reverse prefixtypes)))
(when (or (not completetexttype)
(not (and (semantic-tag-p completetexttype)
(eq (semantic-tag-class completetexttype) 'type))))
;; What should I do here? I think this is an error condition.
(setq completetexttype nil)
;; If we had something that was a completetexttype but it wasn't
;; valid, then express our dismay!
(when (> (length prefix) 1)
(let* ((errprefix (car (cdr (reverse prefix)))))
(error "Cannot find types for `%s'"
(cond ((semantic-tag-p errprefix)
(semantic-format-tag-prototype errprefix))
(t
(format "%S" errprefix)))))
))
;; There are many places to get our completion stream for.
;; Here we go.
(if completetexttype
(setq c (semantic-find-tags-for-completion
completetext
(semantic-analyze-scoped-type-parts completetexttype scope)
))
;; No type based on the completetext. This is a free-range
;; var or function. We need to expand our search beyond this
;; scope into semanticdb, etc.
(setq c (nconc
;; Argument list and local variables
(semantic-find-tags-for-completion completetext localvar)
;; The current scope
(semantic-find-tags-for-completion completetext (oref scope fullscope))
;; The world
(semantic-analyze-find-tags-by-prefix completetext))
)
)
(let ((origc c)
(dtname (semantic-tag-name desired-type)))
;; Reset c.
(setq c nil)
;; Loop over all the found matches, and catagorize them
;; as being possible features.
(while origc
(cond
;; Strip operators
((semantic-tag-get-attribute (car origc) :operator-flag)
nil
)
;; If we are completing from within some prefix,
;; then we want to exclude constructors and destructors
((and completetexttype
(or (semantic-tag-get-attribute (car origc) :constructor-flag)
(semantic-tag-get-attribute (car origc) :destructor-flag)))
nil
)
;; If there is a desired type, we need a pair of restrictions
(desired-type
(cond
;; Ok, we now have a completion list based on the text we found
;; we want to complete on. Now filter that stream against the
;; type we want to search for.
((string= dtname (semantic-analyze-type-to-name (semantic-tag-type (car origc))))
(setq c (cons (car origc) c))
)
;; Now anything that is a compound type which could contain
;; additional things which are of the desired type
((semantic-tag-type (car origc))
(let ((att (semantic-analyze-tag-type (car origc) scope))
)
(if (and att (semantic-tag-type-members att))
(setq c (cons (car origc) c))))
)
) ; cond
); desired type
;; No desired type, no other restrictions. Just add.
(t
(setq c (cons (car origc) c)))
); cond
(setq origc (cdr origc)))
(when desired-type
;; Some types, like the enum in C, have special constant values that
;; we could complete with. Thus, if the target is an enum, we can
;; find possible symbol values to fill in that value.
(let ((constants
(semantic-analyze-type-constants desired-type)))
(if constants
(progn
;; Filter
(setq constants
(semantic-find-tags-for-completion
completetext constants))
;; Add to the list
(setq c (nconc c constants)))
)))
)
(when desired-class
(setq c (semantic-analyze-tags-of-class-list c desired-class)))
;; Pull out trash.
;; NOTE TO SELF: Is this too slow?
;; OTHER NOTE: Do we not want to strip duplicates by name and
;; only by position? When are duplicate by name but not by tag
;; useful?
(setq c (semantic-unique-tag-table-by-name c))
;; All done!
c))
(provide 'semantic/analyze/complete)
;;; semantic/analyze/complete.el ends here
;;; semantic/analyze/debug.el --- Debug the analyzer
;;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
;; Author: Eric M. Ludlam <zappo@gnu.org>
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Provide a top-order debugging tool for figuring out what's going on with
;; smart completion and analyzer mode.
(require 'semantic/analyze)
(require 'semantic/db-typecache)
;;; Code:
(defun semantic-analyze-debug-assist ()
"Debug semantic analysis at the current point."
(interactive)
(let ((actualfcn (fetch-overload 'semantic-analyze-current-context))
(ctxt (semantic-analyze-current-context))
)
;; What to show.
(if actualfcn
(message "Mode %s does not use the default analyzer."
major-mode)
;; Debug our context.
)
(or (semantic-analyzer-debug-test-local-context)
(and ctxt (semantic-analyzer-debug-found-prefix ctxt))
)
))
(defun semantic-analyzer-debug-found-prefix (ctxt)
"Debug the prefix found by the analyzer output CTXT."
(let* ((pf (oref ctxt prefix))
(pft (oref ctxt prefixtypes))
(idx 0)
(stop nil)
(comp (condition-case nil
(semantic-analyze-possible-completions ctxt)
(error nil)))
)
(while (and (nth idx pf) (not stop))
(let ((pentry (nth idx pf))
(ptentry (nth idx pft)))
(if (or (stringp pentry) (not ptentry))
;; Found someting ok. stop
(setq stop t)
(setq idx (1+ idx)))))
;; We found the first non-tag entry. What is the situation?
(cond
((and (eq idx 0) (stringp (car pf)))
;; First part, we couldn't find it.
(semantic-analyzer-debug-global-symbol ctxt (car pf) comp))
((not (nth (1- idx) pft)) ;; idx can't be 0 here.
;; The previous entry failed to have an identifiable data
;; type, which is a global search.
(semantic-analyzer-debug-missing-datatype ctxt idx comp))
((and (nth (1- idx) pft) (stringp (nth idx pf)))
;; Non-first search, didn't find string in known data type.
(semantic-analyzer-debug-missing-innertype ctxt idx comp))
(t
;; Things are ok?
(message "Things look ok."))
)))
(defun semantic-analyzer-debug-global-symbol (ctxt prefix comp)
"Debug why we can't find the first entry in the CTXT PREFIX.
Argument COMP are possible completions here."
(let ((tab semanticdb-current-table)
(finderr nil)
(origbuf (current-buffer))
)
(with-output-to-temp-buffer (help-buffer)
(with-current-buffer standard-output
(princ "Unable to find prefix ")
(princ prefix)
(princ ".\n\n")
;; NOTE: This line is copied from semantic-analyze-current-context.
;; You will need to update both places.
(condition-case err
(save-excursion
(set-buffer origbuf)
(let* ((position (or (cdr-safe (oref ctxt bounds)) (point)))
(prefixtypes nil) ; Used as type return
(scope (semantic-calculate-scope position))
)
(semantic-analyze-find-tag-sequence
(list prefix "") scope 'prefixtypes)
)
)
(error (setq finderr err)))
(if finderr
(progn
(princ "The prefix lookup code threw the following error:\n ")
(prin1 finderr)
(princ "\n\nTo debug this error you can do this:
M-x toggle-debug-on-error RET
and then re-run the debug analyzer.\n")
)
;; No find error, just not found
(princ "The prefix ")
(princ prefix)
(princ " could not be found in the local scope,
nor in any search tables.\n")
)
(princ "\n")
;; Describe local scope, and why we might not be able to
;; find it.
(semantic-analyzer-debug-describe-scope ctxt)
(semantic-analyzer-debug-show-completions comp)
(princ "When Semantic cannot find a symbol, it could be because the include
path was setup incorrectly.\n")
(semantic-analyzer-debug-insert-include-summary tab)
))
(semantic-analyzer-debug-add-buttons)
))
(defun semantic-analyzer-debug-missing-datatype (ctxt idx comp)
"Debug why we can't find a datatype entry for CTXT prefix at IDX.
Argument COMP are possible completions here."
(let* ((prefixitem (nth idx (oref ctxt prefix)))
(dt (nth (1- idx) (oref ctxt prefixtypes)))
(tt (semantic-tag-type prefixitem))
(tab semanticdb-current-table)
)
(when dt (error "Missing Datatype debugger is confused"))
(with-output-to-temp-buffer (help-buffer)
(with-current-buffer standard-output
(princ "Unable to find datatype for: \"")
(princ (semantic-format-tag-prototype prefixitem))
(princ "\".
Declared type is: ")
(when (semantic-tag-p tt)
(semantic-analyzer-debug-insert-tag tt)
(princ "\nRaw data type is: "))
(princ (format "%S" tt))
(princ "
Semantic could not find this data type in any of its global tables.
Semantic locates datatypes through either the local scope, or the global
typecache.
")
;; Describe local scope, and why we might not be able to
;; find it.
(semantic-analyzer-debug-describe-scope ctxt '(type))
;; Describe the typecache.
(princ "\nSemantic creates and maintains a type cache for each buffer.
If the type is a global type, then it should appear in they typecache.
To examine the typecache, type:
M-x semanticdb-typecache-dump RET
Current typecache Statistics:\n")
(princ (format " %4d types global in this file\n %4d types from includes.\n"
(length (semanticdb-typecache-file-tags tab))
(length (semanticdb-typecache-include-tags tab))))
(princ "\nIf the datatype is not in the typecache, then your include
path may be incorrect. ")
(semantic-analyzer-debug-insert-include-summary tab)
;; End with-buffer
))
(semantic-analyzer-debug-add-buttons)
))
(defun semantic-analyzer-debug-missing-innertype (ctxt idx comp)
"Debug why we can't find an entry for CTXT prefix at IDX for known type.
We need to see if we have possible completions against the entry before
being too vocal about it.
Argument COMP are possible completions here."
(let* ((prefixitem (nth idx (oref ctxt prefix)))
(prevprefix (nth (1- idx) (oref ctxt prefix)))
(dt (nth (1- idx) (oref ctxt prefixtypes)))
(desired-type (semantic-analyze-type-constraint ctxt))
(orig-buffer (current-buffer))
(ots (semantic-analyze-tag-type prevprefix
(oref ctxt scope)
t ; Don't deref
))
)
(when (not dt) (error "Missing Innertype debugger is confused"))
(with-output-to-temp-buffer (help-buffer)
(with-current-buffer standard-output
(princ "Cannot find prefix \"")
(princ prefixitem)
(princ "\" in datatype:
")
(semantic-analyzer-debug-insert-tag dt)
(princ "\n")
(cond
;; Any language with a namespace.
((string= (semantic-tag-type dt) "namespace")
(princ "Semantic may not have found all possible namespaces with
the name ")
(princ (semantic-tag-name dt))
(princ ". You can debug the entire typecache, including merged namespaces
with the command:
M-x semanticdb-typecache-dump RET")
)
;; @todo - external declarations??
(nil
nil)
;; A generic explanation
(t
(princ "\nSemantic has found the datatype ")
(semantic-analyzer-debug-insert-tag dt)
(if (or (not (semantic-equivalent-tag-p ots dt))
(not (save-excursion
(set-buffer orig-buffer)
(car (semantic-analyze-dereference-metatype
ots (oref ctxt scope))))))
(let ((lasttype ots)
(nexttype (save-excursion
(set-buffer orig-buffer)
(car (semantic-analyze-dereference-metatype
ots (oref ctxt scope))))))
(if (eq nexttype lasttype)
(princ "\n [ Debugger error trying to help with metatypes ]")
(if (eq ots dt)
(princ "\nwhich is a metatype")
(princ "\nwhich is derived from metatype ")
(semantic-analyzer-debug-insert-tag lasttype)))
(princ ".\nThe Metatype stack is:\n")
(princ " ")
(semantic-analyzer-debug-insert-tag lasttype)
(princ "\n")
(while (and nexttype
(not (eq nexttype lasttype)))
(princ " ")
(semantic-analyzer-debug-insert-tag nexttype)
(princ "\n")
(setq lasttype nexttype
nexttype
(save-excursion
(set-buffer orig-buffer)
(car (semantic-analyze-dereference-metatype
nexttype (oref ctxt scope)))))
)
(when (not nexttype)
(princ " nil\n\n")
(princ
"Last metatype is nil. This means that semantic cannot derive
the list of members because the type referred to cannot be found.\n")
)
)
(princ "\nand its list of members.")
(if (not comp)
(progn
(princ " Semantic does not know what
possible completions there are for \"")
(princ prefixitem)
(princ "\". Examine the known
members below for more."))
(princ " Semantic knows of some
possible completions for \"")
(princ prefixitem)
(princ "\".")))
)
;; end cond
)
(princ "\n")
(semantic-analyzer-debug-show-completions comp)
(princ "\nKnown members of ")
(princ (semantic-tag-name dt))
(princ ":\n")
(dolist (M (semantic-tag-type-members dt))
(princ " ")
;;(princ (semantic-format-tag-prototype M))
(semantic-analyzer-debug-insert-tag M)
(princ "\n"))
;; This doesn't refer to in-type completions.
;;(semantic-analyzer-debug-global-miss-text prefixitem)
;; More explanation
(when desired-type
(princ "\nWhen there are known members that would make good completion
candidates that are not in the completion list, then the most likely
cause is a type constraint. Semantic has determined that there is a
type constraint looking for the type ")
(if (semantic-tag-p desired-type)
(semantic-analyzer-debug-insert-tag desired-type)
(princ (format "%S" desired-type)))
(princ "."))
))
(semantic-analyzer-debug-add-buttons)
))
(defun semantic-analyzer-debug-test-local-context ()
"Test the local context parsed from the file."
(let* ((prefixandbounds (semantic-ctxt-current-symbol-and-bounds (point)))
(prefix (car prefixandbounds))
(bounds (nth 2 prefixandbounds))
)
(when (and (or (not prefixandbounds)
(not prefix)
(not bounds))
)
(with-output-to-temp-buffer (help-buffer)
(with-current-buffer standard-output
(princ "Local Context Parser Failed.