Check if expression looks like a type name, returns true if so. Args: clean_lines: A CleansedLines instance containing the file. nesting_state: A NestingState instance which maintains information about the current stack of nested blocks being parsed. expr:
(clean_lines, nesting_state, expr)
| 4488 | |
| 4489 | |
| 4490 | def _IsType(clean_lines, nesting_state, expr): |
| 4491 | """Check if expression looks like a type name, returns true if so. |
| 4492 | |
| 4493 | Args: |
| 4494 | clean_lines: A CleansedLines instance containing the file. |
| 4495 | nesting_state: A NestingState instance which maintains information about |
| 4496 | the current stack of nested blocks being parsed. |
| 4497 | expr: The expression to check. |
| 4498 | Returns: |
| 4499 | True, if token looks like a type. |
| 4500 | """ |
| 4501 | # Keep only the last token in the expression |
| 4502 | if last_word := re.match(r"^.*(\b\S+)$", expr): |
| 4503 | token = last_word.group(1) |
| 4504 | else: |
| 4505 | token = expr |
| 4506 | |
| 4507 | # Match native types and stdint types |
| 4508 | if _TYPES.match(token): |
| 4509 | return True |
| 4510 | |
| 4511 | # Try a bit harder to match templated types. Walk up the nesting |
| 4512 | # stack until we find something that resembles a typename |
| 4513 | # declaration for what we are looking for. |
| 4514 | typename_pattern = r"\b(?:typename|class|struct)\s+" + re.escape(token) + r"\b" |
| 4515 | block_index = len(nesting_state.stack) - 1 |
| 4516 | while block_index >= 0: |
| 4517 | if isinstance(nesting_state.stack[block_index], _NamespaceInfo): |
| 4518 | return False |
| 4519 | |
| 4520 | # Found where the opening brace is. We want to scan from this |
| 4521 | # line up to the beginning of the function, minus a few lines. |
| 4522 | # template <typename Type1, // stop scanning here |
| 4523 | # ...> |
| 4524 | # class C |
| 4525 | # : public ... { // start scanning here |
| 4526 | last_line = nesting_state.stack[block_index].starting_linenum |
| 4527 | |
| 4528 | next_block_start = 0 |
| 4529 | if block_index > 0: |
| 4530 | next_block_start = nesting_state.stack[block_index - 1].starting_linenum |
| 4531 | first_line = last_line |
| 4532 | while first_line >= next_block_start: |
| 4533 | if clean_lines.elided[first_line].find("template") >= 0: |
| 4534 | break |
| 4535 | first_line -= 1 |
| 4536 | if first_line < next_block_start: |
| 4537 | # Didn't find any "template" keyword before reaching the next block, |
| 4538 | # there are probably no template things to check for this block |
| 4539 | block_index -= 1 |
| 4540 | continue |
| 4541 | |
| 4542 | # Look for typename in the specified range |
| 4543 | for i in range(first_line, last_line + 1, 1): |
| 4544 | if re.search(typename_pattern, clean_lines.elided[i]): |
| 4545 | return True |
| 4546 | block_index -= 1 |
| 4547 |
no outgoing calls
no test coverage detected