xoofx / CppAst.NET

CppAst is a .NET library providing a C/C++ parser for header files powered by Clang/libclang with access to the full AST, comments and macros
BSD 2-Clause "Simplified" License
482 stars 63 forks source link

Stack overflow in CppModelBuilder::GetOrCreateDeclarationContainer() after release 0.10.0 #84

Closed haraldwer closed 11 months ago

haraldwer commented 11 months ago

Hi! Thank you for this tool. We encountered some problems with the latest releases in our project. Parsing code using any release after 0.10.0 results in stack overflow in CppModelBuilder::GetOrCreateDeclarationContainer(). The error occurs even when parsing the provided example code.

Here is our setup: Platform Windows x64 TargetFramework net7.0 LLVM 17.0.2 ClangSharp 16.0.0 ClangSharp.Interop 16.0.0 libClangSharp 16.0.6 libClangSharp.runtime.win-x64 16.0.6 libclang.runtime.win-x64 16.0.6

You can try our setup yourself, you will find the CppParser.Parse in DB::Load() GDExtensionTemplate_CppAsp.Net-Impl

My guess is that this is due to a version mismatch.

haraldwer commented 11 months ago

This is the callstack:

Stack overflow.
   at ClangSharp.Interop.clang.getCursorUSR(ClangSharp.Interop.CXCursor)
   at CppAst.CppModelBuilder.GetOrCreateDeclarationContainer(ClangSharp.Interop.CXCursor, Void*)
   ...
   at CppAst.CppModelBuilder.GetOrCreateDeclarationContainer(ClangSharp.Interop.CXCursor, Void*)
   at CppAst.CppModelBuilder.GetOrCreateDeclarationContainer[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](ClangSharp.Interop.CXCursor, Void*, CppContainerContext ByRef)
   at CppAst.CppModelBuilder.VisitClassDecl(ClangSharp.Interop.CXCursor, Void*)
   at CppAst.CppModelBuilder.VisitMember(ClangSharp.Interop.CXCursor, ClangSharp.Interop.CXCursor, Void*)
   at CppAst.CppModelBuilder.VisitTranslationUnit(ClangSharp.Interop.CXCursor, ClangSharp.Interop.CXCursor, Void*)
   at ClangSharp.Interop.clang.visitChildren(ClangSharp.Interop.CXCursor, ClangSharp.Interop.CXChildVisitResult (ClangSharp.Interop.CXCursor, ClangSharp.Interop.CXCursor, Void*), Void*)
   at ClangSharp.Interop.clang.visitChildren(ClangSharp.Interop.CXCursor, ClangSharp.Interop.CXChildVisitResult (ClangSharp.Interop.CXCursor, ClangSharp.Interop.CXCursor, Void*), Void*)
   at ClangSharp.Interop.CXCursor.VisitChildren(ClangSharp.Interop.CXChildVisitResult (ClangSharp.Interop.CXCursor, ClangSharp.Interop.CXCursor, Void*), ClangSharp.Interop.CXClientData)
   at ClangSharp.Interop.CXCursor.VisitChildren(ClangSharp.Interop.CXCursorVisitor, ClangSharp.Interop.CXClientData)
   at CppAst.CppParser.ParseInternal(System.Collections.Generic.List`1<CppFileOrString>, CppAst.CppParserOptions)
   at CppAst.CppParser.Parse(System.String, CppAst.CppParserOptions, System.String)
   at RegAutomation.DB.Load()
   at RegAutomation.Program.ProcessProject(System.String)
   at RegAutomation.Program.Main(System.String[])
xoofx commented 11 months ago

ClangSharp 16.0.0

Seems that you are overriding ClangSharp. The latest version used officially by CppAst is 15.0.x Does it happen without overriding?

haraldwer commented 11 months ago

We did try to not override anything and using version 15.0.0 for the runtimes, with the same result. I will try this again later just to verify.

haraldwer commented 11 months ago

I have verified that I do also get stack overflow with the following configurations: ClangSharp (15.0.2) ClangSharp.Interop (15.0.2) libClangSharp (15.0.0) libClangSharp.runtime.win-x64 15.0.0 libclang.runtime.win-x64 15.0.0 and ClangSharp (15.0.2) ClangSharp.Interop (15.0.2) libClangSharp (15.0.0) libClangSharp.runtime.win-x64 16.0.6 libclang.runtime.win-x64 16.0.6 Parentheses indicates that its implicitly installed by nuget.

xoofx commented 11 months ago

The error occurs even when parsing the provided example code.

I'm confused, there is a test testing exactly this and it has been passing, so not sure how you can get a stackoverflow exception 🤔

haraldwer commented 11 months ago

I created a new solution with the exact same nuget setup as above, only including the code from the test you linked. The test work as expected.

Steps:

  1. New solution -> .Net Core Console Application using .net framework 7.0
  2. Nuget Add CppAst.NET Add libclang.runtime.win-x64 version 15.0.0 Add libClangSharp.runtime.win-x64 version 15.0.0
  3. Copy your test code into main()
  4. Compile and run

I reopened my original solution, checked that the nuget versions were all identical to the test. My code no longer causes stack overflow. I am sorry for the confusion, I do not know why this happened.

I do however get another compile issue that I do not get with CppAst 0.10.0 with some MSVC includes, but that's probably my own fault. Thank you for your help!