Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / lisp / cedet / semantic / chart.el
CommitLineData
aa8724ae 1;;; semantic/chart.el --- Utilities for use with semantic tag tables
f273dfc6 2
ab422c4d
PE
3;; Copyright (C) 1999-2001, 2003, 2005, 2008-2013 Free Software
4;; Foundation, Inc.
f273dfc6
CY
5
6;; Author: Eric M. Ludlam <zappo@gnu.org>
7
8;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23;;; Commentary:
24;;
25;; A set of simple functions for charting details about a file based on
26;; the output of the semantic parser.
27;;
28
a60f2e7b 29(require 'chart)
3d9d8486 30(require 'semantic/db)
3f2a848d 31(require 'semantic/find)
3d9d8486 32
f273dfc6
CY
33;;; Code:
34
35(defun semantic-chart-tags-by-class (&optional tagtable)
36 "Create a bar chart representing the number of tags for a given tag class.
37Each bar represents how many toplevel tags in TAGTABLE
38exist with a given class. See `semantic-symbol->name-assoc-list'
39for tokens which will be charted.
9bf6c65c 40TAGTABLE is passed to `semantic-something-to-tag-table'."
f273dfc6
CY
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)
50 ))
51 semantic-symbol->name-assoc-list)))
52 (chart-bar-quickie 'vertical
53 "Semantic Toplevel Tag Volume"
54 names "Tag Class"
55 nums "Volume")
56 ))
57
58(defun semantic-chart-database-size (&optional tagtable)
59 "Create a bar chart representing the size of each file in semanticdb.
60Each bar represents how many toplevel tags in TAGTABLE
61exist in each database entry.
62TAGTABLE is passed to `semantic-something-to-tag-table'."
63 (interactive)
3d9d8486
CY
64 (unless (and (fboundp 'semanticdb-minor-mode-p)
65 (semanticdb-minor-mode-p))
66 (error "Semanticdb is not enabled"))
f273dfc6
CY
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))
78 1)
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")
95 ))
96
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)))
102
103(defun semantic-chart-tag-complexity
104 (&optional class tagtable)
105 "Create a bar chart representing the complexity of some tags.
106Complexity is calculated for tags of CLASS. Each bar represents
107the complexity of some tag in TAGTABLE. Only the most complex
9bf6c65c 108items are charted. TAGTABLE is passed to
f273dfc6
CY
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)))
116 ))
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)")
141 ))
142
3d9d8486
CY
143(declare-function semanticdb-get-typecache "semantic/db-typecache")
144(declare-function semantic-calculate-scope "semantic/scope")
145
f273dfc6
CY
146(defun semantic-chart-analyzer ()
147 "Chart the extent of the context analysis."
148 (interactive)
3d9d8486
CY
149 (require 'semantic/db-typecache)
150 (require 'semantic/scope)
f273dfc6
CY
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)))
160 )
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")
167 ))
168
f273dfc6
CY
169(provide 'semantic/chart)
170
aa8724ae 171;;; semantic/chart.el ends here