CppContainerList throws "The item belongs already to a container" when parsing VTK 9.5 headers Body

Đang mở
#122 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
cpp, csharp
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu với CppContainerList.Add và Insert, sau đó lần theo CppModelBuilder.GetCppTypeInternal qua CXType_Unexposed và ParseTemplateSpecializedArguments. Tái hiện lỗi bằng cách parse vtkActor.h từ VTK 9.5 với các tùy chọn được nêu. Được xem là hoàn tất khi header được parse mà không xảy ra container ownership exception, đồng thời hành vi đối với duplicate và parent vẫn được bao phủ.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Description

When parsing VTK 9.5 headers with CppParser.ParseFile, an ArgumentException is thrown:

System.ArgumentException: The item belongs already to a container
   at CppAst.CppContainerList`1.Add(TElement item)
   at CppAst.CppContainerList`1.AddRange(IEnumerable`1 collection)
   at CppAst.CppModelBuilder.GetCppTypeInternal(CXCursor cursor, CXType type, CXCursor parent, Void* data)

Reproduction

var options = new CppParserOptions();
options.ConfigureForWindowsMsvc(CppTargetCpu.X86_64, CppVisualStudioVersion.VS2022);
options.IncludeFolders.Add(@"C:\Program Files\VTK\include\vtk-9.5");
var compilation = CppParser.ParseFile(@"C:\Program Files\VTK\include\vtk-9.5\vtkActor.h", options);

Environment: VTK 9.5 headers, CppAst 0.25.0, ClangSharp 21.1.8.3, Windows x64.

Root Cause

The problem is in CppContainerList<T>.Add() — it enforces strict single-parent ownership:

public void Add(TElement item)
{
    if (item.Parent != null)
        throw new ArgumentException("The item belongs already to a container");
    ...
}

During GetCppTypeInternal → CXType_Unexposed handling, ParseTemplateSpecializedArguments resolves template arguments that return type instances already belonging to a container (e.g., a CppClass that was added to compilation.Classes). When these same instances are added to CppUnexposedType.TemplateParameters via AddRange, the strict check fails.

The stack trace consistently points to:

GetCppTypeInternal → VisitElaboratedDecl → GetCppType → GetCppTypeInternal → VisitFunctionDecl → CppContainerList.AddRange

Suggested Fix

Relax Add and Insert to allow re-parenting. Skip duplicates in the same container, otherwise allow the element to move:

public void Add(TElement item)
{
    if (item.Parent == Container)
        return; // Already in this container
    item.Parent = Container;
    _elements.Add(item);
}

public void Insert(int index, TElement item)
{
    if (item.Parent == Container)
        return; // Already in this container
    item.Parent = Container;
    _elements.Insert(index, item);
}

This is the same approach taken by XML DOM implementations — a node can be moved from one parent to another without error.

Ngôn ngữ chính
C#
Star
625
Fork
79
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của xoofx/CppAst.NET

Tất cả issue của xoofx/CppAst.NET

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.