hasAncestor matching nothing

for the source code below:
short int test16(int a, int b)
{
  short int sum;
  short unsigned int sumus;
  sum = a + b;
  sumus = a + b;

  return sum;
}
with AST:

-FunctionDecl 0x4e52920 <line:821:1, line:829:1> line:821:11 test16

'short (int, int)'

>-ParmVarDecl 0x4e527d8 <col:18, col:22> col:22 used a 'int'
>-ParmVarDecl 0x4e52848 <col:25, col:29> col:29 used b 'int'
`-CompoundStmt 0x4e52d60 <line:822:1, line:829:1>
  >-DeclStmt 0x4e52a48 <line:823:2, col:15>
  > `-VarDecl 0x4e529e8 <col:2, col:12> col:12 used sum 'short'
  >-DeclStmt 0x4e52ad0 <line:824:2, col:26>
  > `-VarDecl 0x4e52a70 <col:2, col:21> col:21 used sumus 'unsigned short'
  >-BinaryOperator 0x4e52bd0 <line:825:2, col:12> 'short' '='
  > >-DeclRefExpr 0x4e52ae8 <col:2> 'short' lvalue Var 0x4e529e8 'sum' 'short'
  > `-ImplicitCastExpr 0x4e52bb8 <col:8, col:12> 'short' <IntegralCast>
  > `-BinaryOperator 0x4e52b90 <col:8, col:12> 'int' '+'
  > >-ImplicitCastExpr 0x4e52b60 <col:8> 'int' <LValueToRValue>
  > > `-DeclRefExpr 0x4e52b10 <col:8> 'int' lvalue ParmVar

0x4e527d8 'a' 'int'

  > `-ImplicitCastExpr 0x4e52b78 <col:12> 'int' <LValueToRValue>
  > `-DeclRefExpr 0x4e52b38 <col:12> 'int' lvalue ParmVar

0x4e52848 'b' 'int'

  >-BinaryOperator 0x4e52ce0 <line:826:2, col:14> 'unsigned short' '='
  > >-DeclRefExpr 0x4e52bf8 <col:2> 'unsigned short' lvalue Var

0x4e52a70 'sumus' 'unsigned short'

  > `-ImplicitCastExpr 0x4e52cc8 <col:10, col:14> 'unsigned short'

<IntegralCast>

  > `-BinaryOperator 0x4e52ca0 <col:10, col:14> 'int' '+'
  > >-ImplicitCastExpr 0x4e52c70 <col:10> 'int' <LValueToRValue>
  > > `-DeclRefExpr 0x4e52c20 <col:10> 'int' lvalue ParmVar

0x4e527d8 'a' 'int'

  > `-ImplicitCastExpr 0x4e52c88 <col:14> 'int' <LValueToRValue>
  > `-DeclRefExpr 0x4e52c48 <col:14> 'int' lvalue ParmVar

0x4e52848 'b' 'int'

  `-ReturnStmt 0x4e52d48 <line:828:2, col:9>
    `-ImplicitCastExpr 0x4e52d30 <col:9> 'short' <LValueToRValue>
      `-DeclRefExpr 0x4e52d08 <col:9> 'short' lvalue Var 0x4e529e8

'sum' 'short'
when i run the below matcher on clang-query:
match declRefExpr(hasAncestor(declRefExpr()))
i get no matches, while in the AST it clearly says that there are such
nodes. so I was wondering what could be causing this.

for the source code below:
short int test16(int a, int b)
{
short int sum;
short unsigned int sumus;
sum = a + b;
sumus = a + b;

return sum;
}
with AST:

-FunctionDecl 0x4e52920 <line:821:1, line:829:1> line:821:11 test16
‘short (int, int)’

-ParmVarDecl 0x4e527d8 <col:18, col:22> col:22 used a ‘int’
-ParmVarDecl 0x4e52848 <col:25, col:29> col:29 used b ‘int’
-CompoundStmt 0x4e52d60 <line:822:1, line:829:1> -DeclStmt 0x4e52a48 <line:823:2, col:15> -VarDecl 0x4e529e8 <col:2, col:12> col:12 used sum ‘short’
-DeclStmt 0x4e52ad0 <line:824:2, col:26>
`-VarDecl 0x4e52a70 <col:2, col:21> col:21 used sumus ‘unsigned short’
-BinaryOperator 0x4e52bd0 <line:825:2, col:12> ‘short’ ‘=’

-DeclRefExpr 0x4e52ae8 col:2 ‘short’ lvalue Var 0x4e529e8 ‘sum’ ‘short’
-ImplicitCastExpr 0x4e52bb8 <col:8, col:12> 'short' <IntegralCast> -BinaryOperator 0x4e52b90 <col:8, col:12> ‘int’ ‘+’
-ImplicitCastExpr 0x4e52b60 col:8 ‘int’
-DeclRefExpr 0x4e52b10 <col:8> 'int' lvalue ParmVar 0x4e527d8 'a' 'int' -ImplicitCastExpr 0x4e52b78 col:12 ‘int’
-DeclRefExpr 0x4e52b38 <col:12> 'int' lvalue ParmVar 0x4e52848 'b' 'int' -BinaryOperator 0x4e52ce0 <line:826:2, col:14> 'unsigned short' '=' -DeclRefExpr 0x4e52bf8 <col:2> 'unsigned short' lvalue Var 0x4e52a70 'sumus' 'unsigned short' -ImplicitCastExpr 0x4e52cc8 <col:10, col:14> ‘unsigned short’

-BinaryOperator 0x4e52ca0 <col:10, col:14> 'int' '+' -ImplicitCastExpr 0x4e52c70 <col:10> 'int' <LValueToRValue> -DeclRefExpr 0x4e52c20 col:10 ‘int’ lvalue ParmVar
0x4e527d8 ‘a’ ‘int’
-ImplicitCastExpr 0x4e52c88 <col:14> 'int' <LValueToRValue> -DeclRefExpr 0x4e52c48 col:14 ‘int’ lvalue ParmVar
0x4e52848 ‘b’ ‘int’
-ReturnStmt 0x4e52d48 <line:828:2, col:9> -ImplicitCastExpr 0x4e52d30 col:9 ‘short’
`-DeclRefExpr 0x4e52d08 col:9 ‘short’ lvalue Var 0x4e529e8
‘sum’ ‘short’
when i run the below matcher on clang-query:
match declRefExpr(hasAncestor(declRefExpr()))
i get no matches, while in the AST it clearly says that there are such

nodes. so I was wondering what could be causing this.

Perhaps we can make the printing easier to read here:
-a

-b
`-c
does not mean that c is a child of b, it means (if I’m not mistaken) that c is the last child of a, but both b and a are equally children of a.

thank you for the explanation. I do realize that, but that is clearly
not the case here.

which declRefExpr do you expect to be a child of which other declRefExpr?

the two in this part of the AST:

  > >-DeclRefExpr 0x4e52ae8 <col:2> 'short' lvalue Var 0x4e529e8 'sum' 'short'
  > `-ImplicitCastExpr 0x4e52bb8 <col:8, col:12> 'short' <IntegralCast>
  > `-BinaryOperator 0x4e52b90 <col:8, col:12> 'int' '+'
  > >-ImplicitCastExpr 0x4e52b60 <col:8> 'int' <LValueToRValue>
  > > `-DeclRefExpr 0x4e52b10 <col:8> 'int' lvalue ParmVar

0x4e527d8 'a' 'int'

If I’m not missing something, both are in different sides of the = binary op… The = is an ancestor to both of them.

true. the declRefExpr for 'sum' and 'a' or 'b' are on different sides
of the '=' and all are descendants of that binaryOp.