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. lines: An array of strings, each representing a line of the file. error:
(filename, lines, error)
| 1133 | |
| 1134 | |
| 1135 | def CheckForHeaderGuard(filename, lines, error): |
| 1136 | """Checks that the file contains a header guard. |
| 1137 | |
| 1138 | Logs an error if no #ifndef header guard is present. For other |
| 1139 | headers, checks that the full pathname is used. |
| 1140 | |
| 1141 | Args: |
| 1142 | filename: The name of the C++ header file. |
| 1143 | lines: An array of strings, each representing a line of the file. |
| 1144 | error: The function to call with any errors found. |
| 1145 | """ |
| 1146 | |
| 1147 | cppvar = GetHeaderGuardCPPVariable(filename) |
| 1148 | |
| 1149 | ifndef = None |
| 1150 | ifndef_linenum = 0 |
| 1151 | define = None |
| 1152 | endif = None |
| 1153 | endif_linenum = 0 |
| 1154 | for linenum, line in enumerate(lines): |
| 1155 | linesplit = line.split() |
| 1156 | if len(linesplit) >= 2: |
| 1157 | # find the first occurrence of #ifndef and #define, save arg |
| 1158 | if not ifndef and linesplit[0] == '#ifndef': |
| 1159 | # set ifndef to the header guard presented on the #ifndef line. |
| 1160 | ifndef = linesplit[1] |
| 1161 | ifndef_linenum = linenum |
| 1162 | if not define and linesplit[0] == '#define': |
| 1163 | define = linesplit[1] |
| 1164 | # find the last occurrence of #endif, save entire line |
| 1165 | if line.startswith('#endif'): |
| 1166 | endif = line |
| 1167 | endif_linenum = linenum |
| 1168 | |
| 1169 | if not ifndef: |
| 1170 | error(filename, 0, 'build/header_guard', 5, |
| 1171 | 'No #ifndef header guard found, suggested CPP variable is: %s' % |
| 1172 | cppvar) |
| 1173 | return |
| 1174 | |
| 1175 | if not define: |
| 1176 | error(filename, 0, 'build/header_guard', 5, |
| 1177 | 'No #define header guard found, suggested CPP variable is: %s' % |
| 1178 | cppvar) |
| 1179 | return |
| 1180 | |
| 1181 | # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__ |
| 1182 | # for backward compatibility. |
| 1183 | if ifndef != cppvar: |
| 1184 | error_level = 0 |
| 1185 | if ifndef != cppvar + '_': |
| 1186 | error_level = 5 |
| 1187 | |
| 1188 | ParseNolintSuppressions(filename, lines[ifndef_linenum], ifndef_linenum, |
| 1189 | error) |
| 1190 | error(filename, ifndef_linenum, 'build/header_guard', error_level, |
| 1191 | '#ifndef header guard has wrong style, please use: %s' % cppvar) |
| 1192 |
no test coverage detected