Skip to content

Commit ce267e9

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

File tree

5 files changed

+194
-134
lines changed

5 files changed

+194
-134
lines changed

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

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
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+
File getFile(Base b) { dbLocationInfo(b, result, _, _, _, _) }
19+
}
20+
621
/**
722
* A location as given by a file, a start line, a start column,
823
* an end line, and an end column.
@@ -11,51 +26,63 @@ private import internal.Locations
1126
*
1227
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
1328
*/
14-
class DbLocation extends TDbLocation {
15-
/** Gets the file for this location. */
16-
File getFile() { dbLocationInfo(this, result, _, _, _, _) }
29+
class DbLocation = LocationClass<DbLocationInput>::Location;
1730

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

21-
/** Gets the 1-based column number (inclusive) where this location starts. */
22-
int getStartColumn() { dbLocationInfo(this, _, _, result, _, _) }
34+
predicate locationInfo(
35+
Base b, string filepath, int startline, int startcolumn, int endline, int endcolumn
36+
) {
37+
DbLocationInput::locationInfo(b, filepath, startline, startcolumn, endline, endcolumn)
38+
or
39+
basicBlockLocationInfo(b, filepath, startline, startcolumn, endline, endcolumn)
40+
}
2341

24-
/** Gets the 1-based line number (inclusive) where this location ends. */
25-
int getEndLine() { dbLocationInfo(this, _, _, _, result, _) }
42+
File getFile(Base b) {
43+
result = DbLocationInput::getFile(b)
44+
or
45+
basicBlockLocationInfo(b, result.getAbsolutePath(), _, _, _, _)
46+
}
47+
}
2648

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

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

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-
)
63+
predicate locationInfo(
64+
Base b, string filepath, int startline, int startcolumn, int endline, int endcolumn
65+
) {
66+
DbOrBasicBlockLocationInput::locationInfo(b, filepath, startline, startcolumn, endline,
67+
endcolumn)
68+
or
69+
dataFlowNodeLocationInfo(b, filepath, startline, startcolumn, endline, endcolumn)
3970
}
4071

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
50-
) {
51-
exists(File f |
52-
dbLocationInfo(this, f, startline, startcolumn, endline, endcolumn) and
53-
filepath = f.getAbsolutePath()
54-
)
72+
File getFile(Base b) {
73+
result = DbOrBasicBlockLocationInput::getFile(b)
74+
or
75+
dataFlowNodeLocationInfo(b, result.getAbsolutePath(), _, _, _, _)
5576
}
5677
}
5778

58-
final class Location = LocationImpl;
79+
/**
80+
* A location as given by a file, a start line, a start column,
81+
* an end line, and an end column.
82+
*
83+
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
84+
*/
85+
class Location = LocationClass<LocationInput>::Location;
5986

6087
/** A program element with a location. */
6188
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)