首页 > 解决方案 > pcpp预处理器API怎么用?

问题描述

我有一个 C 头文件,需要对其进行解析以在构建时进行一些检查。所以我目前正在尝试使用pcpp它。除了源代码中的注释外,我在github上没有找到任何关于如何使用 API 的示例。示例参考似乎不再起作用。我尝试了以下方法:

#!/usr/bin/python3.7
import sys
import pcpp

def check_directive(directive):
    #do the checking
    #...

class Checker(pcpp.Preprocessor):
    def on_directive_handle(self, directive, toks, ifpassthru, precedingtoks):
        print("Checking directive...")
        check_directive(directive)
        super(pcpp.Preprocessor, on_directive_handle)


if __name__ == "__main__":
    c_header_path = sys.argv[1]
    with open(c_header_path) as fd:
        c_header = fd.read()

    checker = Checker()
    checker.parsegen(c_header)

但是没有进行检查,也没有Checking directive...产生诊断输出。

有人可以提供使用 API 的工作示例吗?或者可能为这种任务建议另一个 python 模块?

标签: pythonpython-3.xc-preprocessor

解决方案


推荐阅读