<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/68838>68838</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Python libclang AST discrepancy
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
briandw
</td>
</tr>
</table>
<pre>
I'm seeing a discrepency in the python clang AST output and the terminal (clang) output.
My C code is this single line:
`typedef unsigned char nd_mac_addr[6U];`
Get the AST
```
clang -c -Xclang -ast-dump /tmp/sample.c
results in:
<implicit stuff clipped>
-TypedefDecl 0x1410d1f60 </tmp/sample.c:1:1, col:37> col:23 nd_mac_addr 'unsigned char[6]'
`-ConstantArrayType 0x1410d1f00 'unsigned char[6]' 6
`-BuiltinType 0x14102a170 'unsigned char'
```
Doing the same from python
```
from pathlib import Path
from clang.cindex import Config, Index
source_code = "typedef unsigned char nd_mac_addr[6U];"
source_path = Path("/tmp/sample.c")
source_path.write_text(source_code)
#local clang dynamic library path
dylib_path = Path('/opt/homebrew/opt/llvm/lib/libclang.dylib')
Config.set_library_file(str(dylib_path))
index = Index.create()
tu = index.parse(str(source_path))
def walk_ast(node):
for node in node.get_children():
print(node.spelling, node.kind, node.type.kind)
walk_ast(node)
walk_ast(tu.cursor)
```
produces the output
`nd_mac_addr CursorKind.TYPEDEF_DECL TypeKind.TYPEDEF
CursorKind.INTEGER_LITERAL TypeKind.UINT`
The clang binary results in ConstantArrayType as I would expect, and python is INTEGER_LITERAL?
This doesn't make sense to me.
Here is the install information from my system:
macOS version 13.6 (22G120)
clang --version
Homebrew clang version 17.0.1
Target: arm64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin
python --version
Python 3.11.0
pip show clang
clang
Version: 16.0.1.1
Summary: libclang python bindings
Home-page: http://clang.llvm.org/
Author: LLVM team - pypi upload by Loic Jaquemet
Author-email:
License: Apache-2.0 with LLVM exception
Location: /opt/homebrew/Caskroom/miniconda/base/envs/vartia/lib/python3.11/site-packages
Requires:
Required-by:
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVt2S4roRfhpx04XLlsHABRfsMGwmmXOytTvnVHJFyVKDlZElryTvjN8-JVmA5ydJpWrGQKt_vv661W3mnDxrxC1ZfiHL_Yz1vjF2W1vJtHiZ1UYM2wdCVy04RKnPwEBIxy12qPkAUoNvELrBN0YDV0yfYffjCUzvu94D0yKee7St1EwBoeuoROgm6WQk35N8Nz5_G-AOuBEI0oFvpAMn9VkhKKmRlLupLqlyP3Qo8AS9jkkI4A2zoMWxZfzIhLBk-aX6gyz3pPxCqnxq_RV9RLb78XR1l_7izzGVOQeY_yN9Z87PRd92QOjBtx2hB8faTmHGRxOLrlfegdQ3pOWdbDslufTgfH86AVey61CQ8n7UmD-NOeyRK8hfi0WRi-JU5UDKu49xyl0R_2lgSZFyV65IeZ--03KaOhC6esNL4CJQQVdjZCBVPr8z2nmm_c5aNgQoNwx5_l9cQAXJC0B09KWXyks9cUFZsfrExSX8O77H596EFgt1caxFOFnTpt56V_mp6ajFfKNkDbLtjPXwjflmchormHGpBb5eVO6MPslzoPIhiKcBnOktx2NsRFLugVD6f7UapW_8BGzRT0RF1-H8Q2mDbPPBLHux0uPR46sndD3BdVUmtFSGM5Uunxg0ayUHJWvL7BB5GRXFoGT9CZYVoQfTeUIPjWmxtvhyFSj1qw0fsh6fI43RUbRLEEYmM4f-mMIeT1JhAOwtoetb5GByBR6fY0kCoFiFjFtkHiOwpOf7eBwVs45Zd3M8Ieqj51CtF6aej8wF7nQi7XI1Q-OejAUdp42On9kZ_ZE3UgmLOmG46ndW6oujzHWolNSxfaLgWWpx_RGaJUkSpM-QTLBOTn2f8d46Y28qn92UzhrRc3TxsoyT9Ko9nQJ30dffpBbZ0z-_3e_vD8f9_d0jhIs6lSaYE_WH35_uv95_Pz4-PN1_300s_nj4_ekdmqcGU__VUoe2u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