Skip to content

Commit 6564582

Browse files
committed
C++: Remove the 'BaseSourceVariableInstruction' column as it's functionally determined by the address column.
1 parent 8b91914 commit 6564582

File tree

1 file changed

+37
-55
lines changed

1 file changed

+37
-55
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ predicate hasRawIndirectInstruction(Instruction instr, int indirectionIndex) {
105105
cached
106106
private newtype TDefImpl =
107107
TDefAddressImpl(BaseIRVariable v) or
108-
TDirectDefImpl(BaseSourceVariableInstruction base, Operand address, int indirectionIndex) {
109-
isDef(_, _, address, base, _, indirectionIndex)
108+
TDirectDefImpl(Operand address, int indirectionIndex) {
109+
isDef(_, _, address, _, _, indirectionIndex)
110110
} or
111111
TGlobalDefImpl(GlobalLikeVariable v, IRFunction f, int indirectionIndex) {
112112
// Represents the initial "definition" of a global variable when entering
@@ -116,8 +116,8 @@ private newtype TDefImpl =
116116

117117
cached
118118
private newtype TUseImpl =
119-
TDirectUseImpl(BaseSourceVariableInstruction base, Operand operand, int indirectionIndex) {
120-
isUse(_, operand, base, _, indirectionIndex) and
119+
TDirectUseImpl(Operand operand, int indirectionIndex) {
120+
isUse(_, operand, _, _, indirectionIndex) and
121121
not isDef(true, _, operand, _, _, _)
122122
} or
123123
TGlobalUse(GlobalLikeVariable v, IRFunction f, int indirectionIndex) {
@@ -211,19 +211,11 @@ abstract class DefImpl extends TDefImpl {
211211
*/
212212
abstract int getIndirection();
213213

214-
/**
215-
* Gets the instruction that computes the base of this definition or use.
216-
* This is always a `VariableAddressInstruction` or an `CallInstruction`.
217-
*/
218-
abstract BaseSourceVariableInstruction getBase();
219-
220214
/**
221215
* Gets the base source variable (i.e., the variable without
222216
* any indirection) of this definition or use.
223217
*/
224-
final BaseSourceVariable getBaseSourceVariable() {
225-
this.getBase().getBaseSourceVariable() = result
226-
}
218+
abstract BaseSourceVariable getBaseSourceVariable();
227219

228220
/** Gets the variable that is defined or used. */
229221
SourceVariable getSourceVariable() {
@@ -283,19 +275,11 @@ abstract class UseImpl extends TUseImpl {
283275
/** Gets the indirection index of this use. */
284276
final int getIndirectionIndex() { result = indirectionIndex }
285277

286-
/**
287-
* Gets the instruction that computes the base of this definition or use.
288-
* This is always a `VariableAddressInstruction` or an `CallInstruction`.
289-
*/
290-
abstract BaseSourceVariableInstruction getBase();
291-
292278
/**
293279
* Gets the base source variable (i.e., the variable without
294280
* any indirection) of this definition or use.
295281
*/
296-
final BaseSourceVariable getBaseSourceVariable() {
297-
this.getBase().getBaseSourceVariable() = result
298-
}
282+
abstract BaseSourceVariable getBaseSourceVariable();
299283

300284
/** Gets the variable that is defined or used. */
301285
SourceVariable getSourceVariable() {
@@ -377,14 +361,13 @@ private class DefAddressImpl extends DefImpl, TDefAddressImpl {
377361
result.getIndirection() = 0
378362
}
379363

380-
final override BaseSourceVariableInstruction getBase() { none() }
364+
final override BaseSourceVariable getBaseSourceVariable() { result = v }
381365
}
382366

383367
private class DirectDef extends DefImpl, TDirectDefImpl {
384368
Operand address;
385-
BaseSourceVariableInstruction base;
386369

387-
DirectDef() { this = TDirectDefImpl(base, address, indirectionIndex) }
370+
DirectDef() { this = TDirectDefImpl(address, indirectionIndex) }
388371

389372
override Cpp::Location getLocation() { result = this.getAddressOperand().getUse().getLocation() }
390373

@@ -396,30 +379,36 @@ private class DirectDef extends DefImpl, TDirectDefImpl {
396379

397380
override Operand getAddressOperand() { result = address }
398381

399-
override BaseSourceVariableInstruction getBase() { result = base }
382+
private BaseSourceVariableInstruction getBase() {
383+
isDef(_, _, address, result, _, indirectionIndex)
384+
}
400385

401-
override int getIndirection() { isDef(_, _, address, base, result, indirectionIndex) }
386+
override BaseSourceVariable getBaseSourceVariable() {
387+
result = this.getBase().getBaseSourceVariable()
388+
}
389+
390+
override int getIndirection() { isDef(_, _, address, _, result, indirectionIndex) }
402391

403-
override Node0Impl getValue() { isDef(_, result, address, base, _, _) }
392+
override Node0Impl getValue() { isDef(_, result, address, _, _, _) }
404393

405-
override predicate isCertain() { isDef(true, _, address, base, _, indirectionIndex) }
394+
override predicate isCertain() { isDef(true, _, address, _, _, indirectionIndex) }
406395
}
407396

408397
private class DirectUseImpl extends UseImpl, TDirectUseImpl {
409398
Operand operand;
410-
BaseSourceVariableInstruction base;
411399

412-
DirectUseImpl() { this = TDirectUseImpl(base, operand, indirectionIndex) }
400+
DirectUseImpl() { this = TDirectUseImpl(operand, indirectionIndex) }
413401

414402
override string toString() { result = "Use of " + this.getSourceVariable() }
415403

416404
final override predicate hasIndexInBlock(IRBlock block, int index) {
417405
// See the comment in `ssa0`'s `OperandBasedUse` for an explanation of this
418406
// predicate's implementation.
419-
if base.getAst() = any(Cpp::PostfixCrementOperation c).getOperand()
407+
if this.getBase().getAst() = any(Cpp::PostfixCrementOperation c).getOperand()
420408
then
421-
exists(Operand op, int indirection |
409+
exists(Operand op, int indirection, Instruction base |
422410
indirection = this.getIndirection() and
411+
base = this.getBase() and
423412
op =
424413
min(Operand cand, int i |
425414
isUse(_, cand, base, indirection, indirectionIndex) and
@@ -432,15 +421,19 @@ private class DirectUseImpl extends UseImpl, TDirectUseImpl {
432421
else operand.getUse() = block.getInstruction(index)
433422
}
434423

435-
final override BaseSourceVariableInstruction getBase() { result = base }
424+
private BaseSourceVariableInstruction getBase() { isUse(_, operand, result, _, indirectionIndex) }
425+
426+
override BaseSourceVariable getBaseSourceVariable() {
427+
result = this.getBase().getBaseSourceVariable()
428+
}
436429

437430
final Operand getOperand() { result = operand }
438431

439432
final override Cpp::Location getLocation() { result = operand.getLocation() }
440433

441-
override int getIndirection() { isUse(_, operand, base, result, indirectionIndex) }
434+
override int getIndirection() { isUse(_, operand, _, result, indirectionIndex) }
442435

443-
override predicate isCertain() { isUse(true, operand, base, _, indirectionIndex) }
436+
override predicate isCertain() { isUse(true, operand, _, _, indirectionIndex) }
444437

445438
override Node getNode() { nodeHasOperand(result, operand, indirectionIndex) }
446439
}
@@ -499,13 +492,7 @@ class FinalParameterUse extends UseImpl, TFinalParameterUse {
499492
result instanceof UnknownDefaultLocation
500493
}
501494

502-
override BaseSourceVariableInstruction getBase() {
503-
exists(InitializeParameterInstruction init |
504-
init.getParameter() = p and
505-
// This is always a `VariableAddressInstruction`
506-
result = init.getAnOperand().getDef()
507-
)
508-
}
495+
override BaseIRVariable getBaseSourceVariable() { result.getIRVariable().getAst() = p }
509496
}
510497

511498
/**
@@ -591,8 +578,8 @@ class GlobalUse extends UseImpl, TGlobalUse {
591578
)
592579
}
593580

594-
override SourceVariable getSourceVariable() {
595-
sourceVariableIsGlobal(result, global, f, this.getIndirection())
581+
override BaseSourceVariable getBaseSourceVariable() {
582+
baseSourceVariableIsGlobal(result, global, f)
596583
}
597584

598585
final override Cpp::Location getLocation() { result = f.getLocation() }
@@ -609,8 +596,6 @@ class GlobalUse extends UseImpl, TGlobalUse {
609596
Type getUnderlyingType() { result = global.getUnderlyingType() }
610597

611598
override predicate isCertain() { any() }
612-
613-
override BaseSourceVariableInstruction getBase() { none() }
614599
}
615600

616601
/**
@@ -640,8 +625,8 @@ class GlobalDefImpl extends DefImpl, TGlobalDefImpl {
640625
}
641626

642627
/** Gets the global variable associated with this definition. */
643-
override SourceVariable getSourceVariable() {
644-
sourceVariableIsGlobal(result, global, f, this.getIndirection())
628+
override BaseSourceVariable getBaseSourceVariable() {
629+
baseSourceVariableIsGlobal(result, global, f)
645630
}
646631

647632
override int getIndirection() { result = indirectionIndex }
@@ -664,8 +649,6 @@ class GlobalDefImpl extends DefImpl, TGlobalDefImpl {
664649
override string toString() { result = "Def of " + this.getSourceVariable() }
665650

666651
override Location getLocation() { result = f.getLocation() }
667-
668-
override BaseSourceVariableInstruction getBase() { none() }
669652
}
670653

671654
/**
@@ -978,11 +961,10 @@ predicate fromPhiNode(SsaPhiNode nodeFrom, Node nodeTo) {
978961
)
979962
}
980963

981-
private predicate sourceVariableIsGlobal(
982-
SourceVariable sv, GlobalLikeVariable global, IRFunction func, int indirectionIndex
964+
private predicate baseSourceVariableIsGlobal(
965+
BaseIRVariable base, GlobalLikeVariable global, IRFunction func
983966
) {
984-
exists(IRVariable irVar, BaseIRVariable base |
985-
sourceVariableHasBaseAndIndex(sv, base, indirectionIndex) and
967+
exists(IRVariable irVar |
986968
irVar = base.getIRVariable() and
987969
irVar.getEnclosingIRFunction() = func and
988970
global = irVar.getAst() and

0 commit comments

Comments
 (0)