CppSharp
CppSharp copied to clipboard
Error parsing CXXDeductionGuide
Consider:
using CppSharp;
using CppSharp.AST;
using CppSharp.Parser;
class Program
{
class TestLibrary : ILibrary
{
public void SetupPasses(Driver driver)
{
}
public void Preprocess(Driver driver, ASTContext ctx)
{
}
public void Postprocess(Driver driver, ASTContext ctx)
{
}
public void Setup(Driver driver)
{
DriverOptions driverOptions = driver.Options;
ParserOptions parserOptions = driver.ParserOptions;
parserOptions.LanguageVersion = LanguageVersion.CPP17;
parserOptions.AddCompilationOptions("-frtti");
driverOptions.Quiet = false;
driverOptions.DryRun = true;
driverOptions.OutputDir = Directory.GetCurrentDirectory();
var module = driverOptions.AddModule("test");
string temp = Path.GetTempFileName();
File.WriteAllText(temp, @"
template<class T>
struct container
{
container(T t) {}
template<class Iter>
container(Iter beg, Iter end);
};
// additional deduction guide
template<class Iter>
container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>;
");
module.Headers.Add(temp);
}
}
static void Main(string[] args)
{
ConsoleDriver.Run(new TestLibrary());
}
}
Output:
Parsing libraries...
Parsing code...
Unhandled declaration kind: CXXDeductionGuide
C:\Users\user\AppData\Local\Temp\tmp6CC2.tmp (line 5)
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
at CppSharp.Parser.ClangParser+__Internal.ParseHeader(IntPtr)
--------------------------------
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 Program.Main(System.String[])
Second question:
How to include a precompiled header?
Because when <cassert> is included in a precompiled header I am getting:
error: use of undeclared identifier 'assert'
CppSharp has encountered an error while parsing code.
A bit hard to say what could be wrong, the CXXDeductionGuide might not be the reason this crashes.
Can you try the same with build.sh -configuration Debug to build in debug mode with LLVM symbols?
Actually I was trying to create a tool for a library, and was getting a similar error but with a deeper log.
So I managed to recreate this error with a minimal class template argument deduction snippet.
I am actually using a nuget package with this test.
Should I build from sources?
So on your machine this doesn't throw an error?
Honestly I couldn't generate a Debug solution.
What command are you running for generating a vs2022 Debug solution anyway?
Using ./build.sh generate -configuration Debug -platform x64 is trying to generate for vs2019 and failing.
llvm Debug doesn't work, so the Debug flag isn't working.
Only llvm RelWithDebInfo works and which I am currently using.
Another simpler repro:
#include <string>
template<typename T>
struct Test
{
T t;
};
Test(const char *) -> Test<std::string>;
Test thing{"A String"};
@tritao This must be related to CXXDeductionGuide.
Most likely #1715 is the same issue.
Basically I found where the issue resides, but it needs to be fixed by someone with higher knowledge about the native parser module.
What I did is just silent the issue with dummy patches in the switch cases:
My patch is in the file CppSharp/Parser.cpp like so:
Here I added this dummy case:
https://github.com/mono/CppSharp/blob/40f3a09296ee4f7339483eaaa16e6d2d4b18c2a4/src/CppParser/Parser.cpp#L2326
case clang::Type::DeducedTemplateSpecialization:
{
auto BT = new BuiltinType();
BT->type = PrimitiveType::Void;
Ty = BT;
break;
}
Here I added this dummy case:
https://github.com/mono/CppSharp/blob/40f3a09296ee4f7339483eaaa16e6d2d4b18c2a4/src/CppParser/Parser.cpp#L4014
case Decl::CXXDeductionGuide:
break;
Basically the parser doesn't handle CXXDeductionGuide properly.
PS:
Now the second example compiles (even if it is a dummy patch), but the first one still gives System.AccessViolationException.
@tritao How are you debugging the C++ part on Windows, the only llvm version I see: llvm-791523-linux-x64-gcc-9-Debug.tar.xz which is on linux?
Do you think such issue is fixable at some point?
I haven't been using Windows for CppSharp development for a bit, and yes it's easy to fix.
I haven't been using Windows for CppSharp development for a bit, and yes it's easy to fix.
My main concern is having unfixable bugs, I don't mind things taking their time thought.