Stores information about a namespace.
| 2121 | |
| 2122 | |
| 2123 | class _NamespaceInfo(_BlockInfo): |
| 2124 | """Stores information about a namespace.""" |
| 2125 | |
| 2126 | def __init__(self, name, linenum): |
| 2127 | _BlockInfo.__init__(self, False) |
| 2128 | self.name = name or '' |
| 2129 | self.starting_linenum = linenum |
| 2130 | self.check_namespace_indentation = True |
| 2131 | |
| 2132 | def CheckEnd(self, filename, clean_lines, linenum, error): |
| 2133 | """Check end of namespace comments.""" |
| 2134 | line = clean_lines.raw_lines[linenum] |
| 2135 | |
| 2136 | # Check how many lines is enclosed in this namespace. Don't issue |
| 2137 | # warning for missing namespace comments if there aren't enough |
| 2138 | # lines. However, do apply checks if there is already an end of |
| 2139 | # namespace comment and it's incorrect. |
| 2140 | # |
| 2141 | # TODO(unknown): We always want to check end of namespace comments |
| 2142 | # if a namespace is large, but sometimes we also want to apply the |
| 2143 | # check if a short namespace contained nontrivial things (something |
| 2144 | # other than forward declarations). There is currently no logic on |
| 2145 | # deciding what these nontrivial things are, so this check is |
| 2146 | # triggered by namespace size only, which works most of the time. |
| 2147 | if (linenum - self.starting_linenum < 10 |
| 2148 | and not Match(r'};*\s*(//|/\*).*\bnamespace\b', line)): |
| 2149 | return |
| 2150 | |
| 2151 | # Look for matching comment at end of namespace. |
| 2152 | # |
| 2153 | # Note that we accept C style "/* */" comments for terminating |
| 2154 | # namespaces, so that code that terminate namespaces inside |
| 2155 | # preprocessor macros can be cpplint clean. |
| 2156 | # |
| 2157 | # We also accept stuff like "// end of namespace <name>." with the |
| 2158 | # period at the end. |
| 2159 | # |
| 2160 | # Besides these, we don't accept anything else, otherwise we might |
| 2161 | # get false negatives when existing comment is a substring of the |
| 2162 | # expected namespace. |
| 2163 | if self.name: |
| 2164 | # Named namespace |
| 2165 | if not Match((r'};*\s*(//|/\*).*\bnamespace\s+' + re.escape(self.name) + |
| 2166 | r'[\*/\.\\\s]*$'), |
| 2167 | line): |
| 2168 | error(filename, linenum, 'readability/namespace', 5, |
| 2169 | 'Namespace should be terminated with "// namespace %s"' % |
| 2170 | self.name) |
| 2171 | else: |
| 2172 | # Anonymous namespace |
| 2173 | if not Match(r'};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line): |
| 2174 | # If "// namespace anonymous" or "// anonymous namespace (more text)", |
| 2175 | # mention "// anonymous namespace" as an acceptable form |
| 2176 | if Match(r'}.*\b(namespace anonymous|anonymous namespace)\b', line): |
| 2177 | error(filename, linenum, 'readability/namespace', 5, |
| 2178 | 'Anonymous namespace should be terminated with "// namespace"' |
| 2179 | ' or "// anonymous namespace"') |
| 2180 | else: |