JRR
JRR

Reputation: 6152

running multiple clang passes

I have been using Tool.run(ToolAction) to run a clang tool. If I want to run multiple tools on the same input source, do I just create different ToolActions and call Tool.run one after another? Or do I somehow chain the ASTFrontendActions together?

Upvotes: 2

Views: 697

Answers (2)

Benno
Benno

Reputation: 5456

Assuming you have a ClangTool named Tool and two MatchFinders called MatchFinder1 and MatchFinder2 (with the appropriate matchers already added), the straightforward transformation is:

std::vector<std::unique_ptr<ASTUnit>> ASTs;
Tool.buildASTs(ASTs);

for(auto& ast : ASTs) {
    // First pass
    MatchFinder1.matchAST(ast->getASTContext());
    // Second pass
    MatchFinder2.matchAST(ast->getASTContext());
}

This way, the AST nodes remain valid between the different matcher runs and can be correctly compared against each other.

Note that FrontendAction is not used at all with this approach, but to be honest I'm not completely sure what it actually does.

Upvotes: 1

random
random

Reputation: 4028

It depends on what you want to achieve.

If you want to parse once, and then do multiple things with AST you should better implement multiple ASTConsumers.

Then you can create aggregate ASTConsumer:

class AggregateASTConsumer : public clang::ASTConsumer {

public:
    void HandleTranslationUnit(clang::ASTContext& Ctx) override {
         for (auto consumer: consumers)
              consumer.HandleTranslationUnit(Ctx);

    }
    std::vector<ASTConsumer*> consumers;
}

But if you want to re-parse input source code with some different options then you should run tool multiple times.

Upvotes: 2

Related Questions