【问题标题】:How to use AST for both custom front-end action and clang static analysis如何将 AST 用于自定义前端操作和 clang 静态分析
【发布时间】:2019-02-12 16:32:11
【问题描述】:

我正在开发一个基于 libTooling 的项目,我通过引用 this 编写了一个自定义前端操作类。现在我想在同一个工具中运行 clang 静态分析。目前,我正在再次运行该工具进行 clang 静态分析(在修改编译器选项之后)。但这会解析文件并再次创建 AST。

我想创建一次 AST 并用于自定义前端操作和 clang 静态分析。

我怎样才能做到这一点? MultiplexConsumer 在这里有什么帮助吗?

【问题讨论】:

    标签: c++ frontend llvm-clang clang-static-analyzer libtooling


    【解决方案1】:

    看来 MultiplexConsumer 是要走的路。

    在我的前端操作类中,这对我有用:

    std::unique_ptr<ASTConsumer> CreateASTConsumer(
        CompilerInstance& compiler, StringRef inFile) override {
    
        std::unique_ptr<ASTConsumer> consumer1 =
            std::make_unique<MyConsumer1>(compiler);
    
        std::unique_ptr<ASTConsumer> consumer2 =
            std::make_unique<MyConsumer2>(compiler);
    
        std::vector<std::unique_ptr<ASTConsumer>> consumers;
        consumers.emplace_back(std::move(consumer1));
        consumers.emplace_back(std::move(consumer2));
        return std::make_unique<MultiplexConsumer>(std::move(consumers));
    }
    

    但请注意,如果 consumer1 返回任何错误,则 consumer2 将不会运行。如果 consumer1 仅返回警告或不返回诊断信息,则 consumer2 将运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 2013-10-28
      • 2015-12-03
      • 1970-01-01
      相关资源
      最近更新 更多