Skip to content

Commit 61717ae

Browse files
author
Andrew Pardoe
committed
Merge branch 'python-fix' of https://github.com/tkruse/CppCoreGuidelines into tkruse-python-fix
2 parents 3f33bd9 + 084eb29 commit 61717ae

3 files changed

Lines changed: 71 additions & 12 deletions

File tree

‎scripts/python/Makefile.in‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ CXX_SRCS := $(wildcard *.cpp)
1111
CXX_LINT := ${CXX_SRCS:.cpp=.lint}
1212

1313
.PHONY: cpplint-all
14+
<<<<<<< HEAD
1415
cpplint-all: $(CXX_LINT)
1516

1617
%.lint: %.cpp
1718
@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)
19+
=======
20+
cpplint-all:
21+
@python ../../python/cpplint_wrap.py *.cpp
22+
>>>>>>> 084eb29d426688617b6e19846f508b6d1cb3a564
1823

‎scripts/python/cpplint_wrap.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## wraps local cpplint to produce verbose output without code harness
2+
import cpplint
3+
import sys
4+
5+
def main():
6+
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(' ')
7+
8+
result = False
9+
files = sys.argv[1:]
10+
for loopfile in files:
11+
newargs = FILTERS + [loopfile]
12+
sys.argv = newargs
13+
14+
try:
15+
cpplint.main()
16+
except SystemExit as e:
17+
last_result = e.args[0]
18+
result = result or last_result
19+
if (last_result):
20+
write_code_lines(loopfile)
21+
sys.exit(result)
22+
23+
def write_code_lines(filename):
24+
with open(filename, 'r') as f:
25+
linenum = 1
26+
for line in f:
27+
if (not '// by md-split' in line):
28+
sys.stdout.write('%3d %s' % (linenum, line))
29+
linenum += 1
30+
31+
if __name__ == '__main__':
32+
main()

‎scripts/python/md-split.py‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co
108108
if (not line.strip() == '```'):
109109
if ('???' == no_comment_line or '...' == no_comment_line):
110110
has_question_marks = True
111-
linebuffer.append(dedent(line) if not fenced else line)
111+
linebuffer.append(dedent(line, indent_depth) if not fenced else line)
112112
try:
113113
line = read_filehandle.next()
114114
linenum += 1
@@ -118,10 +118,33 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co
118118
codefile = os.path.join(codedir, '%s%s.cpp' % (name, index))
119119
if fenced:
120120
text_filehandle.write('\n')
121+
121122
if (has_actual_code and not has_question_marks):
122-
# add commonly used headers, so that lines can compile
123-
with io.open(codefile, 'w') as code_filehandle:
124-
code_filehandle.write('''\
123+
linebuffer = clean_trailing_newlines(linebuffer)
124+
write_with_harness(codefile, sourcefile, start_linenum, linebuffer)
125+
return (line, linenum)
126+
127+
128+
def clean_trailing_newlines(linebuffer):
129+
result = []
130+
code_started = False
131+
linebuffer.reverse()
132+
for line in linebuffer:
133+
if not code_started and line == '\n':
134+
continue
135+
code_started = True
136+
result.append(line)
137+
result.reverse()
138+
return result
139+
140+
141+
def write_with_harness(codefile, sourcefile, start_linenum, linebuffer):
142+
'''write output with additional lines to make code likely compilable'''
143+
# add commonly used headers, so that lines can likely compile.
144+
# This is work in progress, the main issue remains handling class
145+
# declarations in in-function code differently
146+
with io.open(codefile, 'w') as code_filehandle:
147+
code_filehandle.write('''\
125148
#include<stdio.h> // by md-split
126149
#include<stdlib.h> // by md-split
127150
#include<tuple> // by md-split
@@ -137,10 +160,9 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co
137160
using namespace std; // by md-split
138161
// %s : %s
139162
''' % (sourcefile, start_linenum))
140-
# TODO: if not toplevel code, wrap inside class
141-
for codeline in linebuffer:
142-
code_filehandle.write(codeline)
143-
return (line, linenum)
163+
# TODO: if not toplevel code, wrap inside class
164+
for codeline in linebuffer:
165+
code_filehandle.write(codeline)
144166

145167

146168
def is_code(line, indent_depth = 4):
@@ -150,17 +172,17 @@ def is_code(line, indent_depth = 4):
150172
return 0
151173

152174
def is_inside_code(line, indent_depth):
153-
return is_code(line, indent_depth) or line.strip() == ''
175+
return is_code(line, indent_depth) > 0 or line.strip() == ''
154176

155177
def stripped(line):
156178
# Remove well-formed html tags, fixing mistakes by legitimate users
157179
sline = TAG_REGEX.sub('', line)
158180
sline = re.sub('[()\[\]#*]', ' ', line)
159181
return sline
160182

161-
def dedent(line):
162-
if line.startswith(' '):
163-
return line[4:]
183+
def dedent(line, indent_depth):
184+
if line.startswith(' ' * indent_depth):
185+
return line[indent_depth:]
164186
if line.startswith('\t'):
165187
return line[1:]
166188
return line

0 commit comments

Comments
 (0)