where myContext is a simple holder that stores the matched nodes in a
set. The test looks like this
myContext context = runOnCode("foo var1;");
auto res = context.getVariables();
Is the problem that the classes "foo" and "bar" are never declared?
Yes; parsing C++ requires knowing which names are types (and which are templates, etc.).
so how could I instantiate the tool with the necessary declarations.
The path of least resistance is to ensure that the translation unit you parse is self-contained, i.e., includes the necessary declarations. A useful sanity check is to compile it with clang from the command line.