MCPcopy Index your code
hub / github.com/nodejs/node / _CheckHeadersHaveIncludeGuards

Function _CheckHeadersHaveIncludeGuards

deps/v8/PRESUBMIT.py:285–338  ·  view source on GitHub ↗

Ensures that all header files have include guards.

(input_api, output_api)

Source from the content-addressed store, hash-verified

283
284
285def _CheckHeadersHaveIncludeGuards(input_api, output_api):
286 """Ensures that all header files have include guards."""
287 file_inclusion_pattern = r'src/.+\.h'
288
289 def FilterFile(affected_file):
290 files_to_skip = _EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP
291 return input_api.FilterSourceFile(
292 affected_file,
293 files_to_check=(file_inclusion_pattern, ),
294 files_to_skip=files_to_skip)
295
296 leading_src_pattern = input_api.re.compile(r'^src[\\\/]')
297 dash_dot_slash_pattern = input_api.re.compile(r'[-.\\\/]')
298
299 def PathToGuardMacro(path):
300 """Guards should be of the form V8_PATH_TO_FILE_WITHOUT_SRC_H_."""
301 x = input_api.re.sub(leading_src_pattern, 'v8_', path)
302 x = input_api.re.sub(dash_dot_slash_pattern, '_', x)
303 x = x.upper() + "_"
304 return x
305
306 problems = []
307 for f in input_api.AffectedSourceFiles(FilterFile):
308 local_path = f.LocalPath()
309 guard_macro = PathToGuardMacro(local_path)
310 guard_patterns = [
311 input_api.re.compile(r'^#ifndef ' + guard_macro + '$'),
312 input_api.re.compile(r'^#define ' + guard_macro + '$'),
313 input_api.re.compile(r'^#endif // ' + guard_macro + '$')]
314 skip_check_pattern = input_api.re.compile(
315 r'^// PRESUBMIT_INTENTIONALLY_MISSING_INCLUDE_GUARD')
316 found_patterns = [ False, False, False ]
317 file_omitted = False
318
319 for line in f.NewContents():
320 for i in range(len(guard_patterns)):
321 if guard_patterns[i].match(line):
322 found_patterns[i] = True
323 if skip_check_pattern.match(line):
324 file_omitted = True
325 break
326
327 if not file_omitted and not all(found_patterns):
328 problems.append('{}: Missing include guard \'{}\''.format(
329 local_path, guard_macro))
330
331 if problems:
332 return [output_api.PresubmitError(
333 'You added one or more header files without an appropriate\n'
334 'include guard. Add the include guard {#ifndef,#define,#endif}\n'
335 'triplet or omit the check entirely through the magic comment:\n'
336 '"// PRESUBMIT_INTENTIONALLY_MISSING_INCLUDE_GUARD".', problems)]
337 else:
338 return []
339
340
341def _CheckNoInlineHeaderIncludesInNormalHeaders(input_api, output_api):

Callers

nothing calls this directly

Calls 7

PathToGuardMacroFunction · 0.85
LocalPathMethod · 0.80
matchMethod · 0.65
formatMethod · 0.65
rangeFunction · 0.50
compileMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…