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 th
(filename, clean_lines, linenum, file_extension, nesting_state, error, cppvar=None)
| 5558 | |
| 5559 | |
| 5560 | def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, error, cppvar=None): |
| 5561 | """Checks rules from the 'C++ style rules' section of cppguide.html. |
| 5562 | |
| 5563 | Most of these rules are hard to test (naming, comment style), but we |
| 5564 | do what we can. In particular we check for 2-space indents, line lengths, |
| 5565 | tab usage, spaces inside code, etc. |
| 5566 | |
| 5567 | Args: |
| 5568 | filename: The name of the current file. |
| 5569 | clean_lines: A CleansedLines instance containing the file. |
| 5570 | linenum: The number of the line to check. |
| 5571 | file_extension: The extension (without the dot) of the filename. |
| 5572 | nesting_state: A NestingState instance which maintains information about |
| 5573 | the current stack of nested blocks being parsed. |
| 5574 | error: The function to call with any errors found. |
| 5575 | cppvar: The header guard variable returned by GetHeaderGuardCPPVar. |
| 5576 | """ |
| 5577 | |
| 5578 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 5579 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 5580 | # raw strings, |
| 5581 | raw_lines = clean_lines.lines_without_raw_strings |
| 5582 | line = raw_lines[linenum] |
| 5583 | prev = raw_lines[linenum - 1] if linenum > 0 else "" |
| 5584 | |
| 5585 | if line.find("\t") != -1: |
| 5586 | error(filename, linenum, "whitespace/tab", 1, "Tab found; better to use spaces") |
| 5587 | |
| 5588 | # One or three blank spaces at the beginning of the line is weird; it's |
| 5589 | # hard to reconcile that with 2-space indents. |
| 5590 | # NOTE: here are the conditions rob pike used for his tests. Mine aren't |
| 5591 | # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces |
| 5592 | # if(RLENGTH > 20) complain = 0; |
| 5593 | # if(match($0, " +(error|private|public|protected):")) complain = 0; |
| 5594 | # if(match(prev, "&& *$")) complain = 0; |
| 5595 | # if(match(prev, "\\|\\| *$")) complain = 0; |
| 5596 | # if(match(prev, "[\",=><] *$")) complain = 0; |
| 5597 | # if(match($0, " <<")) complain = 0; |
| 5598 | # if(match(prev, " +for \\(")) complain = 0; |
| 5599 | # if(prevodd && match(prevprev, " +for \\(")) complain = 0; |
| 5600 | scope_or_label_pattern = r"\s*(?:public|private|protected|signals)(?:\s+(?:slots\s*)?)?:\s*\\?$" |
| 5601 | classinfo = nesting_state.InnermostClass() |
| 5602 | initial_spaces = 0 |
| 5603 | cleansed_line = clean_lines.elided[linenum] |
| 5604 | while initial_spaces < len(line) and line[initial_spaces] == " ": |
| 5605 | initial_spaces += 1 |
| 5606 | # There are certain situations we allow one space, notably for |
| 5607 | # section labels, and also lines containing multi-line raw strings. |
| 5608 | # We also don't check for lines that look like continuation lines |
| 5609 | # (of lines ending in double quotes, commas, equals, or angle brackets) |
| 5610 | # because the rules for how to indent those are non-trivial. |
| 5611 | if ( |
| 5612 | not re.search(r'[",=><] *$', prev) |
| 5613 | and (initial_spaces in {1, 3}) |
| 5614 | and not re.match(scope_or_label_pattern, cleansed_line) |
| 5615 | and not (clean_lines.raw_lines[linenum] != line and re.match(r'^\s*""', line)) |
| 5616 | ): |
| 5617 | error( |
no test coverage detected
searching dependent graphs…