Merge branch 'python-fix' of https://github.com/tkruse/CppCoreGuidelines into tkruse-python-fix

This commit is contained in:
Andrew Pardoe 2016-09-12 11:53:55 -07:00
commit 61717ae4eb
3 changed files with 71 additions and 12 deletions

View File

@ -11,8 +11,13 @@ CXX_SRCS := $(wildcard *.cpp)
CXX_LINT := ${CXX_SRCS:.cpp=.lint} CXX_LINT := ${CXX_SRCS:.cpp=.lint}
.PHONY: cpplint-all .PHONY: cpplint-all
<<<<<<< HEAD
cpplint-all: $(CXX_LINT) cpplint-all: $(CXX_LINT)
%.lint: %.cpp %.lint: %.cpp
@python ../../python/cpplint.py --verbose=0 --linelength=100 --filter=-legal/copyright,-build/include_order,-build/namespaces,-build/include,-build/include_subdir,-readability/inheritance,-readability/casting,-readability/namespace,-readability/alt_tokens,-readability/braces,-readability/fn_size,-whitespace/comments,-whitespace/braces,-whitespace/empty_loop_body,-whitespace/indent,-whitespace/newline,-runtime/explicit,-runtime/arrays,-runtime/int,-runtime/references,-runtime/string,-runtime/operator $< || (cat $< | nl -ba | grep -v 'md-split' && false) @python ../../python/cpplint.py --verbose=0 --linelength=100 --filter=-legal/copyright,-build/include_order,-build/namespaces,-build/include,-build/include_subdir,-readability/inheritance,-readability/casting,-readability/namespace,-readability/alt_tokens,-readability/braces,-readability/fn_size,-whitespace/comments,-whitespace/braces,-whitespace/empty_loop_body,-whitespace/indent,-whitespace/newline,-runtime/explicit,-runtime/arrays,-runtime/int,-runtime/references,-runtime/string,-runtime/operator $< || (cat $< | nl -ba | grep -v 'md-split' && false)
=======
cpplint-all:
@python ../../python/cpplint_wrap.py *.cpp
>>>>>>> 084eb29d426688617b6e19846f508b6d1cb3a564

View File

@ -0,0 +1,32 @@
## wraps local cpplint to produce verbose output without code harness
import cpplint
import sys
def main():
FILTERS='cpplint --verbose=0 --linelength=100 --filter=-legal/copyright,-build/include_order,-build/c++11,-build/namespaces,-build/class,-build/include,-build/include_subdir,-readability/inheritance,-readability/function,-readability/casting,-readability/namespace,-readability/alt_tokens,-readability/braces,-readability/fn_size,-whitespace/comments,-whitespace/braces,-whitespace/empty_loop_body,-whitespace/indent,-whitespace/newline,-runtime/explicit,-runtime/arrays,-runtime/int,-runtime/references,-runtime/string,-runtime/operator'.split(' ')
result = False
files = sys.argv[1:]
for loopfile in files:
newargs = FILTERS + [loopfile]
sys.argv = newargs
try:
cpplint.main()
except SystemExit as e:
last_result = e.args[0]
result = result or last_result
if (last_result):
write_code_lines(loopfile)
sys.exit(result)
def write_code_lines(filename):
with open(filename, 'r') as f:
linenum = 1
for line in f:
if (not '// by md-split' in line):
sys.stdout.write('%3d %s' % (linenum, line))
linenum += 1
if __name__ == '__main__':
main()

View File

@ -108,7 +108,7 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co
if (not line.strip() == '```'): if (not line.strip() == '```'):
if ('???' == no_comment_line or '...' == no_comment_line): if ('???' == no_comment_line or '...' == no_comment_line):
has_question_marks = True has_question_marks = True
linebuffer.append(dedent(line) if not fenced else line) linebuffer.append(dedent(line, indent_depth) if not fenced else line)
try: try:
line = read_filehandle.next() line = read_filehandle.next()
linenum += 1 linenum += 1
@ -118,8 +118,31 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co
codefile = os.path.join(codedir, '%s%s.cpp' % (name, index)) codefile = os.path.join(codedir, '%s%s.cpp' % (name, index))
if fenced: if fenced:
text_filehandle.write('\n') text_filehandle.write('\n')
if (has_actual_code and not has_question_marks): if (has_actual_code and not has_question_marks):
# add commonly used headers, so that lines can compile linebuffer = clean_trailing_newlines(linebuffer)
write_with_harness(codefile, sourcefile, start_linenum, linebuffer)
return (line, linenum)
def clean_trailing_newlines(linebuffer):
result = []
code_started = False
linebuffer.reverse()
for line in linebuffer:
if not code_started and line == '\n':
continue
code_started = True
result.append(line)
result.reverse()
return result
def write_with_harness(codefile, sourcefile, start_linenum, linebuffer):
'''write output with additional lines to make code likely compilable'''
# add commonly used headers, so that lines can likely compile.
# This is work in progress, the main issue remains handling class
# declarations in in-function code differently
with io.open(codefile, 'w') as code_filehandle: with io.open(codefile, 'w') as code_filehandle:
code_filehandle.write('''\ code_filehandle.write('''\
#include<stdio.h> // by md-split #include<stdio.h> // by md-split
@ -140,7 +163,6 @@ using namespace std; // by md-split
# TODO: if not toplevel code, wrap inside class # TODO: if not toplevel code, wrap inside class
for codeline in linebuffer: for codeline in linebuffer:
code_filehandle.write(codeline) code_filehandle.write(codeline)
return (line, linenum)
def is_code(line, indent_depth = 4): def is_code(line, indent_depth = 4):
@ -150,7 +172,7 @@ def is_code(line, indent_depth = 4):
return 0 return 0
def is_inside_code(line, indent_depth): def is_inside_code(line, indent_depth):
return is_code(line, indent_depth) or line.strip() == '' return is_code(line, indent_depth) > 0 or line.strip() == ''
def stripped(line): def stripped(line):
# Remove well-formed html tags, fixing mistakes by legitimate users # Remove well-formed html tags, fixing mistakes by legitimate users
@ -158,9 +180,9 @@ def stripped(line):
sline = re.sub('[()\[\]#*]', ' ', line) sline = re.sub('[()\[\]#*]', ' ', line)
return sline return sline
def dedent(line): def dedent(line, indent_depth):
if line.startswith(' '): if line.startswith(' ' * indent_depth):
return line[4:] return line[indent_depth:]
if line.startswith('\t'): if line.startswith('\t'):
return line[1:] return line[1:]
return line return line