CppSharp icon indicating copy to clipboard operation
CppSharp copied to clipboard

Error parsing CXXDeductionGuide

Open deadlocklogic opened this issue 2 years ago • 8 comments

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.

deadlocklogic avatar Nov 10 '23 18:11 deadlocklogic

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?

tritao avatar Nov 11 '23 11:11 tritao

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?

deadlocklogic avatar Nov 11 '23 20:11 deadlocklogic

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.

deadlocklogic avatar Nov 11 '23 21:11 deadlocklogic

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.

deadlocklogic avatar Nov 12 '23 11:11 deadlocklogic

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.

deadlocklogic avatar Nov 12 '23 13:11 deadlocklogic

@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?

deadlocklogic avatar Nov 15 '23 09:11 deadlocklogic

I haven't been using Windows for CppSharp development for a bit, and yes it's easy to fix.

tritao avatar Nov 17 '23 15:11 tritao

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.

deadlocklogic avatar Nov 17 '23 15:11 deadlocklogic