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)
| 3983 | |
| 3984 | |
| 3985 | def _IsType(clean_lines, nesting_state, expr): |
| 3986 | """Check if expression looks like a type name, returns true if so. |
| 3987 | |
| 3988 | Args: |
| 3989 | clean_lines: A CleansedLines instance containing the file. |
| 3990 | nesting_state: A NestingState instance which maintains information about |
| 3991 | the current stack of nested blocks being parsed. |
| 3992 | expr: The expression to check. |
| 3993 | Returns: |
| 3994 | True, if token looks like a type. |
| 3995 | """ |
| 3996 | # Keep only the last token in the expression |
| 3997 | last_word = Match(r'^.*(\b\S+)$', expr) |
| 3998 | if last_word: |
| 3999 | token = last_word.group(1) |
| 4000 | else: |
| 4001 | token = expr |
| 4002 | |
| 4003 | # Match native types and stdint types |
| 4004 | if _TYPES.match(token): |
| 4005 | return True |
| 4006 | |
| 4007 | # Try a bit harder to match templated types. Walk up the nesting |
| 4008 | # stack until we find something that resembles a typename |
| 4009 | # declaration for what we are looking for. |
| 4010 | typename_pattern = (r'\b(?:typename|class|struct)\s+' + re.escape(token) + |
| 4011 | r'\b') |
| 4012 | block_index = len(nesting_state.stack) - 1 |
| 4013 | while block_index >= 0: |
| 4014 | if isinstance(nesting_state.stack[block_index], _NamespaceInfo): |
| 4015 | return False |
| 4016 | |
| 4017 | # Found where the opening brace is. We want to scan from this |
| 4018 | # line up to the beginning of the function, minus a few lines. |
| 4019 | # template <typename Type1, // stop scanning here |
| 4020 | # ...> |
| 4021 | # class C |
| 4022 | # : public ... { // start scanning here |
| 4023 | last_line = nesting_state.stack[block_index].starting_linenum |
| 4024 | |
| 4025 | next_block_start = 0 |
| 4026 | if block_index > 0: |
| 4027 | next_block_start = nesting_state.stack[block_index - 1].starting_linenum |
| 4028 | first_line = last_line |
| 4029 | while first_line >= next_block_start: |
| 4030 | if clean_lines.elided[first_line].find('template') >= 0: |
| 4031 | break |
| 4032 | first_line -= 1 |
| 4033 | if first_line < next_block_start: |
| 4034 | # Didn't find any "template" keyword before reaching the next block, |
| 4035 | # there are probably no template things to check for this block |
| 4036 | block_index -= 1 |
| 4037 | continue |
| 4038 | |
| 4039 | # Look for typename in the specified range |
| 4040 | for i in xrange(first_line, last_line + 1, 1): |
| 4041 | if Search(typename_pattern, clean_lines.elided[i]): |
| 4042 | return True |
no test coverage detected