chart.el 5.6 KB

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