Skip to content

Commit c60777a

Browse files
committed
bdx: Add a command to find symbol definition
* bdx.el (bdx-find-definition): New command. * bdx-test.el (bdx-find-definition): New test.
1 parent ce15736 commit c60777a

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

bdx-test.el

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,33 @@ int quux(int x) { return x+1; }
475475
(goto-char (point-min))
476476
(should (re-search-forward "<foo>:")))))
477477

478+
(ert-deftest bdx-find-definition ()
479+
(bdx-test--with-files
480+
`(("foo.c" "
481+
int foo() { return 128; }
482+
int quux(int x) { return x+1; }
483+
" :args ("-c" "-g"))
484+
("bar.c" "
485+
int bar(int x) { return x+1; }
486+
int baz(int x) { return x+1; }
487+
" :args ("-c" "-g")))
488+
(bdx-test--index)
489+
490+
(pcase-let ((`(,file . ,line)
491+
(bdx-find-definition
492+
(car (bdx--search "name:quux" :limit 1)))))
493+
(should (stringp file))
494+
(find-file file)
495+
(should (numberp line))
496+
(should (looking-at-p "int quux")))
497+
(pcase-let ((`(,file . ,line)
498+
(bdx-find-definition
499+
(car (bdx--search "name:baz" :limit 1)))))
500+
(should (stringp file))
501+
(find-file file)
502+
(should (numberp line))
503+
(should (looking-at-p "int baz")))))
504+
478505
(ert-deftest bdx-generate-graph ()
479506
(bdx-test--with-files
480507
'(("foo.c" "

bdx.el

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,44 @@ to the length of `bdx-disassembly-stack'."
740740
:lighter " bdx"
741741
:keymap bdx-disassembly-mode-map)
742742

743+
744+
;; Find definition
745+
746+
(defun bdx-find-definition (symbol-plist)
747+
"Find file containing definition of SYMBOL-PLIST and go to definition line.
748+
The return value is a cons (FILE . LINE)."
749+
(interactive (list (bdx-query "Find definition: "
750+
:require-match t)))
751+
(pcase-let (((map :name :demangled :path :section) symbol-plist))
752+
(let ((command
753+
(apply #'bdx--command "find-definition"
754+
(append (list "-n" "1")
755+
(list
756+
(and name (format "fullname:\"%s\"" name))
757+
(and demangled
758+
(format "demangled:\"%s\"" demangled))
759+
(and path (format "path:\"%s\"" path))
760+
(and section
761+
(format "section:\"%s\"" section))))))
762+
file line sym)
763+
(with-temp-buffer
764+
(apply #'call-process (car command)
765+
nil (current-buffer) nil (cdr command))
766+
767+
(goto-char (point-min))
768+
(if (looking-at "^\\(.*\\):\\([0-9]+\\): \\(.*\\)")
769+
(setq file (match-string 1)
770+
line (string-to-number (match-string 2))
771+
sym (match-string 3))
772+
(error "No definition found")))
773+
(when (equal sym name)
774+
(with-current-buffer (find-file-noselect file)
775+
(goto-char (point-min))
776+
(forward-line (1- line))
777+
(pop-to-buffer (current-buffer))
778+
(pulse-momentary-highlight-one-line)
779+
(cons file line))))))
780+
743781

744782
;; Graphs
745783

0 commit comments

Comments
 (0)