Skip to content

Commit bdf121f

Browse files
author
Dave Bartolomeo
committed
C++: Update opcode QLDoc script to handle abstract base classes
This auto-generates even more QLDoc for `Opcode.qll`
1 parent 5f29052 commit bdf121f

File tree

3 files changed

+148
-13
lines changed

3 files changed

+148
-13
lines changed

config/opcode-qldoc.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
end_qldoc_re = re.compile(r'\*/\s*$') # End of a QLDoc comment
99
blank_qldoc_line_re = re.compile(r'^\s*\*\s*$') # A line in a QLDoc comment with only the '*'
1010
instruction_class_re = re.compile(r'^class (?P<name>[A-aa-z0-9]+)Instruction\s') # Declaration of an `Instruction` class
11-
opcode_class_re = re.compile(r'^\s*class (?P<name>[A-aa-z0-9]+)\s') # Declaration of an `Opcode` class
11+
opcode_base_class_re = re.compile(r'^abstract class (?P<name>[A-aa-z0-9]+)Opcode\s') # Declaration of an `Opcode` base class
12+
opcode_class_re = re.compile(r'^ class (?P<name>[A-aa-z0-9]+)\s') # Declaration of an `Opcode` class
1213

1314
script_dir = path.realpath(path.dirname(__file__))
1415
instruction_path = path.realpath(path.join(script_dir, '../cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll'))
@@ -63,17 +64,29 @@
6364
if not end_qldoc_re.search(line):
6465
in_qldoc = True
6566
else:
66-
opcode_match = opcode_class_re.search(line)
67-
if opcode_match:
68-
# Found an `Opcode` that matches a known `Instruction`. Replace the QLDoc with
69-
# a copy of the one from the `Instruction`.
70-
name = opcode_match.group('name')
71-
if instruction_comments.get(name):
67+
name_without_suffix = None
68+
name = None
69+
indent = ''
70+
opcode_base_match = opcode_base_class_re.search(line)
71+
if opcode_base_match:
72+
name_without_suffix = opcode_base_match.group('name')
73+
name = name_without_suffix + 'Opcode'
74+
else:
75+
opcode_match = opcode_class_re.search(line)
76+
if opcode_match:
77+
name_without_suffix = opcode_match.group('name')
78+
name = name_without_suffix
7279
# Indent by two additional spaces, since opcodes are declared in the
7380
# `Opcode` module.
81+
indent = ' '
82+
83+
if name_without_suffix:
84+
# Found an `Opcode` that matches a known `Instruction`. Replace the QLDoc with
85+
# a copy of the one from the `Instruction`.
86+
if instruction_comments.get(name_without_suffix):
7487
# Rename `instruction` to `operation`.
75-
qldoc_lines = [(' ' + qldoc_line.replace(' An instruction ', ' An operation '))
76-
for qldoc_line in instruction_comments[name]]
88+
qldoc_lines = [(indent + qldoc_line.replace(' An instruction ', ' An operation '))
89+
for qldoc_line in instruction_comments[name_without_suffix]]
7790
output_lines.extend(qldoc_lines)
7891
qldoc_lines = []
7992
output_lines.append(line)

cpp/ql/src/semmle/code/cpp/ir/implementation/Opcode.qll

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,53 +139,112 @@ class Opcode extends TOpcode {
139139
predicate hasOperandInternal(OperandTag tag) { none() }
140140
}
141141

142+
/**
143+
* An operation whose result is computed from a single operand.
144+
*/
142145
abstract class UnaryOpcode extends Opcode {
143146
final override predicate hasOperandInternal(OperandTag tag) { tag instanceof UnaryOperandTag }
144147
}
145148

149+
/**
150+
* An operation whose result is computed from two operands.
151+
*/
146152
abstract class BinaryOpcode extends Opcode {
147153
final override predicate hasOperandInternal(OperandTag tag) {
148154
tag instanceof LeftOperandTag or
149155
tag instanceof RightOperandTag
150156
}
151157
}
152158

159+
/**
160+
* An operation that performs a binary arithmetic operation involving at least one pointer
161+
* operand.
162+
*/
153163
abstract class PointerArithmeticOpcode extends BinaryOpcode { }
154164

165+
/**
166+
* An operation that adds or subtracts an integer offset from a pointer.
167+
*/
155168
abstract class PointerOffsetOpcode extends PointerArithmeticOpcode { }
156169

170+
/**
171+
* An operation that computes the result of an arithmetic operation.
172+
*/
157173
abstract class ArithmeticOpcode extends Opcode { }
158174

175+
/**
176+
* An operation that performs an arithmetic operation on two numeric operands.
177+
*/
159178
abstract class BinaryArithmeticOpcode extends BinaryOpcode, ArithmeticOpcode { }
160179

180+
/**
181+
* An operation whose result is computed by performing an arithmetic operation on a single
182+
* numeric operand.
183+
*/
161184
abstract class UnaryArithmeticOpcode extends UnaryOpcode, ArithmeticOpcode { }
162185

186+
/**
187+
* An operation that computes the result of a bitwise operation.
188+
*/
163189
abstract class BitwiseOpcode extends Opcode { }
164190

191+
/**
192+
* An operation that performs a bitwise operation on two integer operands.
193+
*/
165194
abstract class BinaryBitwiseOpcode extends BinaryOpcode, BitwiseOpcode { }
166195

196+
/**
197+
* An operation that performs a bitwise operation on a single integer operand.
198+
*/
167199
abstract class UnaryBitwiseOpcode extends UnaryOpcode, BitwiseOpcode { }
168200

201+
/**
202+
* An operation that compares two numeric operands.
203+
*/
169204
abstract class CompareOpcode extends BinaryOpcode { }
170205

206+
/**
207+
* An operation that does a relative comparison of two values, such as `<` or `>=`.
208+
*/
171209
abstract class RelationalOpcode extends CompareOpcode { }
172210

211+
/**
212+
* An operation that returns a copy of its operand.
213+
*/
173214
abstract class CopyOpcode extends Opcode { }
174215

216+
/**
217+
* An operation that converts from the address of a derived class to the address of a base class.
218+
*/
175219
abstract class ConvertToBaseOpcode extends UnaryOpcode { }
176220

221+
/**
222+
* An operation that returns control to the caller of the function.
223+
*/
177224
abstract class ReturnOpcode extends Opcode { }
178225

226+
/**
227+
* An operation that throws an exception.
228+
*/
179229
abstract class ThrowOpcode extends Opcode { }
180230

231+
/**
232+
* An operation that starts a `catch` handler.
233+
*/
181234
abstract class CatchOpcode extends Opcode { }
182235

183236
abstract private class OpcodeWithCondition extends Opcode {
184237
final override predicate hasOperandInternal(OperandTag tag) { tag instanceof ConditionOperandTag }
185238
}
186239

240+
/**
241+
* An operation representing a built-in operation.
242+
*/
187243
abstract class BuiltInOperationOpcode extends Opcode { }
188244

245+
/**
246+
* An operation representing a side effect of a function call.
247+
*/
189248
abstract class SideEffectOpcode extends Opcode { }
190249

191250
/**
@@ -321,7 +380,8 @@ abstract class OpcodeWithLoad extends IndirectReadOpcode {
321380
}
322381

323382
/**
324-
* An opcode that reads from a set of memory locations as a side effect.
383+
* An operation representing a read side effect of a function call on a
384+
* specific parameter.
325385
*/
326386
abstract class ReadSideEffectOpcode extends SideEffectOpcode {
327387
final override predicate hasOperandInternal(OperandTag tag) {
@@ -330,7 +390,8 @@ abstract class ReadSideEffectOpcode extends SideEffectOpcode {
330390
}
331391

332392
/**
333-
* An opcode that writes to a set of memory locations as a side effect.
393+
* An operation representing a write side effect of a function call on a
394+
* specific parameter.
334395
*/
335396
abstract class WriteSideEffectOpcode extends SideEffectOpcode { }
336397

csharp/ql/src/experimental/ir/implementation/Opcode.qll

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,53 +139,112 @@ class Opcode extends TOpcode {
139139
predicate hasOperandInternal(OperandTag tag) { none() }
140140
}
141141

142+
/**
143+
* An operation whose result is computed from a single operand.
144+
*/
142145
abstract class UnaryOpcode extends Opcode {
143146
final override predicate hasOperandInternal(OperandTag tag) { tag instanceof UnaryOperandTag }
144147
}
145148

149+
/**
150+
* An operation whose result is computed from two operands.
151+
*/
146152
abstract class BinaryOpcode extends Opcode {
147153
final override predicate hasOperandInternal(OperandTag tag) {
148154
tag instanceof LeftOperandTag or
149155
tag instanceof RightOperandTag
150156
}
151157
}
152158

159+
/**
160+
* An operation that performs a binary arithmetic operation involving at least one pointer
161+
* operand.
162+
*/
153163
abstract class PointerArithmeticOpcode extends BinaryOpcode { }
154164

165+
/**
166+
* An operation that adds or subtracts an integer offset from a pointer.
167+
*/
155168
abstract class PointerOffsetOpcode extends PointerArithmeticOpcode { }
156169

170+
/**
171+
* An operation that computes the result of an arithmetic operation.
172+
*/
157173
abstract class ArithmeticOpcode extends Opcode { }
158174

175+
/**
176+
* An operation that performs an arithmetic operation on two numeric operands.
177+
*/
159178
abstract class BinaryArithmeticOpcode extends BinaryOpcode, ArithmeticOpcode { }
160179

180+
/**
181+
* An operation whose result is computed by performing an arithmetic operation on a single
182+
* numeric operand.
183+
*/
161184
abstract class UnaryArithmeticOpcode extends UnaryOpcode, ArithmeticOpcode { }
162185

186+
/**
187+
* An operation that computes the result of a bitwise operation.
188+
*/
163189
abstract class BitwiseOpcode extends Opcode { }
164190

191+
/**
192+
* An operation that performs a bitwise operation on two integer operands.
193+
*/
165194
abstract class BinaryBitwiseOpcode extends BinaryOpcode, BitwiseOpcode { }
166195

196+
/**
197+
* An operation that performs a bitwise operation on a single integer operand.
198+
*/
167199
abstract class UnaryBitwiseOpcode extends UnaryOpcode, BitwiseOpcode { }
168200

201+
/**
202+
* An operation that compares two numeric operands.
203+
*/
169204
abstract class CompareOpcode extends BinaryOpcode { }
170205

206+
/**
207+
* An operation that does a relative comparison of two values, such as `<` or `>=`.
208+
*/
171209
abstract class RelationalOpcode extends CompareOpcode { }
172210

211+
/**
212+
* An operation that returns a copy of its operand.
213+
*/
173214
abstract class CopyOpcode extends Opcode { }
174215

216+
/**
217+
* An operation that converts from the address of a derived class to the address of a base class.
218+
*/
175219
abstract class ConvertToBaseOpcode extends UnaryOpcode { }
176220

221+
/**
222+
* An operation that returns control to the caller of the function.
223+
*/
177224
abstract class ReturnOpcode extends Opcode { }
178225

226+
/**
227+
* An operation that throws an exception.
228+
*/
179229
abstract class ThrowOpcode extends Opcode { }
180230

231+
/**
232+
* An operation that starts a `catch` handler.
233+
*/
181234
abstract class CatchOpcode extends Opcode { }
182235

183236
abstract private class OpcodeWithCondition extends Opcode {
184237
final override predicate hasOperandInternal(OperandTag tag) { tag instanceof ConditionOperandTag }
185238
}
186239

240+
/**
241+
* An operation representing a built-in operation.
242+
*/
187243
abstract class BuiltInOperationOpcode extends Opcode { }
188244

245+
/**
246+
* An operation representing a side effect of a function call.
247+
*/
189248
abstract class SideEffectOpcode extends Opcode { }
190249

191250
/**
@@ -321,7 +380,8 @@ abstract class OpcodeWithLoad extends IndirectReadOpcode {
321380
}
322381

323382
/**
324-
* An opcode that reads from a set of memory locations as a side effect.
383+
* An operation representing a read side effect of a function call on a
384+
* specific parameter.
325385
*/
326386
abstract class ReadSideEffectOpcode extends SideEffectOpcode {
327387
final override predicate hasOperandInternal(OperandTag tag) {
@@ -330,7 +390,8 @@ abstract class ReadSideEffectOpcode extends SideEffectOpcode {
330390
}
331391

332392
/**
333-
* An opcode that writes to a set of memory locations as a side effect.
393+
* An operation representing a write side effect of a function call on a
394+
* specific parameter.
334395
*/
335396
abstract class WriteSideEffectOpcode extends SideEffectOpcode { }
336397

0 commit comments

Comments
 (0)