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)
| 4371 | |
| 4372 | |
| 4373 | def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, |
| 4374 | error): |
| 4375 | """Checks rules from the 'C++ style rules' section of cppguide.html. |
| 4376 | |
| 4377 | Most of these rules are hard to test (naming, comment style), but we |
| 4378 | do what we can. In particular we check for 2-space indents, line lengths, |
| 4379 | tab usage, spaces inside code, etc. |
| 4380 | |
| 4381 | Args: |
| 4382 | filename: The name of the current file. |
| 4383 | clean_lines: A CleansedLines instance containing the file. |
| 4384 | linenum: The number of the line to check. |
| 4385 | file_extension: The extension (without the dot) of the filename. |
| 4386 | nesting_state: A NestingState instance which maintains information about |
| 4387 | the current stack of nested blocks being parsed. |
| 4388 | error: The function to call with any errors found. |
| 4389 | """ |
| 4390 | |
| 4391 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 4392 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 4393 | # raw strings, |
| 4394 | raw_lines = clean_lines.lines_without_raw_strings |
| 4395 | line = raw_lines[linenum] |
| 4396 | |
| 4397 | if line.find('\t') != -1: |
| 4398 | error(filename, linenum, 'whitespace/tab', 1, |
| 4399 | 'Tab found; better to use spaces') |
| 4400 | |
| 4401 | # One or three blank spaces at the beginning of the line is weird; it's |
| 4402 | # hard to reconcile that with 2-space indents. |
| 4403 | # NOTE: here are the conditions rob pike used for his tests. Mine aren't |
| 4404 | # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces |
| 4405 | # if(RLENGTH > 20) complain = 0; |
| 4406 | # if(match($0, " +(error|private|public|protected):")) complain = 0; |
| 4407 | # if(match(prev, "&& *$")) complain = 0; |
| 4408 | # if(match(prev, "\\|\\| *$")) complain = 0; |
| 4409 | # if(match(prev, "[\",=><] *$")) complain = 0; |
| 4410 | # if(match($0, " <<")) complain = 0; |
| 4411 | # if(match(prev, " +for \\(")) complain = 0; |
| 4412 | # if(prevodd && match(prevprev, " +for \\(")) complain = 0; |
| 4413 | scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$' |
| 4414 | classinfo = nesting_state.InnermostClass() |
| 4415 | initial_spaces = 0 |
| 4416 | cleansed_line = clean_lines.elided[linenum] |
| 4417 | while initial_spaces < len(line) and line[initial_spaces] == ' ': |
| 4418 | initial_spaces += 1 |
| 4419 | if line and line[-1].isspace(): |
| 4420 | error(filename, linenum, 'whitespace/end_of_line', 4, |
| 4421 | 'Line ends in whitespace. Consider deleting these extra spaces.') |
| 4422 | # There are certain situations we allow one space, notably for |
| 4423 | # section labels, and also lines containing multi-line raw strings. |
| 4424 | elif ((initial_spaces == 1 or initial_spaces == 3) and |
| 4425 | not Match(scope_or_label_pattern, cleansed_line) and |
| 4426 | not (clean_lines.raw_lines[linenum] != line and |
| 4427 | Match(r'^\s*""', line))): |
| 4428 | error(filename, linenum, 'whitespace/indent', 3, |
| 4429 | 'Weird number of spaces at line-start. ' |
| 4430 | 'Are you using a 2-space indent?') |
no test coverage detected