Stores information about a namespace.
| 3106 | |
| 3107 | |
| 3108 | class _NamespaceInfo(_BlockInfo): |
| 3109 | """Stores information about a namespace.""" |
| 3110 | |
| 3111 | def __init__(self, name, linenum): |
| 3112 | _BlockInfo.__init__(self, linenum, False) |
| 3113 | self.name = name or "" |
| 3114 | self.check_namespace_indentation = True |
| 3115 | |
| 3116 | def CheckEnd(self, filename, clean_lines, linenum, error): |
| 3117 | """Check end of namespace comments.""" |
| 3118 | line = clean_lines.raw_lines[linenum] |
| 3119 | |
| 3120 | # Check how many lines is enclosed in this namespace. Don't issue |
| 3121 | # warning for missing namespace comments if there aren't enough |
| 3122 | # lines. However, do apply checks if there is already an end of |
| 3123 | # namespace comment and it's incorrect. |
| 3124 | # |
| 3125 | # TODO(google): We always want to check end of namespace comments |
| 3126 | # if a namespace is large, but sometimes we also want to apply the |
| 3127 | # check if a short namespace contained nontrivial things (something |
| 3128 | # other than forward declarations). There is currently no logic on |
| 3129 | # deciding what these nontrivial things are, so this check is |
| 3130 | # triggered by namespace size only, which works most of the time. |
| 3131 | if linenum - self.starting_linenum < 10 and not re.match( |
| 3132 | r"^\s*};*\s*(//|/\*).*\bnamespace\b", line |
| 3133 | ): |
| 3134 | return |
| 3135 | |
| 3136 | # Look for matching comment at end of namespace. |
| 3137 | # |
| 3138 | # Note that we accept C style "/* */" comments for terminating |
| 3139 | # namespaces, so that code that terminate namespaces inside |
| 3140 | # preprocessor macros can be cpplint clean. |
| 3141 | # |
| 3142 | # We also accept stuff like "// end of namespace <name>." with the |
| 3143 | # period at the end. |
| 3144 | # |
| 3145 | # Besides these, we don't accept anything else, otherwise we might |
| 3146 | # get false negatives when existing comment is a substring of the |
| 3147 | # expected namespace. |
| 3148 | if self.name: |
| 3149 | # Named namespace |
| 3150 | if not re.match( |
| 3151 | (r"^\s*};*\s*(//|/\*).*\bnamespace\s+" + re.escape(self.name) + r"[\*/\.\\\s]*$"), |
| 3152 | line, |
| 3153 | ): |
| 3154 | error( |
| 3155 | filename, |
| 3156 | linenum, |
| 3157 | "readability/namespace", |
| 3158 | 5, |
| 3159 | f'Namespace should be terminated with "// namespace {self.name}"', |
| 3160 | ) |
| 3161 | else: |
| 3162 | # Anonymous namespace |
| 3163 | if not re.match(r"^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$", line): |
| 3164 | # If "// namespace anonymous" or "// anonymous namespace (more text)", |
| 3165 | # mention "// anonymous namespace" as an acceptable form |