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)
| 4309 | |
| 4310 | |
| 4311 | def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, |
| 4312 | error): |
| 4313 | """Checks rules from the 'C++ style rules' section of cppguide.html. |
| 4314 | |
| 4315 | Most of these rules are hard to test (naming, comment style), but we |
| 4316 | do what we can. In particular we check for 2-space indents, line lengths, |
| 4317 | tab usage, spaces inside code, etc. |
| 4318 | |
| 4319 | Args: |
| 4320 | filename: The name of the current file. |
| 4321 | clean_lines: A CleansedLines instance containing the file. |
| 4322 | linenum: The number of the line to check. |
| 4323 | file_extension: The extension (without the dot) of the filename. |
| 4324 | nesting_state: A NestingState instance which maintains information about |
| 4325 | the current stack of nested blocks being parsed. |
| 4326 | error: The function to call with any errors found. |
| 4327 | """ |
| 4328 | |
| 4329 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 4330 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 4331 | # raw strings, |
| 4332 | raw_lines = clean_lines.lines_without_raw_strings |
| 4333 | line = raw_lines[linenum] |
| 4334 | prev = raw_lines[linenum - 1] if linenum > 0 else '' |
| 4335 | |
| 4336 | if line.find('\t') != -1: |
| 4337 | error(filename, linenum, 'whitespace/tab', 1, |
| 4338 | 'Tab found; better to use spaces') |
| 4339 | |
| 4340 | # One or three blank spaces at the beginning of the line is weird; it's |
| 4341 | # hard to reconcile that with 2-space indents. |
| 4342 | # NOTE: here are the conditions rob pike used for his tests. Mine aren't |
| 4343 | # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces |
| 4344 | # if(RLENGTH > 20) complain = 0; |
| 4345 | # if(match($0, " +(error|private|public|protected):")) complain = 0; |
| 4346 | # if(match(prev, "&& *$")) complain = 0; |
| 4347 | # if(match(prev, "\\|\\| *$")) complain = 0; |
| 4348 | # if(match(prev, "[\",=><] *$")) complain = 0; |
| 4349 | # if(match($0, " <<")) complain = 0; |
| 4350 | # if(match(prev, " +for \\(")) complain = 0; |
| 4351 | # if(prevodd && match(prevprev, " +for \\(")) complain = 0; |
| 4352 | scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$' |
| 4353 | classinfo = nesting_state.InnermostClass() |
| 4354 | initial_spaces = 0 |
| 4355 | cleansed_line = clean_lines.elided[linenum] |
| 4356 | while initial_spaces < len(line) and line[initial_spaces] == ' ': |
| 4357 | initial_spaces += 1 |
| 4358 | # There are certain situations we allow one space, notably for |
| 4359 | # section labels, and also lines containing multi-line raw strings. |
| 4360 | # We also don't check for lines that look like continuation lines |
| 4361 | # (of lines ending in double quotes, commas, equals, or angle brackets) |
| 4362 | # because the rules for how to indent those are non-trivial. |
| 4363 | if (not Search(r'[",=><] *$', prev) and |
| 4364 | (initial_spaces == 1 or initial_spaces == 3) and |
| 4365 | not Match(scope_or_label_pattern, cleansed_line) and |
| 4366 | not (clean_lines.raw_lines[linenum] != line and |
| 4367 | Match(r'^\s*""', line))): |
| 4368 | error(filename, linenum, 'whitespace/indent', 3, |
no test coverage detected