Commit | Line | Data |
---|---|---|
aa8724ae | 1 | ;;; semantic/chart.el --- Utilities for use with semantic tag tables |
f273dfc6 | 2 | |
acaf905b | 3 | ;; Copyright (C) 1999-2001, 2003, 2005, 2008-2012 |
9bf6c65c | 4 | ;; Free Software 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 | ||
aa8724ae | 29 | (require 'semantic) |
a60f2e7b | 30 | (require 'chart) |
3d9d8486 CY |
31 | (require 'semantic/db) |
32 | (require 'semantic/tag) | |
33 | ||
34 | (eval-when-compile (require 'semantic/find)) | |
35 | ||
f273dfc6 CY |
36 | ;;; Code: |
37 | ||
38 | (defun semantic-chart-tags-by-class (&optional tagtable) | |
39 | "Create a bar chart representing the number of tags for a given tag class. | |
40 | Each bar represents how many toplevel tags in TAGTABLE | |
41 | exist with a given class. See `semantic-symbol->name-assoc-list' | |
42 | for tokens which will be charted. | |
9bf6c65c | 43 | TAGTABLE is passed to `semantic-something-to-tag-table'." |
f273dfc6 CY |
44 | (interactive) |
45 | (let* ((stream (semantic-something-to-tag-table | |
46 | (or tagtable (current-buffer)))) | |
47 | (names (mapcar 'cdr semantic-symbol->name-assoc-list)) | |
48 | (nums (mapcar | |
49 | (lambda (symname) | |
50 | (length | |
51 | (semantic-brute-find-tag-by-class (car symname) | |
52 | stream) | |
53 | )) | |
54 | semantic-symbol->name-assoc-list))) | |
55 | (chart-bar-quickie 'vertical | |
56 | "Semantic Toplevel Tag Volume" | |
57 | names "Tag Class" | |
58 | nums "Volume") | |
59 | )) | |
60 | ||
61 | (defun semantic-chart-database-size (&optional tagtable) | |
62 | "Create a bar chart representing the size of each file in semanticdb. | |
63 | Each bar represents how many toplevel tags in TAGTABLE | |
64 | exist in each database entry. | |
65 | TAGTABLE is passed to `semantic-something-to-tag-table'." | |
66 | (interactive) | |
3d9d8486 CY |
67 | (unless (and (fboundp 'semanticdb-minor-mode-p) |
68 | (semanticdb-minor-mode-p)) | |
69 | (error "Semanticdb is not enabled")) | |
f273dfc6 CY |
70 | (let* ((db semanticdb-current-database) |
71 | (dbt (semanticdb-get-database-tables db)) | |
72 | (names (mapcar 'car | |
73 | (object-assoc-list | |
74 | 'file | |
75 | dbt))) | |
76 | (numnuts (mapcar (lambda (dba) | |
77 | (prog1 | |
78 | (cons | |
79 | (if (slot-boundp dba 'tags) | |
80 | (length (oref dba tags)) | |
81 | 1) | |
82 | (car names)) | |
83 | (setq names (cdr names)))) | |
84 | dbt)) | |
85 | (nums nil) | |
86 | (fh (/ (- (frame-height) 7) 4))) | |
87 | (setq numnuts (sort numnuts (lambda (a b) (> (car a) (car b))))) | |
88 | (setq names (mapcar 'cdr numnuts) | |
89 | nums (mapcar 'car numnuts)) | |
90 | (if (> (length names) fh) | |
91 | (progn | |
92 | (setcdr (nthcdr fh names) nil) | |
93 | (setcdr (nthcdr fh nums) nil))) | |
94 | (chart-bar-quickie 'horizontal | |
95 | "Semantic DB Toplevel Tag Volume" | |
96 | names "File" | |
97 | nums "Volume") | |
98 | )) | |
99 | ||
100 | (defun semantic-chart-token-complexity (tok) | |
101 | "Calculate the `complexity' of token TOK." | |
102 | (count-lines | |
103 | (semantic-tag-end tok) | |
104 | (semantic-tag-start tok))) | |
105 | ||
106 | (defun semantic-chart-tag-complexity | |
107 | (&optional class tagtable) | |
108 | "Create a bar chart representing the complexity of some tags. | |
109 | Complexity is calculated for tags of CLASS. Each bar represents | |
110 | the complexity of some tag in TAGTABLE. Only the most complex | |
9bf6c65c | 111 | items are charted. TAGTABLE is passed to |
f273dfc6 CY |
112 | `semantic-something-to-tag-table'." |
113 | (interactive) | |
114 | (let* ((sym (if (not class) 'function)) | |
115 | (stream | |
116 | (semantic-find-tags-by-class | |
117 | sym (semantic-something-to-tag-table (or tagtable | |
118 | (current-buffer))) | |
119 | )) | |
120 | (name (cond ((semantic-tag-with-position-p (car stream)) | |
121 | (buffer-name (semantic-tag-buffer (car stream)))) | |
122 | (t ""))) | |
123 | (cplx (mapcar (lambda (tok) | |
124 | (cons tok (semantic-chart-token-complexity tok))) | |
125 | stream)) | |
126 | (namelabel (cdr (assoc 'function semantic-symbol->name-assoc-list))) | |
127 | (names nil) | |
128 | (nums nil)) | |
129 | (setq cplx (sort cplx (lambda (a b) (> (cdr a) (cdr b))))) | |
130 | (while (and cplx (<= (length names) (/ (- (frame-height) 7) 4))) | |
131 | (setq names (cons (semantic-tag-name (car (car cplx))) | |
132 | names) | |
133 | nums (cons (cdr (car cplx)) nums) | |
134 | cplx (cdr cplx))) | |
135 | ;; ;; (setq names (mapcar (lambda (str) | |
136 | ;; ;; (substring str (- (length str) 10))) | |
137 | ;; ;; names)) | |
138 | (chart-bar-quickie 'horizontal | |
139 | (format "%s Complexity in %s" | |
140 | (capitalize (symbol-name sym)) | |
141 | name) | |
142 | names namelabel | |
143 | nums "Complexity (Lines of code)") | |
144 | )) | |
145 | ||
3d9d8486 CY |
146 | (declare-function semanticdb-get-typecache "semantic/db-typecache") |
147 | (declare-function semantic-calculate-scope "semantic/scope") | |
148 | ||
f273dfc6 CY |
149 | (defun semantic-chart-analyzer () |
150 | "Chart the extent of the context analysis." | |
151 | (interactive) | |
3d9d8486 CY |
152 | (require 'semantic/db-typecache) |
153 | (require 'semantic/scope) | |
f273dfc6 CY |
154 | (let* ((p (semanticdb-find-translate-path nil nil)) |
155 | (plen (length p)) | |
156 | (tab semanticdb-current-table) | |
157 | (tc (semanticdb-get-typecache tab)) | |
158 | (tclen (+ (length (oref tc filestream)) | |
159 | (length (oref tc includestream)))) | |
160 | (scope (semantic-calculate-scope)) | |
161 | (fslen (length (oref scope fullscope))) | |
162 | (lvarlen (length (oref scope localvar))) | |
163 | ) | |
164 | (chart-bar-quickie 'vertical | |
165 | (format "Analyzer Overhead in %s" (buffer-name)) | |
166 | '("includes" "typecache" "scopelen" "localvar") | |
167 | "Overhead Entries" | |
168 | (list plen tclen fslen lvarlen) | |
169 | "Number of tags") | |
170 | )) | |
171 | ||
f273dfc6 CY |
172 | (provide 'semantic/chart) |
173 | ||
aa8724ae | 174 | ;;; semantic/chart.el ends here |