Check end of namespace comments.
(self, filename, clean_lines, linenum, error)
| 3174 | self.check_namespace_indentation = True |
| 3175 | |
| 3176 | def CheckEnd(self, filename, clean_lines, linenum, error): |
| 3177 | """Check end of namespace comments.""" |
| 3178 | line = clean_lines.raw_lines[linenum] |
| 3179 | |
| 3180 | # Check how many lines is enclosed in this namespace. Don't issue |
| 3181 | # warning for missing namespace comments if there aren't enough |
| 3182 | # lines. However, do apply checks if there is already an end of |
| 3183 | # namespace comment and it's incorrect. |
| 3184 | # |
| 3185 | # TODO(google): We always want to check end of namespace comments |
| 3186 | # if a namespace is large, but sometimes we also want to apply the |
| 3187 | # check if a short namespace contained nontrivial things (something |
| 3188 | # other than forward declarations). There is currently no logic on |
| 3189 | # deciding what these nontrivial things are, so this check is |
| 3190 | # triggered by namespace size only, which works most of the time. |
| 3191 | if linenum - self.starting_linenum < 10 and not re.match( |
| 3192 | r"^\s*};*\s*(//|/\*).*\bnamespace\b", line |
| 3193 | ): |
| 3194 | return |
| 3195 | |
| 3196 | # Look for matching comment at end of namespace. |
| 3197 | # |
| 3198 | # Note that we accept C style "/* */" comments for terminating |
| 3199 | # namespaces, so that code that terminate namespaces inside |
| 3200 | # preprocessor macros can be cpplint clean. |
| 3201 | # |
| 3202 | # We also accept stuff like "// end of namespace <name>." with the |
| 3203 | # period at the end. |
| 3204 | # |
| 3205 | # Besides these, we don't accept anything else, otherwise we might |
| 3206 | # get false negatives when existing comment is a substring of the |
| 3207 | # expected namespace. |
| 3208 | if self.name: |
| 3209 | # Named namespace |
| 3210 | if not re.match( |
| 3211 | (r"^\s*};*\s*(//|/\*).*\bnamespace\s+" + re.escape(self.name) + r"[\*/\.\\\s]*$"), |
| 3212 | line, |
| 3213 | ): |
| 3214 | error( |
| 3215 | filename, |
| 3216 | linenum, |
| 3217 | "readability/namespace", |
| 3218 | 5, |
| 3219 | f'Namespace should be terminated with "// namespace {self.name}"', |
| 3220 | ) |
| 3221 | else: |
| 3222 | # Anonymous namespace |
| 3223 | if not re.match(r"^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$", line): |
| 3224 | # If "// namespace anonymous" or "// anonymous namespace (more text)", |
| 3225 | # mention "// anonymous namespace" as an acceptable form |
| 3226 | if re.match(r"^\s*}.*\b(namespace anonymous|anonymous namespace)\b", line): |
| 3227 | error( |
| 3228 | filename, |
| 3229 | linenum, |
| 3230 | "readability/namespace", |
| 3231 | 5, |
| 3232 | 'Anonymous namespace should be terminated with "// namespace"' |
| 3233 | ' or "// anonymous namespace"', |