emacs计算.c / .h src文件中的c函数

时间:2014-06-17 12:57:54

标签: c emacs

emacs是否具有计算no的功能。 .c / .h C src文件中的函数?

我希望能算上一个(大)没有。 .c文件中的函数,并将其与no进行比较。关联单元测试.c文件中的函数,以确定每个函数是否具有单元测试

理想情况下,有一些内置功能,而不需要某种reg-exp?

1 个答案:

答案 0 :(得分:2)

您可以使用语义标记生成来实现此目的。

语义是Emacs非常强大且未得到充分利用的功能。 Semantic可以解析您的.c和.h文件并生成您可以查看的标记以找到您的答案。我已经为你写了一些例子:

首先,确保已加载semantic库。

(defun c--count-things-in-buffer (thing buffer)
  "return the count of THINGs in BUFFER.
THING may be: 'function, 'variable, or 'type"
  (with-current-buffer buffer
    ;; get the buffers tags, they will be generated if not already
    ;; then remove the ones that are not 'function tags
    ;; return the count of what is left
    (length (remove-if-not (lambda (tag) (equal thing (second tag)))
                          (semantic-fetch-tags)))))

(defun c-count-functions-in-buffer (buffer)
  "Count and message the number of function declarations in BUFFER"
  (interactive "b")
  (message "%s has %d functions"
           buffer
           (c--count-things-in-buffer 'function buffer)))

(defun c-count-variables-in-buffer (buffer)
  "Count and message the number of variable declarations in BUFFER"
  (interactive "b")
  (message "%s has %d variables"
           buffer
           (c--count-things-in-buffer 'variable buffer)))

(defun c-count-types-in-buffer (buffer)
  "Count and message the number of type declarations in BUFFER"
  (interactive "b")
  (message "%s has %d types"
           buffer
           (c--count-things-in-buffer 'type buffer)))

尝试在暂存缓冲区中对此进行评估,然后切换到.c文件并执行M-x c-count-functions-in-buffer

semantic-fetch-tags带回的信息包含解决单元测试问题所需的一切。

我们假设您有一个名为Foobar的函数,您的单元测试类似于:Test_Foobar。您可以获取.c文件的标签和测试文件的标签,并检查c文件中的每个函数,测试文件中是否存在与Test_匹配的标记。这可能比简单地计算函数总数更好。

使用C-j在暂存缓冲区中运行此代码:

(with-current-buffer "what-ever-your-c-buffer-is.c" (semantic-fetch-tags))

在这里,您将能够看到所有可用的信息。