Return the function's name from a function_definition node, or "". Walks declarator -> identifier / qualified_identifier / field_identifier. For nested `qualified_identifier` (e.g. `A::B::C::foo`), the rightmost segment is the actual function name.
(func_node, src: bytes)
| 2174 | |
| 2175 | |
| 2176 | def _function_name(func_node, src: bytes) -> str: |
| 2177 | """Return the function's name from a function_definition node, or "". |
| 2178 | Walks declarator -> identifier / qualified_identifier / field_identifier. |
| 2179 | For nested `qualified_identifier` (e.g. `A::B::C::foo`), the rightmost |
| 2180 | segment is the actual function name.""" |
| 2181 | decl = func_node.child_by_field_name("declarator") |
| 2182 | while decl is not None and decl.type == "function_declarator": |
| 2183 | decl = decl.child_by_field_name("declarator") |
| 2184 | if decl is None: |
| 2185 | return "" |
| 2186 | |
| 2187 | # Walk to the rightmost segment of any qualified-identifier chain. |
| 2188 | while decl is not None and decl.type == "qualified_identifier": |
| 2189 | nested = None |
| 2190 | for child in decl.children: |
| 2191 | if child.type in ( |
| 2192 | "identifier", |
| 2193 | "field_identifier", |
| 2194 | "destructor_name", |
| 2195 | "qualified_identifier", |
| 2196 | "operator_name", |
| 2197 | "template_function", |
| 2198 | ): |
| 2199 | nested = child |
| 2200 | if nested is None: |
| 2201 | return "" |
| 2202 | decl = nested |
| 2203 | |
| 2204 | if decl.type in ("identifier", "field_identifier", "destructor_name"): |
| 2205 | return _node_text(decl, src) |
| 2206 | if decl.type == "operator_name": |
| 2207 | return _node_text(decl, src) |
| 2208 | if decl.type == "template_function": |
| 2209 | name = decl.child_by_field_name("name") |
| 2210 | if name is not None: |
| 2211 | return _node_text(name, src) |
| 2212 | return "" |
| 2213 | |
| 2214 | |
| 2215 | def _type_name(node, src: bytes) -> str: |
no test coverage detected