[cfe-dev] AST Matcher for Decl subclass CilkSpawnDecl

Georg Altmann georg.altmann at informatik.stud.uni-erlangen.de
Wed Nov 19 06:35:21 PST 2014


Thank you for the quick reply.

I found a partial solution to my problem. I dug down into the AST
matching code and noticed RecursiveASTVisitor did not recurse into
CapturedStmt in CilkSpawnDecl. So I did the patch


diff --git a/include/clang/AST/RecursiveASTVisitor.h
b/include/clang/AST/RecursiveASTVisitor.h
index 35eec69..a16e772 100755
--- a/include/clang/AST/RecursiveASTVisitor.h
+++ b/include/clang/AST/RecursiveASTVisitor.h
@@ -1305,6 +1305,7 @@ DEF_TRAVERSE_DECL(CapturedDecl, {
   })

 DEF_TRAVERSE_DECL(CilkSpawnDecl, {
+    TRY_TO(TraverseStmt(D->getCapturedStmt()));
   })

 DEF_TRAVERSE_DECL(EmptyDecl, { })


Again, this is what I want to match:
The AST:
|-CompoundStmt
| `-CilkSpawnExpr
|   |-CilkSpawnDecl
|   | `-CapturedStmt
|   |   |-CapturedDecl
|   |   | `-ImplicitParamDecl
|   |   |-ExprWithCleanups
|   |   | `-CallExpr
--------------^ this guy


After the patch, these match now:

// 1) matches
compoundStmt(
  has(cilkSpawnExpr(
   has(cilkSpawnDecl(
     has(capturedStmt()))))))

// 2) matches and binds
compoundStmt(
  has(cilkSpawnExpr(
    has(cilkSpawnDecl(
      has(capturedStmt(
        hasDescendant(exprWithCleanups(
          has(callExpr().bind("callExpr"))))))))))


However this doesn't match, while it should IMHO.

// 3) does not match
compoundStmt(
  has(cilkSpawnExpr(
    has(cilkSpawnDecl(
        has(capturedStmt(
            has(exprWithCleanups())))))))


Notice that I used hasDescendant(exprWithCleanups()) in 2) and
has(exprWithCleanups()) in 3).
ExprWithCleanups is a direct child of CapturedStmt. Any clue why 2)
doesn't match?

Thanks,
Georg

On 18.11.2014 16:29, Manuel Klimek wrote:
> 
> 
> On Tue Nov 18 2014 at 3:42:46 PM Georg Altmann
> <georg.altmann at informatik.stud.uni-erlangen.de
> <mailto:georg.altmann at informatik.stud.uni-erlangen.de>> wrote:
> 
>     Hi,
> 
>     I am working on a modified version of clang for Intel Cilk Plus, from
>     https://github.com/cilkplus
>     based on clang 3.4.
> 
>     They added
> 
>     class CilkSpawnDecl : public Decl {
>       // ...
>       public:
>       // ...
>       /// \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
> 
>     |-CompoundStmt
>     | `-CilkSpawnExpr
>     |   |-CilkSpawnDecl
>     |   | `-CapturedStmt
>     |   |   |-CapturedDecl
>     |   |   | `-ImplicitParamDecl
>     |   |   |-ExprWithCleanups
>     |   |   | `-CallExpr
>     ------------^ this guy
> 
>     which does not seem to be possible without a customized matcher for
>     CilkSpawnDecl.
> 
>     I would like to define a AST Traversal Matcher, so I can do something
>     like this
> 
>     compoundStmt(
>       has(cilkSpawnExpr(
>         has(cilkSpawnDecl(
>           hasCapturedStmt(
>           ^ 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;
>       else
>         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:
> http://reviews.llvm.org/diffusion/L/browse/cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h;222237$3536
> 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...
>  
> 
> 
>     Thanks!
> 
>     Georg
> 
> 
> 
> 
>     _________________________________________________
>     cfe-dev mailing list
>     cfe-dev at cs.uiuc.edu <mailto:cfe-dev at cs.uiuc.edu>
>     http://lists.cs.uiuc.edu/__mailman/listinfo/cfe-dev
>     <http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev>
> 



-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20141119/a1b6f775/attachment.sig>


More information about the cfe-dev mailing list