如何获得按长度排序的 C 代码函数列表?

How to get a list of C code functions ordered by length?

我正在分析一大块 C 代码,想将其拆分成模块。有没有办法自动生成 C 函数列表,从最长的函数开始?

使用 GNU 用户区(例如 Linux),您可以使用 nm -S --size-sort object.o 从目标文件中获取按大小排序的符号列表。这应该与源代码长度大致成正比。

如果我们可以假设带有函数声明的行没有缩进,以 { 结尾并且函数定义以非缩进的 } 结尾,这个 Python 片段可以帮助:

#!/usr/bin/python

import sys

started = None
started_line = None
for lineno, line in enumerate(sys.stdin):
    if line.startswith(' '):
        continue
    if line.strip().endswith('{'):
        started = lineno
        started_line = line.rstrip()
    if line.strip().endswith('}'):
        print("%s\t%s" % (lineno - started, started_line))