Skip to content

Commit 93346a5

Browse files
committed
C++: Add a new 'Location.isBefore' predicate that also considers columns.
1 parent c6c3206 commit 93346a5

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

cpp/ql/lib/semmle/code/cpp/Location.qll

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,24 @@ class Location extends @location {
7373

7474
/** Holds if `this` comes on a line strictly before `l`. */
7575
pragma[inline]
76-
predicate isBefore(Location l) {
77-
this.getFile() = l.getFile() and this.getEndLine() < l.getStartLine()
76+
predicate isBefore(Location l) { isBefore(l, false) }
77+
78+
/**
79+
* Holds if `this` comes strictly before `l`. The boolean `sameLine` is
80+
* true if `l` is on the same line as `this`, but starts at a later column.
81+
* Otherwise, `sameLine` is false.
82+
*/
83+
pragma[inline]
84+
predicate isBefore(Location l, boolean sameLine) {
85+
this.getFile() = l.getFile() and
86+
(
87+
sameLine = false and
88+
this.getEndLine() < l.getStartLine()
89+
or
90+
sameLine = true and
91+
this.getEndLine() = l.getStartLine() and
92+
this.getEndColumn() < l.getStartColumn()
93+
)
7894
}
7995

8096
/** Holds if location `l` is completely contained within this one. */

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ Instruction getInstructionBackEdgeSuccessor(Instruction instruction, EdgeKind ki
349349

350350
/** Holds if `goto` jumps strictly forward in the program text. */
351351
private predicate isStrictlyForwardGoto(GotoStmt goto) {
352-
goto.getLocation().isBefore(goto.getTarget().getLocation())
352+
goto.getLocation().isBefore(goto.getTarget().getLocation(), _)
353353
}
354354

355355
Locatable getInstructionAst(TStageInstruction instr) {

cpp/ql/test/library-tests/ir/ir/raw_ir.expected

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8844,16 +8844,13 @@ ir.cpp:
88448844

88458845
# 1693| int goto_on_same_line()
88468846
# 1693| Block 0
8847-
# 1693| v1693_1(void) = EnterFunction :
8848-
# 1693| mu1693_2(unknown) = AliasedDefinition :
8849-
# 1693| mu1693_3(unknown) = InitializeNonLocal :
8850-
# 1694| r1694_1(glval<int>) = VariableAddress[x] :
8851-
# 1694| r1694_2(int) = Constant[42] :
8852-
# 1694| mu1694_3(int) = Store[x] : &:r1694_1, r1694_2
8853-
# 1695| v1695_1(void) = NoOp :
8854-
#-----| Goto (back edge) -> Block 1
8855-
8856-
# 1695| Block 1
8847+
# 1693| v1693_1(void) = EnterFunction :
8848+
# 1693| mu1693_2(unknown) = AliasedDefinition :
8849+
# 1693| mu1693_3(unknown) = InitializeNonLocal :
8850+
# 1694| r1694_1(glval<int>) = VariableAddress[x] :
8851+
# 1694| r1694_2(int) = Constant[42] :
8852+
# 1694| mu1694_3(int) = Store[x] : &:r1694_1, r1694_2
8853+
# 1695| v1695_1(void) = NoOp :
88578854
# 1695| v1695_2(void) = NoOp :
88588855
# 1696| r1696_1(glval<int>) = VariableAddress[#return] :
88598856
# 1696| r1696_2(glval<int>) = VariableAddress[x] :

0 commit comments

Comments
 (0)