db-ref.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ;;; semantic/db-ref.el --- Handle cross-db file references
  2. ;;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Handle cross-database file references.
  18. ;;
  19. ;; Any given database may be referred to by some other database. For
  20. ;; example, if a .cpp file has a #include in a header, then that
  21. ;; header file should have a reference to the .cpp file that included
  22. ;; it.
  23. ;;
  24. ;; This is critical for purposes where a file (such as a .cpp file)
  25. ;; needs to have its caches flushed because of changes in the
  26. ;; header. Changing a header may cause a referring file to be
  27. ;; reparsed due to account for changes in defined macros, or perhaps
  28. ;; a change to files the header includes.
  29. ;;; Code:
  30. (require 'eieio)
  31. (require 'semantic)
  32. (require 'semantic/db)
  33. (require 'semantic/tag)
  34. ;; For the semantic-find-tags-by-name-regexp macro.
  35. (eval-when-compile (require 'semantic/find))
  36. (defmethod semanticdb-add-reference ((dbt semanticdb-abstract-table)
  37. include-tag)
  38. "Add a reference for the database table DBT based on INCLUDE-TAG.
  39. DBT is the database table that owns the INCLUDE-TAG. The reference
  40. will be added to the database that INCLUDE-TAG refers to."
  41. ;; NOTE: I should add a check to make sure include-tag is in DB.
  42. ;; but I'm too lazy.
  43. (let* ((semanticdb-find-default-throttle
  44. (if (featurep 'semantic/db-find)
  45. (remq 'unloaded semanticdb-find-default-throttle)
  46. nil))
  47. (refdbt (semanticdb-find-table-for-include include-tag dbt))
  48. ;;(fullfile (semanticdb-full-filename dbt))
  49. )
  50. (when refdbt
  51. ;; Add our filename (full path)
  52. ;; (object-add-to-list refdbt 'file-refs fullfile)
  53. ;; Add our database.
  54. (object-add-to-list refdbt 'db-refs dbt)
  55. t)))
  56. (defmethod semanticdb-check-references ((dbt semanticdb-abstract-table))
  57. "Check and cleanup references in the database DBT.
  58. Abstract tables would be difficult to reference."
  59. ;; Not sure how an abstract table can have references.
  60. nil)
  61. (defmethod semanticdb-includes-in-table ((dbt semanticdb-abstract-table))
  62. "Return a list of direct includes in table DBT."
  63. (semantic-find-tags-by-class 'include (semanticdb-get-tags dbt)))
  64. (defmethod semanticdb-check-references ((dbt semanticdb-table))
  65. "Check and cleanup references in the database DBT.
  66. Any reference to a file that cannot be found, or whos file no longer
  67. refers to DBT will be removed."
  68. (let ((refs (oref dbt db-refs))
  69. (myexpr (concat "\\<" (oref dbt file)))
  70. )
  71. (while refs
  72. (let* ((ok t)
  73. (db (car refs))
  74. (f (when (semanticdb-table-child-p db)
  75. (semanticdb-full-filename db)))
  76. )
  77. ;; The file was deleted
  78. (when (and f (not (file-exists-p f)))
  79. (setq ok nil))
  80. ;; The reference no longer includes the textual reference?
  81. (let* ((refs (semanticdb-includes-in-table db))
  82. (inc (semantic-find-tags-by-name-regexp
  83. myexpr refs)))
  84. (when (not inc)
  85. (setq ok nil)))
  86. ;; Remove not-ok databases from the list.
  87. (when (not ok)
  88. (object-remove-from-list dbt 'db-refs db)
  89. ))
  90. (setq refs (cdr refs)))))
  91. (defmethod semanticdb-refresh-references ((dbt semanticdb-abstract-table))
  92. "Refresh references to DBT in other files."
  93. ;; alternate tables can't be edited, so can't be changed.
  94. nil
  95. )
  96. (defmethod semanticdb-refresh-references ((dbt semanticdb-table))
  97. "Refresh references to DBT in other files."
  98. (let ((refs (semanticdb-includes-in-table dbt))
  99. )
  100. (while refs
  101. (if (semanticdb-add-reference dbt (car refs))
  102. nil
  103. ;; If we succeeded, then do... nothing?
  104. nil
  105. )
  106. (setq refs (cdr refs)))
  107. ))
  108. (defmethod semanticdb-notify-references ((dbt semanticdb-table)
  109. method)
  110. "Notify all references of the table DBT using method.
  111. METHOD takes two arguments.
  112. (METHOD TABLE-TO-NOTIFY DBT)
  113. TABLE-TO-NOTIFY is a semanticdb-table which is being notified.
  114. DBT, the second argument is DBT."
  115. (mapc (lambda (R) (funcall method R dbt))
  116. (oref dbt db-refs)))
  117. ;;; DEBUG
  118. ;;
  119. (defclass semanticdb-ref-adebug ()
  120. ((i-depend-on :initarg :i-depend-on)
  121. (local-table :initarg :local-table)
  122. (i-include :initarg :i-include))
  123. "Simple class to allow ADEBUG to show a nice list.")
  124. (declare-function data-debug-new-buffer "data-debug")
  125. (declare-function data-debug-insert-object-slots "eieio-datadebug")
  126. (defun semanticdb-ref-test (refresh)
  127. "Dump out the list of references for the current buffer.
  128. If REFRESH is non-nil, cause the current table to have its references
  129. refreshed before dumping the result."
  130. (interactive "p")
  131. (require 'eieio-datadebug)
  132. ;; If we need to refresh... then do so.
  133. (when refresh
  134. (semanticdb-refresh-references semanticdb-current-table))
  135. ;; Do the debug system
  136. (let* ((tab semanticdb-current-table)
  137. (myrefs (oref tab db-refs))
  138. (myinc (semanticdb-includes-in-table tab))
  139. (adbc (semanticdb-ref-adebug "DEBUG"
  140. :i-depend-on myrefs
  141. :local-table tab
  142. :i-include myinc)))
  143. (data-debug-new-buffer "*References ADEBUG*")
  144. (data-debug-insert-object-slots adbc "!"))
  145. )
  146. (provide 'semantic/db-ref)
  147. ;;; semantic/db-ref.el ends here