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)
| 5421 | |
| 5422 | |
| 5423 | def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, error, cppvar=None): |
| 5424 | """Checks rules from the 'C++ style rules' section of cppguide.html. |
| 5425 | |
| 5426 | Most of these rules are hard to test (naming, comment style), but we |
| 5427 | do what we can. In particular we check for 2-space indents, line lengths, |
| 5428 | tab usage, spaces inside code, etc. |
| 5429 | |
| 5430 | Args: |
| 5431 | filename: The name of the current file. |
| 5432 | clean_lines: A CleansedLines instance containing the file. |
| 5433 | linenum: The number of the line to check. |
| 5434 | file_extension: The extension (without the dot) of the filename. |
| 5435 | nesting_state: A NestingState instance which maintains information about |
| 5436 | the current stack of nested blocks being parsed. |
| 5437 | error: The function to call with any errors found. |
| 5438 | cppvar: The header guard variable returned by GetHeaderGuardCPPVar. |
| 5439 | """ |
| 5440 | |
| 5441 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 5442 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 5443 | # raw strings, |
| 5444 | raw_lines = clean_lines.lines_without_raw_strings |
| 5445 | line = raw_lines[linenum] |
| 5446 | prev = raw_lines[linenum - 1] if linenum > 0 else "" |
| 5447 | |
| 5448 | if line.find("\t") != -1: |
| 5449 | error(filename, linenum, "whitespace/tab", 1, "Tab found; better to use spaces") |
| 5450 | |
| 5451 | # One or three blank spaces at the beginning of the line is weird; it's |
| 5452 | # hard to reconcile that with 2-space indents. |
| 5453 | # NOTE: here are the conditions rob pike used for his tests. Mine aren't |
| 5454 | # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces |
| 5455 | # if(RLENGTH > 20) complain = 0; |
| 5456 | # if(match($0, " +(error|private|public|protected):")) complain = 0; |
| 5457 | # if(match(prev, "&& *$")) complain = 0; |
| 5458 | # if(match(prev, "\\|\\| *$")) complain = 0; |
| 5459 | # if(match(prev, "[\",=><] *$")) complain = 0; |
| 5460 | # if(match($0, " <<")) complain = 0; |
| 5461 | # if(match(prev, " +for \\(")) complain = 0; |
| 5462 | # if(prevodd && match(prevprev, " +for \\(")) complain = 0; |
| 5463 | scope_or_label_pattern = r"\s*(?:public|private|protected|signals)(?:\s+(?:slots\s*)?)?:\s*\\?$" |
| 5464 | classinfo = nesting_state.InnermostClass() |
| 5465 | initial_spaces = 0 |
| 5466 | cleansed_line = clean_lines.elided[linenum] |
| 5467 | while initial_spaces < len(line) and line[initial_spaces] == " ": |
| 5468 | initial_spaces += 1 |
| 5469 | # There are certain situations we allow one space, notably for |
| 5470 | # section labels, and also lines containing multi-line raw strings. |
| 5471 | # We also don't check for lines that look like continuation lines |
| 5472 | # (of lines ending in double quotes, commas, equals, or angle brackets) |
| 5473 | # because the rules for how to indent those are non-trivial. |
| 5474 | if ( |
| 5475 | not re.search(r'[",=><] *$', prev) |
| 5476 | and (initial_spaces in {1, 3}) |
| 5477 | and not re.match(scope_or_label_pattern, cleansed_line) |
| 5478 | and not (clean_lines.raw_lines[linenum] != line and re.match(r'^\s*""', line)) |
| 5479 | ): |
| 5480 | error( |
no test coverage detected