Skip to content

Commit 4101601

Browse files
committed
Go: Add BasicBlock.getLocation
1 parent b08f535 commit 4101601

File tree

5 files changed

+175
-134
lines changed

5 files changed

+175
-134
lines changed

go/ql/lib/semmle/go/Locations.qll

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
import go
44
private import internal.Locations
55

6+
private module DbLocationInput implements LocationClassInputSig {
7+
class Base = TDbLocation;
8+
9+
predicate locationInfo(
10+
Base b, string filepath, int startline, int startcolumn, int endline, int endcolumn
11+
) {
12+
exists(File f |
13+
dbLocationInfo(b, f, startline, startcolumn, endline, endcolumn) and
14+
filepath = f.getAbsolutePath()
15+
)
16+
}
17+
}
18+
619
/**
720
* A location as given by a file, a start line, a start column,
821
* an end line, and an end column.
@@ -11,51 +24,57 @@ private import internal.Locations
1124
*
1225
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
1326
*/
14-
class DbLocation extends TDbLocation {
27+
final class DbLocation extends LocationClass<DbLocationInput>::Location {
1528
/** Gets the file for this location. */
1629
File getFile() { dbLocationInfo(this, result, _, _, _, _) }
30+
}
1731

18-
/** Gets the 1-based line number (inclusive) where this location starts. */
19-
int getStartLine() { dbLocationInfo(this, _, result, _, _, _) }
20-
21-
/** Gets the 1-based column number (inclusive) where this location starts. */
22-
int getStartColumn() { dbLocationInfo(this, _, _, result, _, _) }
32+
private module DbOrBasicBlockLocationInput implements LocationClassInputSig {
33+
class Base = TDbLocation or TBasicBlockLocation;
2334

24-
/** Gets the 1-based line number (inclusive) where this location ends. */
25-
int getEndLine() { dbLocationInfo(this, _, _, _, result, _) }
35+
predicate locationInfo(
36+
Base b, string filepath, int startline, int startcolumn, int endline, int endcolumn
37+
) {
38+
DbLocationInput::locationInfo(b, filepath, startline, startcolumn, endline, endcolumn)
39+
or
40+
basicBlockLocationInfo(b, filepath, startline, startcolumn, endline, endcolumn)
41+
}
42+
}
2643

27-
/** Gets the 1-based column number (inclusive) where this location ends. */
28-
int getEndColumn() { dbLocationInfo(this, _, _, _, _, result) }
44+
/**
45+
* A location as given by a file, a start line, a start column,
46+
* an end line, and an end column.
47+
*
48+
* This class is restricted to locations created by the extractor or
49+
* synthesized for basic blocks.
50+
*
51+
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
52+
*/
53+
class DbOrBasicBlockLocation = LocationClass<DbOrBasicBlockLocationInput>::Location;
2954

30-
/** Gets the number of lines covered by this location. */
31-
int getNumLines() { result = this.getEndLine() - this.getStartLine() + 1 }
55+
private module LocationInput implements LocationClassInputSig {
56+
class Base = TLocation;
3257

33-
/** Gets a textual representation of this element. */
34-
string toString() {
35-
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
36-
this.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
37-
result = filepath + "@" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
38-
)
39-
}
40-
41-
/**
42-
* Holds if this element is at the specified location.
43-
* The location spans column `startcolumn` of line `startline` to
44-
* column `endcolumn` of line `endline` in file `filepath`.
45-
* For more information, see
46-
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
47-
*/
48-
predicate hasLocationInfo(
49-
string filepath, int startline, int startcolumn, int endline, int endcolumn
58+
predicate locationInfo(
59+
Base b, string filepath, int startline, int startcolumn, int endline, int endcolumn
5060
) {
51-
exists(File f |
52-
dbLocationInfo(this, f, startline, startcolumn, endline, endcolumn) and
53-
filepath = f.getAbsolutePath()
54-
)
61+
DbOrBasicBlockLocationInput::locationInfo(b, filepath, startline, startcolumn, endline,
62+
endcolumn)
63+
or
64+
dataFlowNodeLocationInfo(b, filepath, startline, startcolumn, endline, endcolumn)
5565
}
5666
}
5767

58-
final class Location = LocationImpl;
68+
/**
69+
* A location as given by a file, a start line, a start column,
70+
* an end line, and an end column.
71+
*
72+
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
73+
*/
74+
final class Location extends LocationClass<LocationInput>::Location {
75+
/** Gets the file for this location. */
76+
File getFile() { dbLocationInfo(this, result, _, _, _, _) }
77+
}
5978

6079
/** A program element with a location. */
6180
class Locatable extends @locatable {

go/ql/lib/semmle/go/controlflow/BasicBlocks.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import go
66
private import ControlFlowGraphImpl
7+
private import semmle.go.internal.Locations
78

89
/**
910
* Holds if `nd` starts a new basic block.
@@ -121,12 +122,14 @@ class BasicBlock extends TControlFlowNode {
121122
* For more information, see
122123
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
123124
*/
124-
predicate hasLocationInfo(
125+
deprecated predicate hasLocationInfo(
125126
string filepath, int startline, int startcolumn, int endline, int endcolumn
126127
) {
127-
this.getFirstNode().hasLocationInfo(filepath, startline, startcolumn, _, _) and
128-
this.getLastNode().hasLocationInfo(_, _, _, endline, endcolumn)
128+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
129129
}
130+
131+
/** Gets the location of this basic block. */
132+
DbOrBasicBlockLocation getLocation() { result = getBasicBlockLocation(this) }
130133
}
131134

132135
/**

go/ql/lib/semmle/go/dataflow/SSA.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ abstract class SsaImplicitDefinition extends SsaDefinition {
219219
) {
220220
endline = startline and
221221
endcolumn = startcolumn and
222-
this.getBasicBlock().hasLocationInfo(filepath, startline, startcolumn, _, _)
222+
this.getBasicBlock().getLocation().hasLocationInfo(filepath, startline, startcolumn, _, _)
223223
}
224224
}
225225

@@ -298,7 +298,7 @@ class SsaPhiNode extends SsaPseudoDefinition, TPhi {
298298
) {
299299
endline = startline and
300300
endcolumn = startcolumn and
301-
this.getBasicBlock().hasLocationInfo(filepath, startline, startcolumn, _, _)
301+
this.getBasicBlock().getLastNode().hasLocationInfo(filepath, startline, startcolumn, _, _)
302302
}
303303
}
304304

go/ql/lib/semmle/go/dataflow/internal/DataFlowNodes.qll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ private import semmle.go.dataflow.FlowSummary
44
private import DataFlowPrivate
55
private import FlowSummaryImpl as FlowSummaryImpl
66
private import semmle.go.dataflow.ExternalFlow
7+
private import semmle.go.internal.Locations
78

89
cached
910
private newtype TNode =
@@ -158,12 +159,7 @@ module Public {
158159
}
159160

160161
/** Gets the location of this node. */
161-
Location getLocation() {
162-
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
163-
this.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
164-
result.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
165-
)
166-
}
162+
Location getLocation() { result = getDataFlowNodeLocation(this) }
167163

168164
/** Gets the file in which this node appears. */
169165
File getFile() { this.hasLocationInfo(result.getAbsolutePath(), _, _, _, _) }

0 commit comments

Comments
 (0)