CppSharp icon indicating copy to clipboard operation
CppSharp copied to clipboard

Suppress Generation of Copy Constructors

Open upretyrohan opened this issue 2 years ago • 6 comments

Is it possible to supress generation of copy constructors in generated C# code for all the classes

upretyrohan avatar Mar 03 '22 11:03 upretyrohan

There is no option available for this, but you should be able to do it by writing a pass.

It should be relatively simple, override VisitMethod and check if its a copy constructor, if so ignore it.

Hopefully that does it without bad side effects on the code generator.

tritao avatar Mar 03 '22 14:03 tritao

It should be relatively simple, override VisitMethod and check if its a copy constructor, if so ignore it.

How do you recommend to override the pass in a program that references the CppSharp DLL? I see that VisitMethod is defined in DeclConverter.

tomthecarrot avatar Sep 25 '22 06:09 tomthecarrot

DeclConverter should not be used for this. The class I mentioned is TranslationUnitPass defined in Pass.cs and the method name should be VisitMethodDecl from its base AstVisitor in https://github.com/mono/CppSharp/blob/main/src/AST/ASTVisitor.cs#L473.

tritao avatar Sep 25 '22 10:09 tritao

DeclConverter should not be used for this. The class I mentioned is TranslationUnitPass defined in Pass.cs and the method name should be VisitMethodDecl from its based AstVisitor in https://github.com/mono/CppSharp/blob/main/src/AST/ASTVisitor.cs#L473.

Gotcha, makes sense. Given that TranslationUnitPass is the base class for each existing pass, how should I override TranslationUnitPass in my project when CppSharp is included as a DLL, such that the behavior of VisitMethodDecl() is changed for all passes? Or is that not necessary?

tomthecarrot avatar Sep 25 '22 18:09 tomthecarrot

DeclConverter should not be used for this. The class I mentioned is TranslationUnitPass defined in Pass.cs and the method name should be VisitMethodDecl from its based AstVisitor in https://github.com/mono/CppSharp/blob/main/src/AST/ASTVisitor.cs#L473.

Gotcha, makes sense. Given that TranslationUnitPass is the base class for each existing pass, how should I override TranslationUnitPass in my project when CppSharp is included as a DLL, such that the behavior of VisitMethodDecl() is changed for all passes? Or is that not necessary?

You should create your own class that derives from TranslationUnitPass and then either add it with driver.TranslationUnitPasses.AddPass on SetupPasses or run it from Preprocess. See for instance https://github.com/mono/CppSharp/blob/main/src/CppParser/Bootstrap/IgnoreMethodsWithParametersPass.cs#L10 and https://github.com/mono/CppSharp/blob/main/src/CppParser/Bootstrap/Bootstrap.cs#L92.

tritao avatar Sep 25 '22 19:09 tritao

You should create your own class that derives from TranslationUnitPass and then either add it with driver.TranslationUnitPasses.AddPass on SetupPasses or run it from Preprocess. See for instance https://github.com/mono/CppSharp/blob/main/src/CppParser/Bootstrap/IgnoreMethodsWithParametersPass.cs#L10 and https://github.com/mono/CppSharp/blob/main/src/CppParser/Bootstrap/Bootstrap.cs#L92.

Works well, thank you!

tomthecarrot avatar Sep 25 '22 23:09 tomthecarrot