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)
| 1883 | |
| 1884 | |
| 1885 | def CheckForHeaderGuard(filename, clean_lines, error): |
| 1886 | """Checks that the file contains a header guard. |
| 1887 | |
| 1888 | Logs an error if no #ifndef header guard is present. For other |
| 1889 | headers, checks that the full pathname is used. |
| 1890 | |
| 1891 | Args: |
| 1892 | filename: The name of the C++ header file. |
| 1893 | clean_lines: A CleansedLines instance containing the file. |
| 1894 | error: The function to call with any errors found. |
| 1895 | """ |
| 1896 | |
| 1897 | # Don't check for header guards if there are error suppression |
| 1898 | # comments somewhere in this file. |
| 1899 | # |
| 1900 | # Because this is silencing a warning for a nonexistent line, we |
| 1901 | # only support the very specific NOLINT(build/header_guard) syntax, |
| 1902 | # and not the general NOLINT or NOLINT(*) syntax. |
| 1903 | raw_lines = clean_lines.lines_without_raw_strings |
| 1904 | for i in raw_lines: |
| 1905 | if Search(r'//\s*NOLINT\(build/header_guard\)', i): |
| 1906 | return |
| 1907 | |
| 1908 | cppvar = GetHeaderGuardCPPVariable(filename) |
| 1909 | |
| 1910 | ifndef = '' |
| 1911 | ifndef_linenum = 0 |
| 1912 | define = '' |
| 1913 | endif = '' |
| 1914 | endif_linenum = 0 |
| 1915 | for linenum, line in enumerate(raw_lines): |
| 1916 | linesplit = line.split() |
| 1917 | if len(linesplit) >= 2: |
| 1918 | # find the first occurrence of #ifndef and #define, save arg |
| 1919 | if not ifndef and linesplit[0] == '#ifndef': |
| 1920 | # set ifndef to the header guard presented on the #ifndef line. |
| 1921 | ifndef = linesplit[1] |
| 1922 | ifndef_linenum = linenum |
| 1923 | if not define and linesplit[0] == '#define': |
| 1924 | define = linesplit[1] |
| 1925 | # find the last occurrence of #endif, save entire line |
| 1926 | if line.startswith('#endif'): |
| 1927 | endif = line |
| 1928 | endif_linenum = linenum |
| 1929 | |
| 1930 | if not ifndef or not define or ifndef != define: |
| 1931 | error(filename, 0, 'build/header_guard', 5, |
| 1932 | 'No #ifndef header guard found, suggested CPP variable is: %s' % |
| 1933 | cppvar) |
| 1934 | return |
| 1935 | |
| 1936 | # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__ |
| 1937 | # for backward compatibility. |
| 1938 | if ifndef != cppvar: |
| 1939 | error_level = 0 |
| 1940 | if ifndef != cppvar + '_': |
| 1941 | error_level = 5 |
| 1942 |
no test coverage detected
searching dependent graphs…