Skip to content

Python clang package recognizes std::string_view as int #162210

@OppositeNor

Description

@OppositeNor

The pip package clang recognizes the C++ standard library type std::string_view as int.

Reproduce:

#include <string_view>

std::string_view str_view_global;

struct TestStruct {
    std::string_view str_view;
};
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: 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions