Check end of namespace comments.
(self, filename, clean_lines, linenum, error)
| 2242 | self.check_namespace_indentation = True |
| 2243 | |
| 2244 | def CheckEnd(self, filename, clean_lines, linenum, error): |
| 2245 | """Check end of namespace comments.""" |
| 2246 | line = clean_lines.raw_lines[linenum] |
| 2247 | |
| 2248 | # Check how many lines is enclosed in this namespace. Don't issue |
| 2249 | # warning for missing namespace comments if there aren't enough |
| 2250 | # lines. However, do apply checks if there is already an end of |
| 2251 | # namespace comment and it's incorrect. |
| 2252 | # |
| 2253 | # TODO(unknown): We always want to check end of namespace comments |
| 2254 | # if a namespace is large, but sometimes we also want to apply the |
| 2255 | # check if a short namespace contained nontrivial things (something |
| 2256 | # other than forward declarations). There is currently no logic on |
| 2257 | # deciding what these nontrivial things are, so this check is |
| 2258 | # triggered by namespace size only, which works most of the time. |
| 2259 | if (linenum - self.starting_linenum < 10 |
| 2260 | and not Match(r'^\s*};*\s*(//|/\*).*\bnamespace\b', line)): |
| 2261 | return |
| 2262 | |
| 2263 | # Look for matching comment at end of namespace. |
| 2264 | # |
| 2265 | # Note that we accept C style "/* */" comments for terminating |
| 2266 | # namespaces, so that code that terminate namespaces inside |
| 2267 | # preprocessor macros can be cpplint clean. |
| 2268 | # |
| 2269 | # We also accept stuff like "// end of namespace <name>." with the |
| 2270 | # period at the end. |
| 2271 | # |
| 2272 | # Besides these, we don't accept anything else, otherwise we might |
| 2273 | # get false negatives when existing comment is a substring of the |
| 2274 | # expected namespace. |
| 2275 | if self.name: |
| 2276 | # Named namespace |
| 2277 | if not Match((r'^\s*};*\s*(//|/\*).*\bnamespace\s+' + |
| 2278 | re.escape(self.name) + r'[\*/\.\\\s]*$'), |
| 2279 | line): |
| 2280 | error(filename, linenum, 'readability/namespace', 5, |
| 2281 | 'Namespace should be terminated with "// namespace %s"' % |
| 2282 | self.name) |
| 2283 | else: |
| 2284 | # Anonymous namespace |
| 2285 | if not Match(r'^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line): |
| 2286 | # If "// namespace anonymous" or "// anonymous namespace (more text)", |
| 2287 | # mention "// anonymous namespace" as an acceptable form |
| 2288 | if Match(r'^\s*}.*\b(namespace anonymous|anonymous namespace)\b', line): |
| 2289 | error(filename, linenum, 'readability/namespace', 5, |
| 2290 | 'Anonymous namespace should be terminated with "// namespace"' |
| 2291 | ' or "// anonymous namespace"') |
| 2292 | else: |
| 2293 | error(filename, linenum, 'readability/namespace', 5, |
| 2294 | 'Anonymous namespace should be terminated with "// namespace"') |
| 2295 | |
| 2296 | |
| 2297 | class _PreprocessorInfo(object): |