Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン
#122 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
55/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
cpp, csharp
領域
compilers

調査の方向性

CppContainerList.Add と Insert から始め、CXType_Unexposed と ParseTemplateSpecializedArguments を通じて CppModelBuilder.GetCppTypeInternal を追跡します。示されているオプションで VTK 9.5 の vtkActor.h をパースして失敗を再現します。コンテナの ownership exception なしでヘッダーをパースでき、重複と parent の動作も引き続きカバーされていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
C#
スター
623
フォーク
79
平均マージ
2日 2時間
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

xoofx/CppAst.NET のほかの issue

xoofx/CppAst.NET の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。