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)
| 4805 | |
| 4806 | |
| 4807 | def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, |
| 4808 | error): |
| 4809 | """Checks rules from the 'C++ style rules' section of cppguide.html. |
| 4810 | |
| 4811 | Most of these rules are hard to test (naming, comment style), but we |
| 4812 | do what we can. In particular we check for 2-space indents, line lengths, |
| 4813 | tab usage, spaces inside code, etc. |
| 4814 | |
| 4815 | Args: |
| 4816 | filename: The name of the current file. |
| 4817 | clean_lines: A CleansedLines instance containing the file. |
| 4818 | linenum: The number of the line to check. |
| 4819 | file_extension: The extension (without the dot) of the filename. |
| 4820 | nesting_state: A NestingState instance which maintains information about |
| 4821 | the current stack of nested blocks being parsed. |
| 4822 | error: The function to call with any errors found. |
| 4823 | """ |
| 4824 | |
| 4825 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 4826 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 4827 | # raw strings, |
| 4828 | raw_lines = clean_lines.lines_without_raw_strings |
| 4829 | line = raw_lines[linenum] |
| 4830 | prev = raw_lines[linenum - 1] if linenum > 0 else '' |
| 4831 | |
| 4832 | if line.find('\t') != -1: |
| 4833 | error(filename, linenum, 'whitespace/tab', 1, |
| 4834 | 'Tab found; better to use spaces') |
| 4835 | |
| 4836 | # One or three blank spaces at the beginning of the line is weird; it's |
| 4837 | # hard to reconcile that with 2-space indents. |
| 4838 | # NOTE: here are the conditions rob pike used for his tests. Mine aren't |
| 4839 | # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces |
| 4840 | # if(RLENGTH > 20) complain = 0; |
| 4841 | # if(match($0, " +(error|private|public|protected):")) complain = 0; |
| 4842 | # if(match(prev, "&& *$")) complain = 0; |
| 4843 | # if(match(prev, "\\|\\| *$")) complain = 0; |
| 4844 | # if(match(prev, "[\",=><] *$")) complain = 0; |
| 4845 | # if(match($0, " <<")) complain = 0; |
| 4846 | # if(match(prev, " +for \\(")) complain = 0; |
| 4847 | # if(prevodd && match(prevprev, " +for \\(")) complain = 0; |
| 4848 | scope_or_label_pattern = r'\s*(?:public|private|protected|signals)(?:\s+(?:slots\s*)?)?:\s*\\?$' |
| 4849 | classinfo = nesting_state.InnermostClass() |
| 4850 | initial_spaces = 0 |
| 4851 | cleansed_line = clean_lines.elided[linenum] |
| 4852 | while initial_spaces < len(line) and line[initial_spaces] == ' ': |
| 4853 | initial_spaces += 1 |
| 4854 | # There are certain situations we allow one space, notably for |
| 4855 | # section labels, and also lines containing multi-line raw strings. |
| 4856 | # We also don't check for lines that look like continuation lines |
| 4857 | # (of lines ending in double quotes, commas, equals, or angle brackets) |
| 4858 | # because the rules for how to indent those are non-trivial. |
| 4859 | if (not Search(r'[",=><] *$', prev) and |
| 4860 | (initial_spaces == 1 or initial_spaces == 3) and |
| 4861 | not Match(scope_or_label_pattern, cleansed_line) and |
| 4862 | not (clean_lines.raw_lines[linenum] != line and |
| 4863 | Match(r'^\s*""', line))): |
| 4864 | error(filename, linenum, 'whitespace/indent', 3, |
no test coverage detected