Skip to content

Provide option for parsing function bodies #95

Description

@IsaacMarovitz

Hi there, I'm trying to use CppAst.NET to parse metal-cpp header files, which are a wrapper of the Objective-C API. The underlying Objective-C functions being called are contained within a macro within the function body.

Screenshot 2024-03-30 at 21 21 35

To generate valid C# code, I need to be able to access the string used to generate the selector inside the function bodies, however currently, function body parsing is disabled.

Could an option be added to enable function body parsing? I'm happy to PR it myself, however, I don't know what other modifications would be required.

Activity

  1. xoofx commented on Mar 31, 2024

    @xoofx
    Owner

    Could an option be added to enable function body parsing? I'm happy to PR it myself, however, I don't know what other modifications would be required.

    Adding such an option would require also to parse the full content, handle all the syntax elements, create all the associated AST classes. Unfortunately, I don't want to support this as it would require a significant amount of work - and maintenance.
    This library is meant for parsing interfaces, signatures of methods and so mostly header files.

  2. IsaacMarovitz commented on Mar 31, 2024

    @IsaacMarovitz
    Author

    Adding such an option would require also to parse the full content, handle all the syntax elements, create all the associated AST classes. Unfortunately, I don't want to support this as it would require a significant amount of work - and maintenance. This library is meant for parsing interfaces, signatures of methods and so mostly header files.

    If I'm not mistaken, if function body parsing was enabled, could I get this information from the generic CppToken? I don't need full function parsing; I just need to be able to get the _MTL_PRIVATE_SEL contained within.

  3. xoofx commented on Mar 31, 2024

    @xoofx
    Owner

    If I'm not mistaken, if function body parsing was enabled, could I get this information from the generic CppToken? I don't need full function parsing; I just need to be able to get the _MTL_PRIVATE_SEL contained within.

    I would believe that the visitor would still go through lots of nodes - and generate several warnings or errors along the way, but maybe be that could be fine, you will have to try.

  4. IsaacMarovitz commented on Mar 31, 2024

    @IsaacMarovitz
    Author

    I conducted an inital test with the following code:

    [Test]
            public void TestComplex()
            {
                ParseAssert(@"
    #include <objc/runtime.h>
    
    #define _NS_PRIVATE_SEL(accessor) (Private::Selector::s_k##accessor)
    #define _NS_PRIVATE_DEF_SEL(accessor, symbol) extern SEL s_k##accessor
    
    namespace Private
    {
        namespace Selector
        {
            _NS_PRIVATE_DEF_SEL(test,
                ""test"");
        }
    }
    
    SEL method()
    {
        return _NS_PRIVATE_SEL(test);
    }
    ",
                    compilation =>
                    {
                        Assert.False(compilation.HasErrors);
                    },
                    new CppParserOptions
                    {
                        SystemIncludeFolders =
                        {
                            "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1",
                            "/Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include"
                        },
                        AdditionalArguments =
                        {
                            "-isysroot",
                            "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk",
                        },
                        TargetCpu = CppTargetCpu.ARM64,
                        TargetSystem = "darwin",
                        TargetVendor = "apple",
                        ParseSystemIncludes = true,
                        ParseMacros = true
                    });
            }

    It seemed to run without errors, with the only problem being that the method's contents are not available in the AST yet. But I think it might be possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions