<div dir="ltr"><div dir="ltr">On Mon, Jun 10, 2019 at 8:31 AM Michael Schellenberger Costa via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
I am trying to learn more about libTooling and the ast_matcher. In my <br>
first toy example I wanted to try to match arbitrary variable <br>
declaration against a given type name, e.g.<br>
<br>
foo var1;<br>
<br>
baz var2;<br>
<br>
<br>
I have tried the following matcher<br>
<br>
<br>
varDecl(hasType(cxxRecordDecl(hasAnyName("foo", "bar")))).bind("var")<br>
<br>
<br>
However, it seems that this is not able to match the declaration. My <br>
unit test harness looks like this<br>
<br>
myContext runOnCode(const char* const Code = "")<br>
{<br>
     myContext Context;<br>
     auto Factory = llvm::make_unique<myActionFactory>(&Context);<br>
     tooling::runToolOnCode(Factory->create(), Code);<br>
     return Context;<br>
}<br>
<br>
where myContext is a simple holder that stores the matched nodes in a <br>
set. The test looks like this<br>
<br>
TEST(variableMatcher, variable)<br>
{<br>
     myContext context = runOnCode("foo var1;");<br>
     auto res = context.getVariables();<br>
     EXPECT_EQ(res.size(), size_t(1));<br>
}<br>
<br>
<br>
Is the problem that the classes "foo" and "bar" are never declared?</blockquote><div><br></div><div>Yes; parsing C++ requires knowing which names are types (and which are templates, etc.).</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> If <br>
so how could I instantiate the tool with the necessary declarations.</blockquote><div><br></div><div>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.</div><div><br></div><div>-- James </div></div></div>