mono / CppSharp

Tools and libraries to glue C/C++ APIs to high-level languages
MIT License
3.14k stars 517 forks source link

SymbolsCodeGenerator: System.NullReferenceException #1064

Open nikoltchovskaa opened 6 years ago

nikoltchovskaa commented 6 years ago
Brief Description

Hello, I'm trying to generate C# bindings for a project made with grpc (https://grpc.io/), using all headers used by the grpc interface. I am using CppSharp for the first time and I am getting the following exception:

System.NullReferenceException: Object reference not set to an instance of an object
  at CppSharp.AST.QualifiedType.Visit[T] (CppSharp.AST.ITypeVisitor`1[T] visitor) [0x0000e] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.Passes.SymbolsCodeGenerator.<GetFunctionName>m__6 (CppSharp.AST.TemplateArgument a) [0x00000] in <ee554a8a50744b808e95927caba493c0>:0
  at System.Linq.Enumerable+SelectListIterator`2[TSource,TResult].MoveNext () [0x00048] in <3fb92ec709f44a579da6a23087df403d>:0
  at System.String.Join (System.String separator, System.Collections.Generic.IEnumerable`1[T] values) [0x0006b] in <4fdc5ed61a074cafb49fa42deb20d521>:0
  at CppSharp.Passes.SymbolsCodeGenerator.GetFunctionName (CppSharp.AST.Function function, System.String namespace) [0x00073] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Passes.SymbolsCodeGenerator.TakeFunctionAddress (CppSharp.AST.Function function) [0x00073] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Passes.SymbolsCodeGenerator.VisitFunctionDecl (CppSharp.AST.Function function) [0x00001] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Passes.SymbolsCodeGenerator.VisitMethodDecl (CppSharp.AST.Method method) [0x00074] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.AST.Method.Visit[T] (CppSharp.AST.IDeclVisitor`1[T] visitor) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.Passes.GenerateSymbolsPass.VisitFunctionDecl (CppSharp.AST.Function function) [0x000d5] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.AST.AstVisitor.VisitMethodDecl (CppSharp.AST.Method method) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitClassDecl (CppSharp.AST.Class class) [0x001d1] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.Passes.GenerateSymbolsPass.VisitClassDecl (CppSharp.AST.Class class) [0x00001] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.AST.Class.Visit[T] (CppSharp.AST.IDeclVisitor`1[T] visitor) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitDeclarationContext (CppSharp.AST.DeclarationContext context) [0x00054] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitNamespace (CppSharp.AST.Namespace namespace) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.Namespace.Visit[T] (CppSharp.AST.IDeclVisitor`1[T] visitor) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitDeclarationContext (CppSharp.AST.DeclarationContext context) [0x0029d] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitNamespace (CppSharp.AST.Namespace namespace) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.Namespace.Visit[T] (CppSharp.AST.IDeclVisitor`1[T] visitor) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitDeclarationContext (CppSharp.AST.DeclarationContext context) [0x0029d] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitNamespace (CppSharp.AST.Namespace namespace) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.Namespace.Visit[T] (CppSharp.AST.IDeclVisitor`1[T] visitor) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitDeclarationContext (CppSharp.AST.DeclarationContext context) [0x0029d] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.Passes.TranslationUnitPass.VisitTranslationUnit (CppSharp.AST.TranslationUnit unit) [0x00034] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Passes.TranslationUnitPass.VisitASTContext (CppSharp.AST.ASTContext context) [0x0001b] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Passes.GenerateSymbolsPass.VisitASTContext (CppSharp.AST.ASTContext context) [0x00001] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Generators.BindingContext.<RunPasses>m__0 (CppSharp.Passes.TranslationUnitPass pass) [0x00029] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.PassBuilder`1[T].RunPasses (System.Action`1[T] action) [0x0001b] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Generators.BindingContext.RunPasses () [0x00007] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Driver.ProcessCode () [0x00007] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.ConsoleDriver.Run (CppSharp.ILibrary library) [0x00128] in <ee554a8a50744b808e95927caba493c0>:0
  at Wrapper.WrapperClass.Main (System.String[] args) [0x00006] in <28f4e98ae36f41a297e59a8b423f1898>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at CppSharp.AST.QualifiedType.Visit[T] (CppSharp.AST.ITypeVisitor`1[T] visitor) [0x0000e] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.Passes.SymbolsCodeGenerator.<GetFunctionName>m__6 (CppSharp.AST.TemplateArgument a) [0x00000] in <ee554a8a50744b808e95927caba493c0>:0
  at System.Linq.Enumerable+SelectListIterator`2[TSource,TResult].MoveNext () [0x00048] in <3fb92ec709f44a579da6a23087df403d>:0
  at System.String.Join (System.String separator, System.Collections.Generic.IEnumerable`1[T] values) [0x0006b] in <4fdc5ed61a074cafb49fa42deb20d521>:0
  at CppSharp.Passes.SymbolsCodeGenerator.GetFunctionName (CppSharp.AST.Function function, System.String namespace) [0x00073] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Passes.SymbolsCodeGenerator.TakeFunctionAddress (CppSharp.AST.Function function) [0x00073] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Passes.SymbolsCodeGenerator.VisitFunctionDecl (CppSharp.AST.Function function) [0x00001] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Passes.SymbolsCodeGenerator.VisitMethodDecl (CppSharp.AST.Method method) [0x00074] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.AST.Method.Visit[T] (CppSharp.AST.IDeclVisitor`1[T] visitor) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.Passes.GenerateSymbolsPass.VisitFunctionDecl (CppSharp.AST.Function function) [0x000d5] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.AST.AstVisitor.VisitMethodDecl (CppSharp.AST.Method method) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitClassDecl (CppSharp.AST.Class class) [0x001d1] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.Passes.GenerateSymbolsPass.VisitClassDecl (CppSharp.AST.Class class) [0x00001] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.AST.Class.Visit[T] (CppSharp.AST.IDeclVisitor`1[T] visitor) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitDeclarationContext (CppSharp.AST.DeclarationContext context) [0x00054] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitNamespace (CppSharp.AST.Namespace namespace) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.Namespace.Visit[T] (CppSharp.AST.IDeclVisitor`1[T] visitor) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitDeclarationContext (CppSharp.AST.DeclarationContext context) [0x0029d] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitNamespace (CppSharp.AST.Namespace namespace) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.Namespace.Visit[T] (CppSharp.AST.IDeclVisitor`1[T] visitor) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitDeclarationContext (CppSharp.AST.DeclarationContext context) [0x0029d] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitNamespace (CppSharp.AST.Namespace namespace) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.Namespace.Visit[T] (CppSharp.AST.IDeclVisitor`1[T] visitor) [0x00001] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.AST.AstVisitor.VisitDeclarationContext (CppSharp.AST.DeclarationContext context) [0x0029d] in <7e1a31dac5fd4a36b487efa8f6f3a798>:0
  at CppSharp.Passes.TranslationUnitPass.VisitTranslationUnit (CppSharp.AST.TranslationUnit unit) [0x00034] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Passes.TranslationUnitPass.VisitASTContext (CppSharp.AST.ASTContext context) [0x0001b] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Passes.GenerateSymbolsPass.VisitASTContext (CppSharp.AST.ASTContext context) [0x00001] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Generators.BindingContext.<RunPasses>m__0 (CppSharp.Passes.TranslationUnitPass pass) [0x00029] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.PassBuilder`1[T].RunPasses (System.Action`1[T] action) [0x0001b] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Generators.BindingContext.RunPasses () [0x00007] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.Driver.ProcessCode () [0x00007] in <ee554a8a50744b808e95927caba493c0>:0
  at CppSharp.ConsoleDriver.Run (CppSharp.ILibrary library) [0x00128] in <ee554a8a50744b808e95927caba493c0>:0
  at Wrapper.WrapperClass.Main (System.String[] args) [0x00006] in <28f4e98ae36f41a297e59a8b423f1898>:0

OS: OS X

Used headers

I am using quite a lot of headers:

Used settings
void ILibrary.Setup(Driver driver)
        {
            var options = driver.Options;
            driver.ParserOptions.EnableRTTI = true;
            options.GeneratorKind = GeneratorKind.CSharp;
            var module = options.AddModule("OctaneGRpc_Alex");
            module.IncludeDirs.Add("/Users/maxnagy/Downloads/Archive/OctaneGRpc_Alex");
            module.IncludeDirs.Add("/usr/local/include");
            module.IncludeDirs.Add("/usr/local/include/grps++/impl/codegen");
            module.IncludeDirs.Add("/usr/local/include/grps++/impl/codegen/security");
            module.Headers.Add("/Users/maxnagy/Downloads/Archive/OctaneGRpc_Alex/include/atomicops.h");
            module.Headers.Add("/Users/maxnagy/Downloads/Archive/OctaneGRpc_Alex/include/grpc-cpp-gen/ThirdPartyServer.pb.h");
            module.Headers.Add("/Users/maxnagy/Downloads/Archive/OctaneGRpc_Alex/include/readerwriterqueue.h");
            module.Headers.Add("/Users/maxnagy/Downloads/Archive/OctaneGRpc_Alex/include/grpc-cpp-gen/ThirdPartyServer.grpc.pb.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/async_stream.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/async_unary_call.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/byte_buffer.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/call.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/call_hook.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/channel_interface.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/completion_queue.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/completion_queue_tag.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/client_context.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/client_unary_call.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/config.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/config_protobuf.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/core_codegen.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/core_codegen_interface.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/create_auth_context.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/grpc_library.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/metadata_map.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/method_handler_impl.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/proto_utils.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/rpc_method.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/rpc_service_method.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/security/auth_context.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/serialization_traits.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/server_context.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/server_interface.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/service_type.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/slice.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/status.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/status_code_enum.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/string_ref.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/stub_options.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/sync_stream.h");
            module.Headers.Add("/usr/local/include/grpc++/impl/codegen/time.h");

            module.Headers.Add("/usr/local/include/google/protobuf/stubs/common.h");
            module.Headers.Add("/usr/local/include/google/protobuf/io/coded_stream.h");
            module.Headers.Add("/usr/local/include/google/protobuf/arena.h");
            module.Headers.Add("/usr/local/include/google/protobuf/arenastring.h");
            module.Headers.Add("/usr/local/include/google/protobuf/generated_message_table_driven.h");
            module.Headers.Add("/usr/local/include/google/protobuf/generated_message_util.h");
            module.Headers.Add("/usr/local/include/google/protobuf/metadata.h");
            module.Headers.Add("/usr/local/include/google/protobuf/message.h");
            module.Headers.Add("/usr/local/include/google/protobuf/repeated_field.h");
            module.Headers.Add("/usr/local/include/google/protobuf/extension_set.h");
            module.Headers.Add("/usr/local/include/google/protobuf/unknown_field_set.h");

            module.Headers.Add("/usr/local/include/google/protobuf/stubs/port.h");
            module.Headers.Add("/usr/local/include/google/protobuf/stubs/atomicops.h");
            module.Headers.Add("/usr/local/include/google/protobuf/arena_impl.h");
            module.Headers.Add("/usr/local/include/google/protobuf/arena.h");
            module.Headers.Add("/usr/local/include/google/protobuf/stubs/logging.h");
            module.Headers.Add("/usr/local/include/google/protobuf/stubs/type_traits.h");
            module.Headers.Add("/usr/local/include/google/protobuf/stubs/casts.h");
            module.Headers.Add("/usr/local/include/google/protobuf/stubs/port.h");

        }

Target: Clang

ddobrev commented 6 years ago

@nikoltchovskaa could you please try building from source code and then running? This way you'll be able to see which declaration causes the crash.

tritao commented 6 years ago

Another option is to get us a minified repro to work with... The easiest way to do that is to create a single header file that includes all the files you want to bind. Then you run that file against clang -E and that should give you a single self-contained (no #include directives) header file that you can pass to CppSharp.

You should be able to replicate the crash at this point with this new header file. Now you can start to remove portions of the header file manually until it stops reproducing the crash. Repeat this until you have a reasonable amount of code left and send it to us.

nikoltchovskaa commented 6 years ago

@ddobrev I already build it from source code..

ddobrev commented 6 years ago

@nikoltchovskaa could you then try attaching a debugger? If not, @tritao 's advice is also useful.