Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide option for parsing function bodies #95

Open
IsaacMarovitz opened this issue Mar 31, 2024 · 4 comments
Open

Provide option for parsing function bodies #95

IsaacMarovitz opened this issue Mar 31, 2024 · 4 comments
Labels
question Further information is requested

Comments

@IsaacMarovitz
Copy link

IsaacMarovitz commented Mar 31, 2024

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.

@xoofx xoofx added the question Further information is requested label Mar 31, 2024
@xoofx
Copy link
Owner

xoofx commented Mar 31, 2024

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.

@IsaacMarovitz
Copy link
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.

@xoofx
Copy link
Owner

xoofx commented Mar 31, 2024

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.

@IsaacMarovitz
Copy link
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
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants