MCPcopy Index your code
hub / github.com/cpplint/cpplint / CheckStyle

Function CheckStyle

cpplint.py:5423–5565  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

5421
5422
5423def 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(

Callers 1

ProcessLineFunction · 0.85

Calls 15

IsHeaderExtensionFunction · 0.85
GetLineWidthFunction · 0.85
GetPreviousNonBlankLineFunction · 0.85
CheckBracesFunction · 0.85
CheckTrailingSemicolonFunction · 0.85
CheckEmptyBlockBodyFunction · 0.85
CheckSpacingFunction · 0.85
CheckOperatorSpacingFunction · 0.85
CheckParenthesisSpacingFunction · 0.85
CheckCommaSpacingFunction · 0.85
CheckBracesSpacingFunction · 0.85

Tested by

no test coverage detected