MCPcopy Index your code
hub / github.com/cpplint/cpplint / CheckGlobalStatic

Function CheckGlobalStatic

cpplint.py:6116–6192  ·  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

6114
6115
6116def CheckGlobalStatic(filename, clean_lines, linenum, error):
6117 """Check for unsafe global or static objects.
6118
6119 Args:
6120 filename: The name of the current file.
6121 clean_lines: A CleansedLines instance containing the file.
6122 linenum: The number of the line to check.
6123 error: The function to call with any errors found.
6124 """
6125 line = clean_lines.elided[linenum]
6126
6127 # Match two lines at a time to support multiline declarations
6128 if linenum + 1 < clean_lines.NumLines() and not re.search(r"[;({]", line):
6129 line += clean_lines.elided[linenum + 1].strip()
6130
6131 # Check for people declaring static/global STL strings at the top level.
6132 # This is dangerous because the C++ language does not guarantee that
6133 # globals with constructors are initialized before the first access, and
6134 # also because globals can be destroyed when some threads are still running.
6135 # TODO(google): Generalize this to also find static unique_ptr instances.
6136 # TODO(google): File bugs for clang-tidy to find these.
6137 match = re.match(
6138 r"((?:|static +)(?:|const +))(?::*std::)?string( +const)? +"
6139 r"([a-zA-Z0-9_:]+)\b(.*)",
6140 line,
6141 )
6142
6143 # Remove false positives:
6144 # - String pointers (as opposed to values).
6145 # string *pointer
6146 # const string *pointer
6147 # string const *pointer
6148 # string *const pointer
6149 #
6150 # - Functions and template specializations.
6151 # string Function<Type>(...
6152 # string Class<Type>::Method(...
6153 #
6154 # - Operators. These are matched separately because operator names
6155 # cross non-word boundaries, and trying to match both operators
6156 # and functions at the same time would decrease accuracy of
6157 # matching identifiers.
6158 # string Class::operator*()
6159 if (
6160 match
6161 and not re.search(r"\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w", line)
6162 and not re.search(r"\boperator\W", line)
6163 and not re.match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))
6164 ):
6165 if re.search(r"\bconst\b", line):
6166 error(
6167 filename,
6168 linenum,
6169 "runtime/string",
6170 4,
6171 "For a static/global string constant, use a C style string instead:"
6172 f' "{match.group(1)}char{match.group(2) or ""} {match.group(3)}[]".',
6173 )

Callers 1

CheckLanguageFunction · 0.85

Calls 1

NumLinesMethod · 0.80

Tested by

no test coverage detected