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)
| 4841 | |
| 4842 | |
| 4843 | def CheckGlobalStatic(filename, clean_lines, linenum, error): |
| 4844 | """Check for unsafe global or static objects. |
| 4845 | |
| 4846 | Args: |
| 4847 | filename: The name of the current file. |
| 4848 | clean_lines: A CleansedLines instance containing the file. |
| 4849 | linenum: The number of the line to check. |
| 4850 | error: The function to call with any errors found. |
| 4851 | """ |
| 4852 | line = clean_lines.elided[linenum] |
| 4853 | |
| 4854 | # Match two lines at a time to support multiline declarations |
| 4855 | if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line): |
| 4856 | line += clean_lines.elided[linenum + 1].strip() |
| 4857 | |
| 4858 | # Check for people declaring static/global STL strings at the top level. |
| 4859 | # This is dangerous because the C++ language does not guarantee that |
| 4860 | # globals with constructors are initialized before the first access, and |
| 4861 | # also because globals can be destroyed when some threads are still running. |
| 4862 | # TODO(unknown): Generalize this to also find static unique_ptr instances. |
| 4863 | # TODO(unknown): File bugs for clang-tidy to find these. |
| 4864 | match = Match( |
| 4865 | r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +' |
| 4866 | r'([a-zA-Z0-9_:]+)\b(.*)', |
| 4867 | line) |
| 4868 | |
| 4869 | # Remove false positives: |
| 4870 | # - String pointers (as opposed to values). |
| 4871 | # string *pointer |
| 4872 | # const string *pointer |
| 4873 | # string const *pointer |
| 4874 | # string *const pointer |
| 4875 | # |
| 4876 | # - Functions and template specializations. |
| 4877 | # string Function<Type>(... |
| 4878 | # string Class<Type>::Method(... |
| 4879 | # |
| 4880 | # - Operators. These are matched separately because operator names |
| 4881 | # cross non-word boundaries, and trying to match both operators |
| 4882 | # and functions at the same time would decrease accuracy of |
| 4883 | # matching identifiers. |
| 4884 | # string Class::operator*() |
| 4885 | if (match and |
| 4886 | not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and |
| 4887 | not Search(r'\boperator\W', line) and |
| 4888 | not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))): |
| 4889 | if Search(r'\bconst\b', line): |
| 4890 | error(filename, linenum, 'runtime/string', 4, |
| 4891 | 'For a static/global string constant, use a C style string ' |
| 4892 | 'instead: "%schar%s %s[]".' % |
| 4893 | (match.group(1), match.group(2) or '', match.group(3))) |
| 4894 | else: |
| 4895 | error(filename, linenum, 'runtime/string', 4, |
| 4896 | 'Static/global string variables are not permitted.') |
| 4897 | |
| 4898 | if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or |
| 4899 | Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)): |
| 4900 | error(filename, linenum, 'runtime/init', 4, |
no test coverage detected