Hi All, It would be useful to automated tools if check_GNU_style[_lib] supported returning the result in a structured format like json.
With this change calling: > cat patch | ./contrib/check_GNU_style.py --format json - | jq . produces: [ { "type": 1, "msg": "lines should not exceed 80 characters", "count": 22, "errors": [ { "file": "gcc/fortran/trans-array.cc", "row": "4893", "column": "80", "err": " && (ss_info->expr->value.function.isym->id == GFC_ISYM_MINLOC" }, { "file": "gcc/fortran/trans-array.cc", "row": "4931", "column": "80", "err": " tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node," }, { Ok for master? Thanks, Tamar contrib/ChangeLog: * check_GNU_style.py: Add json format. * check_GNU_style_lib.py: Likewise. --- diff --git a/contrib/check_GNU_style.py b/contrib/check_GNU_style.py index 6b946a5bc3610b8ef70ba372ea800f892eeac85b..0890947f1f9b60c37ff62e23007c3a0735fd9c14 100755 --- a/contrib/check_GNU_style.py +++ b/contrib/check_GNU_style.py @@ -31,7 +31,7 @@ def main(): parser.add_argument('file', help = 'File with a patch') parser.add_argument('-f', '--format', default = 'stdio', help = 'Display format', - choices = ['stdio', 'quickfix']) + choices = ['stdio', 'quickfix', 'json']) args = parser.parse_args() filename = args.file format = args.format diff --git a/contrib/check_GNU_style_lib.py b/contrib/check_GNU_style_lib.py index 6dbe4b53559c63d2e0276d0ff88619cd2f7f8e06..f2d7527487007ab125c4b9c27ebf2b0d97df6fea 100755 --- a/contrib/check_GNU_style_lib.py +++ b/contrib/check_GNU_style_lib.py @@ -29,6 +29,7 @@ import sys import re import unittest +import json def import_pip3(*args): missing=[] @@ -317,6 +318,30 @@ def check_GNU_style_file(file, format): else: print('%d error(s) written to %s file.' % (len(errors), f)) exit(1) + elif format == 'json': + fn = lambda x: x.error_message + i = 1 + result = [] + for (k, errors) in groupby(sorted(errors, key = fn), fn): + errors = list(errors) + entry = {} + entry['type'] = i + entry['msg'] = k + entry['count'] = len(errors) + i += 1 + errlines = [] + for e in errors: + locs = e.error_location ().split(':') + errlines.append({ "file" : locs[0], "row" : locs[1], "column" : locs[2], "err" : e.console_error}) + entry['errors'] = errlines + result.append (entry) + + if len(errors) == 0: + exit(0) + else: + json_string = json.dumps(result) + print (json_string) + exit(1) else: assert False --
diff --git a/contrib/check_GNU_style.py b/contrib/check_GNU_style.py index 6b946a5bc3610b8ef70ba372ea800f892eeac85b..0890947f1f9b60c37ff62e23007c3a0735fd9c14 100755 --- a/contrib/check_GNU_style.py +++ b/contrib/check_GNU_style.py @@ -31,7 +31,7 @@ def main(): parser.add_argument('file', help = 'File with a patch') parser.add_argument('-f', '--format', default = 'stdio', help = 'Display format', - choices = ['stdio', 'quickfix']) + choices = ['stdio', 'quickfix', 'json']) args = parser.parse_args() filename = args.file format = args.format diff --git a/contrib/check_GNU_style_lib.py b/contrib/check_GNU_style_lib.py index 6dbe4b53559c63d2e0276d0ff88619cd2f7f8e06..f2d7527487007ab125c4b9c27ebf2b0d97df6fea 100755 --- a/contrib/check_GNU_style_lib.py +++ b/contrib/check_GNU_style_lib.py @@ -29,6 +29,7 @@ import sys import re import unittest +import json def import_pip3(*args): missing=[] @@ -317,6 +318,30 @@ def check_GNU_style_file(file, format): else: print('%d error(s) written to %s file.' % (len(errors), f)) exit(1) + elif format == 'json': + fn = lambda x: x.error_message + i = 1 + result = [] + for (k, errors) in groupby(sorted(errors, key = fn), fn): + errors = list(errors) + entry = {} + entry['type'] = i + entry['msg'] = k + entry['count'] = len(errors) + i += 1 + errlines = [] + for e in errors: + locs = e.error_location ().split(':') + errlines.append({ "file" : locs[0], "row" : locs[1], "column" : locs[2], "err" : e.console_error}) + entry['errors'] = errlines + result.append (entry) + + if len(errors) == 0: + exit(0) + else: + json_string = json.dumps(result) + print (json_string) + exit(1) else: assert False