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)
| 4763 | |
| 4764 | |
| 4765 | def CheckGlobalStatic(filename, clean_lines, linenum, error): |
| 4766 | """Check for unsafe global or static objects. |
| 4767 | |
| 4768 | Args: |
| 4769 | filename: The name of the current file. |
| 4770 | clean_lines: A CleansedLines instance containing the file. |
| 4771 | linenum: The number of the line to check. |
| 4772 | error: The function to call with any errors found. |
| 4773 | """ |
| 4774 | line = clean_lines.elided[linenum] |
| 4775 | |
| 4776 | # Match two lines at a time to support multiline declarations |
| 4777 | if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line): |
| 4778 | line += clean_lines.elided[linenum + 1].strip() |
| 4779 | |
| 4780 | # Check for people declaring static/global STL strings at the top level. |
| 4781 | # This is dangerous because the C++ language does not guarantee that |
| 4782 | # globals with constructors are initialized before the first access, and |
| 4783 | # also because globals can be destroyed when some threads are still running. |
| 4784 | # TODO(unknown): Generalize this to also find static unique_ptr instances. |
| 4785 | # TODO(unknown): File bugs for clang-tidy to find these. |
| 4786 | match = Match( |
| 4787 | r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +' |
| 4788 | r'([a-zA-Z0-9_:]+)\b(.*)', |
| 4789 | line) |
| 4790 | |
| 4791 | # Remove false positives: |
| 4792 | # - String pointers (as opposed to values). |
| 4793 | # string *pointer |
| 4794 | # const string *pointer |
| 4795 | # string const *pointer |
| 4796 | # string *const pointer |
| 4797 | # |
| 4798 | # - Functions and template specializations. |
| 4799 | # string Function<Type>(... |
| 4800 | # string Class<Type>::Method(... |
| 4801 | # |
| 4802 | # - Operators. These are matched separately because operator names |
| 4803 | # cross non-word boundaries, and trying to match both operators |
| 4804 | # and functions at the same time would decrease accuracy of |
| 4805 | # matching identifiers. |
| 4806 | # string Class::operator*() |
| 4807 | if (match and |
| 4808 | not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and |
| 4809 | not Search(r'\boperator\W', line) and |
| 4810 | not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))): |
| 4811 | if Search(r'\bconst\b', line): |
| 4812 | error(filename, linenum, 'runtime/string', 4, |
| 4813 | 'For a static/global string constant, use a C style string ' |
| 4814 | 'instead: "%schar%s %s[]".' % |
| 4815 | (match.group(1), match.group(2) or '', match.group(3))) |
| 4816 | else: |
| 4817 | error(filename, linenum, 'runtime/string', 4, |
| 4818 | 'Static/global string variables are not permitted.') |
| 4819 | |
| 4820 | if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or |
| 4821 | Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)): |
| 4822 | error(filename, linenum, 'runtime/init', 4, |
no test coverage detected