ASTContext::getParents returning empty list

Hi clang developers,

I wasn't sure if this was the relevant channel to ask this, and I apologize if so.

I've hit a puzzle with an out-of-tree build based on 6.0.0 and was wondering if anyone could share their wisdom on it.
ASTContext::getParents seems to be returning an empty list in a particular case:
Run on ArraySubscriptExpr node looking for Stmt parents.

I have four ArraySubscriptExprs in my AST dump, the faulty one being:

-FunctionDecl 0x6190f58 <line:33:1, line:35:1> line:33:5 used fib 'int


>-ParmVarDecl 0x6190ec8 <col:9, col:13> col:13 used n 'int'$
`-CompoundStmt 0x61915d8 <col:16, line:35:1>$
`-ReturnStmt 0x6191518 <line:34:3, col:13>$
`-ImplicitCastExpr 0x6191460 <col:10, col:13> 'int' <LValueToRValue>$
`-ArraySubscriptExpr 0x6191390 <col:10, col:13> 'int' lvalue$
>-ImplicitCastExpr 0x6191220 <col:10> 'int *'


     &gt; \`\-DeclRefExpr 0x6191080 &lt;col:10&gt; &#39;int \[16\]&#39; lvalue Var 

0x618c780 'b' 'int [16]'$

     \`\-ImplicitCastExpr 0x61912d8 &lt;col:12&gt; &#39;int&#39; &lt;LValueToRValue&gt;$
       \`\-DeclRefExpr 0x6191150 &lt;col:12&gt; &#39;int&#39; lvalue ParmVar 

0x6190ec8 'n' 'int'$

In this case, getParents returns empty list even though ImplicitCastExpr is clearly a parent and is a Stmt. My modification simply calls "const auto Parents = getContext().getParents<Stmt>(ERef);" in EmitArraySubscriptExpr. Has anyone run into a similar problem before?



I believe the parent map that powers getParents is not populated by default (it’s not used by Clang proper - so it doesn’t want to pay that cost) - not sure where/when/how you call to populate it - but hopefully that gives you some hints for what to look for.

I did some digging and it looks like it builds the “Map” on demand, storing it in ASTContext (PointerParents, OtherParents). I thought it might be a buggy implementation of ParentMapASTVisitor, so I tried writing my own ASTVisitor. However, after doing so, both my own and ParentMapASTVisitor reported the TranslationUnitDecl traversing only one user-defined FunctionDecl and not all of them (there are 4 in my test). The root cause appears to be TUDecl missing some of the function decls. I think all that needs to repro this is calling “getContext().getParents(*E)” inside CodeGenFunction::EmitArraySubscriptExpr(). DAE know if there is any specific reason why I can’t getParents from CGExpr.cpp?



Also note that once the parent map has been built, it will not update even if the AST changes. Do you maybe call getParents before finalizing the AST?

This was an issue for me when using the ASTImporter (Analyzer+CrossTU). See

In that review Richard also brought up that it might be better to move getParents from the ASTContext to libTooling.