db-ref.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;;; semantic/db-ref.el --- Handle cross-db file references
  2. ;;; Copyright (C) 2007-2017 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 'cl-generic)
  32. (require 'semantic)
  33. (require 'semantic/db)
  34. (require 'semantic/tag)
  35. ;; For the semantic-find-tags-by-name-regexp macro.
  36. (eval-when-compile (require 'semantic/find))
  37. (cl-defmethod semanticdb-add-reference ((dbt semanticdb-abstract-table)
  38. include-tag)
  39. "Add a reference for the database table DBT based on INCLUDE-TAG.
  40. DBT is the database table that owns the INCLUDE-TAG. The reference
  41. will be added to the database that INCLUDE-TAG refers to."
  42. ;; NOTE: I should add a check to make sure include-tag is in DB.
  43. ;; but I'm too lazy.
  44. (let* ((semanticdb-find-default-throttle
  45. (if (featurep 'semantic/db-find)
  46. (remq 'unloaded semanticdb-find-default-throttle)
  47. nil))
  48. (refdbt (semanticdb-find-table-for-include include-tag dbt))
  49. ;;(fullfile (semanticdb-full-filename dbt))
  50. )
  51. (when refdbt
  52. ;; Add our filename (full path)
  53. ;; (object-add-to-list refdbt 'file-refs fullfile)
  54. ;; Add our database.
  55. (object-add-to-list refdbt 'db-refs dbt)
  56. t)))
  57. (cl-defmethod semanticdb-check-references ((dbt semanticdb-abstract-table))
  58. "Check and cleanup references in the database DBT.
  59. Abstract tables would be difficult to reference."
  60. ;; Not sure how an abstract table can have references.
  61. nil)
  62. (cl-defmethod semanticdb-includes-in-table ((dbt semanticdb-abstract-table))
  63. "Return a list of direct includes in table DBT."
  64. (semantic-find-tags-by-class 'include (semanticdb-get-tags dbt)))
  65. (cl-defmethod semanticdb-check-references ((dbt semanticdb-table))
  66. "Check and cleanup references in the database DBT.
  67. Any reference to a file that cannot be found, or whos file no longer
  68. refers to DBT will be removed."
  69. (let ((refs (oref dbt db-refs))
  70. (myexpr (concat "\\<" (oref dbt file)))
  71. )
  72. (while refs
  73. (let* ((ok t)
  74. (db (car refs))
  75. (f (when (semanticdb-table-child-p db)
  76. (semanticdb-full-filename db)))
  77. )
  78. ;; The file was deleted
  79. (when (and f (not (file-exists-p f)))
  80. (setq ok nil))
  81. ;; The reference no longer includes the textual reference?
  82. (let* ((refs (semanticdb-includes-in-table db))
  83. (inc (semantic-find-tags-by-name-regexp
  84. myexpr refs)))
  85. (when (not inc)
  86. (setq ok nil)))
  87. ;; Remove not-ok databases from the list.
  88. (when (not ok)
  89. (object-remove-from-list dbt 'db-refs db)
  90. ))
  91. (setq refs (cdr refs)))))
  92. (cl-defmethod semanticdb-refresh-references ((dbt semanticdb-abstract-table))
  93. "Refresh references to DBT in other files."
  94. ;; alternate tables can't be edited, so can't be changed.
  95. nil
  96. )
  97. (cl-defmethod semanticdb-refresh-references ((dbt semanticdb-table))
  98. "Refresh references to DBT in other files."
  99. (let ((refs (semanticdb-includes-in-table dbt))
  100. )
  101. (while refs
  102. (if (semanticdb-add-reference dbt (car refs))
  103. nil
  104. ;; If we succeeded, then do... nothing?
  105. nil
  106. )
  107. (setq refs (cdr refs)))
  108. ))
  109. (cl-defmethod semanticdb-notify-references ((dbt semanticdb-table)
  110. method)
  111. "Notify all references of the table DBT using method.
  112. METHOD takes two arguments.
  113. (METHOD TABLE-TO-NOTIFY DBT)
  114. TABLE-TO-NOTIFY is a semanticdb-table which is being notified.
  115. DBT, the second argument is DBT."
  116. (mapc (lambda (R) (funcall method R dbt))
  117. (oref dbt db-refs)))
  118. ;;; DEBUG
  119. ;;
  120. (defclass semanticdb-ref-adebug ()
  121. ((i-depend-on :initarg :i-depend-on)
  122. (local-table :initarg :local-table)
  123. (i-include :initarg :i-include))
  124. "Simple class to allow ADEBUG to show a nice list.")
  125. (declare-function data-debug-new-buffer "data-debug")
  126. (declare-function data-debug-insert-object-slots "eieio-datadebug")
  127. (defun semanticdb-ref-test (refresh)
  128. "Dump out the list of references for the current buffer.
  129. If REFRESH is non-nil, cause the current table to have its references
  130. refreshed before dumping the result."
  131. (interactive "p")
  132. (require 'eieio-datadebug)
  133. ;; If we need to refresh... then do so.
  134. (when refresh
  135. (semanticdb-refresh-references semanticdb-current-table))
  136. ;; Do the debug system
  137. (let* ((tab semanticdb-current-table)
  138. (myrefs (oref tab db-refs))
  139. (myinc (semanticdb-includes-in-table tab))
  140. (adbc (semanticdb-ref-adebug "DEBUG"
  141. :i-depend-on myrefs
  142. :local-table tab
  143. :i-include myinc)))
  144. (data-debug-new-buffer "*References ADEBUG*")
  145. (data-debug-insert-object-slots adbc "!"))
  146. )
  147. (provide 'semantic/db-ref)
  148. ;;; semantic/db-ref.el ends here