Skip to content

Commit a126154

Browse files
committed
C++: use -1 for this in dataflow Position
1 parent 1890a14 commit a126154

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ private class PrimaryArgumentNode extends ArgumentNode {
4343
PrimaryArgumentNode() { exists(CallInstruction call | op = call.getAnArgumentOperand()) }
4444

4545
override predicate argumentOf(DataFlowCall call, ArgumentPosition pos) {
46-
op = call.getArgumentOperand(pos.(Positional).getIndex())
47-
or
48-
op = call.getArgumentOperand(-1) and
49-
pos instanceof ThisPosition
46+
op = call.getArgumentOperand(pos.(DirectPosition).getIndex())
5047
}
5148

5249
override string toString() {
@@ -79,10 +76,7 @@ private class SideEffectArgumentNode extends ArgumentNode {
7976
override predicate argumentOf(DataFlowCall call, ArgumentPosition pos) {
8077
read.getPrimaryInstruction() = call and
8178
(
82-
pos.(PositionalIndirection).getIndex() = read.getIndex()
83-
or
84-
pos instanceof ThisIndirectionPosition and
85-
read.getIndex() = -1
79+
pos.(IndirectionPosition).getIndex() = read.getIndex()
8680
)
8781
}
8882

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

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -501,42 +501,41 @@ class Position extends TPosition {
501501
abstract string toString();
502502
}
503503

504-
class ThisPosition extends TThisPosition {
505-
string toString() { result = "this" }
506-
}
507-
508-
class ThisIndirectionPosition extends TThisIndirectionPosition {
509-
string toString() { result = "this" }
510-
}
511-
512-
class Positional extends TPositional {
504+
class DirectPosition extends TDirectPosition {
513505
int index;
514506

515-
Positional() { this = TPositional(index) }
516-
517-
string toString() { result = index.toString() }
507+
DirectPosition() { this = TDirectPosition(index) }
518508

519-
int getIndex() {
520-
result = index
509+
string toString() {
510+
index = -1 and
511+
result = "this"
512+
or
513+
index != -1 and
514+
result = index.toString()
521515
}
516+
517+
int getIndex() { result = index }
522518
}
523519

524-
class PositionalIndirection extends TPositionalIndirection {
520+
class IndirectionPosition extends TIndirectionPosition {
525521
int index;
526522

527-
PositionalIndirection() { this = TPositionalIndirection(index) }
523+
IndirectionPosition() { this = TIndirectionPosition(index) }
528524

529-
string toString() { result = index.toString() }
530-
int getIndex() {
531-
result = index
525+
string toString() {
526+
index = -1 and
527+
result = "this"
528+
or
529+
index != -1 and
530+
result = index.toString()
532531
}
532+
533+
int getIndex() { result = index }
533534
}
534535

535536
newtype TPosition =
536-
TThisPosition() or
537-
TThisIndirectionPosition() or
538-
TPositional(int index) { exists(any(Call c).getArgument(index)) } or
539-
TPositionalIndirection(int index) { exists(any(Call c).getArgument(index)) }
537+
TDirectPosition(int index) { exists(any(CallInstruction c).getArgument(index))} or
538+
TIndirectionPosition(int index) { exists(any(CallInstruction c).getArgument(index)) }
540539

541540
/**
542541
* The value of a parameter at function entry, viewed as a node in a data
@@ -570,7 +569,7 @@ private class ExplicitParameterNode extends ParameterNode {
570569
ExplicitParameterNode() { exists(instr.getParameter()) }
571570

572571
override predicate isParameterOf(Function f, ParameterPosition pos) {
573-
f.getParameter(pos.(Positional).getIndex()) = instr.getParameter()
572+
f.getParameter(pos.(DirectPosition).getIndex()) = instr.getParameter()
574573
}
575574

576575
/** Gets the `Parameter` associated with this node. */
@@ -586,7 +585,7 @@ class ThisParameterNode extends ParameterNode {
586585
ThisParameterNode() { instr.getIRVariable() instanceof IRThisVariable }
587586

588587
override predicate isParameterOf(Function f, ParameterPosition pos) {
589-
pos instanceof ThisPosition and instr.getEnclosingFunction() = f
588+
pos.(DirectPosition).getIndex() = -1 and instr.getEnclosingFunction() = f
590589
}
591590

592591
override string toString() { result = "this" }
@@ -601,12 +600,8 @@ class ParameterIndirectionNode extends ParameterNode {
601600
instr.getEnclosingFunction() = f and
602601
instr.hasIndex(index)
603602
|
604-
pos.(PositionalIndirection).getIndex() = index
603+
pos.(IndirectionPosition).getIndex() = index
605604
)
606-
or
607-
instr.getEnclosingFunction() = f and
608-
instr.hasIndex(-1) and
609-
pos instanceof ThisIndirectionPosition
610605
}
611606

612607
override string toString() { result = "*" + instr.getIRVariable().toString() }

0 commit comments

Comments
 (0)