llvm / llvm/llvm-project

Python clang package recognizes std::string_view as int

Open
#162,210 4 comments 0 reactions 0 assignees View on GitHub
clang:as-a-library
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The pip package [clang](https://pypi.org/project/clang/) recognizes the C++ standard library type `std::string_view` as `int`.

Reproduce:

```cpp
#include

std::string_view str_view_global;

struct TestStruct {
std::string_view str_view;
};
```

```python
import clang.cindex

index = clang.cindex.Index.create()
tu = index.parse(
"test.cpp",
options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD
)

if tu is None or tu.cursor is None:
raise RuntimeError("Failed to parse C++ file.")

for cursor in tu.cursor.get_children():
if cursor.spelling == "str_view_global":
print(cursor.type.spelling, cursor.spelling)
if cursor.kind == clang.cindex.CursorKind.STRUCT_DECL and cursor.spelling == "TestStruct":
for field in cursor.get_children():
if field.kind != clang.cindex.CursorKind.FIELD_DECL:
continue
print(field.type.spelling, field.spelling)
```

Output:

```
❯ python ./test.py
int str_view_global
int str_view
```

Version info:

```
❯ pip show clang
Name: clang
Version: 20.1.5
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: /home/oppositenormal/miniforge3/envs/white-bird-engine/lib/python3.12/site-packages
Requires:
Required-by:
```

Contributor guide

Open the contributing guide

Research direction

Start by running the Python reproduction in test.py and inspect how clang.cindex reports the types of the global and struct field. Trace the type-spelling path for std::string_view; done means both reported types are std::string_view rather than int.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
compilers, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.