Add yapf and reformat python code

This commit is contained in:
Wilson Snyder 2020-12-18 22:34:14 -05:00
parent a16ebaf79c
commit ec4e408b2b
6 changed files with 125 additions and 77 deletions

View File

@ -461,6 +461,8 @@ analyzer-include:
-rm -rf examples/*/obj* -rm -rf examples/*/obj*
scan-build $(MAKE) -k examples scan-build $(MAKE) -k examples
format: clang-format yapf
CLANGFORMAT = clang-format CLANGFORMAT = clang-format
CLANGFORMAT_FLAGS = -i CLANGFORMAT_FLAGS = -i
CLANGFORMAT_FILES = $(CPPCHECK_CPP) $(CPPCHECK_H) $(CPPCHECK_YL) test_regress/t/*.c* test_regress/t/*.h CLANGFORMAT_FILES = $(CPPCHECK_CPP) $(CPPCHECK_H) $(CPPCHECK_YL) test_regress/t/*.c* test_regress/t/*.h
@ -470,6 +472,17 @@ clang-format:
|| echo "*** You are not using clang-format 10.0, indents may differ from master's ***" || echo "*** You are not using clang-format 10.0, indents may differ from master's ***"
$(CLANGFORMAT) $(CLANGFORMAT_FLAGS) $(CLANGFORMAT_FILES) $(CLANGFORMAT) $(CLANGFORMAT_FLAGS) $(CLANGFORMAT_FILES)
YAPF = yapf
YAPF_FLAGS = -i
YAPF_FILES = \
nodist/fuzzer/actual_fail \
nodist/fuzzer/generate_dictionary \
examples/xml_py/vl_file_copy \
examples/xml_py/vl_hier_graph \
yapf:
$(YAPF) $(YAPF_FLAGS) $(YAPF_FILES)
ftp: info ftp: info
install-msg: install-msg:

View File

@ -14,8 +14,10 @@ from pprint import pprint,pformat
####################################################################### #######################################################################
class VlFileCopy: class VlFileCopy:
def __init__(self, def __init__(
self,
verilator_args, # presently all verilator options are passed-thru verilator_args, # presently all verilator options are passed-thru
# ideally this script would check against options mentioned in help # ideally this script would check against options mentioned in help
debug=0, debug=0,
@ -24,10 +26,13 @@ class VlFileCopy:
xml_temp = tempfile.NamedTemporaryFile() xml_temp = tempfile.NamedTemporaryFile()
args = ['--xml-output', xml_temp.name, args = [
'--xml-output',
xml_temp.name,
'--bbox-sys', # Parse some stuff can't translate '--bbox-sys', # Parse some stuff can't translate
'--bbox-unsup', '--bbox-unsup',
'--prefix vlxml'] # So we know name of .xml output '--prefix vlxml'
] # So we know name of .xml output
args += verilator_args args += verilator_args
self.run_verilator(args) self.run_verilator(args)
self.tree = ET.parse(xml_temp.name) self.tree = ET.parse(xml_temp.name)
@ -55,7 +60,9 @@ class VlFileCopy:
print("\t%s " % command) print("\t%s " % command)
status = subprocess.call(command, shell=True) status = subprocess.call(command, shell=True)
if status != 0: if status != 0:
raise Exception("Command failed running Verilator with '"+command+"', stopped") raise Exception("Command failed running Verilator with '" +
command + "', stopped")
####################################################################### #######################################################################
@ -72,8 +79,7 @@ Example usage:
vl_file_copy -f input.vc top.v -odir mycopy vl_file_copy -f input.vc top.v -odir mycopy
# This will make at least mycopy/top.v # This will make at least mycopy/top.v
""", """,
epilog= epilog="""All other arguments are pass-thru to Verilator: e.g.:
"""All other arguments are pass-thru to Verilator: e.g.:
+define+<var>=<value> Set preprocessor define +define+<var>=<value> Set preprocessor define
-F <file> Parse options from a file, relatively -F <file> Parse options from a file, relatively
@ -87,26 +93,30 @@ Example usage:
This file ONLY is placed under the Creative Commons Public Domain, for This file ONLY is placed under the Creative Commons Public Domain, for
any use, without warranty, 2019 by Wilson Snyder. any use, without warranty, 2019 by Wilson Snyder.
SPDX-License-Identifier: CC0-1.0 SPDX-License-Identifier: CC0-1.0
""" """)
) parser.add_argument('-debug',
parser.add_argument('-debug', '--debug', '--debug',
action='store_const', const=9, action='store_const',
const=9,
help='enable debug') help='enable debug')
parser.add_argument('-odir', '--odir', parser.add_argument('-odir',
action='store', metavar='directory', required=True, '--odir',
action='store',
metavar='directory',
required=True,
help='target output directory') help='target output directory')
(args, rem) = parser.parse_known_args() (args, rem) = parser.parse_known_args()
print("NOTE: vl_file_copy is only an example starting point for writing your own tool.") print(
"NOTE: vl_file_copy is only an example starting point for writing your own tool."
)
# That is: # That is:
# 1. We will accept basic patches # 1. We will accept basic patches
# 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir) # 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir)
# 3. "make install" will not install this. # 3. "make install" will not install this.
# 4. This has not had production-worthy validation. # 4. This has not had production-worthy validation.
fc = VlFileCopy(output_dir = args.odir, fc = VlFileCopy(output_dir=args.odir, debug=args.debug, verilator_args=rem)
debug = args.debug,
verilator_args = rem)
###################################################################### ######################################################################
### Local Variables: ### Local Variables:

View File

@ -14,8 +14,10 @@ from pprint import pprint,pformat
####################################################################### #######################################################################
class VlHierGraph: class VlHierGraph:
def __init__(self, def __init__(
self,
verilator_args, # presently all verilator options are passed-thru verilator_args, # presently all verilator options are passed-thru
# ideally this script would check against options mentioned in help # ideally this script would check against options mentioned in help
debug=0, debug=0,
@ -26,10 +28,13 @@ class VlHierGraph:
xml_temp = tempfile.NamedTemporaryFile() xml_temp = tempfile.NamedTemporaryFile()
args = ['--xml-output', xml_temp.name, args = [
'--xml-output',
xml_temp.name,
'--bbox-sys', # Parse some stuff can't translate '--bbox-sys', # Parse some stuff can't translate
'--bbox-unsup', '--bbox-unsup',
'--prefix vlxml'] # So we know name of .xml output '--prefix vlxml'
] # So we know name of .xml output
args += verilator_args args += verilator_args
self.run_verilator(args) self.run_verilator(args)
self.tree = ET.parse(xml_temp.name) self.tree = ET.parse(xml_temp.name)
@ -39,7 +44,9 @@ class VlHierGraph:
fh.write("digraph {\n") fh.write("digraph {\n")
fh.write(" dpi=300;\n") fh.write(" dpi=300;\n")
fh.write(" order=LR;\n") fh.write(" order=LR;\n")
fh.write(" node [fontsize=8 shape=\"box\" margin=0.01 width=0 height=0]") fh.write(
" node [fontsize=8 shape=\"box\" margin=0.01 width=0 height=0]"
)
fh.write(" edge [fontsize=6]") fh.write(" edge [fontsize=6]")
# Find cells # Find cells
root = self.tree.getroot() root = self.tree.getroot()
@ -48,8 +55,7 @@ class VlHierGraph:
# origNames are before parameterization, name if after # origNames are before parameterization, name if after
mod_name = module.get('name') mod_name = module.get('name')
mod_number = self.name_to_vertex_number(mod_name) mod_number = self.name_to_vertex_number(mod_name)
fh.write(" n%d [label=\"%s\"" fh.write(" n%d [label=\"%s\"" % (mod_number, mod_name))
% (mod_number, mod_name))
if module.get('topModule'): if module.get('topModule'):
fh.write(" color=\"red\" rank=1") fh.write(" color=\"red\" rank=1")
fh.write("];\n") fh.write("];\n")
@ -58,8 +64,8 @@ class VlHierGraph:
inst_name = instance.get('name') inst_name = instance.get('name')
def_name = instance.get('defName') def_name = instance.get('defName')
def_number = self.name_to_vertex_number(def_name) def_number = self.name_to_vertex_number(def_name)
fh.write(" n%d->n%d [label=\"%s\"];\n" fh.write(" n%d->n%d [label=\"%s\"];\n" %
% (mod_number, def_number, inst_name)); (mod_number, def_number, inst_name))
fh.write("}\n") fh.write("}\n")
@ -80,7 +86,9 @@ class VlHierGraph:
print("\t%s " % command) print("\t%s " % command)
status = subprocess.call(command, shell=True) status = subprocess.call(command, shell=True)
if status != 0: if status != 0:
raise Exception("Command failed running Verilator with '"+command+"', stopped") raise Exception("Command failed running Verilator with '" +
command + "', stopped")
####################################################################### #######################################################################
@ -96,8 +104,7 @@ Example usage:
vl_hier_graph -f input.vc top.v -o graph.dot vl_hier_graph -f input.vc top.v -o graph.dot
dot -Tpdf -o graph.pdf graph.dot dot -Tpdf -o graph.pdf graph.dot
""", """,
epilog= epilog="""All other arguments are pass-thru to Verilator: e.g.:
"""All other arguments are pass-thru to Verilator: e.g.:
+define+<var>=<value> Set preprocessor define +define+<var>=<value> Set preprocessor define
-F <file> Parse options from a file, relatively -F <file> Parse options from a file, relatively
@ -111,17 +118,23 @@ Example usage:
This file ONLY is placed under the Creative Commons Public Domain, for This file ONLY is placed under the Creative Commons Public Domain, for
any use, without warranty, 2019 by Wilson Snyder. any use, without warranty, 2019 by Wilson Snyder.
SPDX-License-Identifier: CC0-1.0 SPDX-License-Identifier: CC0-1.0
""" """)
) parser.add_argument('-debug',
parser.add_argument('-debug', '--debug', '--debug',
action='store_const', const=9, action='store_const',
const=9,
help='enable debug') help='enable debug')
parser.add_argument('-o', '--o', parser.add_argument('-o',
action='store', metavar='filename', required=True, '--o',
action='store',
metavar='filename',
required=True,
help='output filename') help='output filename')
(args, rem) = parser.parse_known_args() (args, rem) = parser.parse_known_args()
print("NOTE: vl_hier_graph is only an example starting point for writing your own tool.") print(
"NOTE: vl_hier_graph is only an example starting point for writing your own tool."
)
# That is: # That is:
# 1. We will accept basic patches # 1. We will accept basic patches
# 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir) # 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir)

View File

@ -16,6 +16,7 @@ from glob import glob
from subprocess import getstatusoutput from subprocess import getstatusoutput
from argparse import ArgumentParser from argparse import ArgumentParser
def interesting(s): def interesting(s):
if 'assert' in s: return 1 if 'assert' in s: return 1
if 'Assert' in s: return 1 if 'Assert' in s: return 1
@ -39,11 +40,13 @@ def main():
for infile in glob(args.dir + '/*'): for infile in glob(args.dir + '/*'):
# Input filenames are known not to contain spaces or other unusual # Input filenames are known not to contain spaces or other unusual
# characters, therefore this works. # characters, therefore this works.
status,output = getstatusoutput('../../bin/verilator_bin --cc '+infile) status, output = getstatusoutput('../../bin/verilator_bin --cc ' +
infile)
if interesting(output): if interesting(output):
print(infile) print(infile)
print(status) print(status)
print(output) print(output)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -15,6 +15,7 @@
from subprocess import getstatusoutput from subprocess import getstatusoutput
from os import system from os import system
def take_while(f, a): def take_while(f, a):
# any(a) => (a->bool)->[a]->[a] # any(a) => (a->bool)->[a]->[a]
# Does the same think as Haskell's takewhile. # Does the same think as Haskell's takewhile.
@ -26,6 +27,7 @@ def take_while(f,a):
return out return out
return out return out
def skip_while(f, a): def skip_while(f, a):
# any(a) => (a->bool)->[a]->[a] # any(a) => (a->bool)->[a]->[a]
# Basically, the opposite thing from skipwhile # Basically, the opposite thing from skipwhile
@ -33,16 +35,19 @@ def skip_while(f,a):
a = a[1:] a = a[1:]
return a return a
def print_lines(a): def print_lines(a):
# printable(a) => [a]->void # printable(a) => [a]->void
for elem in a: for elem in a:
print(elem) print(elem)
def write_file(filename, contents): def write_file(filename, contents):
# str->str->void # str->str->void
f = open(filename, 'w') f = open(filename, 'w')
f.write(contents) f.write(contents)
def parse_line(s): def parse_line(s):
# str->maybe str # str->maybe str
if len(s) == 0: return if len(s) == 0: return
@ -51,6 +56,7 @@ def parse_line(s):
literal_part = take_while(lambda x: x != '"', part[1:]) literal_part = take_while(lambda x: x != '"', part[1:])
return ''.join(filter(lambda x: x != '\\', literal_part)) return ''.join(filter(lambda x: x != '\\', literal_part))
def main(): def main():
status, output = getstatusoutput('flex -T ../../src/verilog.l') status, output = getstatusoutput('flex -T ../../src/verilog.l')
assert status == 0 assert status == 0
@ -65,5 +71,6 @@ def main():
for i, token in enumerate(tokens): for i, token in enumerate(tokens):
write_file(dirname + '/' + str(i), token) write_file(dirname + '/' + str(i), token)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -82,6 +82,8 @@ config_rev.h: ${srcdir}/config_rev.pl $(GIT_CHANGE_DEP)
$(PERL) ${srcdir}/config_rev.pl ${srcdir} >$@ $(PERL) ${srcdir}/config_rev.pl ${srcdir} >$@
# Human convenience # Human convenience
format:
$(MAKE) -C .. $@
clang-format: clang-format:
$(MAKE) -C .. $@ $(MAKE) -C .. $@