cppast icon indicating copy to clipboard operation
cppast copied to clipboard

Function parameters are not visited

Open sztomi opened this issue 8 years ago • 5 comments

I think this is because member_function_t is not treated as a container in detail::visit, and simply the callback is called instead of recursive visitation. I'd assume standardese would need to parse function paramters, wouldn't it?

  • cppast version: 2be20f60a
  • parser: libclang_parser
  • clang version: 4.0

Explanation of the error.

Input:

    auto code = R"(
          class foo {
            void f(int a, int b){};
          };
    )";

    cpp_entity_index idx;
    auto file = parse(idx, "cpp_class.cpp", code);
    unsigned filtered_count = 0;
    auto visitor_callback = [&](const cpp_entity &e, cppast::visitor_info info) {
      if (info.event == cppast::visitor_info::container_entity_exit)
        return true;
      std::cout << e.name() << " : " << cppast::to_string(e.kind()) << "\n";
    };

    cppast::visit(*file, visitor_callback);

Output:

cpp_class.cpp : file
foo : class
f : member function

I would expect the function parameters a and b to be visible in the output.

sztomi avatar May 14 '17 11:05 sztomi

Are they visited if you visit a function? If not, that's by design, template parameters aren't visited as well, you'd have to iterate over . parameters () then.

Feel free to question that design decision though.

foonathan avatar May 14 '17 21:05 foonathan

So do you mean I should cast the entity to cpp_member_function and it has a paramaters() member? That's workable for me. Not sure if I would criticize the design, though I'm not sure if this scales well for when function bodies will be parsed.

sztomi avatar May 15 '17 11:05 sztomi

Yes, cast​ and you'll have everything.

The general problem is: how do I handle entities that have more than one kind of children? The best example is a class, it has base classes and members, should both be visited?

foonathan avatar May 15 '17 13:05 foonathan

It does feel natural to have everything in the tree, and allows uniform appending/deleting of source "items" when source rewriting is implemented. I wonder libclang or libtooling do in this case?

sztomi avatar May 15 '17 13:05 sztomi

It depends on the entity, they sometimes visit, sometimes don't. I'll see what I can come up with.

foonathan avatar May 25 '17 18:05 foonathan