[ast_matcher] Match variable declaration by type name

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[ast_matcher] Match variable declaration by type name

Nathan Ridge via cfe-dev
Hi,

I am trying to learn more about libTooling and the ast_matcher. In my
first toy example I wanted to try to match arbitrary variable
declaration against a given type name, e.g.

foo var1;

baz var2;


I have tried the following matcher


varDecl(hasType(cxxRecordDecl(hasAnyName("foo", "bar")))).bind("var")


However, it seems that this is not able to match the declaration. My
unit test harness looks like this

myContext runOnCode(const char* const Code = "")
{
     myContext Context;
     auto Factory = llvm::make_unique<myActionFactory>(&Context);
     tooling::runToolOnCode(Factory->create(), Code);
     return Context;
}

where myContext is a simple holder that stores the matched nodes in a
set. The test looks like this

TEST(variableMatcher, variable)
{
     myContext context = runOnCode("foo var1;");
     auto res = context.getVariables();
     EXPECT_EQ(res.size(), size_t(1));
}


Is the problem that the classes "foo" and "bar" are never declared? If
so how could I instantiate the tool with the necessary declarations.
Could I match differently?

As a followup is there a way to differentiate different properties of
the declarations such as parentheses

foo var1;

foo (var2);


Cheers

Michael

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

[ast_matcher] Match variable declaration by type name

Nathan Ridge via cfe-dev
Hi,

I am trying to learn more about libTooling and the ast_matcher. In my
first toy example I wanted to try to match arbitrary variable
declaration against a given type name, e.g.

foo var1;

baz var2;


I have tried the following matcher


varDecl(hasType(cxxRecordDecl(hasAnyName("foo", "bar")))).bind("var")


However, it seems that this is not able to match the declaration. My
unit test harness looks like this

myContext runOnCode(const char* const Code = "")
{
     myContext Context;
     auto Factory = llvm::make_unique<myActionFactory>(&Context);
     tooling::runToolOnCode(Factory->create(), Code);
     return Context;
}

where myContext is a simple holder that stores the matched nodes in a
set. The test looks like this

TEST(variableMatcher, variable)
{
     myContext context = runOnCode("foo var1;");
     auto res = context.getVariables();
     EXPECT_EQ(res.size(), size_t(1));
}


Is the problem that the classes "foo" and "bar" are never declared? If
so how could I instantiate the tool with the necessary declarations.
Could I match differently?

As a followup is there a way to differentiate different properties of
the declarations such as parentheses

foo var1;

foo (var2);


Cheers

Michael
>
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [ast_matcher] Match variable declaration by type name

Nathan Ridge via cfe-dev
In reply to this post by Nathan Ridge via cfe-dev
On Mon, Jun 10, 2019 at 8:31 AM Michael Schellenberger Costa via cfe-dev <[hidden email]> wrote:
Hi,

I am trying to learn more about libTooling and the ast_matcher. In my
first toy example I wanted to try to match arbitrary variable
declaration against a given type name, e.g.

foo var1;

baz var2;


I have tried the following matcher


varDecl(hasType(cxxRecordDecl(hasAnyName("foo", "bar")))).bind("var")


However, it seems that this is not able to match the declaration. My
unit test harness looks like this

myContext runOnCode(const char* const Code = "")
{
     myContext Context;
     auto Factory = llvm::make_unique<myActionFactory>(&Context);
     tooling::runToolOnCode(Factory->create(), Code);
     return Context;
}

where myContext is a simple holder that stores the matched nodes in a
set. The test looks like this

TEST(variableMatcher, variable)
{
     myContext context = runOnCode("foo var1;");
     auto res = context.getVariables();
     EXPECT_EQ(res.size(), size_t(1));
}


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.).
 
If
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.

-- James 

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev