branch: elpa/flamegraph
commit 26e7176b57f72947b35710d7b629a8a82732f31b
Author: Dmitry Gutov <[email protected]>
Commit: Dmitry Gutov <[email protected]>
Ignore call-site matches in strings and comments
A callee named in a docstring or comment was treated as a call; worse,
backward-up-list from inside a string escaped to a form spanning
unrelated defuns, so the snippet pulled in their code (e.g.
ivy--queue-exhibit showed several other functions).
Skip matches whose position is in a string or comment, and clamp each
narrowed sub-region to its parent so a stray enclosing form cannot widen
the search.
---
flamegraph.el | 22 ++++++++++++++++------
test/flamegraph-tests.el | 18 ++++++++++++++++++
2 files changed, 34 insertions(+), 6 deletions(-)
diff --git a/flamegraph.el b/flamegraph.el
index ff5b879ec1..78024ac578 100644
--- a/flamegraph.el
+++ b/flamegraph.el
@@ -733,14 +733,24 @@ tree may show cold callees the snippet omits."
;; A match narrows the region for this child's own
;; walk, whose callee-body children are then hidden.
(while (re-search-forward re (cdr region) t)
- (setq found t)
(let ((mb (match-beginning 0))
(me (match-end 0)))
- (when (>= weight flamegraph-call-site-threshold)
- (push (list mb me weight) regions))
- (when-let* ((sub (flamegraph--enclosing-form-region
- mb)))
- (walk k sub nil))))
+ ;; Skip occurrences in strings or comments (e.g. a
+ ;; name mentioned in a docstring): they are not
+ ;; calls, and their enclosing form would escape the
+ ;; current region. `syntax-ppss' moves point, so
+ ;; guard it.
+ (unless (save-excursion (nth 8 (syntax-ppss mb)))
+ (setq found t)
+ (when (>= weight flamegraph-call-site-threshold)
+ (push (list mb me weight) regions))
+ (when-let* ((sub
(flamegraph--enclosing-form-region
+ mb)))
+ ;; Clamp to REGION so a stray enclosing form
+ ;; can never widen the search.
+ (walk k (cons (max (car sub) (car region))
+ (min (cdr sub) (cdr region)))
+ nil)))))
(when (or found concealed-ok)
(when shown (puthash k t shown))
(setq any t))))))))
diff --git a/test/flamegraph-tests.el b/test/flamegraph-tests.el
index dda20a1547..74bab16492 100644
--- a/test/flamegraph-tests.el
+++ b/test/flamegraph-tests.el
@@ -181,6 +181,24 @@ callees outside it must still be found."
'("company-abort" "company-call-frontends"
"run-with-timer"))))))
+(ert-deftest flamegraph-test-walker-ignores-string-and-comment-matches ()
+ "A callee name mentioned in a docstring or comment is not a call site:
+only the real call in code is matched, not the earlier textual mentions."
+ (flamegraph-test--with-elisp-source
+ "(defun outer ()
+ \"Docstring mentioning foo.\"
+ ;; A comment mentioning foo.
+ (foo))"
+ (let* ((foo (profiler-make-calltree :entry 'foo :count 1))
+ (outer (profiler-make-calltree :entry 'outer :count 1
+ :children (list foo)))
+ (regions (flamegraph--collect-nested-call-sites
+ outer (point-min) (point-max))))
+ ;; Exactly one region: the call on the last line, not the docstring
+ ;; or comment occurrences.
+ (should (equal (flamegraph-test--region-texts regions) '("foo")))
+ (should (= 4 (line-number-at-pos (car (car regions))))))))
+
(ert-deftest flamegraph-test-walker-unsearchable-names-skipped ()
"An unsearchable name (e.g. \"[unknown]\") must not error or recurse."
(flamegraph-test--with-elisp-source