MCPcopy Create free account
hub / github.com/alibaba/GraphScope / CheckGlobalStatic

Function CheckGlobalStatic

analytical_engine/misc/cpplint.py:5387–5445  ·  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

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

Callers 1

CheckLanguageFunction · 0.85

Calls 4

SearchFunction · 0.85
NumLinesMethod · 0.80
MatchFunction · 0.70
groupMethod · 0.45

Tested by

no test coverage detected