MCPcopy Create free account
hub / github.com/4paradigm/OpenMLDB / CheckGlobalStatic

Function CheckGlobalStatic

steps/cpplint.py:5397–5455  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 1

CheckLanguageFunction · 0.85

Calls 3

SearchFunction · 0.85
MatchFunction · 0.85
NumLinesMethod · 0.80

Tested by

no test coverage detected