介紹了 Emacs 配合 cflow 制作函數(shù)的調用樹 (call graph)Table of Contents
1、Introduction to cflow
GNU cflow analyzes a collection of C source files and prints a graph, charting control flow within the program.
GNU cflow is able to produce both direct and inverted flowgraphs for C sources. Optionally a cross-reference listing can be generated. Two output formats are implemented: POSIX and GNU (extended).
Input files can optionally be preprocessed before analyzing.
The package also provides Emacs major mode for examining the produced flowcharts in Emacs.
以上內容取自 cflow 的網(wǎng)站: http://www.gnu.org/software/cflow/manual/html_node/Intro.html#Intro。
2、Emacs & cflow
cflow 的代碼包中提供了cflow-mode.el, 該文件提供了 emacs 的 cflow-mode, 用于顯示 cflow 輸出的內容,但該文件中并未提供自動生成指定函數(shù)的 call tree 的功能,用起來多少有點不方便。
下面是一個簡單的 elisp 函數(shù),通過該函數(shù)可以為指定的函數(shù)生成調用樹。
(defvar cmd nil nil)(defvar cflow-buf nil nil)
(defvar cflow-buf-name nil nil)
(defun yyc/cflow-function (function-name)
"Get call graph of inputed function. "
;(interactive "sFunction name:\n")
(interactive (list (car (senator-jump-interactive "Function name: "
nil nil nil))))
(setq cmd (format "cflow -b --main=%s %s" function-name buffer-file-name))
(setq cflow-buf-name (format "**cflow-%s:%s**"
(file-name-nondirectory buffer-file-name)
function-name))
(setq cflow-buf (get-buffer-create cflow-buf-name))
(set-buffer cflow-buf)
(setq buffer-read-only nil)
(erase-buffer)
(insert (shell-command-to-string cmd))
(pop-to-buffer cflow-buf)
(goto-char (point-min))
(cflow-mode)
)
函數(shù)中使用了 CEDET 提供的 senator-jump-interactive 用于實現(xiàn)函數(shù)名的自動補全, 如果你的機器上沒有 CEDET, 那么就把 (interactive "sFunction name:\n") 的注釋去掉, 而將后面的那句話注釋掉即可。
用法很簡單,將上面的代碼添加到 ~/.emacs 中后,重新載入配置文件,然后打開一個 C 文件, M-x, 輸入: yyc/cflow-function , 隨后在 Mini-buffer 中輸入函數(shù)名字, 回車,即可生成一個新的 buffer, 并在其中填寫了生成的調用樹。
如下圖所示:
在調用樹中,還可以通過鍵盤從調用樹中跳轉到相應的源代碼中。
這樣, cflow 配置 GNU Global 和 cedet , 代碼的閱讀和理解效率會大大提高。