|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +path = os.path |
| 6 | + |
| 7 | +needs_an_re = re.compile(r'^(?!Unary)[AEIOU]') # Name requiring "an" instead of "a". |
| 8 | +start_qldoc_re = re.compile(r'^\s*/\*\*') # Start of a QLDoc comment |
| 9 | +end_qldoc_re = re.compile(r'\*/\s*$') # End of a QLDoc comment |
| 10 | +blank_qldoc_line_re = re.compile(r'^\s*\*\s*$') # A line in a QLDoc comment with only the '*' |
| 11 | +instruction_class_re = re.compile(r'^class (?P<name>[A-aa-z0-9]+)Instruction\s') # Declaration of an `Instruction` class |
| 12 | +opcode_base_class_re = re.compile(r'^abstract class (?P<name>[A-aa-z0-9]+)Opcode\s') # Declaration of an `Opcode` base class |
| 13 | +opcode_class_re = re.compile(r'^ class (?P<name>[A-aa-z0-9]+)\s') # Declaration of an `Opcode` class |
| 14 | + |
| 15 | +script_dir = path.realpath(path.dirname(__file__)) |
| 16 | +instruction_path = path.realpath(path.join(script_dir, '../cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll')) |
| 17 | +opcode_path = path.realpath(path.join(script_dir, '../cpp/ql/src/semmle/code/cpp/ir/implementation/Opcode.qll')) |
| 18 | + |
| 19 | +# Scan `Instruction.qll`, keeping track of the QLDoc comment attached to each declaration of a class |
| 20 | +# whose name ends with `Instruction`. |
| 21 | +instruction_comments = {} |
| 22 | +in_qldoc = False |
| 23 | +saw_blank_line_in_qldoc = False |
| 24 | +qldoc_lines = [] |
| 25 | +with open(instruction_path, 'r', encoding='utf-8') as instr: |
| 26 | + for line in instr: |
| 27 | + if in_qldoc: |
| 28 | + if end_qldoc_re.search(line): |
| 29 | + qldoc_lines.append(line) |
| 30 | + in_qldoc = False |
| 31 | + elif blank_qldoc_line_re.search(line): |
| 32 | + # We're going to skip any lines after the first blank line, to avoid duplicating all |
| 33 | + # of the verbose description. |
| 34 | + saw_blank_line_in_qldoc = True |
| 35 | + elif not saw_blank_line_in_qldoc: |
| 36 | + qldoc_lines.append(line) |
| 37 | + else: |
| 38 | + if start_qldoc_re.search(line): |
| 39 | + # Starting a new QLDoc comment. |
| 40 | + saw_blank_line_in_qldoc = False |
| 41 | + qldoc_lines.append(line) |
| 42 | + if not end_qldoc_re.search(line): |
| 43 | + in_qldoc = True |
| 44 | + else: |
| 45 | + instruction_match = instruction_class_re.search(line) |
| 46 | + if instruction_match: |
| 47 | + # Found the declaration of an `Instruction` class. Record the QLDoc comments. |
| 48 | + instruction_comments[instruction_match.group('name')] = qldoc_lines |
| 49 | + qldoc_lines = [] |
| 50 | + |
| 51 | +# Scan `Opcode.qll`. Whenever we see the declaration of an `Opcode` class for which we have a |
| 52 | +# corresponding `Instruction` class, we'll attach a copy of the `Instruction`'s QLDoc comment. |
| 53 | +in_qldoc = False |
| 54 | +qldoc_lines = [] |
| 55 | +output_lines = [] |
| 56 | +with open(opcode_path, 'r', encoding='utf-8') as opcode: |
| 57 | + for line in opcode: |
| 58 | + if in_qldoc: |
| 59 | + qldoc_lines.append(line) |
| 60 | + if end_qldoc_re.search(line): |
| 61 | + in_qldoc = False |
| 62 | + else: |
| 63 | + if start_qldoc_re.search(line): |
| 64 | + qldoc_lines.append(line) |
| 65 | + if not end_qldoc_re.search(line): |
| 66 | + in_qldoc = True |
| 67 | + else: |
| 68 | + name_without_suffix = None |
| 69 | + name = None |
| 70 | + indent = '' |
| 71 | + opcode_base_match = opcode_base_class_re.search(line) |
| 72 | + if opcode_base_match: |
| 73 | + name_without_suffix = opcode_base_match.group('name') |
| 74 | + name = name_without_suffix + 'Opcode' |
| 75 | + else: |
| 76 | + opcode_match = opcode_class_re.search(line) |
| 77 | + if opcode_match: |
| 78 | + name_without_suffix = opcode_match.group('name') |
| 79 | + name = name_without_suffix |
| 80 | + # Indent by two additional spaces, since opcodes are declared in the |
| 81 | + # `Opcode` module. |
| 82 | + indent = ' ' |
| 83 | + |
| 84 | + if name_without_suffix: |
| 85 | + # Found an `Opcode` that matches a known `Instruction`. Replace the QLDoc with |
| 86 | + # a copy of the one from the `Instruction`. |
| 87 | + if instruction_comments.get(name_without_suffix): |
| 88 | + article = 'an' if needs_an_re.search(name_without_suffix) else 'a' |
| 89 | + qldoc_lines = [ |
| 90 | + indent + '/**\n', |
| 91 | + indent + ' * The `Opcode` for ' + article + ' `' + name_without_suffix + 'Instruction`.\n', |
| 92 | + indent + ' *\n', |
| 93 | + indent + ' * See the `' + name_without_suffix + 'Instruction` documentation for more details.\n', |
| 94 | + indent + ' */\n' |
| 95 | + ] |
| 96 | + output_lines.extend(qldoc_lines) |
| 97 | + qldoc_lines = [] |
| 98 | + output_lines.append(line) |
| 99 | + |
| 100 | +# Write out the updated `Opcode.qll` |
| 101 | +with open(opcode_path, 'w', encoding='utf-8') as opcode: |
| 102 | + opcode.writelines(output_lines) |
0 commit comments