Crash on spaceship operator
duckdoom5 opened this issue · 21 comments
I just installed this nuget package so I'm not very familiar with this tool yet. It seems the converter for the spaceship operator is not implemented. I get the following debug crash if I try to parse a file that has the spaceship operator.
If I was more familiar I'd try to fix it myself, but I don't feel comfortable enough to do so yet. In the meantime, is there a way to pre-process this file to convert this operator implementation into the equivalent <, <=, ==, >=, > operator pairs?
You could try compiling from source and adding spaceship operator starting here: https://github.com/mono/CppSharp/blob/main/src/Parser/ASTConverter.cs#L1367
Otherwise probably easier to just rewrite the input C++ code, as annoying as it is.
Sure, I'll give it a go. Thanks for the quick reply :)
You could try compiling from source and adding spaceship operator starting here: https://github.com/mono/CppSharp/blob/main/src/Parser/ASTConverter.cs#L1367
Otherwise probably easier to just rewrite the input C++ code, as annoying as it is.
So I managed to get it to build in c++20 debug mode (with some edits that I'll make a PR for soon) so I can see if my changes actually work, but I'm running into the following error:
10>------ Rebuild All started: Project: Encodings.Gen, Configuration: Debug x64 ------
10>Encodings.Gen -> CppSharp\bin\Debug\Encodings.Gen.dll
10>
10>Generating bindings for Encodings (CSharp)
10>Assert at CppSharp\src\CppParser\Parser.cpp:963 in GetCXXRecordDeclFromBaseType failed. Could not get base CXX record from type. Unhandled type: Filename C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xmemory:1486
I'll continue investigating tomorrow, but if you have any idea what might cause this missing record or if you see something I did incorrectly let me know
That seems to be some new kind of C++ type we cannot handle yet:
CppSharp/src/CppParser/Parser.cpp
Line 963 in 56f1b7c
Can you share the lines around C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xmemory:1486?
You can also try calling dump() on the Clang type, see: https://clang.llvm.org/doxygen/classclang_1_1Type.html#a90c89709fd5e5e1d96455aec8b0c3916
Some kind of templated type, can you dump the type? Think that should tell us what we need to know.
The dump output is:
1>TemplateTypeParmType 0x24e820a7b70 '_Ty1' dependent depth 0 index 0
1>`-TemplateTypeParm 0x24e820a7b20 '_Ty1'
What's the call stack when the assert hits?
And you can try something like:
else if (auto TPT = Ty->getAs<clang::TemplateTypeParmType>())
return nullptr;Not sure how to debug the c++ bit atm.
It came up as a part of building another project:
1>------ Build started: Project: Common.Gen, Configuration: Debug x64 ------
1>Skipping analyzers to speed up the build. You can execute 'Build' or 'Rebuild' command to run analyzers.
1>Common.Gen -> C:\SSD_WorkProjects\CppSharp\bin\Debug\Common.Gen.dll
1>
1>Generating bindings for Common (CLI)
1>TemplateTypeParmType 0x24e820a7b70 '_Ty1' dependent depth 0 index 0
1>`-TemplateTypeParm 0x24e820a7b20 '_Ty1'
1>Assert at C:\SSD_WorkProjects\CppSharp\src\CppParser\Parser.cpp:964 in GetCXXRecordDeclFromBaseTypefailed. Could not get base CXX record from type. Unhandled type: Filename C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xmemory:1486
Edit: return nullptr seems to work so far
Ah, now a new error appeared.
10>Assert at `CppSharp\src\CppParser\Parser.cpp:2768`` in CppSharp::CppParser::Parser::WalkType` failed. Dependent template only accepted! Filename `C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xstring:532`
10>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(5933,5): error MSB3073: The command ""C:\Program Files\dotnet\dotnet.exe" "C:\SSD_WorkProjects\CppSharp\bin\Debug\Common.Gen.dll"" exited with code 3.
Which is around this source:
Anyway, thanks for the help so far. I'm gonna take the rest of the night off though, I'll be back tomorrow to continue trying to get this working :p
You could try compiling from source and adding spaceship operator starting here: https://github.com/mono/CppSharp/blob/main/src/Parser/ASTConverter.cs#L1367
Otherwise probably easier to just rewrite the input C++ code, as annoying as it is.So I managed to get it to build in c++20 debug mode (with some edits that I'll make a PR for soon) so I can see if my changes actually work, but I'm running into the following error:
10>------ Rebuild All started: Project: Encodings.Gen, Configuration: Debug x64 ------ 10>
Encodings.Gen->CppSharp\bin\Debug\Encodings.Gen.dll10> 10>Generating bindings for Encodings (CSharp) 10>Assert atCppSharp\src\CppParser\Parser.cpp:963inGetCXXRecordDeclFromBaseTypefailed. Could not get base CXX record from type. Unhandled type: FilenameC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xmemory:1486I'll continue investigating tomorrow, but if you have any idea what might cause this missing record or if you see something I did incorrectly let me know
Okay, made a PR for those compiler error fixes #1892
@tritao
Okay, made some progress again. I'm now able to debug the c++ code, so that makes it a lot easier to see what's going on.
Got a quick question for you though. Does the shipped version of llvm already support c++20 ?
I'm getting syntax errors, so I was wondering if the version needs to be updated of if I need to change some other setting somewhere.
(Though I think I might know already. I guess I need to change the C# hardcoded config in the test projects somewhere)
@tritao Okay, made some progress again. I'm now able to debug the c++ code, so that makes it a lot easier to see what's going on.
Got a quick question for you though. Does the shipped version of llvm already support c++20 ? I'm getting syntax errors, so I was wondering if the version needs to be updated of if I need to change some other setting somewhere.
(Though I think I might know already. I guess I need to change the C# hardcoded config in the test projects somewhere)
Nice.
It should support C++20, you probably need to enable it in the parser options around here:
https://github.com/mono/CppSharp/blob/main/src/Generator.Tests/GeneratorTest.cs#L23
@tritao Heya, I made some progress on this again now that I finished the other PR.
I've managed to generate some code for the spaceship operator, but it's not quite there yet. I'll need to improve the logic a bit before I make a PR.
That said, the reason I ping you here is because I was looking into some other code around the ==operator and I noticed there is a project called CppSharp.Parser.Bootstrap which (as far as I could tell) is the project that generates the C#/C++cli bindings that are used by the C# parser code.
I have a feeling I need to regenerate those as well, but I'm unable to run this project due to build errors.
First I got this one:
Error parsing 'clang/AST/Stmt.h, clang/AST/StmtCXX.h, clang/AST/Expr.h, clang/AST/ExprCXX.h'
C:\SSD_WorkProjects\CppSharp\build\llvm\llvm-6eb36a-windows-vs2022-x64-Debug\clang\include\clang/AST/DeclGroup.h(16,10): fatal: 'llvm/Support/TrailingObjects.h' file not found
CppSharp has encountered an error while parsing code.
But I resolved that one by changing Path.Combine(llvmPath, "include"), to Path.Combine(llvmPath, "llvm", "include"),.
After that I'm getting a whole bunch of errors some of them are:
..\CppSharp\build\llvm\llvm-6eb36a-windows-vs2022-x64-Debug\llvm\include\llvm/Support/Alignment.h(117,33): error: unknown template name 'optional'
..\CppSharp\build\llvm\llvm-6eb36a-windows-vs2022-x64-Debug\llvm\include\llvm/Support/Alignment.h(119,19): error: no template named 'optional' in namespace 'std'
..\CppSharp\build\llvm\llvm-6eb36a-windows-vs2022-x64-Debug\llvm\include\llvm/Support/Alignment.h(131,29): error: no type named 'nullopt_t' in namespace 'std'; did you mean 'nullptr_t'?
Which I resolved by setting driver.ParserOptions.LanguageVersion = LanguageVersion.CPP17_GNU;.
But now it's crashing on:
An unhandled exception of type 'System.ExecutionEngineException' occurred in CppSharp.Parser.CLI.dll
Fatal error. 0xC0000005
at <Module>.CppSharp.CppParser.ClangParser.ParseHeader(CppSharp.CppParser.CppParserOptions*)
at CppSharp.Parser.ClangParser.ParseHeader(CppSharp.Parser.CppParserOptions)
at CppSharp.ClangParser.ParseSourceFiles(System.Collections.Generic.IEnumerable`1<System.String>, CppSharp.Parser.ParserOptions)
at CppSharp.Driver.ParseCode()
at CppSharp.ConsoleDriver.Run(CppSharp.ILibrary)
at CppSharp.Bootstrap.Main(System.String[])
Any ideas how to get this to run again?
Hey, cool, nice to hear you're making progress on this, looking forward to the PR.
About Parser.Bootstrap, it's an attempt at binding the Clang statement/expr APIs/decls directly, instead of doing any manual bindings, but it's been a few years (6, I think) since it's been worked on really, so it doesn't surprise me there are issues to fix.
It generates these:
https://github.com/mono/CppSharp/blob/main/src/CppParser/ParseExpr.cpp
https://github.com/mono/CppSharp/blob/main/src/CppParser/ParseStmt.cpp
https://github.com/mono/CppSharp/blob/main/src/CppParser/Expr.h
https://github.com/mono/CppSharp/blob/main/src/CppParser/Stmt.h
https://github.com/mono/CppSharp/blob/main/src/AST/Expr.cs
https://github.com/mono/CppSharp/blob/main/src/AST/Stmt.cs
Not really sure about the crash, there isn't enough info to figure it out, seems like it's crashing somewhere in the native parser.
Is it necessary to touch these for the spaceship operator?
Has this been fixed with the latest PRs or still needs some fixes?
No not yet. The crash might be fixed, but there's still some issue with it generating the correct operator functions




