Skip to content

NullReferenceException - parent is null #106

Closed

Description

@Braidon

Hi,
I tried to parse C++ code, but always exception raised.
Excluded almost everything, I found file it causes - std string from msvc.
You can reproduce the error using following code
string file = "C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\VC\\Tools\\MSVC\\14.40.33807\\include\\string";
CppCompilation compilation = CppParser.ParseFiles(new List<string>() { file }, null);

Stack trace
at CppAst.CppModelBuilder.GetOrCreateDeclarationContainer(CXCursor cursor, Void* data) in D:\Projects.....\CppAst.NET\src\CppAst\CppModelBuilder.cs:line 142

Parsing at:
{Line 625, Column 9 in C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\corecrt.h to Line 629, Column 2 in C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\corecrt.h}

Activity

  1. xoofx commented on Aug 19, 2024

    @xoofx
    Owner

    Clang doesn't provide support by default for MSVC header files, so you will have to setup this yourselve. There is a helper method CppParserOptions.ConfigureForWindowsMsvc that should help with this.

    public CppParserOptions ConfigureForWindowsMsvc(CppTargetCpu targetCpu = CppTargetCpu.X86, CppVisualStudioVersion vsVersion = CppVisualStudioVersion.VS2022)
    {
    // 1920
    var highVersion = ((int)vsVersion) / 100; // => 19
    var lowVersion = ((int)vsVersion) % 100; // => 20
    var versionAsString = $"{highVersion}.{lowVersion}";
    TargetCpu = targetCpu;
    TargetCpuSub = string.Empty;
    TargetVendor = "pc";
    TargetSystem = "windows";
    TargetAbi = $"msvc{versionAsString}";
    // See https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019
    Defines.Add($"_MSC_VER={(int)vsVersion}");
    Defines.Add("_WIN32=1");
    switch (targetCpu)
    {
    case CppTargetCpu.X86:
    Defines.Add("_M_IX86=600");
    break;
    case CppTargetCpu.X86_64:
    Defines.Add("_M_AMD64=100");
    Defines.Add("_M_X64=100");
    Defines.Add("_WIN64=1");
    break;
    case CppTargetCpu.ARM:
    Defines.Add("_M_ARM=7");
    break;
    case CppTargetCpu.ARM64:
    Defines.Add("_M_ARM64=1");
    Defines.Add("_WIN64=1");
    break;
    default:
    throw new ArgumentOutOfRangeException(nameof(targetCpu), targetCpu, null);
    }
    AdditionalArguments.Add("-fms-extensions");
    AdditionalArguments.Add("-fms-compatibility");
    AdditionalArguments.Add($"-fms-compatibility-version={versionAsString}");
    return this;
    }

  2. Braidon commented on Aug 19, 2024

    @Braidon
    Author

    No, it's not a msvc-relative problem. At cppast 0.8.0-alpha001/clang 12 everything works fine without options manipulations. It seems, problem is in struct parsing (_Mbstatet {...} in this case), old version gets SemanticParent of kind CXCursorKind.CXCursor_UnexposedDecl = CXCursor_FirstDecl and container for structure set as _rootContainerContext.
    New version work with SemanticParent = CXCursor_LinkageSpec and it falls at default Unhandled(cursor);
    As far I understand one of this versions handle struct incorrectly.

  3. xoofx commented on Aug 20, 2024

    @xoofx
    Owner

    I haven't been able to reproduce it in Debug with latest repository. Though, I found a problem in debug that should be fixed via c2cbfe5.

    0.8.0 is extremely old and lots of things have changed regarding template parameter/argument parsing. I personally don't use/care about C++ parsing so you will have to investigate this further within the existing codebase.

  4. xoofx commented on Jun 7, 2026

    @xoofx
    Owner

    Fixed via commit 320bc7e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions