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?
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.
There's an M3 modifiers
relation which might have the info you're looking for:
o
operator with the qualified names of your methods to see which modifiers are declared on which method However, that relation must be extracted of course. Perhaps that still needs to be added to ClaiR?
I have some code the looks like this: MyClass { public: void myFunc1(); private: void myFunc2(); public: void myFunc3(); void myFunc4();
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)?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.