How to debug an AST matcher

Hi all,

I’m a Clang newbie. So, I’ve been trying to write some basic checkers that do AST traversal. Now, one of the difficulties I’m running into is on how to debug when my AST matcher doesn’t work as expected.

My current workflow is this:

  1. clang -Xclang -ast-dump -fsyntax-only test.cc and get a sense of how the AST tree looks like for code snippets the AST checker is supposed to figure out.
  2. Write the checker.
  3. Build the checker and fix syntactic plus other rookie errors when the compiler points out.
  4. Run the checker
  5. Notice that it doesn’t work as expected, so stare at the checker. Do 1) again and see if I missed anything. If I spot anything, fix the checker and restart from 3).
  6. If ran into 5) and couldn’t spot anything in the ast-dump output, then go through ASTMatcher.h and try to see if you have a Eureka moment.
  7. Twiddle my thumbs for a while and then email cfe-dev for help.

Ideally I want to rely on lldb in this kind of situation, and I suspect the answer lies in the Debug Prints and Tricks section (from https://clang-analyzer.llvm.org/checker_dev_manual.html). But I was hoping someone can perhaps go in more detail than what’s in the Debug Prints and Tricks section to help a beginner like me. I’ll be even more specific: Say there’s a DeclStmt which has a VarDecl descendant, and I want to see if DeclRefExpr contains an IntegerLiteral of value 10 in the denominator assuming it’s a divide operation. Now assuming that after looking at the ast-dump output, you’re still not able to figure out what might be wrong with your matcher. How would you go about this predicament of yours ? I don’t want to tell what the specific issue is because I want to go through the debugging process myself to figure out the problem with my matcher.

Copy-pasting an old email response for the same type of question should suffice.

Tom

Have you tried the binary “clang-query”? It can save you a few steps, particularly 3) , 4) and 5).

Cheers,
Gabor