Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cedet / semantic / chart.el
blobc15329a400e82bab445862104d60888063eb2cff
1 ;;; semantic/chart.el --- Utilities for use with semantic tag tables
3 ;; Copyright (C) 1999-2001, 2003, 2005, 2008-2014 Free Software
4 ;; Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; A set of simple functions for charting details about a file based on
26 ;; the output of the semantic parser.
29 (require 'chart)
30 (require 'semantic/db)
31 (require 'semantic/find)
33 ;;; Code:
35 (defun semantic-chart-tags-by-class (&optional tagtable)
36 "Create a bar chart representing the number of tags for a given tag class.
37 Each bar represents how many toplevel tags in TAGTABLE
38 exist with a given class. See `semantic-symbol->name-assoc-list'
39 for tokens which will be charted.
40 TAGTABLE is passed to `semantic-something-to-tag-table'."
41 (interactive)
42 (let* ((stream (semantic-something-to-tag-table
43 (or tagtable (current-buffer))))
44 (names (mapcar 'cdr semantic-symbol->name-assoc-list))
45 (nums (mapcar
46 (lambda (symname)
47 (length
48 (semantic-brute-find-tag-by-class (car symname)
49 stream)
51 semantic-symbol->name-assoc-list)))
52 (chart-bar-quickie 'vertical
53 "Semantic Toplevel Tag Volume"
54 names "Tag Class"
55 nums "Volume")
58 (defun semantic-chart-database-size (&optional tagtable)
59 "Create a bar chart representing the size of each file in semanticdb.
60 Each bar represents how many toplevel tags in TAGTABLE
61 exist in each database entry.
62 TAGTABLE is passed to `semantic-something-to-tag-table'."
63 (interactive)
64 (unless (and (fboundp 'semanticdb-minor-mode-p)
65 (semanticdb-minor-mode-p))
66 (error "Semanticdb is not enabled"))
67 (let* ((db semanticdb-current-database)
68 (dbt (semanticdb-get-database-tables db))
69 (names (mapcar 'car
70 (object-assoc-list
71 'file
72 dbt)))
73 (numnuts (mapcar (lambda (dba)
74 (prog1
75 (cons
76 (if (slot-boundp dba 'tags)
77 (length (oref dba tags))
79 (car names))
80 (setq names (cdr names))))
81 dbt))
82 (nums nil)
83 (fh (/ (- (frame-height) 7) 4)))
84 (setq numnuts (sort numnuts (lambda (a b) (> (car a) (car b)))))
85 (setq names (mapcar 'cdr numnuts)
86 nums (mapcar 'car numnuts))
87 (if (> (length names) fh)
88 (progn
89 (setcdr (nthcdr fh names) nil)
90 (setcdr (nthcdr fh nums) nil)))
91 (chart-bar-quickie 'horizontal
92 "Semantic DB Toplevel Tag Volume"
93 names "File"
94 nums "Volume")
97 (defun semantic-chart-token-complexity (tok)
98 "Calculate the `complexity' of token TOK."
99 (count-lines
100 (semantic-tag-end tok)
101 (semantic-tag-start tok)))
103 (defun semantic-chart-tag-complexity
104 (&optional class tagtable)
105 "Create a bar chart representing the complexity of some tags.
106 Complexity is calculated for tags of CLASS. Each bar represents
107 the complexity of some tag in TAGTABLE. Only the most complex
108 items are charted. TAGTABLE is passed to
109 `semantic-something-to-tag-table'."
110 (interactive)
111 (let* ((sym (if (not class) 'function))
112 (stream
113 (semantic-find-tags-by-class
114 sym (semantic-something-to-tag-table (or tagtable
115 (current-buffer)))
117 (name (cond ((semantic-tag-with-position-p (car stream))
118 (buffer-name (semantic-tag-buffer (car stream))))
119 (t "")))
120 (cplx (mapcar (lambda (tok)
121 (cons tok (semantic-chart-token-complexity tok)))
122 stream))
123 (namelabel (cdr (assoc 'function semantic-symbol->name-assoc-list)))
124 (names nil)
125 (nums nil))
126 (setq cplx (sort cplx (lambda (a b) (> (cdr a) (cdr b)))))
127 (while (and cplx (<= (length names) (/ (- (frame-height) 7) 4)))
128 (setq names (cons (semantic-tag-name (car (car cplx)))
129 names)
130 nums (cons (cdr (car cplx)) nums)
131 cplx (cdr cplx)))
132 ;; ;; (setq names (mapcar (lambda (str)
133 ;; ;; (substring str (- (length str) 10)))
134 ;; ;; names))
135 (chart-bar-quickie 'horizontal
136 (format "%s Complexity in %s"
137 (capitalize (symbol-name sym))
138 name)
139 names namelabel
140 nums "Complexity (Lines of code)")
143 (declare-function semanticdb-get-typecache "semantic/db-typecache")
144 (declare-function semantic-calculate-scope "semantic/scope")
146 (defun semantic-chart-analyzer ()
147 "Chart the extent of the context analysis."
148 (interactive)
149 (require 'semantic/db-typecache)
150 (require 'semantic/scope)
151 (let* ((p (semanticdb-find-translate-path nil nil))
152 (plen (length p))
153 (tab semanticdb-current-table)
154 (tc (semanticdb-get-typecache tab))
155 (tclen (+ (length (oref tc filestream))
156 (length (oref tc includestream))))
157 (scope (semantic-calculate-scope))
158 (fslen (length (oref scope fullscope)))
159 (lvarlen (length (oref scope localvar)))
161 (chart-bar-quickie 'vertical
162 (format "Analyzer Overhead in %s" (buffer-name))
163 '("includes" "typecache" "scopelen" "localvar")
164 "Overhead Entries"
165 (list plen tclen fslen lvarlen)
166 "Number of tags")
169 (provide 'semantic/chart)
171 ;;; semantic/chart.el ends here