Skip to content

clang query batch mode

Timothy Kelley edited this page Feb 13, 2020 · 1 revision

Raw notes whilst learning how to use clang-query to do some batch queries.

Let's try to find all the variable declarations in the call graph of a function "f".

Normally, I'd do this interactively by starting clang-query:

$ clang-query /path/to/code.cc -p compile_commands.json

And then at the prompt, first define then apply a matcher:

clang-query> let mvdecls varDecl(hasAncestor(functionDecl(hasName("f"))))
clang-query> m mvdecls
<... many matches reported ...>
Match #44:

/path/to/code.cc:217:5: note: "root" binds here
    some_ns::a_type value1 = object.method1();
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Match #45:

/path/to/code.cc:218:5: note: "root" binds here
    some_ns::a_type value2 = object.method2();
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45 matches.

OK, let's reproduce just that simple beginning using a command file. First, I drop the matcher and the match command into a file I call "var_decls.cqc":

<In file var_decls.cqc >
let mvdecls varDecl(hasAncestor(functionDecl(hasName("f"))))
m mvdecls   # Hey I can use comments like this!

And then I run the command file like so:

clang-query /path/to/code.cc  -f var_decls.cqc -p compile_commands.json

I get all the same output as before. So far so good.

Clone this wiki locally