diff options
author | Martin Mathieson <martin.mathieson@keysight.com> | 2020-09-26 22:33:21 +0100 |
---|---|---|
committer | Martin Mathieson <martin.r.mathieson@googlemail.com> | 2020-09-28 08:01:21 +0000 |
commit | 6dd9c098ad94c366f5248cfe2d48e86763046f44 (patch) | |
tree | 4aa3b4196aaa96ccd667a5cd71aef24eb38185bf | |
parent | 24ef8400fe967d286435e3f732c12729aa990999 (diff) |
check_typed_item_calls.py: add --consecutive flag
Add a check to point out where consecutive items have the same filter
but different labels. Quite a few of these look like bugs.
Also, make some REs raw strings, as identified as an issue in
https://gitlab.com/wireshark/wireshark/-/merge_requests/346
-rwxr-xr-x | tools/check_spelling.py | 8 | ||||
-rwxr-xr-x | tools/check_tfs.py | 6 | ||||
-rwxr-xr-x | tools/check_typed_item_calls.py | 61 |
3 files changed, 59 insertions, 16 deletions
diff --git a/tools/check_spelling.py b/tools/check_spelling.py index 1f910148d4..f8b1309273 100755 --- a/tools/check_spelling.py +++ b/tools/check_spelling.py @@ -53,7 +53,7 @@ missing_words = [] # Split camelCase string into separate words. def camelCaseSplit(identifier): - matches = re.finditer('.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)', identifier) + matches = re.finditer(r'.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)', identifier) return [m.group(0) for m in matches] @@ -191,9 +191,9 @@ class File: missing_words.append(word) def removeComments(code_string): - code_string = re.sub(re.compile("/\*.*?\*/",re.DOTALL ) ,"" ,code_string) # C-style comment + code_string = re.sub(re.compile(r"/\*.*?\*/",re.DOTALL ) ,"" ,code_string) # C-style comment # Remove this for now as can get tripped up if see htpps://www.... within a string! - #code_string = re.sub(re.compile("//.*?\n" ) ,"" ,code_string) # C++-style comment + #code_string = re.sub(re.compile(r"//.*?\n" ) ,"" ,code_string) # C++-style comment return code_string def removeSingleQuotes(code_string): @@ -364,7 +364,7 @@ else: # If scanning a subset of files, list them here. print('Examining:') -if args.file or args.commits or args.open: +if args.file or args.folder or args.commits or args.open: if files: print(' '.join(files), '\n') else: diff --git a/tools/check_tfs.py b/tools/check_tfs.py index 89581d00f0..5d8a06d740 100755 --- a/tools/check_tfs.py +++ b/tools/check_tfs.py @@ -60,8 +60,8 @@ class TFS: def removeComments(code_string): - code_string = re.sub(re.compile("/\*.*?\*/",re.DOTALL ) ,"" ,code_string) # C-style comment - code_string = re.sub(re.compile("//.*?\n" ) ,"" ,code_string) # C++-style comment + code_string = re.sub(re.compile(r"/\*.*?\*/",re.DOTALL ) ,"" ,code_string) # C-style comment + code_string = re.sub(re.compile(r"//.*?\n" ) ,"" ,code_string) # C++-style comment return code_string @@ -89,7 +89,7 @@ def findItems(filename): def is_dissector_file(filename): - p = re.compile('.*packet-.*\.c') + p = re.compile(r'.*packet-.*\.c') return p.match(filename) def findDissectorFilesInFolder(folder): diff --git a/tools/check_typed_item_calls.py b/tools/check_typed_item_calls.py index 3bc7a6ed54..122d46d775 100755 --- a/tools/check_typed_item_calls.py +++ b/tools/check_typed_item_calls.py @@ -113,11 +113,22 @@ field_widths = { # The relevant parts of an hf item. Used as value in dict where hf variable name is key. class Item: - def __init__(self, filename, filter, label, item_type, mask=None, check_mask=False, check_label=False): + + previousItem = None + + def __init__(self, filename, filter, label, item_type, mask=None, check_mask=False, check_label=False, check_consecutive=False): self.filename = filename self.filter = filter self.label = label + if check_consecutive: + if Item.previousItem and Item.previousItem.filter == filter: + if label != Item.previousItem.label: + print('Warn: ' + filename + ': - filter "' + filter + + '" appears consecutively - labels are "' + Item.previousItem.label + '" and "' + label + '"') + Item.previousItem = self + + # Optionally check label. if check_label: if label.startswith(' ') or label.endswith(' '): @@ -241,12 +252,39 @@ apiChecks.append(APICheck('proto_tree_add_none_format', { 'FT_NONE'})) def removeComments(code_string): - code_string = re.sub(re.compile("/\*.*?\*/",re.DOTALL ) ,"" ,code_string) # C-style comment - code_string = re.sub(re.compile("//.*?\n" ) ,"" ,code_string) # C++-style comment + code_string = re.sub(re.compile(r"/\*.*?\*/",re.DOTALL ) ,"" , code_string) # C-style comment + code_string = re.sub(re.compile(r"//.*?\n" ) ,"" , code_string) # C++-style comment return code_string +# Test for whether the given file was automatically generated. +def isGeneratedFile(filename): + # Open file + f_read = open(os.path.join(filename), 'r') + lines_tested = 0 + for line in f_read: + # The comment to say that its generated is near the top, so give up once + # get a few lines down. + if lines_tested > 10: + f_read.close() + return False + if (line.find('Generated automatically') != -1 or + line.find('Autogenerated from') != -1 or + line.find('is autogenerated') != -1 or + line.find('automatically generated by Pidl') != -1 or + line.find('Created by: The Qt Meta Object Compiler') != -1 or + line.find('This file was generated') != -1): + + f_read.close() + return True + lines_tested = lines_tested + 1 + + # OK, looks like a hand-written file! + f_read.close() + return False + # Look for hf items in a dissector file. -def find_items(filename, check_mask=False, check_label=False): +def find_items(filename, check_mask=False, check_label=False, check_consecutive=False): + is_generated = isGeneratedFile(filename) items = {} with open(filename, 'r') as f: contents = f.read() @@ -257,13 +295,15 @@ def find_items(filename, check_mask=False, check_label=False): # Store this item. hf = m.group(1) items[hf] = Item(filename, filter=m.group(3), label=m.group(2), item_type=m.group(4), mask=m.group(5), - check_mask=check_mask, check_label=check_label) + check_mask=check_mask, + check_label=check_label, + check_consecutive=(not is_generated and check_consecutive)) return items def is_dissector_file(filename): - p = re.compile('.*packet-.*\.c') + p = re.compile(r'.*packet-.*\.c') return p.match(filename) def findDissectorFilesInFolder(folder, dissector_files=[], recursive=False): @@ -287,9 +327,9 @@ def findDissectorFilesInFolder(folder, dissector_files=[], recursive=False): # Check the given dissector file. -def checkFile(filename, check_mask=False, check_label=False): +def checkFile(filename, check_mask=False, check_label=False, check_consecutive=False): # Find important parts of items. - items = find_items(filename, check_mask, check_label) + items = find_items(filename, check_mask, check_label, check_consecutive) # Check each API for c in apiChecks: @@ -313,6 +353,9 @@ parser.add_argument('--mask', action='store_true', help='when set, check mask field too') parser.add_argument('--label', action='store_true', help='when set, check label field too') +parser.add_argument('--consecutive', action='store_true', + help='when set, copy copy/paste errors between consecutive items') + args = parser.parse_args() @@ -372,7 +415,7 @@ else: for f in files: if should_exit: exit(1) - checkFile(f, check_mask=args.mask, check_label=args.label) + checkFile(f, check_mask=args.mask, check_label=args.label, check_consecutive=args.consecutive) # Show summary. print(issues_found, 'issues found') |