Check for unsafe global or static objects. Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. error: The function to call with any errors found.
(filename, clean_lines, linenum, error)
| 5382 | |
| 5383 | |
| 5384 | def CheckGlobalStatic(filename, clean_lines, linenum, error): |
| 5385 | """Check for unsafe global or static objects. |
| 5386 | |
| 5387 | Args: |
| 5388 | filename: The name of the current file. |
| 5389 | clean_lines: A CleansedLines instance containing the file. |
| 5390 | linenum: The number of the line to check. |
| 5391 | error: The function to call with any errors found. |
| 5392 | """ |
| 5393 | line = clean_lines.elided[linenum] |
| 5394 | |
| 5395 | # Match two lines at a time to support multiline declarations |
| 5396 | if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line): |
| 5397 | line += clean_lines.elided[linenum + 1].strip() |
| 5398 | |
| 5399 | # Check for people declaring static/global STL strings at the top level. |
| 5400 | # This is dangerous because the C++ language does not guarantee that |
| 5401 | # globals with constructors are initialized before the first access, and |
| 5402 | # also because globals can be destroyed when some threads are still running. |
| 5403 | # TODO(unknown): Generalize this to also find static unique_ptr instances. |
| 5404 | # TODO(unknown): File bugs for clang-tidy to find these. |
| 5405 | match = Match( |
| 5406 | r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +' |
| 5407 | r'([a-zA-Z0-9_:]+)\b(.*)', |
| 5408 | line) |
| 5409 | |
| 5410 | # Remove false positives: |
| 5411 | # - String pointers (as opposed to values). |
| 5412 | # string *pointer |
| 5413 | # const string *pointer |
| 5414 | # string const *pointer |
| 5415 | # string *const pointer |
| 5416 | # |
| 5417 | # - Functions and template specializations. |
| 5418 | # string Function<Type>(... |
| 5419 | # string Class<Type>::Method(... |
| 5420 | # |
| 5421 | # - Operators. These are matched separately because operator names |
| 5422 | # cross non-word boundaries, and trying to match both operators |
| 5423 | # and functions at the same time would decrease accuracy of |
| 5424 | # matching identifiers. |
| 5425 | # string Class::operator*() |
| 5426 | if (match and |
| 5427 | not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and |
| 5428 | not Search(r'\boperator\W', line) and |
| 5429 | not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))): |
| 5430 | if Search(r'\bconst\b', line): |
| 5431 | error(filename, linenum, 'runtime/string', 4, |
| 5432 | 'For a static/global string constant, use a C style string ' |
| 5433 | 'instead: "%schar%s %s[]".' % |
| 5434 | (match.group(1), match.group(2) or '', match.group(3))) |
| 5435 | else: |
| 5436 | error(filename, linenum, 'runtime/string', 4, |
| 5437 | 'Static/global string variables are not permitted.') |
| 5438 | |
| 5439 | if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or |
| 5440 | Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)): |
| 5441 | error(filename, linenum, 'runtime/init', 4, |
no test coverage detected