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)
| 1788 | |
| 1789 | |
| 1790 | def CheckForHeaderGuard(filename, clean_lines, error): |
| 1791 | """Checks that the file contains a header guard. |
| 1792 | |
| 1793 | Logs an error if no #ifndef header guard is present. For other |
| 1794 | headers, checks that the full pathname is used. |
| 1795 | |
| 1796 | Args: |
| 1797 | filename: The name of the C++ header file. |
| 1798 | clean_lines: A CleansedLines instance containing the file. |
| 1799 | error: The function to call with any errors found. |
| 1800 | """ |
| 1801 | |
| 1802 | # Don't check for header guards if there are error suppression |
| 1803 | # comments somewhere in this file. |
| 1804 | # |
| 1805 | # Because this is silencing a warning for a nonexistent line, we |
| 1806 | # only support the very specific NOLINT(build/header_guard) syntax, |
| 1807 | # and not the general NOLINT or NOLINT(*) syntax. |
| 1808 | raw_lines = clean_lines.lines_without_raw_strings |
| 1809 | for i in raw_lines: |
| 1810 | if Search(r'//\s*NOLINT\(build/header_guard\)', i): |
| 1811 | return |
| 1812 | |
| 1813 | cppvar = GetHeaderGuardCPPVariable(filename) |
| 1814 | |
| 1815 | ifndef = '' |
| 1816 | ifndef_linenum = 0 |
| 1817 | define = '' |
| 1818 | endif = '' |
| 1819 | endif_linenum = 0 |
| 1820 | for linenum, line in enumerate(raw_lines): |
| 1821 | linesplit = line.split() |
| 1822 | if len(linesplit) >= 2: |
| 1823 | # find the first occurrence of #ifndef and #define, save arg |
| 1824 | if not ifndef and linesplit[0] == '#ifndef': |
| 1825 | # set ifndef to the header guard presented on the #ifndef line. |
| 1826 | ifndef = linesplit[1] |
| 1827 | ifndef_linenum = linenum |
| 1828 | if not define and linesplit[0] == '#define': |
| 1829 | define = linesplit[1] |
| 1830 | # find the last occurrence of #endif, save entire line |
| 1831 | if line.startswith('#endif'): |
| 1832 | endif = line |
| 1833 | endif_linenum = linenum |
| 1834 | |
| 1835 | if not ifndef or not define or ifndef != define: |
| 1836 | error(filename, 0, 'build/header_guard', 5, |
| 1837 | 'No #ifndef header guard found, suggested CPP variable is: %s' % |
| 1838 | cppvar) |
| 1839 | return |
| 1840 | |
| 1841 | # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__ |
| 1842 | # for backward compatibility. |
| 1843 | if ifndef != cppvar: |
| 1844 | error_level = 0 |
| 1845 | if ifndef != cppvar + '_': |
| 1846 | error_level = 5 |
| 1847 |
no test coverage detected