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

Function CheckForHeaderGuard

cpplint.py:2575–2695  ·  view source on GitHub ↗

Checks that the file contains a header guard. Logs an error if no #ifndef header guard is present. For other headers, checks that the full pathname is used. Args: filename: The name of the C++ header file. clean_lines: A CleansedLines instance containing the file. er

(filename, clean_lines, error, cppvar)

Source from the content-addressed store, hash-verified

2573
2574
2575def CheckForHeaderGuard(filename, clean_lines, error, cppvar):
2576 """Checks that the file contains a header guard.
2577
2578 Logs an error if no #ifndef header guard is present. For other
2579 headers, checks that the full pathname is used.
2580
2581 Args:
2582 filename: The name of the C++ header file.
2583 clean_lines: A CleansedLines instance containing the file.
2584 error: The function to call with any errors found.
2585 """
2586
2587 # Don't check for header guards if there are error suppression
2588 # comments somewhere in this file.
2589 #
2590 # Because this is silencing a warning for a nonexistent line, we
2591 # only support the very specific NOLINT(build/header_guard) syntax,
2592 # and not the general NOLINT or NOLINT(*) syntax.
2593 raw_lines = clean_lines.lines_without_raw_strings
2594 for i in raw_lines:
2595 if re.search(r"//\s*NOLINT\(build/header_guard\)", i):
2596 return
2597
2598 # Allow pragma once instead of header guards
2599 for i in raw_lines:
2600 if re.search(r"^\s*#pragma\s+once", i):
2601 return
2602
2603 ifndef = ""
2604 ifndef_linenum = 0
2605 define = ""
2606 endif = ""
2607 endif_linenum = 0
2608 for linenum, line in enumerate(raw_lines):
2609 linesplit = line.split()
2610 if len(linesplit) >= 2:
2611 # find the first occurrence of #ifndef and #define, save arg
2612 if not ifndef and linesplit[0] == "#ifndef":
2613 # set ifndef to the header guard presented on the #ifndef line.
2614 ifndef = linesplit[1]
2615 ifndef_linenum = linenum
2616 if not define and linesplit[0] == "#define":
2617 define = linesplit[1]
2618 # find the last occurrence of #endif, save entire line
2619 if line.startswith("#endif"):
2620 endif = line
2621 endif_linenum = linenum
2622
2623 if not ifndef or not define or ifndef != define:
2624 error(
2625 filename,
2626 0,
2627 "build/header_guard",
2628 5,
2629 f"No #ifndef header guard found, suggested CPP variable is: {cppvar}",
2630 )
2631 return
2632

Callers 1

ProcessFileDataFunction · 0.85

Calls 1

ParseNolintSuppressionsFunction · 0.85

Tested by

no test coverage detected