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