Using an ASTMatcher in combination ClangdServer

Hello,

I am developing an application that uses the ClangdServer class to get information about the code.
But for some tasks, ClangdServer doesnt provide enough information. Therefore, I would like to run an ASTMatcher as done in this demo project (GitHub - lanl/CoARCT: Code Analysis and Refactoring with Clang Tools).
The problem is, the apps in this demos always start from scratch and it is quite slow. My hope is, that when I use an available ParsedAST it would be a lot faster to run a matcher on it. Unfortunately, I cannot find anything how to use an ParsedAST with an ASTMatcher.
Clangd already has these ParsedASTs during runtime (If I understand the code correctly), and I would like to reuse these.

Is this in anyway possible?

Cheers
Mirko

Hi, I haven’t tried it myself, but you should be able to build a MatchFinder. Then use addMatcher methods to build matchers to loop over the AST. Finally Match over the ParsedAST ASTContext.
This is pretty simplistic overview of how it should work.

class MyCallback : public MatchCallback {
  void run(const ast_matchers::MatchResult &Result) { 
    // Do something with the match result
  }
};
MyCallback CB;
ast_matchers::MatchFinder Finder;
Finder.addMatcher(functionDecl().bind("Function"), &CB);
Finder.matchAST(ParsedAST.getASTContext());

I’m again not 100% sure on the specifics, but the AST won’t contain decls that aren’t referenced in the main source file or function bodies from header files, therefore any matcher will also fail to see these.

1 Like

Great, thanks! That works.

Sadly I cannot access without editing ClangdServer.h the TUScheduler in ClangdServer to get the ParsedAST. Any Idea around that?