[cfe-dev] AST Matcher for Decl subclass CilkSpawnDecl

Pan, Wei wei.pan at intel.com
Tue Nov 18 08:03:23 PST 2014

Note that a CilkSpawnDecl adaptor always contains a CapturedStmt AST. The captured stuff might not be a simple CallExpr because of "receivers" initialized by spawns. You may experiment the following cases:

_Cilk_spawn foo();
x = _Cilk_spawn bar();
T x = _Cilk_spawn foo(), y = bar(), z = _Cilk_spawn baz();


From: cfe-dev-bounces at cs.uiuc.edu [mailto:cfe-dev-bounces at cs.uiuc.edu] On Behalf Of Manuel Klimek
Sent: Tuesday, November 18, 2014 7:29 AM
To: Georg Altmann; cfe-dev at cs.uiuc.edu
Subject: Re: [cfe-dev] AST Matcher for Decl subclass CilkSpawnDecl

On Tue Nov 18 2014 at 3:42:46 PM Georg Altmann <georg.altmann at informatik.stud.uni-erlangen.de> wrote:

I am working on a modified version of clang for Intel Cilk Plus, from
based on clang 3.4.

They added

class CilkSpawnDecl : public Decl {
  // ...
  // ...
  /// \brief Returns the associated CapturedStmt.
  CapturedStmt *getCapturedStmt() { return CapturedSpawn; }
  const CapturedStmt *getCapturedStmt() const { return CapturedSpawn; }
  // ...

However there don't seem to be any releated AST matchers and I need to match

| `-CilkSpawnExpr
|   |-CilkSpawnDecl
|   | `-CapturedStmt
|   |   |-CapturedDecl
|   |   | `-ImplicitParamDecl
|   |   |-ExprWithCleanups
|   |   | `-CallExpr
------------^ this guy

which does not seem to be possible without a customized matcher for

I would like to define a AST Traversal Matcher, so I can do something
like this

      ^ has(exprWithCleanups(has(callExpr))))))))
      `-- How?

I already defined cilkSpawnExpr and cilkSpawnDecl by using
VariadicDynCastAllOfMatcher which is working great.
Yet, I still need hasCapturedStmt and I can't get my head around the AST
matcher internals. I guess, I somehow need to define a matcher that
takes a CilkSpawnDecl node and returns true if getCapturedStmt() != 0, e.g.

match(CilkSpawnDecl const& spawnDecl) {
  if (spawnDecl.getCapturedStmt())
    return true;
    return false;

But how do I define hasCapturedStmt() so that has(exprWithCleanups(...))
can act on the children of CapturedStmt?
I.e. how does an AST matcher hand down the list of children to the
following matcher expressions?

I looked at the macro AST_MATCHER_P but I'm unsure about ParamType,
ASTMatchFinder and BoundNodesTreeBuilder. Can I use that?

Yes. Take a look at for example:
AST_MATCHER_P(ElaboratedType, hasQualifier,
              internal::Matcher<NestedNameSpecifier>, InnerMatcher) {
  if (const NestedNameSpecifier *Qualifier = Node.getQualifier())
    return InnerMatcher.matches(*Qualifier, Finder, Builder);

  return false;

You basically want to adapt this to your use case...



cfe-dev mailing list
cfe-dev at cs.uiuc.edu

More information about the cfe-dev mailing list