<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/65112>65112</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang/bindings/python] Macro-expanded CALL_EXPR arguments have incorrect spelling
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dmcardle
</td>
</tr>
</table>
<pre>
Hi, folks! I believe I'm seeing an incorrect spelling for arguments when a CALL_EXPR is generated by macro expansion.
Here's a Python test that shows what I'm talking about. This test fails with the following commands on the main branch (34e2f4f2e28a464b127d878979efbc87bc148db5).
I'm happy to set up a Phabricator account and upload a PR if that would be helpful.
```diff
diff --git a/clang/bindings/python/tests/cindex/test_cursor.py b/clang/bindings/python/tests/cindex/test_cursor.py
index 84cd81394184..30cf0f0c30fb 100644
--- a/clang/bindings/python/tests/cindex/test_cursor.py
+++ b/clang/bindings/python/tests/cindex/test_cursor.py
@@ -631,6 +631,30 @@ class TestCursor(unittest.TestCase):
self.assertEqual(arguments[0].spelling, "i")
self.assertEqual(arguments[1].spelling, "j")
+ def test_get_arguments_of_macro_expanded_call(self):
+ tu = get_tu(
+ """
+#define CONST_FOO 0
+#define CALL_MAGIC(name) magic(CONST_##name)
+void magic(int register_offset);
+void entry_point(void) {
+ CALL_MAGIC(FOO);
+}
+"""
+ )
+
+ cursors = get_cursors(tu, "magic")
+ [magic_decl, magic_call, magic_unexposed, magic_decl_ref] = cursors
+ self.assertEqual(magic_decl.kind, CursorKind.FUNCTION_DECL)
+ self.assertEqual(magic_call.kind, CursorKind.CALL_EXPR)
+ self.assertEqual(magic_unexposed.kind, CursorKind.UNEXPOSED_EXPR)
+ self.assertEqual(magic_decl_ref.kind, CursorKind.DECL_REF_EXPR)
+
+ # Get arguments from the CALL_EXPR node.
+ [arg] = list(magic_call.get_arguments())
+ tokens = [t.spelling for t in arg.get_tokens()]
+ self.assertEqual(tokens, ["CONST_FOO"])
+
def test_get_num_template_arguments(self):
tu = get_tu(kTemplateArgTest, lang="cpp")
foos = get_cursors(tu, "foo")
```
Configuration:
```bash
cmake -S llvm -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_PROJECTS=clang
```
Output:
```console
$ ninja -C build check-clang-python
ninja: Entering directory `build'
[12/12] cd /home/dan/REPOS/llvm-project/clang/bindings/python && /nix/store/p6nyqsw5qbg114gprcj4ci8j4nx9nhnb-cmake-3....lvm-project/build/lib /nix/store/2c7sgx69p6mmp76cvmi5j6c72dj76jj8-python3-3.10.12/bin/python3.10 -m unittest discover
FAILED: tools/clang/bindings/python/tests/CMakeFiles/check-clang-python /home/dan/REPOS/llvm-project/build/tools/clang/bindings/python/tests/CMakeFiles/check-clang-python
cd /home/dan/REPOS/llvm-project/clang/bindings/python && /nix/store/p6nyqsw5qbg114gprcj4ci8j4nx9nhnb-cmake-3.26.4/bin/cmake -E env CLANG_NO_DEFAULT_CONFIG=1 CLANG_LIBRARY_PATH=/home/dan/REPOS/llvm-project/build/lib /nix/store/2c7sgx69p6mmp76cvmi5j6c72dj76jj8-python3-3.10.12/bin/python3.10 -m unittest discover
........................F.........................................................................................................
======================================================================
FAIL: test_get_arguments_of_macro_expanded_call (tests.cindex.test_cursor.TestCursor)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/dan/REPOS/llvm-project/clang/bindings/python/tests/cindex/test_cursor.py", line 656, in test_get_arguments_of_macro_expanded_call
self.assertEqual(tokens, ["CONST_FOO"])
AssertionError: Lists differ: ['0', '#', 'define', 'CALL_MAGIC', '(', 'na[182 chars] ')'] != ['CONST_FOO']
First differing element 0:
'0'
'CONST_FOO'
First list contains 29 additional elements.
First extra element 1:
'#'
+ ['CONST_FOO']
- ['0',
- '#',
- 'define',
- 'CALL_MAGIC',
- '(',
- 'name',
- ')',
- 'magic',
- '(',
- 'CONST_',
- '##',
- 'name',
- ')',
- 'void',
- 'magic',
- '(',
- 'int',
- 'register_offset',
- ')',
- ';',
- 'void',
- 'entry_point',
- '(',
- 'void',
- ')',
- '{',
- 'CALL_MAGIC',
- '(',
- 'FOO',
- ')']
----------------------------------------------------------------------
Ran 130 tests in 0.375s
FAILED (failures=1)
ninja: build stopped: subcommand failed.
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWFtvo7r2_zTuy1IQGALkoQ-5Mbv_nWmqTuevvZ-QMSa4BZuNTS_f_siGXGjSOR3NSOfoIBTFt59_a9nrBlGK7wRj12i6QNPVFel0KdvrvKakzSt2lcn87foPjvASClk9KYQ9uIGMVZw9M7hBOKpBMcbFDogALqhsW0Y1qIZVlektZAuk3XU1E1rBS8kEEFjON5t0_dfdPXAFOyZYSzTLIXuDmtBWAnttiFBcCge5K-TO-98_WMsQjhQQuHvTpRSgmdKgS6JBlfLFwBM9kNKkerKsMtlpBx5KrvrpBeGVgheuS9AlM1JV8sXMpLKuicgVGOCSQU24gKwlgpaAcOwHDBdBgRmOSRAGmYejPI7iWTRjRUbjKKNeEOfZFOHZiHVPpyRN8wZagmIausaIUJKs5ZRooyBKZSc0EJFD11SS5GbCPfCil-5FdlUOGYOSVU3RVaMNUOj2b86Lou8y_2Ay2XENBOGEVkTsEE4yLnIudgrhpLEKRDgxOjEdlIucvQ4dKe1aJVuneYPs19b3fOwYxAHNY8-fBV4cOI7v0sItXOq7RQae64ZB0E-eTCa_ynpQDF707-8RAgUuClyYhL6H8DIEhBf9X9-FYYxWRCl4YEov7VqE405wbdAc20sUQ3iG_OHoQLGqcIhSrNXrfzpSIRwfjAVNFy6arpy9KRkbRBhzhLHB-BSAdwHg8RTgoCkAgJwV1kbSHdPpASaVRWrNMrVmmbM8paQyO5nNT8UZYMyjO0D-CgyQ7hCOzyaYxxKx7_G8_JwVXDBYbm-_PaTJdgvupVHjQL7Ov9wsEY4FqY1WoSY7ThGO-6UI-wj7w9gB4lny_DCRCw0t23GlWZvKolBMW3EW4-lM6PYtbS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