Stores information about a class.
| 3017 | |
| 3018 | |
| 3019 | class _ClassInfo(_BlockInfo): |
| 3020 | """Stores information about a class.""" |
| 3021 | |
| 3022 | def __init__(self, name, class_or_struct, clean_lines, linenum): |
| 3023 | _BlockInfo.__init__(self, linenum, False) |
| 3024 | self.name = name |
| 3025 | self.is_derived = False |
| 3026 | self.check_namespace_indentation = True |
| 3027 | if class_or_struct == "struct": |
| 3028 | self.access = "public" |
| 3029 | self.is_struct = True |
| 3030 | else: |
| 3031 | self.access = "private" |
| 3032 | self.is_struct = False |
| 3033 | |
| 3034 | # Remember initial indentation level for this class. Using raw_lines here |
| 3035 | # instead of elided to account for leading comments. |
| 3036 | self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum]) |
| 3037 | |
| 3038 | # Try to find the end of the class. This will be confused by things like: |
| 3039 | # class A { |
| 3040 | # } *x = { ... |
| 3041 | # |
| 3042 | # But it's still good enough for CheckSectionSpacing. |
| 3043 | self.last_line = 0 |
| 3044 | depth = 0 |
| 3045 | for i in range(linenum, clean_lines.NumLines()): |
| 3046 | line = clean_lines.elided[i] |
| 3047 | depth += line.count("{") - line.count("}") |
| 3048 | if not depth: |
| 3049 | self.last_line = i |
| 3050 | break |
| 3051 | |
| 3052 | def CheckBegin(self, filename, clean_lines, linenum, error): |
| 3053 | # Look for a bare ':' |
| 3054 | if re.search("(^|[^:]):($|[^:])", clean_lines.elided[linenum]): |
| 3055 | self.is_derived = True |
| 3056 | |
| 3057 | def CheckEnd(self, filename, clean_lines, linenum, error): |
| 3058 | # If there is a DISALLOW macro, it should appear near the end of |
| 3059 | # the class. |
| 3060 | seen_last_thing_in_class = False |
| 3061 | for i in range(linenum - 1, self.starting_linenum, -1): |
| 3062 | match = re.search( |
| 3063 | r"\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(" |
| 3064 | + self.name |
| 3065 | + r"\)", |
| 3066 | clean_lines.elided[i], |
| 3067 | ) |
| 3068 | if match: |
| 3069 | if seen_last_thing_in_class: |
| 3070 | error( |
| 3071 | filename, |
| 3072 | i, |
| 3073 | "readability/constructors", |
| 3074 | 3, |
| 3075 | match.group(1) + " should be the last thing in the class", |
| 3076 | ) |