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)
| 1406 | |
| 1407 | |
| 1408 | def CheckForHeaderGuard(filename, lines, error): |
| 1409 | """Checks that the file contains a header guard. |
| 1410 | |
| 1411 | Logs an error if no #ifndef header guard is present. For other |
| 1412 | headers, checks that the full pathname is used. |
| 1413 | |
| 1414 | Args: |
| 1415 | filename: The name of the C++ header file. |
| 1416 | lines: An array of strings, each representing a line of the file. |
| 1417 | error: The function to call with any errors found. |
| 1418 | """ |
| 1419 | |
| 1420 | cppvar = GetHeaderGuardCPPVariable(filename) |
| 1421 | |
| 1422 | ifndef = None |
| 1423 | ifndef_linenum = 0 |
| 1424 | define = None |
| 1425 | endif = None |
| 1426 | endif_linenum = 0 |
| 1427 | for linenum, line in enumerate(lines): |
| 1428 | linesplit = line.split() |
| 1429 | if len(linesplit) >= 2: |
| 1430 | # find the first occurrence of #ifndef and #define, save arg |
| 1431 | if not ifndef and linesplit[0] == '#ifndef': |
| 1432 | # set ifndef to the header guard presented on the #ifndef line. |
| 1433 | ifndef = linesplit[1] |
| 1434 | ifndef_linenum = linenum |
| 1435 | if not define and linesplit[0] == '#define': |
| 1436 | define = linesplit[1] |
| 1437 | # find the last occurrence of #endif, save entire line |
| 1438 | if line.startswith('#endif'): |
| 1439 | endif = line |
| 1440 | endif_linenum = linenum |
| 1441 | |
| 1442 | if not ifndef: |
| 1443 | error(filename, 0, 'build/header_guard', 5, |
| 1444 | 'No #ifndef header guard found, suggested CPP variable is: %s' % |
| 1445 | cppvar) |
| 1446 | return |
| 1447 | |
| 1448 | if not define: |
| 1449 | error(filename, 0, 'build/header_guard', 5, |
| 1450 | 'No #define header guard found, suggested CPP variable is: %s' % |
| 1451 | cppvar) |
| 1452 | return |
| 1453 | |
| 1454 | # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__ |
| 1455 | # for backward compatibility. |
| 1456 | if ifndef != cppvar: |
| 1457 | error_level = 0 |
| 1458 | if ifndef != cppvar + '_': |
| 1459 | error_level = 5 |
| 1460 | |
| 1461 | ParseNolintSuppressions(filename, lines[ifndef_linenum], ifndef_linenum, |
| 1462 | error) |
| 1463 | error(filename, ifndef_linenum, 'build/header_guard', error_level, |
| 1464 | '#ifndef header guard has wrong style, please use: %s' % cppvar) |
| 1465 |
no test coverage detected