所以,我打算将ea建议标记为“答案”,因为我认为这可能是一般最好的想法。尽管我认为cmock建议在tdd方法中非常有效,在这种方法中,图书馆开发是由测试失败驱动的,我可能最终会尝试这种方式。但现在,我需要一个更快捷+更肮脏的方法,以交互方式工作(所讨论的库是另一个动态加载的插件,交互式程序,我试图对api调用的顺序进行逆向工程...)
所以我最终做的是编写一个调用pycparse的python脚本。我将它包含在它的情况下,以帮助其他人,但它根本不是一般的(假设所有函数都返回int,并且有一个hack来避免typedef中的func defs)。
from pycparser import parse_file
from pycparser.c_ast import NodeVisitor
class AncestorVisitor(NodeVisitor):
def __init__(self):
self.current = None
self.ancestors = []
def visit(self, node):
if self.current:
self.ancestors.append(self.current)
self.current = node
try:
return super(AncestorVisitor, self).visit(node)
finally:
if self.ancestors:
self.ancestors.pop(-1)
class FunctionVisitor(AncestorVisitor):
def visit_FuncDecl(self, node):
if len(self.ancestors) < 3: # avoid typedefs
print node.type.type.names[0], node.type.declname, '(',
first = True
for param in node.args.params:
if first: first = False
else: print ',',
print param.type.type.names[0], param.type.declname,
print ')'
print '{fprintf(stderr, "%s\\n"); return 0;}' % node.type.declname
print '#include "myheader.h"'
print '#include <stdio.h>'
ast = parse_file('myheader.h', use_cpp=True)
FunctionVisitor().visit(ast)
现在你还没有真正问过一个问题,据我所知,但我猜你在问一个现有的工具可以做到这一点?不幸的是,我不知道任何工具,但它听起来基本足以做一个快速的python(或任何其他高级语言)实现来解决它? – Jite
我已经澄清了这个问题。是的,我可以写一个程序来做到这一点。但它不是微不足道的 - 例如,它需要一个体面的解析器。头部足够大,并且足够复杂,基于特定正则表达式的kludge将会耗费大量时间。 –
嗨,过程是否需要轻松重复?例如,你是否希望能够在新版本的API发布时更新存根实现? – OlduwanSteve