Search code examples
javacommentseclipse-cdt

How to link a IASTComment to a IASTDeclaration in CDT ASTParser


I'm using CDT ASTParser to parse some content of C/C++ source file. Example:

//Docs for function min
int min(int a[], int n) {
    //Comment here
}

int min1(){}

/*
Docs for function min2
*/
int min2(){}

By using ASTVisitor, I can parse the list of function definition in the code, but I don't know how to "link" the IASTComment above the definition code:

void handle(IASTTranslationUnit unit){
        unit.accept(new ASTVisitor() {
            { shouldVisitDeclarations = true; }

            @Override
            public int visit(IASTDeclaration declaration) {

                if (declaration instanceof IASTFunctionDefinition) {
                    IASTFunctionDefinition fnDefine = 
                            (IASTFunctionDefinition) declaration;
                    IASTFunctionDeclarator fnDeclare = fnDefine.getDeclarator();

                    System.out.printf("Function: %s, type: %s, comment: %s\n",
                            fnDeclare.getName().getRawSignature(),
                            fnDefine.getDeclSpecifier().getRawSignature(),
                            "Somehow get the comment above function define???"
                            );
                }
                return PROCESS_SKIP;
            }

            @Override
            public int visit(IASTComment comment) {
                System.out.println("Comment: " + comment.getRawSignature());
                return PROCESS_CONTINUE;
            }

        });

        for (IASTComment cmt: unit.getComments())
            System.out.println("Comment: " + cmt.getRawSignature());
    }

The visit(IASTComment comment) in ASTVisitor class is deprecated, and IASTTranslationUnit.getComments() method just return the list of comment in whole source file, no structure here.
So, how to get the IASTComment object that is link to the function definition (if have)?


Solution

  • You can use ASTCommenter.getCommentedNodeMap(IASTTranslationUnit) in the package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler. The Method returns a NodeCommentMap which maps comments to nodes in the AST. There are three types of comments that can be assigned to a node. In the example below the method declartion is the node:

    //this is a leading comment
    void method() //this is a trailing comment
    //this is a freestanding comment
    {
    ...
    

    NodeCommentMap.getOffsetIncludingComments(IASTNode)returns the offset of a node including its assigned leading comments. NodeCommentMap.getEndOffsetIncludingComments(IASTNode) returns the end offset of a node including its assigned trailing comments. Assigned freestanding comments have to be handled by your self. You can get the freestanding comments with NodeCommentMap.getFreestandingCommentsForNode(IASTNode). At least I couldn`t find any default approach in the cdt packages.

    If you want to learn more it is advisable to read the documentation in the follwoing classes:

    • org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter
    • org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
    • org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommenter

    Note: Most of this packages/classes are internal.