Checks rules from the 'C++ style rules' section of cppguide.html. Most of these rules are hard to test (naming, comment style), but we do what we can. In particular we check for 2-space indents, line lengths, tab usage, spaces inside code, etc. Args: filename: The name of the current
(filename, clean_lines, linenum, file_extension, nesting_state,
error)
| 3457 | |
| 3458 | |
| 3459 | def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, |
| 3460 | error): |
| 3461 | """Checks rules from the 'C++ style rules' section of cppguide.html. |
| 3462 | |
| 3463 | Most of these rules are hard to test (naming, comment style), but we |
| 3464 | do what we can. In particular we check for 2-space indents, line lengths, |
| 3465 | tab usage, spaces inside code, etc. |
| 3466 | |
| 3467 | Args: |
| 3468 | filename: The name of the current file. |
| 3469 | clean_lines: A CleansedLines instance containing the file. |
| 3470 | linenum: The number of the line to check. |
| 3471 | file_extension: The extension (without the dot) of the filename. |
| 3472 | nesting_state: A _NestingState instance which maintains information about |
| 3473 | the current stack of nested blocks being parsed. |
| 3474 | error: The function to call with any errors found. |
| 3475 | """ |
| 3476 | |
| 3477 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 3478 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 3479 | # raw strings, |
| 3480 | raw_lines = clean_lines.lines_without_raw_strings |
| 3481 | line = raw_lines[linenum] |
| 3482 | |
| 3483 | if line.find('\t') != -1: |
| 3484 | error(filename, linenum, 'whitespace/tab', 1, |
| 3485 | 'Tab found; better to use spaces') |
| 3486 | |
| 3487 | # One or three blank spaces at the beginning of the line is weird; it's |
| 3488 | # hard to reconcile that with 2-space indents. |
| 3489 | # NOTE: here are the conditions rob pike used for his tests. Mine aren't |
| 3490 | # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces |
| 3491 | # if(RLENGTH > 20) complain = 0; |
| 3492 | # if(match($0, " +(error|private|public|protected):")) complain = 0; |
| 3493 | # if(match(prev, "&& *$")) complain = 0; |
| 3494 | # if(match(prev, "\\|\\| *$")) complain = 0; |
| 3495 | # if(match(prev, "[\",=><] *$")) complain = 0; |
| 3496 | # if(match($0, " <<")) complain = 0; |
| 3497 | # if(match(prev, " +for \\(")) complain = 0; |
| 3498 | # if(prevodd && match(prevprev, " +for \\(")) complain = 0; |
| 3499 | initial_spaces = 0 |
| 3500 | cleansed_line = clean_lines.elided[linenum] |
| 3501 | while initial_spaces < len(line) and line[initial_spaces] == ' ': |
| 3502 | initial_spaces += 1 |
| 3503 | if line and line[-1].isspace(): |
| 3504 | error(filename, linenum, 'whitespace/end_of_line', 4, |
| 3505 | 'Line ends in whitespace. Consider deleting these extra spaces.') |
| 3506 | # There are certain situations we allow one space, notably for section labels |
| 3507 | elif ((initial_spaces == 1 or initial_spaces == 3) and |
| 3508 | not Match(r'\s*\w+\s*:\s*$', cleansed_line)): |
| 3509 | error(filename, linenum, 'whitespace/indent', 3, |
| 3510 | 'Weird number of spaces at line-start. ' |
| 3511 | 'Are you using a 2-space indent?') |
| 3512 | |
| 3513 | # Check if the line is a header guard. |
| 3514 | is_header_guard = False |
| 3515 | if file_extension == 'h': |
| 3516 | cppvar = GetHeaderGuardCPPVariable(filename) |
no test coverage detected