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

Offsets are problematic in the presence of comments #336

Open
pouryafard75 opened this issue Nov 30, 2024 · 4 comments
Open

Offsets are problematic in the presence of comments #336

pouryafard75 opened this issue Nov 30, 2024 · 4 comments

Comments

@pouryafard75
Copy link

pouryafard75 commented Nov 30, 2024

Hello, while evaluating Spoon with cases from our benchmark, I encountered a recurring and serious issue:

//and test
class po3{
    //test
    void run(){
        // print("hello");
        print("yes");
    }

    void exe(){              
        run(52);
    }

    int a = 30;
    int b = 20;              
    
}

Leads to the following tree:

root [0,0]
    RootPac: unnamed package [0,0]
        Class: po3 [11,211]
            Method: run [26,103]
                RETURN_TYPE: void [37,41]
                Invocation: print [84,97]
                    TypeAccess: po3 [0,0]
                    Literal: "yes" [90,95]
            Method: exe [109,157]
                RETURN_TYPE: void [109,113]
                Invocation: run [143,151]
                    TypeAccess: po3 [0,0]
                    Literal: 52 [147,149]
            Field: a [163,174]
                VARIABLE_TYPE: int [163,166]
                Literal: 30 [171,173]
            Field: b [179,190]
                VARIABLE_TYPE: int [179,182]
                Literal: 20 [187,189]

The offset for Method: run [26,103] is problematic due to the presence of the comment. Specifically, the non-JavaDoc comment is unnecessarily affecting the method's offset. Since this is not a JavaDoc comment, it shouldn't influence the method's boundaries.

Notably, GumTree's JDT visitor behaves correctly, as seen here where the comment does not impact the method's offset:

CompilationUnit [0,211]
    LineComment: //and test [0,10]
    TypeDeclaration [11,211]
        TYPE_DECLARATION_KIND: class [11,16]
        SimpleName: po3 [17,20]
        LineComment: //test [26,32]
        MethodDeclaration [37,103]
            PrimitiveType: void [37,41]

I rely entirely on offsets to evaluate tools, and this minor discrepancy is causing significant conflicts. It would be very helpful to have a workaround or a fix for this issue so that comments like these do not affect method offsets.

@pouryafard75 pouryafard75 changed the title Offsets Are Problematic in the presence of Comments Offsets are problematic in the presence of comments Nov 30, 2024
@pouryafard75
Copy link
Author

Also there are some subtrees, with [0,0] as offsets. Does it mean something?

@monperrus
Copy link
Contributor

thanks for the bug report.

This probably comes from the advanced algorithm in Spoon to attach comments to elements. It has gone over many iterations. indeed it may be argued that the comment is semantically part of the method. If we change for your case, it may break somebody else's case.

are comments relevant in your experiment?

one option would be to diff without comment.

@pouryafard75
Copy link
Author

pouryafard75 commented Nov 30, 2024

@monperrus
Thanks for the answer,
Actually, I'm not concerned about validating the mappings for the comments. But we care a lot about program elements including methods, and now the offsets are different than what I expect to get.

Does Spoon have a mode that disables the parsing for the comments? This could help me get the correct offsets.
Based on my experiments, this is causing too many issues regarding the method declaration, so it is quite prevalent.

@pouryafard75
Copy link
Author

pouryafard75 commented Dec 2, 2024

Because I am actually running it as
new AstComparator().getCtPackage(resource)
which by default has the comments disabled but the offsets are still getting affected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants