sarif-to-dot: cleanup for and preparation for sarif table extraction

This commit is contained in:
Michael Hohn
2022-02-01 22:41:29 -08:00
committed by =Michael Hohn
parent c664ae2f8f
commit cf8096446b
4 changed files with 14 additions and 10 deletions

View File

@@ -15,7 +15,8 @@ context = S.Context(
"string" : "String",
"int" : "Int",
"bool" : "Bool"
})
}
)
parser = argparse.ArgumentParser(description='Produce a summary of signatures found in the sarif file.')
parser.add_argument('file', metavar='sarif-file', type=str, help='input file, - for stdin')
@@ -65,7 +66,7 @@ if args.dot_output:
elif args.typedef_signatures:
S._signature(args, sarif_struct, context)
struct_graph = [(typedef, sig) for sig,typedef in context.sig_to_typedef.items()]
pprint(struct_graph, sys.stdout, indent=2)
pprint(struct_graph, sys.stdout, indent=4)
else:
pprint(S._signature(args, sarif_struct, context), sys.stdout, indent=2)
pprint(S._signature(args, sarif_struct, context), sys.stdout, indent=4)

Binary file not shown.

View File

@@ -30,16 +30,15 @@ import zlib
#
@dataclass
class Context:
sig_to_typedef: dict # signature to typedef name map
sig_to_typedef: dict # signature -> typedef name map
def shorthash(signature):
return zlib.adler32(str(signature).encode('utf-8')) % 10000
#
# Signature formation
#
def _signature_dict(args, elem, context):
def _signature_dict(args, elem, context: Context):
""" Assemble and return the signature for a dictionary.
"""
# Collect signatures
@@ -55,7 +54,9 @@ def _signature_dict(args, elem, context):
# Give every unique struct a name and use a reference to it as value.
if signature not in context.sig_to_typedef:
context.sig_to_typedef[signature] = "Struct%04d" % shorthash(signature)
signature = context.sig_to_typedef[signature]
typedef = context.sig_to_typedef[signature]
return typedef
else:
return signature
def _signature_list(args, elem, context):
@@ -79,7 +80,9 @@ def _signature_list(args, elem, context):
# Give every unique array a name and use a reference to it as value.
if signature not in context.sig_to_typedef:
context.sig_to_typedef[signature] = "Array%04d" % shorthash(signature)
signature = context.sig_to_typedef[signature]
typedef = context.sig_to_typedef[signature]
return typedef
else:
return signature
def _signature(args, elem, context):