使用eclipse CDT解析C/C++文件语法树

maven依赖

        <dependency>
            <groupId>org.eclipse.core</groupId>
            <artifactId>org.eclipse.core.resources</artifactId>
            <version>3.7.100</version>
        </dependency>
        <dependency>
            <groupId>com.github.mhoffrog.attached</groupId>
            <artifactId>org.eclipse.cdt.core</artifactId>
            <version>5.11.0</version>
        </dependency>

代码:

public class CDTTest {
    public static void main(String[] args) throws Exception {
        IASTTranslationUnit u = getTranslationUnit(new File("E:\\xxx.c"));
        //输出的是⽂件的所有内容
        // System.out.println("simpleDeclaration.getRawSignature():" + u.getRawSignature());
        /**
         获取注释
         *
         */
        IASTComment[] com = u.getComments();
        for (IASTComment iastComment : com) {
            System.out.println("IASTComment:" + iastComment);
        }
        /**
         得到预处理语句
         *
         * #include <queue>
         这⼀类
         * #define maxnum 120
         */
        IASTPreprocessorStatement[] ps = u.getAllPreprocessorStatements();
        for (IASTPreprocessorStatement iastPreprocessorStatement : ps) {
            System.out.println(iastPreprocessorStatement.getRawSignature());
        }

        //得到⽂件中定义的声明
        IASTDeclaration[] decs = u.getDeclarations();
        for (IASTDeclaration child : decs) {
            //    ⽅法声明
            if (child instanceof IASTFunctionDefinition) {
                // // 获得函数说明符
                // 例
                // void
                System.out.println(((IASTFunctionDefinition) child).getDeclSpecifier().getRawSignature());
                // 获得函数的函数声明符
                // 例 函数名
                System.out.println(((IASTFunctionDefinition) child).getDeclarator().getRawSignature());
                // 获得函数体的内容
                System.out.println(((IASTFunctionDefinition) child).getBody().getRawSignature());
                // 输出函数的全部内容
                System.out.println(child.getRawSignature());
                // 与函数的起始位置有关
                IASTFileLocation FileLocation = child.getFileLocation();
                int startLine = FileLocation.getStartingLineNumber();
                int endLine = FileLocation.getEndingLineNumber();
                System.out.println("length:" + (endLine - startLine));
            }
        }
    }

    /**
     * 创建解析单元
     *
     * @param source
     * @return
     * @throws Exception
     */
    static IASTTranslationUnit getTranslationUnit(File source) throws Exception {
        FileContent reader = FileContent.create(source.getAbsolutePath(), getContentFile(source).toCharArray());

        //C++ 用 GPPLanguage 解析
        //C 用 GCCLanguage 解析

        return GPPLanguage.getDefault()
            .getASTTranslationUnit(reader, new ScannerInfo(), IncludeFileContentProvider.getSavedFilesProvider(), null,
                ILanguage.OPTION_IS_SOURCE_UNIT, new DefaultLogService());
        //        return GCCLanguage.getDefault().getASTTranslationUnit(
        //                reader,
        //                new ScannerInfo(),
        //                IncludeFileContentProvider.getSavedFilesProvider(),
        //                null,
        //                ILanguage.OPTION_IS_SOURCE_UNIT,
        //                new DefaultLogService());
    }

    /**
     * 获得⽂件中的内容
     *
     * @param file
     * @return
     * @throws IOException
     */
    static String getContentFile(File file) throws IOException {
        StringBuilder content = new StringBuilder();
        String line;
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
            while ((line = br.readLine()) != null) {
                content.append(line).append('\n');
            }
        }
        return content.toString();
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值