0

I am parsing an C++ header file using ClaiR and want to get a list of the public functions.

visit(ast) {
    case \class(_, name(n), _, decs): {
        println("class name: <n>");
        isPublic = true;
        for (dec <- decs) {
            switch(dec) {
                case \visibilityLabel(\public()): {
                    println("Public functions");
                    isPublic = true;
                }
                case \visibilityLabel(\protected()): {
                    println("Protected functions");
                    isPublic = false;
                }
                case \visibilityLabel(\private()): {
                    println("Private functions");
                    isPublic = false;
                }
                case \simpleDeclaration(_, [\functionDeclarator([*_], [*_], name(na), [*_], [*_])]): {
                    if (isPublic) {
                        println("public function: <na>");
                    }
                }
            }
        }
    }
}

The above code works. But is there a better (smaller) way of acquiring the public functions?

3 Answers 3

3

In C++, the public/protected/private access modifiers aren't proper "modifiers" on declarations; instead, all member declarations following an access modifier (up to a possible next access modifier) have the declared visiblity (in your example, the second public: also makes myFunc4 public). It would be straightforward to implement an AST traversal to obtain members' visiblity information and add it to a new M3 table, though. Your suggestion of public void myFunc5(); is invalid syntax.

The ProblemType in the decl indicates that the first argument of the myFunc method is unresolved (likely due to a missing import). The toString of this ProblemType in the type information should not be there, though, that is a bug.

Sign up to request clarification or add additional context in comments.

3 Comments

Aha, this response is more of an answer to your answer below. Something along the lines of the traversal in your question seems reasonable.
If it is inline with M3 format/contents, it would be nice to have the visibility info in it.
I added a table to ClaiR's M3 containing the visibility information.
1

There's an M3 modifiers relation which might have the info you're looking for:

However, that relation must be extracted of course. Perhaps that still needs to be added to ClaiR?

Comments

0

I have some code the looks like this: MyClass { public: void myFunc1(); private: void myFunc2(); public: void myFunc3(); void myFunc4();

m3.modifiers does not provide public/private information. I guess (have not tried), it will work for public void myFunc5();

I also see some strange errors. <|cpp+method:///MyClass/myFunc(org.eclipse.cdt.internal.core.dom.parser.ProblemType@38270bb,unsigned.int,unsigned.int)|,virtual()>, Is this for a type it cannot resolve (include not provided to parser)?

1 Comment

Ask Rodin to have a look; but yes that looks like a partially unresolved type; a resolved method of an unresolved class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.