Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException - parent is null #106

Open
Braidon opened this issue Aug 19, 2024 · 3 comments
Open

NullReferenceException - parent is null #106

Braidon opened this issue Aug 19, 2024 · 3 comments
Labels
question Further information is requested

Comments

@Braidon
Copy link

Braidon commented Aug 19, 2024

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}

@xoofx xoofx added the question Further information is requested label Aug 19, 2024
@xoofx
Copy link
Owner

xoofx commented Aug 19, 2024

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;
}

@Braidon
Copy link
Author

Braidon commented Aug 19, 2024

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.

@xoofx
Copy link
Owner

xoofx commented Aug 20, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants