Skip to content

Commit 136b5d1

Browse files
committed
C++: Small cleanup by making 'GlobalUse' extend 'UseImpl'.
1 parent 6a91e85 commit 136b5d1

1 file changed

Lines changed: 24 additions & 54 deletions

File tree

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

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ abstract private class DefOrUseImpl extends TDefOrUseImpl {
221221
}
222222

223223
/** Gets the variable that is defined or used. */
224-
final SourceVariable getSourceVariable() {
224+
SourceVariable getSourceVariable() {
225225
exists(BaseSourceVariable v, int ind |
226226
sourceVariableHasBaseAndIndex(result, v, ind) and
227227
defOrUseHasSourceVariable(this, v, ind)
@@ -339,9 +339,7 @@ abstract private class OperandBasedUse extends UseImpl {
339339
operand.getUse() = block.getInstruction(index)
340340
}
341341

342-
final Operand getOperand() {
343-
result = operand
344-
}
342+
final Operand getOperand() { result = operand }
345343

346344
final override Cpp::Location getLocation() { result = operand.getLocation() }
347345
}
@@ -372,17 +370,22 @@ private class IteratorUse extends OperandBasedUse, TIteratorUse {
372370
override Node getNode() { nodeHasOperand(result, operand, ind) }
373371
}
374372

373+
pragma[nomagic]
374+
private predicate finalParameterNodeHasParameterAndIndex(
375+
FinalParameterNode n, Parameter p, int indirectionIndex
376+
) {
377+
n.getParameter() = p and
378+
n.getIndirectionIndex() = indirectionIndex
379+
}
380+
375381
class FinalParameterUse extends UseImpl, TFinalParameterUse {
376382
Parameter p;
377383

378384
FinalParameterUse() { this = TFinalParameterUse(p, ind) }
379385

380386
Parameter getParameter() { result = p }
381387

382-
override Node getNode() {
383-
result.(FinalParameterNode).getParameter() = p and
384-
result.(FinalParameterNode).getIndirectionIndex() = ind
385-
}
388+
override Node getNode() { finalParameterNodeHasParameterAndIndex(result, p, ind) }
386389

387390
override int getIndirection() { result = ind + 1 }
388391

@@ -413,42 +416,36 @@ class FinalParameterUse extends UseImpl, TFinalParameterUse {
413416
}
414417
}
415418

416-
class GlobalUse extends TGlobalUse {
419+
class GlobalUse extends UseImpl, TGlobalUse {
417420
Cpp::GlobalOrNamespaceVariable global;
418421
IRFunction f;
419-
int indirectionIndex;
420422

421-
GlobalUse() { this = TGlobalUse(global, f, indirectionIndex) }
423+
GlobalUse() { this = TGlobalUse(global, f, ind) }
424+
425+
override FinalGlobalValue getNode() { result.getGlobalUse() = this }
426+
427+
override int getIndirection() { result = ind + 1 } // TODO
422428

423429
Cpp::GlobalOrNamespaceVariable getVariable() { result = global }
424430

425431
IRFunction getIRFunction() { result = f }
426432

427-
final predicate hasIndexInBlock(IRBlock block, int index) {
433+
final override predicate hasIndexInBlock(IRBlock block, int index) {
428434
exists(ExitFunctionInstruction exit |
429435
exit = f.getExitFunctionInstruction() and
430436
block.getInstruction(index) = exit
431437
)
432438
}
433439

434-
int getIndirectionIndex() { result = indirectionIndex }
440+
override SourceVariable getSourceVariable() { sourceVariableIsGlobal(result, global, f, ind) }
435441

436-
SourceVariable getSourceVariable() { sourceVariableIsGlobal(result, global, f, indirectionIndex) }
442+
final override Cpp::Location getLocation() { result = f.getLocation() }
437443

438-
final predicate hasIndexInBlock(IRBlock block, int index, SourceVariable sv) {
439-
this.hasIndexInBlock(block, index) and
440-
sv = this.getSourceVariable()
441-
}
442-
443-
final Cpp::Location getLocation() { result = f.getLocation() }
444+
Type getUnspecifiedType() { result = global.getUnspecifiedType() }
444445

445-
string toString() {
446-
if indirectionIndex = 1
447-
then result = global.toString()
448-
else result = global.toString() + " indirection"
449-
}
446+
override predicate isCertain() { any() }
450447

451-
Type getUnspecifiedType() { result = global.getUnspecifiedType() }
448+
override BaseSourceVariableInstruction getBase() { none() }
452449
}
453450

454451
class GlobalDef extends TGlobalDef {
@@ -515,19 +512,6 @@ predicate adjacentDefRead(DefOrUse defOrUse1, UseOrPhi use) {
515512
)
516513
}
517514

518-
/**
519-
* Holds if `defOrUse` should flow to the final use of the
520-
* global variable use represetned by `globalUse`.
521-
*/
522-
private predicate defOrUseToGlobalUse(DefOrUse defOrUse, GlobalUse globalUse) {
523-
exists(IRBlock bb1, int i1, IRBlock bb2, int i2, SourceVariable v |
524-
defOrUse.asDefOrUse().hasIndexInBlock(bb1, i1, v) and
525-
globalUse.hasIndexInBlock(bb2, i2, v) and
526-
adjacentDefRead(_, pragma[only_bind_into](bb1), pragma[only_bind_into](i1),
527-
pragma[only_bind_into](bb2), pragma[only_bind_into](i2))
528-
)
529-
}
530-
531515
/**
532516
* Holds if `globalDef` represents the initial definition of a global variable that
533517
* flows to `useOrPhi`.
@@ -541,14 +525,7 @@ private predicate globalDefToUse(GlobalDef globalDef, UseOrPhi useOrPhi) {
541525
)
542526
}
543527

544-
private predicate useToNode(UseOrPhi use, Node nodeTo) {
545-
exists(OperandBasedUse useImpl |
546-
useImpl = use.asDefOrUse() and
547-
nodeHasOperand(nodeTo, useImpl.getOperand(), useImpl.getIndirectionIndex())
548-
)
549-
or
550-
nodeTo.(SsaPhiNode).getPhiNode() = use.asPhi()
551-
}
528+
private predicate useToNode(UseOrPhi use, Node nodeTo) { use.getNode() = nodeTo }
552529

553530
pragma[noinline]
554531
predicate outNodeHasAddressAndIndex(
@@ -636,13 +613,6 @@ private predicate ssaFlowImpl(Node nodeFrom, Node nodeTo, boolean uncertain) {
636613
useToNode(use, nodeTo)
637614
)
638615
or
639-
// Def/use to final value of global vairable
640-
exists(DefOrUse defOrUse, GlobalUse globalUse |
641-
nodeToDefOrUse(nodeFrom, defOrUse, uncertain) and
642-
defOrUseToGlobalUse(defOrUse, globalUse) and
643-
nodeTo.(FinalGlobalValue).getGlobalUse() = globalUse
644-
)
645-
or
646616
// Initial global variable value to a first use
647617
exists(GlobalDef globalDef, UseOrPhi use |
648618
nodeFrom.(InitialGlobalValue).getGlobalDef() = globalDef and

0 commit comments

Comments
 (0)