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: The expre
(clean_lines, nesting_state, expr)
| 3417 | |
| 3418 | |
| 3419 | def _IsType(clean_lines, nesting_state, expr): |
| 3420 | """Check if expression looks like a type name, returns true if so. |
| 3421 | |
| 3422 | Args: |
| 3423 | clean_lines: A CleansedLines instance containing the file. |
| 3424 | nesting_state: A NestingState instance which maintains information about |
| 3425 | the current stack of nested blocks being parsed. |
| 3426 | expr: The expression to check. |
| 3427 | Returns: |
| 3428 | True, if token looks like a type. |
| 3429 | """ |
| 3430 | # Keep only the last token in the expression |
| 3431 | last_word = Match(r'^.*(\b\S+)$', expr) |
| 3432 | if last_word: |
| 3433 | token = last_word.group(1) |
| 3434 | else: |
| 3435 | token = expr |
| 3436 | |
| 3437 | # Match native types and stdint types |
| 3438 | if _TYPES.match(token): |
| 3439 | return True |
| 3440 | |
| 3441 | # Try a bit harder to match templated types. Walk up the nesting |
| 3442 | # stack until we find something that resembles a typename |
| 3443 | # declaration for what we are looking for. |
| 3444 | typename_pattern = (r'\b(?:typename|class|struct)\s+' + re.escape(token) + |
| 3445 | r'\b') |
| 3446 | block_index = len(nesting_state.stack) - 1 |
| 3447 | while block_index >= 0: |
| 3448 | if isinstance(nesting_state.stack[block_index], _NamespaceInfo): |
| 3449 | return False |
| 3450 | |
| 3451 | # Found where the opening brace is. We want to scan from this |
| 3452 | # line up to the beginning of the function, minus a few lines. |
| 3453 | # template <typename Type1, // stop scanning here |
| 3454 | # ...> |
| 3455 | # class C |
| 3456 | # : public ... { // start scanning here |
| 3457 | last_line = nesting_state.stack[block_index].starting_linenum |
| 3458 | |
| 3459 | next_block_start = 0 |
| 3460 | if block_index > 0: |
| 3461 | next_block_start = nesting_state.stack[block_index - 1].starting_linenum |
| 3462 | first_line = last_line |
| 3463 | while first_line >= next_block_start: |
| 3464 | if clean_lines.elided[first_line].find('template') >= 0: |
| 3465 | break |
| 3466 | first_line -= 1 |
| 3467 | if first_line < next_block_start: |
| 3468 | # Didn't find any "template" keyword before reaching the next block, |
| 3469 | # there are probably no template things to check for this block |
| 3470 | block_index -= 1 |
| 3471 | continue |
| 3472 | |
| 3473 | # Look for typename in the specified range |
| 3474 | for i in range(first_line, last_line + 1, 1): |
| 3475 | if Search(typename_pattern, clean_lines.elided[i]): |
| 3476 | return True |
no test coverage detected