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