db.el 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. ;;; semantic/db.el --- Semantic tag database manager
  2. ;; Copyright (C) 2000-2017 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: tags
  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. ;; Maintain a database of tags for a group of files and enable
  19. ;; queries into the database.
  20. ;;
  21. ;; By default, assume one database per directory.
  22. ;;
  23. ;;; Code:
  24. (require 'eieio-base)
  25. (require 'semantic)
  26. (eval-when-compile
  27. (require 'semantic/find))
  28. (declare-function semantic-lex-spp-save-table "semantic/lex-spp")
  29. ;; Use autoload to avoid recursive require of semantic/db-ref
  30. (autoload 'semanticdb-refresh-references "semantic/db-ref"
  31. "Refresh references to DBT in other files.")
  32. ;;; Variables:
  33. (defgroup semanticdb nil
  34. "Parser Generator Persistent Database interface."
  35. :group 'semantic)
  36. (defvar semanticdb-database-list nil
  37. "List of all active databases.")
  38. (defvar semanticdb-new-database-class 'semanticdb-project-database-file
  39. "The default type of database created for new files.
  40. This can be changed on a per file basis, so that some directories
  41. are saved using one mechanism, and some directories via a different
  42. mechanism.")
  43. (make-variable-buffer-local 'semanticdb-new-database-class)
  44. (defvar semanticdb-default-find-index-class 'semanticdb-find-search-index
  45. "The default type of search index to use for a `semanticdb-table's.
  46. This can be changed to try out new types of search indices.")
  47. (make-variable-buffer-local 'semanticdb-default-find=index-class)
  48. ;;;###autoload
  49. (defvar semanticdb-current-database nil
  50. "For a given buffer, this is the currently active database.")
  51. (make-variable-buffer-local 'semanticdb-current-database)
  52. ;;;###autoload
  53. (defvar semanticdb-current-table nil
  54. "For a given buffer, this is the currently active database table.")
  55. (make-variable-buffer-local 'semanticdb-current-table)
  56. ;;; ABSTRACT CLASSES
  57. ;;
  58. (defclass semanticdb-abstract-table ()
  59. ((parent-db ;; :initarg :parent-db
  60. ;; Do not set an initarg, or you get circular writes to disk.
  61. :documentation "Database Object containing this table.")
  62. (major-mode :initarg :major-mode
  63. :initform nil
  64. :documentation "Major mode this table belongs to.
  65. Sometimes it is important for a program to know if a given table has the
  66. same major mode as the current buffer.")
  67. (tags :initarg :tags
  68. :accessor semanticdb-get-tags
  69. :printer semantic-tag-write-list-slot-value
  70. :documentation "The tags belonging to this table.")
  71. (db-refs :initform nil
  72. :documentation
  73. "List of `semanticdb-table' objects refering to this one.
  74. These aren't saved, but are instead recalculated after load.
  75. See the file semanticdb-ref.el for how this slot is used.")
  76. (index :type semanticdb-abstract-search-index
  77. :documentation "The search index.
  78. Used by semanticdb-find to store additional information about
  79. this table for searching purposes.
  80. Note: This index will not be saved in a persistent file.")
  81. (cache :type list
  82. :initform nil
  83. :documentation "List of cache information for tools.
  84. Any particular tool can cache data to a database at runtime
  85. with `semanticdb-cache-get'.
  86. Using a semanticdb cache does not save any information to a file,
  87. so your cache will need to be recalculated at runtime. Caches can be
  88. referenced even when the file is not in a buffer.
  89. Note: This index will not be saved in a persistent file.")
  90. )
  91. "A simple table for semantic tags.
  92. This table is the root of tables, and contains the minimum needed
  93. for a new table not associated with a buffer."
  94. :abstract t)
  95. (cl-defmethod semanticdb-in-buffer-p ((obj semanticdb-abstract-table))
  96. "Return a nil, meaning abstract table OBJ is not in a buffer."
  97. nil)
  98. (cl-defmethod semanticdb-get-buffer ((obj semanticdb-abstract-table))
  99. "Return a buffer associated with OBJ.
  100. If the buffer is not in memory, load it with `find-file-noselect'."
  101. nil)
  102. ;; This generic method allows for sloppier coding. Many
  103. ;; functions treat "table" as something that could be a buffer,
  104. ;; file name, or other. This makes use of table more robust.
  105. (cl-defmethod semanticdb-full-filename (buffer-or-string)
  106. "Fetch the full filename that BUFFER-OR-STRING refers to.
  107. This uses semanticdb to get a better file name."
  108. (cond ((bufferp buffer-or-string)
  109. (with-current-buffer buffer-or-string
  110. (semanticdb-full-filename semanticdb-current-table)))
  111. ((and (stringp buffer-or-string) (file-exists-p buffer-or-string))
  112. (expand-file-name buffer-or-string))))
  113. (cl-defmethod semanticdb-full-filename ((obj semanticdb-abstract-table))
  114. "Fetch the full filename that OBJ refers to.
  115. Abstract tables do not have file names associated with them."
  116. nil)
  117. (cl-defmethod semanticdb-dirty-p ((obj semanticdb-abstract-table))
  118. "Return non-nil if OBJ is dirty."
  119. nil)
  120. (cl-defmethod semanticdb-set-dirty ((obj semanticdb-abstract-table))
  121. "Mark the abstract table OBJ dirty.
  122. Abstract tables can not be marked dirty, as there is nothing
  123. for them to synchronize against."
  124. ;; The abstract table can not be dirty.
  125. nil)
  126. (cl-defmethod semanticdb-normalize-tags ((obj semanticdb-abstract-table) tags)
  127. "For the table OBJ, convert a list of TAGS, into standardized form.
  128. The default is to return TAGS.
  129. Some databases may default to searching and providing simplified tags
  130. based on whichever technique used. This method provides a hook for
  131. them to convert TAG into a more complete form."
  132. tags)
  133. (cl-defmethod semanticdb-normalize-one-tag ((obj semanticdb-abstract-table) tag)
  134. "For the table OBJ, convert a TAG, into standardized form.
  135. This method returns a list of the form (DATABASE . NEWTAG).
  136. The default is to just return (OBJ TAG).
  137. Some databases may default to searching and providing simplified tags
  138. based on whichever technique used. This method provides a hook for
  139. them to convert TAG into a more complete form."
  140. (cons obj tag))
  141. (cl-defmethod object-print ((obj semanticdb-abstract-table) &rest strings)
  142. "Pretty printer extension for `semanticdb-abstract-table'.
  143. Adds the number of tags in this file to the object print name."
  144. (if (or (not strings)
  145. (and (= (length strings) 1) (stringp (car strings))
  146. (string= (car strings) "")))
  147. ;; Else, add a tags quantifier.
  148. (cl-call-next-method obj (format " (%d tags)" (length (semanticdb-get-tags obj))))
  149. ;; Pass through.
  150. (apply #'cl-call-next-method obj strings)
  151. ))
  152. ;;; Index Cache
  153. ;;
  154. (defclass semanticdb-abstract-search-index ()
  155. ((table :initarg :table
  156. :type semanticdb-abstract-table
  157. :documentation "XRef to the table this belongs to.")
  158. )
  159. "A place where semanticdb-find can store search index information.
  160. The search index will store data about which other tables might be
  161. needed, or perhaps create hash or index tables for the current buffer."
  162. :abstract t)
  163. (cl-defmethod semanticdb-get-table-index ((obj semanticdb-abstract-table))
  164. "Return the search index for the table OBJ.
  165. If one doesn't exist, create it."
  166. (if (slot-boundp obj 'index)
  167. (oref obj index)
  168. (let ((idx nil))
  169. (setq idx (funcall semanticdb-default-find-index-class
  170. (concat (eieio-object-name obj) " index")
  171. ;; Fill in the defaults
  172. :table obj
  173. ))
  174. (oset obj index idx)
  175. idx)))
  176. (cl-defmethod semanticdb-synchronize ((idx semanticdb-abstract-search-index)
  177. new-tags)
  178. "Synchronize the search index IDX with some NEW-TAGS."
  179. ;; The abstract class will do... NOTHING!
  180. )
  181. (cl-defmethod semanticdb-partial-synchronize ((idx semanticdb-abstract-search-index)
  182. new-tags)
  183. "Synchronize the search index IDX with some changed NEW-TAGS."
  184. ;; The abstract class will do... NOTHING!
  185. )
  186. ;;; SEARCH RESULTS TABLE
  187. ;;
  188. ;; Needed for system databases that may not provide
  189. ;; a semanticdb-table associated with a file.
  190. ;;
  191. (defclass semanticdb-search-results-table (semanticdb-abstract-table)
  192. ()
  193. "Table used for search results when there is no file or table association.
  194. Examples include search results from external sources such as from
  195. Emacs's own symbol table, or from external libraries.")
  196. (cl-defmethod semanticdb-refresh-table ((obj semanticdb-search-results-table) &optional force)
  197. "If the tag list associated with OBJ is loaded, refresh it.
  198. This will call `semantic-fetch-tags' if that file is in memory."
  199. nil)
  200. ;;; CONCRETE TABLE CLASSES
  201. ;;
  202. (defclass semanticdb-table (semanticdb-abstract-table)
  203. ((file :initarg :file
  204. :documentation "File name relative to the parent database.
  205. This is for the file whose tags are stored in this TABLE object.")
  206. (buffer :initform nil
  207. :documentation "The buffer associated with this table.
  208. If nil, the table's buffer is no in Emacs. If it has a value, then
  209. it is in Emacs.")
  210. (dirty :initform nil
  211. :documentation
  212. "Non nil if this table needs to be `Saved'.")
  213. (db-refs :initform nil
  214. :documentation
  215. "List of `semanticdb-table' objects referring to this one.
  216. These aren't saved, but are instead recalculated after load.
  217. See the file semantic/db-ref.el for how this slot is used.")
  218. (pointmax :initarg :pointmax
  219. :initform nil
  220. :documentation "Size of buffer when written to disk.
  221. Checked on retrieval to make sure the file is the same.")
  222. (fsize :initarg :fsize
  223. :initform nil
  224. :documentation "Size of the file when it was last referenced.
  225. Checked when deciding if a loaded table needs updating from changes
  226. outside of Semantic's control.")
  227. (lastmodtime :initarg :lastmodtime
  228. :initform nil
  229. :documentation "Last modification time of the file referenced.
  230. Checked when deciding if a loaded table needs updating from changes outside of
  231. Semantic's control.")
  232. ;; @todo - need to add `last parsed time', so we can also have
  233. ;; refresh checks if spp tables or the parser gets rebuilt.
  234. (unmatched-syntax :initarg :unmatched-syntax
  235. :documentation
  236. "List of vectors specifying unmatched syntax.")
  237. (lexical-table :initarg :lexical-table
  238. :initform nil
  239. :printer semantic-lex-spp-table-write-slot-value
  240. :documentation
  241. "Table that might be needed by the lexical analyzer.
  242. For C/C++, the C preprocessor macros can be saved here.")
  243. )
  244. "A single table of tags derived from file.")
  245. (cl-defmethod semanticdb-in-buffer-p ((obj semanticdb-table))
  246. "Return a buffer associated with OBJ.
  247. If the buffer is in memory, return that buffer."
  248. (let ((buff (oref obj buffer)))
  249. (if (buffer-live-p buff)
  250. buff
  251. (oset obj buffer nil))))
  252. (cl-defmethod semanticdb-get-buffer ((obj semanticdb-table))
  253. "Return a buffer associated with OBJ.
  254. If the buffer is in memory, return that buffer.
  255. If the buffer is not in memory, load it with `find-file-noselect'."
  256. (or (semanticdb-in-buffer-p obj)
  257. ;; Save match data to protect against odd stuff in mode hooks.
  258. (save-match-data
  259. (find-file-noselect (semanticdb-full-filename obj) t))))
  260. (cl-defmethod semanticdb-set-buffer ((obj semanticdb-table))
  261. "Set the current buffer to be a buffer owned by OBJ.
  262. If OBJ's file is not loaded, read it in first."
  263. (set-buffer (semanticdb-get-buffer obj)))
  264. (cl-defmethod semanticdb-full-filename ((obj semanticdb-table))
  265. "Fetch the full filename that OBJ refers to."
  266. (expand-file-name (oref obj file)
  267. (oref (oref obj parent-db) reference-directory)))
  268. (cl-defmethod semanticdb-dirty-p ((obj semanticdb-table))
  269. "Return non-nil if OBJ is dirty."
  270. (oref obj dirty))
  271. (cl-defmethod semanticdb-set-dirty ((obj semanticdb-table))
  272. "Mark the abstract table OBJ dirty."
  273. (oset obj dirty t)
  274. )
  275. (cl-defmethod object-print ((obj semanticdb-table) &rest strings)
  276. "Pretty printer extension for `semanticdb-table'.
  277. Adds the number of tags in this file to the object print name."
  278. (apply #'cl-call-next-method obj
  279. (format " (%d tags)" (length (semanticdb-get-tags obj)))
  280. (if (oref obj dirty) ", DIRTY" "")
  281. strings))
  282. ;;; DATABASE BASE CLASS
  283. ;;
  284. (unless (fboundp 'semanticdb-abstract-table-list-p)
  285. (cl-deftype semanticdb-abstract-table-list ()
  286. '(list-of semanticdb-abstract-table)))
  287. (defclass semanticdb-project-database (eieio-instance-tracker)
  288. ((tracking-symbol :initform semanticdb-database-list)
  289. (reference-directory :type string
  290. :documentation "Directory this database refers to.
  291. When a cache directory is specified, then this refers to the directory
  292. this database contains symbols for.")
  293. (new-table-class :initform semanticdb-table
  294. :type class
  295. :documentation
  296. "New tables created for this database are of this class.")
  297. (cache :type list
  298. :initform nil
  299. :documentation "List of cache information for tools.
  300. Any particular tool can cache data to a database at runtime
  301. with `semanticdb-cache-get'.
  302. Using a semanticdb cache does not save any information to a file,
  303. so your cache will need to be recalculated at runtime.
  304. Note: This index will not be saved in a persistent file.")
  305. (tables :initarg :tables
  306. :type semanticdb-abstract-table-list
  307. ;; Need this protection so apps don't try to access
  308. ;; the tables without using the accessor.
  309. :accessor semanticdb-get-database-tables
  310. :protection :protected
  311. :documentation "List of `semantic-db-table' objects."))
  312. "Database of file tables.")
  313. (cl-defmethod semanticdb-full-filename ((obj semanticdb-project-database))
  314. "Fetch the full filename that OBJ refers to.
  315. Abstract tables do not have file names associated with them."
  316. nil)
  317. (cl-defmethod semanticdb-dirty-p ((DB semanticdb-project-database))
  318. "Return non-nil if DB is dirty.
  319. A database is dirty if the state of the database changed in a way
  320. where it may need to resynchronize with some persistent storage."
  321. (let ((dirty nil)
  322. (tabs (oref DB tables)))
  323. (while (and (not dirty) tabs)
  324. (setq dirty (semanticdb-dirty-p (car tabs)))
  325. (setq tabs (cdr tabs)))
  326. dirty))
  327. (cl-defmethod object-print ((obj semanticdb-project-database) &rest strings)
  328. "Pretty printer extension for `semanticdb-project-database'.
  329. Adds the number of tables in this file to the object print name."
  330. (apply #'cl-call-next-method obj
  331. (format " (%d tables%s)"
  332. (length (semanticdb-get-database-tables obj))
  333. (if (semanticdb-dirty-p obj)
  334. " DIRTY" "")
  335. )
  336. strings))
  337. (cl-defmethod semanticdb-create-database ((dbc (subclass semanticdb-project-database)) directory)
  338. "Create a new semantic database of class DBC for DIRECTORY and return it.
  339. If a database for DIRECTORY has already been created, return it.
  340. If DIRECTORY doesn't exist, create a new one."
  341. (let ((db (semanticdb-directory-loaded-p directory)))
  342. (unless db
  343. (setq db (semanticdb-project-database
  344. (file-name-nondirectory directory)
  345. :tables nil))
  346. ;; Set this up here. We can't put it in the constructor because it
  347. ;; would be saved, and we want DB files to be portable.
  348. (oset db reference-directory (file-truename directory)))
  349. db))
  350. (cl-defmethod semanticdb-flush-database-tables ((db semanticdb-project-database))
  351. "Reset the tables in DB to be empty."
  352. (oset db tables nil))
  353. (cl-defmethod semanticdb-create-table ((db semanticdb-project-database) file)
  354. "Create a new table in DB for FILE and return it.
  355. The class of DB contains the class name for the type of table to create.
  356. If the table for FILE exists, return it.
  357. If the table for FILE does not exist, create one."
  358. (let ((newtab (semanticdb-file-table db file)))
  359. (unless newtab
  360. ;; This implementation will satisfy autoloaded classes
  361. ;; for tables.
  362. (setq newtab (funcall (oref db new-table-class)
  363. (file-name-nondirectory file)
  364. :file (file-name-nondirectory file)
  365. ))
  366. (oset newtab parent-db db)
  367. (object-add-to-list db 'tables newtab t))
  368. newtab))
  369. (cl-defmethod semanticdb-file-table ((obj semanticdb-project-database) filename)
  370. "From OBJ, return FILENAME's associated table object."
  371. (object-assoc (file-relative-name (file-truename filename)
  372. (oref obj reference-directory))
  373. 'file (oref obj tables)))
  374. ;; DATABASE FUNCTIONS
  375. (defun semanticdb-get-database (filename)
  376. "Get a database for FILENAME.
  377. If one isn't found, create one."
  378. (semanticdb-create-database semanticdb-new-database-class (file-truename filename)))
  379. (defun semanticdb-directory-loaded-p (path)
  380. "Return the project belonging to PATH if it was already loaded."
  381. (eieio-instance-tracker-find path 'reference-directory 'semanticdb-database-list))
  382. (defun semanticdb-create-table-for-file (filename)
  383. "Initialize a database table for FILENAME, and return it.
  384. If FILENAME exists in the database already, return that.
  385. If there is no database for the table to live in, create one."
  386. (let ((cdb nil)
  387. (tbl nil)
  388. (dd (file-name-directory (file-truename filename)))
  389. )
  390. ;; Allow a database override function
  391. (setq cdb (semanticdb-create-database semanticdb-new-database-class
  392. dd))
  393. ;; Get a table for this file.
  394. (setq tbl (semanticdb-create-table cdb filename))
  395. ;; Return the pair.
  396. (cons cdb tbl)
  397. ))
  398. ;;; Cache Cache.
  399. ;;
  400. (defclass semanticdb-abstract-cache ()
  401. ((table :initarg :table
  402. :type semanticdb-abstract-table
  403. :documentation
  404. "Cross reference to the table this belongs to.")
  405. )
  406. "Abstract baseclass for tools to use to cache information in semanticdb.
  407. Tools needing a per-file cache must subclass this, and then get one as
  408. needed. Cache objects are identified in semanticdb by subclass.
  409. In order to keep your cache up to date, be sure to implement
  410. `semanticdb-synchronize', and `semanticdb-partial-synchronize'.
  411. See the file semantic/scope.el for an example."
  412. :abstract t)
  413. (cl-defmethod semanticdb-cache-get ((table semanticdb-abstract-table)
  414. desired-class)
  415. "Get a cache object on TABLE of class DESIRED-CLASS.
  416. This method will create one if none exists with no init arguments
  417. other than :table."
  418. (unless (child-of-class-p desired-class 'semanticdb-abstract-cache)
  419. (error "Invalid SemanticDB cache"))
  420. (let ((cache (oref table cache))
  421. (obj nil))
  422. (while (and (not obj) cache)
  423. (if (eq (eieio-object-class (car cache)) desired-class)
  424. (setq obj (car cache)))
  425. (setq cache (cdr cache)))
  426. (if obj
  427. obj ;; Just return it.
  428. ;; No object, let's create a new one and return that.
  429. (setq obj (funcall desired-class "Cache" :table table))
  430. (object-add-to-list table 'cache obj)
  431. obj)))
  432. (cl-defmethod semanticdb-cache-remove ((table semanticdb-abstract-table)
  433. cache)
  434. "Remove from TABLE the cache object CACHE."
  435. (object-remove-from-list table 'cache cache))
  436. (cl-defmethod semanticdb-synchronize ((cache semanticdb-abstract-cache)
  437. new-tags)
  438. "Synchronize a CACHE with some NEW-TAGS."
  439. ;; The abstract class will do... NOTHING!
  440. )
  441. (cl-defmethod semanticdb-partial-synchronize ((cache semanticdb-abstract-cache)
  442. new-tags)
  443. "Synchronize a CACHE with some changed NEW-TAGS."
  444. ;; The abstract class will do... NOTHING!
  445. )
  446. (defclass semanticdb-abstract-db-cache ()
  447. ((db :initarg :db
  448. :type semanticdb-project-database
  449. :documentation
  450. "Cross reference to the database this belongs to.")
  451. )
  452. "Abstract baseclass for tools to use to cache information in semanticdb.
  453. Tools needing a database cache must subclass this, and then get one as
  454. needed. Cache objects are identified in semanticdb by subclass.
  455. In order to keep your cache up to date, be sure to implement
  456. `semanticdb-synchronize', and `semanticdb-partial-synchronize'.
  457. See the file semantic/scope.el for an example."
  458. :abstract t)
  459. (cl-defmethod semanticdb-cache-get ((db semanticdb-project-database)
  460. desired-class)
  461. "Get a cache object on DB of class DESIRED-CLASS.
  462. This method will create one if none exists with no init arguments
  463. other than :table."
  464. (unless (child-of-class-p desired-class 'semanticdb-abstract-cache)
  465. (error "Invalid SemanticDB cache"))
  466. (let ((cache (oref db cache))
  467. (obj nil))
  468. (while (and (not obj) cache)
  469. (if (eq (eieio-object-class (car cache)) desired-class)
  470. (setq obj (car cache)))
  471. (setq cache (cdr cache)))
  472. (if obj
  473. obj ;; Just return it.
  474. ;; No object, let's create a new one and return that.
  475. (setq obj (funcall desired-class "Cache" :db db))
  476. (object-add-to-list db 'cache obj)
  477. obj)))
  478. (cl-defmethod semanticdb-cache-remove ((db semanticdb-project-database)
  479. cache)
  480. "Remove from TABLE the cache object CACHE."
  481. (object-remove-from-list db 'cache cache))
  482. (cl-defmethod semanticdb-synchronize ((cache semanticdb-abstract-db-cache)
  483. new-tags)
  484. "Synchronize a CACHE with some NEW-TAGS."
  485. ;; The abstract class will do... NOTHING!
  486. )
  487. (cl-defmethod semanticdb-partial-synchronize ((cache semanticdb-abstract-db-cache)
  488. new-tags)
  489. "Synchronize a CACHE with some changed NEW-TAGS."
  490. ;; The abstract class will do... NOTHING!
  491. )
  492. ;;; REFRESH
  493. (cl-defmethod semanticdb-refresh-table ((obj semanticdb-table) &optional force)
  494. "If the tag list associated with OBJ is loaded, refresh it.
  495. Optional argument FORCE will force a refresh even if the file in question
  496. is not in a buffer. Avoid using FORCE for most uses, as an old cache
  497. may be sufficient for the general case. Forced updates can be slow.
  498. This will call `semantic-fetch-tags' if that file is in memory."
  499. (cond
  500. ;;
  501. ;; Already in a buffer, just do it.
  502. ((semanticdb-in-buffer-p obj)
  503. (save-excursion
  504. (semanticdb-set-buffer obj)
  505. (semantic-fetch-tags)))
  506. ;;
  507. ;; Not in a buffer. Forcing a load.
  508. (force
  509. ;; Patch from Iain Nicol. --
  510. ;; @TODO: I wonder if there is a way to recycle
  511. ;; semanticdb-create-table-for-file-not-in-buffer
  512. (save-excursion
  513. (let ((buff (semantic-find-file-noselect
  514. (semanticdb-full-filename obj) t)))
  515. (set-buffer buff)
  516. (semantic-fetch-tags)
  517. ;; Kill off the buffer if it didn't exist when we were called.
  518. (kill-buffer buff))))))
  519. (cl-defmethod semanticdb-needs-refresh-p ((obj semanticdb-table))
  520. "Return non-nil of OBJ's tag list is out of date.
  521. The file associated with OBJ does not need to be in a buffer."
  522. (let* ((ff (semanticdb-full-filename obj))
  523. (buff (semanticdb-in-buffer-p obj))
  524. )
  525. (if buff
  526. (with-current-buffer buff
  527. ;; Use semantic's magic tracker to determine of the buffer is up
  528. ;; to date or not.
  529. (not (semantic-parse-tree-up-to-date-p))
  530. ;; We assume that semanticdb is keeping itself up to date.
  531. ;; via all the clever hooks
  532. )
  533. ;; Buffer isn't loaded. The only clue we have is if the file
  534. ;; is somehow different from our mark in the semanticdb table.
  535. (let* ((stats (file-attributes ff))
  536. (actualsize (nth 7 stats))
  537. (actualmod (nth 5 stats))
  538. )
  539. (or (not (slot-boundp obj 'tags))
  540. ;; (not (oref obj tags)) --> not needed anymore?
  541. (/= (or (oref obj fsize) 0) actualsize)
  542. (not (equal (oref obj lastmodtime) actualmod))
  543. )
  544. ))))
  545. ;;; Synchronization
  546. ;;
  547. (cl-defmethod semanticdb-synchronize ((table semanticdb-abstract-table)
  548. new-tags)
  549. "Synchronize the table TABLE with some NEW-TAGS."
  550. (oset table tags new-tags)
  551. (oset table pointmax (point-max))
  552. (let ((fattr (file-attributes (semanticdb-full-filename table))))
  553. (oset table fsize (nth 7 fattr))
  554. (oset table lastmodtime (nth 5 fattr))
  555. )
  556. ;; Assume it is now up to date.
  557. (oset table unmatched-syntax semantic-unmatched-syntax-cache)
  558. ;; The lexical table should be good too.
  559. (when (featurep 'semantic/lex-spp)
  560. (oset table lexical-table (semantic-lex-spp-save-table)))
  561. ;; this implies dirtiness
  562. (semanticdb-set-dirty table)
  563. ;; Synchronize the index
  564. (when (slot-boundp table 'index)
  565. (let ((idx (oref table index)))
  566. (when idx (semanticdb-synchronize idx new-tags))))
  567. ;; Synchronize application caches.
  568. (dolist (C (oref table cache))
  569. (semanticdb-synchronize C new-tags)
  570. )
  571. ;; Update cross references
  572. (semanticdb-refresh-references table)
  573. )
  574. (cl-defmethod semanticdb-partial-synchronize ((table semanticdb-abstract-table)
  575. new-tags)
  576. "Synchronize the table TABLE where some NEW-TAGS changed."
  577. ;; You might think we need to reset the tags, but since the partial
  578. ;; parser splices the lists, we don't need to do anything
  579. ;;(oset table tags new-tags)
  580. ;; We do need to mark ourselves dirty.
  581. (semanticdb-set-dirty table)
  582. ;; The lexical table may be modified.
  583. (when (featurep 'semantic/lex-spp)
  584. (oset table lexical-table (semantic-lex-spp-save-table)))
  585. ;; Incremental parser doesn't monkey around with this.
  586. (oset table unmatched-syntax semantic-unmatched-syntax-cache)
  587. ;; Synchronize the index
  588. (when (slot-boundp table 'index)
  589. (let ((idx (oref table index)))
  590. (when idx (semanticdb-partial-synchronize idx new-tags))))
  591. ;; Synchronize application caches.
  592. (dolist (C (oref table cache))
  593. (semanticdb-synchronize C new-tags)
  594. )
  595. ;; Update cross references
  596. (when (semantic-find-tags-by-class 'include new-tags)
  597. (semanticdb-refresh-references table))
  598. )
  599. ;;; SAVE/LOAD
  600. ;;
  601. (cl-defmethod semanticdb-save-db ((DB semanticdb-project-database)
  602. &optional suppress-questions)
  603. "Cause a database to save itself.
  604. The database base class does not save itself persistently.
  605. Subclasses could save themselves to a file, or to a database, or other
  606. form."
  607. nil)
  608. (defun semanticdb-save-current-db ()
  609. "Save the current tag database."
  610. (interactive)
  611. (unless noninteractive
  612. (message "Saving current tag summaries..."))
  613. (semanticdb-save-db semanticdb-current-database)
  614. (unless noninteractive
  615. (message "Saving current tag summaries...done")))
  616. ;; This prevents Semanticdb from querying multiple times if the users
  617. ;; answers "no" to creating the Semanticdb directory.
  618. (defvar semanticdb--inhibit-create-file-directory)
  619. (defun semanticdb-save-all-db ()
  620. "Save all semantic tag databases."
  621. (interactive)
  622. (unless noninteractive
  623. (message "Saving tag summaries..."))
  624. (let ((semanticdb--inhibit-make-directory noninteractive))
  625. (mapc 'semanticdb-save-db semanticdb-database-list))
  626. (unless noninteractive
  627. (message "Saving tag summaries...done")))
  628. (defun semanticdb-save-all-db-idle ()
  629. "Save all semantic tag databases from idle time.
  630. Exit the save between databases if there is user input."
  631. (semantic-safe "Auto-DB Save: %S"
  632. ;; FIXME: Use `while-no-input'?
  633. (semantic-exit-on-input 'semanticdb-idle-save
  634. (mapc (lambda (db)
  635. (semantic-throw-on-input 'semanticdb-idle-save)
  636. (semanticdb-save-db db t))
  637. semanticdb-database-list))
  638. ))
  639. ;;; Directory Project support
  640. ;;
  641. (defvar semanticdb-project-predicate-functions nil
  642. "List of predicates to try that indicate a directory belongs to a project.
  643. This list is used when `semanticdb-persistent-path' contains the value
  644. 'project. If the predicate list is nil, then presume all paths are valid.
  645. Project Management software (such as EDE and JDE) should add their own
  646. predicates with `add-hook' to this variable, and semanticdb will save tag
  647. caches in directories controlled by them.")
  648. (cl-defmethod semanticdb-write-directory-p ((obj semanticdb-project-database))
  649. "Return non-nil if OBJ should be written to disk.
  650. Uses `semanticdb-persistent-path' to determine the return value."
  651. nil)
  652. ;;; Utilities
  653. ;;
  654. ;; What is the current database, are two tables of an equivalent mode,
  655. ;; and what databases are a part of the same project.
  656. (defun semanticdb-current-database ()
  657. "Return the currently active database."
  658. (or semanticdb-current-database
  659. (and default-directory
  660. (semanticdb-create-database semanticdb-new-database-class
  661. default-directory)
  662. )
  663. nil))
  664. (defvar semanticdb-match-any-mode nil
  665. "Non-nil to temporarily search any major mode for a tag.
  666. If a particular major mode wants to search any mode, put the
  667. `semantic-match-any-mode' symbol onto the symbol of that major mode.
  668. Do not set the value of this variable permanently.")
  669. (defmacro semanticdb-with-match-any-mode (&rest body)
  670. "A Semanticdb search occurring withing BODY will search tags in all modes.
  671. This temporarily sets `semanticdb-match-any-mode' while executing BODY."
  672. `(let ((semanticdb-match-any-mode t))
  673. ,@body))
  674. (put 'semanticdb-with-match-any-mode 'lisp-indent-function 0)
  675. (cl-defmethod semanticdb-equivalent-mode-for-search (table &optional buffer)
  676. "Return non-nil if TABLE's mode is equivalent to BUFFER.
  677. See `semanticdb-equivalent-mode' for details.
  678. This version is used during searches. Major-modes that opt
  679. to set the `semantic-match-any-mode' property will be able to search
  680. all files of any type."
  681. (or (get major-mode 'semantic-match-any-mode)
  682. semanticdb-match-any-mode
  683. (semanticdb-equivalent-mode table buffer))
  684. )
  685. (cl-defmethod semanticdb-equivalent-mode ((table semanticdb-abstract-table) &optional buffer)
  686. "Return non-nil if TABLE's mode is equivalent to BUFFER.
  687. Equivalent modes are specified by the `semantic-equivalent-major-modes'
  688. local variable."
  689. nil)
  690. (cl-defmethod semanticdb-equivalent-mode ((table semanticdb-table) &optional buffer)
  691. "Return non-nil if TABLE's mode is equivalent to BUFFER.
  692. Equivalent modes are specified by the `semantic-equivalent-major-modes'
  693. local variable."
  694. (save-excursion
  695. (if buffer (set-buffer buffer))
  696. (or
  697. ;; nil major mode in table means we don't know yet. Assume yes for now?
  698. (null (oref table major-mode))
  699. ;; nil means the same as major-mode
  700. (and (not semantic-equivalent-major-modes)
  701. (mode-local-use-bindings-p major-mode (oref table major-mode)))
  702. (and semantic-equivalent-major-modes
  703. (member (oref table major-mode) semantic-equivalent-major-modes))
  704. )
  705. ))
  706. ;;; Associations
  707. ;;
  708. ;; These routines determine associations between a file, and multiple
  709. ;; associated databases.
  710. (defcustom semanticdb-project-roots nil
  711. "List of directories, where each directory is the root of some project.
  712. All subdirectories of a root project are considered a part of one project.
  713. Values in this string can be overridden by project management programs
  714. via the `semanticdb-project-root-functions' variable."
  715. :group 'semanticdb
  716. :type '(repeat string))
  717. (defvar semanticdb-project-root-functions nil
  718. "List of functions used to determine a given directories project root.
  719. Functions in this variable can override `semanticdb-project-roots'.
  720. Functions set in the variable are given one argument (a directory) and
  721. must return a string, (the root directory) or a list of strings (multiple
  722. root directories in a more complex system). This variable should be used
  723. by project management programs like EDE or JDE.")
  724. (defvar semanticdb-project-system-databases nil
  725. "List of databases containing system library information.
  726. Mode authors can create their own system databases which know
  727. detailed information about the system libraries for querying purposes.
  728. Put those into this variable as a buffer-local, or mode-local
  729. value.")
  730. (make-variable-buffer-local 'semanticdb-project-system-databases)
  731. (defvar semanticdb-search-system-databases t
  732. "Non nil if search routines are to include a system database.")
  733. (defun semanticdb-current-database-list (&optional dir)
  734. "Return a list of databases associated with the current buffer.
  735. If optional argument DIR is non-nil, then use DIR as the starting directory.
  736. If this buffer has a database, but doesn't have a project associated
  737. with it, return nil.
  738. First, it checks `semanticdb-project-root-functions', and if that
  739. has no results, it checks `semanticdb-project-roots'. If that fails,
  740. it returns the results of function `semanticdb-current-database'.
  741. Always append `semanticdb-project-system-databases' if
  742. `semanticdb-search-system' is non-nil."
  743. (let ((root nil) ; found root directory
  744. (dbs nil) ; collected databases
  745. (roots semanticdb-project-roots) ;all user roots
  746. (dir (file-truename (or dir default-directory)))
  747. )
  748. ;; Find the root based on project functions.
  749. (setq root (run-hook-with-args-until-success
  750. 'semanticdb-project-root-functions
  751. dir))
  752. (if root
  753. (setq root (file-truename root))
  754. ;; Else, Find roots based on strings
  755. (while roots
  756. (let ((r (file-truename (car roots))))
  757. (if (string-match (concat "^" (regexp-quote r)) dir)
  758. (setq root r)))
  759. (setq roots (cdr roots))))
  760. ;; If no roots are found, use this directory.
  761. (unless root (setq root dir))
  762. ;; Find databases based on the root directory.
  763. (when root
  764. ;; The rootlist allows the root functions to possibly
  765. ;; return several roots which are in different areas but
  766. ;; all apart of the same system.
  767. (let ((regexp (concat "^" (regexp-quote root)))
  768. (adb semanticdb-database-list) ; all databases
  769. )
  770. (while adb
  771. ;; I don't like this part, but close enough.
  772. (if (and (slot-boundp (car adb) 'reference-directory)
  773. (string-match regexp (oref (car adb) reference-directory)))
  774. (setq dbs (cons (car adb) dbs)))
  775. (setq adb (cdr adb))))
  776. )
  777. ;; Add in system databases
  778. (when semanticdb-search-system-databases
  779. (setq dbs (nconc dbs semanticdb-project-system-databases)))
  780. ;; Return
  781. dbs))
  782. ;;; Generic Accessor Routines
  783. ;;
  784. ;; These routines can be used to get at tags in files w/out
  785. ;; having to know a lot about semanticDB.
  786. (defvar semanticdb-file-table-hash (make-hash-table :test 'equal)
  787. "Hash table mapping file names to database tables.")
  788. (defun semanticdb-file-table-object-from-hash (file)
  789. "Retrieve a DB table from the hash for FILE.
  790. Does not use `file-truename'."
  791. (gethash file semanticdb-file-table-hash 'no-hit))
  792. (defun semanticdb-file-table-object-put-hash (file dbtable)
  793. "For FILE, associate DBTABLE in the hash table."
  794. (puthash file dbtable semanticdb-file-table-hash))
  795. ;;;###autoload
  796. (defun semanticdb-file-table-object (file &optional dontload)
  797. "Return a semanticdb table belonging to FILE, make it up to date.
  798. If file has database tags available in the database, return it.
  799. If file does not have tags available, and DONTLOAD is nil,
  800. then load the tags for FILE, and create a new table object for it.
  801. DONTLOAD does not affect the creation of new database objects."
  802. ;; (message "Object Translate: %s" file)
  803. (when (and file (file-exists-p file) (file-regular-p file))
  804. (let* ((default-directory (file-name-directory file))
  805. (tab (semanticdb-file-table-object-from-hash file))
  806. (fullfile nil))
  807. ;; If it is not in the cache, then extract the more traditional
  808. ;; way by getting the database, and finding a table in that database.
  809. ;; Once we have a table, add it to the hash.
  810. (when (eq tab 'no-hit)
  811. (setq fullfile (file-truename file))
  812. (let ((db (or ;; This line will pick up system databases.
  813. (semanticdb-directory-loaded-p default-directory)
  814. ;; this line will make a new one if needed.
  815. (semanticdb-get-database default-directory))))
  816. (setq tab (semanticdb-file-table db fullfile))
  817. (when tab
  818. (semanticdb-file-table-object-put-hash file tab)
  819. (when (not (string= fullfile file))
  820. (semanticdb-file-table-object-put-hash fullfile tab)
  821. ))
  822. ))
  823. (cond
  824. ((and tab
  825. ;; Is this in a buffer?
  826. ;;(find-buffer-visiting (semanticdb-full-filename tab))
  827. (semanticdb-in-buffer-p tab)
  828. )
  829. (save-excursion
  830. ;;(set-buffer (find-buffer-visiting (semanticdb-full-filename tab)))
  831. (semanticdb-set-buffer tab)
  832. (semantic-fetch-tags)
  833. ;; Return the table.
  834. tab))
  835. ((and tab dontload)
  836. ;; If we have table, and we don't want to load it, just return it.
  837. tab)
  838. ((and tab
  839. ;; Is table fully loaded, or just a proxy?
  840. (number-or-marker-p (oref tab pointmax))
  841. ;; Is this table up to date with the file?
  842. (not (semanticdb-needs-refresh-p tab)))
  843. ;; A-ok!
  844. tab)
  845. ((or (and fullfile (get-file-buffer fullfile))
  846. (get-file-buffer file))
  847. ;; are these two calls this faster than `find-buffer-visiting'?
  848. ;; If FILE is being visited, but none of the above state is
  849. ;; true (meaning, there is no table object associated with it)
  850. ;; then it is a file not supported by Semantic, and can be safely
  851. ;; ignored.
  852. nil)
  853. ((not dontload) ;; We must load the file.
  854. ;; Full file should have been set by now. Debug why not?
  855. (when (and (not tab) (not fullfile))
  856. ;; This case is if a 'nil is erroneously put into the hash table. This
  857. ;; would need fixing
  858. (setq fullfile (file-truename file))
  859. )
  860. ;; If we have a table, but no fullfile, that's ok. Let's get the filename
  861. ;; from the table which is pre-truenamed.
  862. (when (and (not fullfile) tab)
  863. (setq fullfile (semanticdb-full-filename tab)))
  864. (setq tab (semanticdb-create-table-for-file-not-in-buffer fullfile))
  865. ;; Save the new table.
  866. (semanticdb-file-table-object-put-hash file tab)
  867. (when (not (string= fullfile file))
  868. (semanticdb-file-table-object-put-hash fullfile tab)
  869. )
  870. ;; Done!
  871. tab)
  872. (t
  873. ;; Full file should have been set by now. Debug why not?
  874. ;; One person found this. Is it a file that failed to parse
  875. ;; in the past?
  876. (when (not fullfile)
  877. (setq fullfile (file-truename file)))
  878. ;; We were asked not to load the file in and parse it.
  879. ;; Instead just create a database table with no tags
  880. ;; and a claim of being empty.
  881. ;;
  882. ;; This will give us a starting point for storing
  883. ;; database cross-references so when it is loaded,
  884. ;; the cross-references will fire and caches will
  885. ;; be cleaned.
  886. (let ((ans (semanticdb-create-table-for-file file)))
  887. (setq tab (cdr ans))
  888. ;; Save the new table.
  889. (semanticdb-file-table-object-put-hash file tab)
  890. (when (not (string= fullfile file))
  891. (semanticdb-file-table-object-put-hash fullfile tab)
  892. )
  893. ;; Done!
  894. tab))
  895. )
  896. )))
  897. (defvar semanticdb-out-of-buffer-create-table-fcn nil
  898. "When non-nil, a function for creating a semanticdb table.
  899. This should take a filename to be parsed.")
  900. (make-variable-buffer-local 'semanticdb-out-of-buffer-create-table-fcn)
  901. (defun semanticdb-create-table-for-file-not-in-buffer (filename)
  902. "Create a table for the file FILENAME.
  903. If there are no language specific configurations, this
  904. function will read in the buffer, parse it, and kill the buffer."
  905. (if (and semanticdb-out-of-buffer-create-table-fcn
  906. (not (file-remote-p filename)))
  907. ;; Use external parser only of the file is accessible to the
  908. ;; local file system.
  909. (funcall semanticdb-out-of-buffer-create-table-fcn filename)
  910. (save-excursion
  911. (let* ( ;; Remember the buffer to kill
  912. (kill-buffer-flag (find-buffer-visiting filename))
  913. (buffer-to-kill (or kill-buffer-flag
  914. (semantic-find-file-noselect filename t))))
  915. ;; This shouldn't ever be set. Debug some issue here?
  916. ;; (when kill-buffer-flag (debug))
  917. (set-buffer buffer-to-kill)
  918. ;; Find file should automatically do this for us.
  919. ;; Sometimes the DB table doesn't contains tags and needs
  920. ;; a refresh. For example, when the file is loaded for
  921. ;; the first time, and the idle scheduler didn't get a
  922. ;; chance to trigger a parse before the file buffer is
  923. ;; killed.
  924. (when semanticdb-current-table
  925. (semantic-fetch-tags))
  926. (prog1
  927. semanticdb-current-table
  928. (when (not kill-buffer-flag)
  929. ;; If we had to find the file, then we should kill it
  930. ;; to keep the master buffer list clean.
  931. (kill-buffer buffer-to-kill)
  932. )))))
  933. )
  934. (defun semanticdb-file-stream (file)
  935. "Return a list of tags belonging to FILE.
  936. If file has database tags available in the database, return them.
  937. If file does not have tags available, then load the file, and create them."
  938. (let ((table (semanticdb-file-table-object file)))
  939. (when table
  940. (semanticdb-get-tags table))))
  941. (provide 'semantic/db)
  942. ;; Local variables:
  943. ;; generated-autoload-file: "loaddefs.el"
  944. ;; generated-autoload-load-name: "semantic/db"
  945. ;; End:
  946. ;;; semantic/db.el ends here