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. error: The fun
(filename, clean_lines, error)
| 1675 | |
| 1676 | |
| 1677 | def CheckForHeaderGuard(filename, clean_lines, error): |
| 1678 | """Checks that the file contains a header guard. |
| 1679 | |
| 1680 | Logs an error if no #ifndef header guard is present. For other |
| 1681 | headers, checks that the full pathname is used. |
| 1682 | |
| 1683 | Args: |
| 1684 | filename: The name of the C++ header file. |
| 1685 | clean_lines: A CleansedLines instance containing the file. |
| 1686 | error: The function to call with any errors found. |
| 1687 | """ |
| 1688 | |
| 1689 | # Don't check for header guards if there are error suppression |
| 1690 | # comments somewhere in this file. |
| 1691 | # |
| 1692 | # Because this is silencing a warning for a nonexistent line, we |
| 1693 | # only support the very specific NOLINT(build/header_guard) syntax, |
| 1694 | # and not the general NOLINT or NOLINT(*) syntax. |
| 1695 | raw_lines = clean_lines.lines_without_raw_strings |
| 1696 | for i in raw_lines: |
| 1697 | if Search(r'//\s*NOLINT\(build/header_guard\)', i): |
| 1698 | return |
| 1699 | |
| 1700 | cppvar = GetHeaderGuardCPPVariable(filename) |
| 1701 | |
| 1702 | ifndef = '' |
| 1703 | ifndef_linenum = 0 |
| 1704 | define = '' |
| 1705 | endif = '' |
| 1706 | endif_linenum = 0 |
| 1707 | for linenum, line in enumerate(raw_lines): |
| 1708 | linesplit = line.split() |
| 1709 | if len(linesplit) >= 2: |
| 1710 | # find the first occurrence of #ifndef and #define, save arg |
| 1711 | if not ifndef and linesplit[0] == '#ifndef': |
| 1712 | # set ifndef to the header guard presented on the #ifndef line. |
| 1713 | ifndef = linesplit[1] |
| 1714 | ifndef_linenum = linenum |
| 1715 | if not define and linesplit[0] == '#define': |
| 1716 | define = linesplit[1] |
| 1717 | # find the last occurrence of #endif, save entire line |
| 1718 | if line.startswith('#endif'): |
| 1719 | endif = line |
| 1720 | endif_linenum = linenum |
| 1721 | |
| 1722 | if not ifndef or not define or ifndef != define: |
| 1723 | error(filename, 0, 'build/header_guard', 5, |
| 1724 | 'No #ifndef header guard found, suggested CPP variable is: %s' % |
| 1725 | cppvar) |
| 1726 | return |
| 1727 | |
| 1728 | # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__ |
| 1729 | # for backward compatibility. |
| 1730 | if ifndef != cppvar: |
| 1731 | error_level = 0 |
| 1732 | if ifndef != cppvar + '_': |
| 1733 | error_level = 5 |
| 1734 |
no test coverage detected