Skip to content

Commit a81024a

Browse files
authored
Merge pull request #8525 from MathiasVP/more-precise-is-before
C++: Consider columns in `Location.isBefore`
2 parents 0eab54d + c35b385 commit a81024a

File tree

6 files changed

+72
-3
lines changed

6 files changed

+72
-3
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) { this.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/PrintAST.expected

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13035,6 +13035,23 @@ ir.cpp:
1303513035
# 1689| getEntryPoint(): [BlockStmt] { ... }
1303613036
# 1689| getStmt(0): [EmptyStmt] ;
1303713037
# 1689| getStmt(1): [ReturnStmt] return ...
13038+
# 1693| [TopLevelFunction] int goto_on_same_line()
13039+
# 1693| <params>:
13040+
# 1693| getEntryPoint(): [BlockStmt] { ... }
13041+
# 1694| getStmt(0): [DeclStmt] declaration
13042+
# 1694| getDeclarationEntry(0): [VariableDeclarationEntry] definition of x
13043+
# 1694| Type = [IntType] int
13044+
# 1694| getVariable().getInitializer(): [Initializer] initializer for x
13045+
# 1694| getExpr(): [Literal] 42
13046+
# 1694| Type = [IntType] int
13047+
# 1694| Value = [Literal] 42
13048+
# 1694| ValueCategory = prvalue
13049+
# 1695| getStmt(1): [GotoStmt] goto ...
13050+
# 1695| getStmt(2): [LabelStmt] label ...:
13051+
# 1696| getStmt(3): [ReturnStmt] return ...
13052+
# 1696| getExpr(): [VariableAccess] x
13053+
# 1696| Type = [IntType] int
13054+
# 1696| ValueCategory = prvalue(load)
1303813055
perf-regression.cpp:
1303913056
# 4| [CopyAssignmentOperator] Big& Big::operator=(Big const&)
1304013057
# 4| <params>:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,4 +1690,10 @@ void captured_lambda(int x, int &y, int &&z)
16901690
};
16911691
}
16921692

1693+
int goto_on_same_line() {
1694+
int x = 42;
1695+
goto next; next:
1696+
return x;
1697+
}
1698+
16931699
// semmle-extractor-options: -std=c++17 --clang

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7527,6 +7527,17 @@
75277527
| ir.cpp:1689:50:1689:50 | Load | m1689_6 |
75287528
| ir.cpp:1689:50:1689:50 | SideEffect | m1689_3 |
75297529
| ir.cpp:1689:50:1689:50 | SideEffect | m1689_8 |
7530+
| ir.cpp:1693:5:1693:21 | Address | &:r1693_5 |
7531+
| ir.cpp:1693:5:1693:21 | ChiPartial | partial:m1693_3 |
7532+
| ir.cpp:1693:5:1693:21 | ChiTotal | total:m1693_2 |
7533+
| ir.cpp:1693:5:1693:21 | Load | m1696_4 |
7534+
| ir.cpp:1693:5:1693:21 | SideEffect | m1693_3 |
7535+
| ir.cpp:1694:7:1694:7 | Address | &:r1694_1 |
7536+
| ir.cpp:1694:10:1694:12 | StoreValue | r1694_2 |
7537+
| ir.cpp:1696:3:1696:11 | Address | &:r1696_1 |
7538+
| ir.cpp:1696:10:1696:10 | Address | &:r1696_2 |
7539+
| ir.cpp:1696:10:1696:10 | Load | m1694_3 |
7540+
| ir.cpp:1696:10:1696:10 | StoreValue | r1696_3 |
75307541
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |
75317542
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |
75327543
| perf-regression.cpp:6:3:6:5 | Address | &:r6_7 |

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8842,6 +8842,25 @@ ir.cpp:
88428842
# 1689| v1689_12(void) = AliasedUse : ~m?
88438843
# 1689| v1689_13(void) = ExitFunction :
88448844

8845+
# 1693| int goto_on_same_line()
8846+
# 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+
# 1695| v1695_2(void) = NoOp :
8855+
# 1696| r1696_1(glval<int>) = VariableAddress[#return] :
8856+
# 1696| r1696_2(glval<int>) = VariableAddress[x] :
8857+
# 1696| r1696_3(int) = Load[x] : &:r1696_2, ~m?
8858+
# 1696| mu1696_4(int) = Store[#return] : &:r1696_1, r1696_3
8859+
# 1693| r1693_4(glval<int>) = VariableAddress[#return] :
8860+
# 1693| v1693_5(void) = ReturnValue : &:r1693_4, ~m?
8861+
# 1693| v1693_6(void) = AliasedUse : ~m?
8862+
# 1693| v1693_7(void) = ExitFunction :
8863+
88458864
perf-regression.cpp:
88468865
# 6| void Big::Big()
88478866
# 6| Block 0

0 commit comments

Comments
 (0)