Skip to content

Commit 76ad107

Browse files
authored
Merge pull request #18883 from owen-mc/go/get-location
Go: Use `getLocation` instead of `hasLocationInfo`
2 parents f7d95e4 + a94854b commit 76ad107

File tree

105 files changed

+430
-823
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+430
-823
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: deprecated
3+
---
4+
* The member predicate `hasLocationInfo` has been deprecated on the following classes: `BasicBlock`, `Callable`, `Content`, `ContentSet`, `ControlFlow::Node`, `DataFlowCallable`, `DataFlow::Node`, `Entity`, `GVN`, `HtmlTemplate::TemplateStmt`, `IR:WriteTarget`, `SourceSinkInterpretationInput::SourceOrSinkElement`, `SourceSinkInterpretationInput::InterpretNode`, `SsaVariable`, `SsaDefinition`, `SsaWithFields`, `StringOps::ConcatenationElement`, `Type`, and `VariableWithFields`. Use `getLocation()` instead.

go/ql/lib/semmle/go/DiagnosticsReporting.qll

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** Provides classes for working with errors and warnings recorded during extraction. */
22

33
import go
4-
private import semmle.go.internal.Locations
54

65
/** Gets the SARIF severity level that indicates an error. */
76
private int getErrorSeverity() { result = 2 }
@@ -20,18 +19,10 @@ private class Diagnostic extends @diagnostic {
2019
string getMessage() { diagnostics(this, _, _, result, _, _) }
2120

2221
/** Gets the file that this error is associated with, if any. */
23-
File getFile() { this.hasLocationInfo(result.getAbsolutePath(), _, _, _, _) }
22+
File getFile() { result = this.getLocation().getFile() }
2423

25-
/**
26-
* Holds if this element is at the specified location.
27-
* The location spans column `startcolumn` of line `startline` to
28-
* column `endcolumn` of line `endline` in file `filepath`.
29-
* For more information, see
30-
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
31-
*/
32-
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
33-
getDiagnosticLocation(this).hasLocationInfo(path, sl, sc, el, ec)
34-
}
24+
/** Gets the location for this error. */
25+
Location getLocation() { diagnostics(this, _, _, _, _, result) }
3526

3627
string toString() { result = this.getMessage() }
3728
}
@@ -68,7 +59,7 @@ predicate reportableDiagnostics(Diagnostic d, string msg, int sev) {
6859
exists(File f | f = d.getFile() |
6960
exists(f.getAChild()) and
7061
msg =
71-
"Extraction failed in " + d.getFile().getRelativePath() + " with error " +
62+
"Extraction failed in " + f.getRelativePath() + " with error " +
7263
removeAbsolutePaths(d.getMessage())
7364
)
7465
or

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

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** Provides classes for working with locations and program elements that have locations. */
22

33
import go
4-
private import internal.Locations
54

65
/**
76
* A location as given by a file, a start line, a start column,
@@ -11,21 +10,21 @@ private import internal.Locations
1110
*
1211
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
1312
*/
14-
class DbLocation extends TDbLocation {
13+
class Location extends @location {
1514
/** Gets the file for this location. */
16-
File getFile() { dbLocationInfo(this, result, _, _, _, _) }
15+
File getFile() { locations_default(this, result, _, _, _, _) }
1716

1817
/** Gets the 1-based line number (inclusive) where this location starts. */
19-
int getStartLine() { dbLocationInfo(this, _, result, _, _, _) }
18+
int getStartLine() { locations_default(this, _, result, _, _, _) }
2019

2120
/** Gets the 1-based column number (inclusive) where this location starts. */
22-
int getStartColumn() { dbLocationInfo(this, _, _, result, _, _) }
21+
int getStartColumn() { locations_default(this, _, _, result, _, _) }
2322

2423
/** Gets the 1-based line number (inclusive) where this location ends. */
25-
int getEndLine() { dbLocationInfo(this, _, _, _, result, _) }
24+
int getEndLine() { locations_default(this, _, _, _, result, _) }
2625

2726
/** Gets the 1-based column number (inclusive) where this location ends. */
28-
int getEndColumn() { dbLocationInfo(this, _, _, _, _, result) }
27+
int getEndColumn() { locations_default(this, _, _, _, _, result) }
2928

3029
/** Gets the number of lines covered by this location. */
3130
int getNumLines() { result = this.getEndLine() - this.getStartLine() + 1 }
@@ -48,22 +47,22 @@ class DbLocation extends TDbLocation {
4847
predicate hasLocationInfo(
4948
string filepath, int startline, int startcolumn, int endline, int endcolumn
5049
) {
51-
exists(File f |
52-
dbLocationInfo(this, f, startline, startcolumn, endline, endcolumn) and
50+
exists(File f | locations_default(this, f, startline, startcolumn, endline, endcolumn) |
5351
filepath = f.getAbsolutePath()
5452
)
5553
}
5654
}
5755

58-
final class Location = LocationImpl;
59-
6056
/** A program element with a location. */
6157
class Locatable extends @locatable {
6258
/** Gets the file this program element comes from. */
6359
File getFile() { result = this.getLocation().getFile() }
6460

6561
/** Gets this element's location. */
66-
final DbLocation getLocation() { result = getLocatableLocation(this) }
62+
final Location getLocation() {
63+
has_location(this, result) or
64+
xmllocations(this, result)
65+
}
6766

6867
/** Gets the number of lines covered by this element. */
6968
int getNumLines() { result = this.getLocation().getNumLines() }

go/ql/lib/semmle/go/Scopes.qll

+26-22
Original file line numberDiff line numberDiff line change
@@ -144,36 +144,34 @@ class Entity extends @object {
144144
/** Gets a textual representation of this entity. */
145145
string toString() { result = this.getName() }
146146

147-
private predicate hasRealLocationInfo(
148-
string filepath, int startline, int startcolumn, int endline, int endcolumn
149-
) {
150-
// take the location of the declaration if there is one
151-
this.getDeclaration().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) or
152-
any(CaseClause cc | this = cc.getImplicitlyDeclaredVariable())
153-
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
147+
/** Gets the location of this entity. */
148+
Location getLocation() {
149+
result = this.getDeclaration().getLocation()
150+
or
151+
result = any(CaseClause cc | this = cc.getImplicitlyDeclaredVariable()).getLocation()
154152
}
155153

156154
/**
155+
* DEPRECATED: Use `getLocation()` instead.
156+
*
157157
* Holds if this element is at the specified location.
158158
* The location spans column `startcolumn` of line `startline` to
159159
* column `endcolumn` of line `endline` in file `filepath`.
160160
* For more information, see
161161
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
162162
*/
163-
predicate hasLocationInfo(
163+
deprecated predicate hasLocationInfo(
164164
string filepath, int startline, int startcolumn, int endline, int endcolumn
165165
) {
166-
// take the location of the declaration if there is one
167-
if this.hasRealLocationInfo(_, _, _, _, _)
168-
then this.hasRealLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
169-
else (
170-
// otherwise fall back on dummy location
171-
filepath = "" and
172-
startline = 0 and
173-
startcolumn = 0 and
174-
endline = 0 and
175-
endcolumn = 0
176-
)
166+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
167+
or
168+
// otherwise fall back on dummy location
169+
not exists(this.getLocation()) and
170+
filepath = "" and
171+
startline = 0 and
172+
startcolumn = 0 and
173+
endline = 0 and
174+
endcolumn = 0
177175
}
178176
}
179177

@@ -680,16 +678,22 @@ class Callable extends TCallable {
680678
result = this.asFuncLit().getName()
681679
}
682680

681+
/** Gets the location of this callable. */
682+
Location getLocation() {
683+
result = this.asFunction().getLocation() or result = this.asFuncLit().getLocation()
684+
}
685+
683686
/**
687+
* DEPRECATED: Use `getLocation()` instead.
688+
*
684689
* Holds if this element is at the specified location.
685690
* The location spans column `sc` of line `sl` to
686691
* column `ec` of line `el` in file `fp`.
687692
* For more information, see
688693
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
689694
*/
690-
predicate hasLocationInfo(string fp, int sl, int sc, int el, int ec) {
691-
this.asFunction().hasLocationInfo(fp, sl, sc, el, ec) or
692-
this.asFuncLit().hasLocationInfo(fp, sl, sc, el, ec)
695+
deprecated predicate hasLocationInfo(string fp, int sl, int sc, int el, int ec) {
696+
this.getLocation().hasLocationInfo(fp, sl, sc, el, ec)
693697
}
694698
}
695699

go/ql/lib/semmle/go/StringOps.qll

+8-3
Original file line numberDiff line numberDiff line change
@@ -548,20 +548,25 @@ module StringOps {
548548
else result = "concatenation element"
549549
}
550550

551+
/** Gets the location of this element. */
552+
Location getLocation() { result = this.asNode().getLocation() }
553+
551554
/**
555+
* DEPRECATED: Use `getLocation()` instead.
556+
*
552557
* Holds if this element is at the specified location.
553558
* The location spans column `startcolumn` of line `startline` to
554559
* column `endcolumn` of line `endline` in file `filepath`.
555560
* For more information, see
556561
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
557562
*/
558-
predicate hasLocationInfo(
563+
deprecated predicate hasLocationInfo(
559564
string filepath, int startline, int startcolumn, int endline, int endcolumn
560565
) {
561-
this.asNode().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
566+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
562567
or
563568
// use dummy location for elements that don't have a corresponding node
564-
not exists(this.asNode()) and
569+
not exists(this.getLocation()) and
565570
filepath = "" and
566571
startline = 0 and
567572
startcolumn = 0 and

go/ql/lib/semmle/go/Types.qll

+8-3
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,24 @@ class Type extends @type {
144144
*/
145145
string toString() { result = this.getName() }
146146

147+
/** Gets the location of this type. */
148+
Location getLocation() { result = this.getEntity().getLocation() }
149+
147150
/**
151+
* DEPRECATED: Use `getLocation()` instead.
152+
*
148153
* Holds if this element is at the specified location.
149154
* The location spans column `startcolumn` of line `startline` to
150155
* column `endcolumn` of line `endline` in file `filepath`.
151156
* For more information, see
152157
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
153158
*/
154-
predicate hasLocationInfo(
159+
deprecated predicate hasLocationInfo(
155160
string filepath, int startline, int startcolumn, int endline, int endcolumn
156161
) {
157-
this.getEntity().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
162+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
158163
or
159-
not exists(this.getEntity()) and
164+
not exists(this.getLocation()) and
160165
filepath = "" and
161166
startline = 0 and
162167
startcolumn = 0 and

go/ql/lib/semmle/go/VariableWithFields.qll

+7-2
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,21 @@ class VariableWithFields extends TVariableWithFields {
183183
*/
184184
string getElement() { this = TVariableElementStep(_, result) }
185185

186+
/** Gets the location of this variable with fields. */
187+
Location getLocation() { result = this.getBaseVariable().getLocation() }
188+
186189
/**
190+
* DEPRECATED: Use `getLocation()` instead.
191+
*
187192
* Holds if this element is at the specified location.
188193
* The location spans column `startcolumn` of line `startline` to
189194
* column `endcolumn` of line `endline` in file `filepath`.
190195
* For more information, see
191196
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
192197
*/
193-
predicate hasLocationInfo(
198+
deprecated predicate hasLocationInfo(
194199
string filepath, int startline, int startcolumn, int endline, int endcolumn
195200
) {
196-
this.getBaseVariable().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
201+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
197202
}
198203
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,22 @@ class BasicBlock extends TControlFlowNode {
114114
/** Gets a textual representation of this basic block. */
115115
string toString() { result = "basic block" }
116116

117+
/** Gets the source location for this element. */
118+
Location getLocation() { result = this.getFirstNode().getLocation() }
119+
117120
/**
121+
* DEPRECATED: Use `getLocation()` instead.
122+
*
118123
* Holds if this basic block is at the specified location.
119124
* The location spans column `startcolumn` of line `startline` to
120125
* column `endcolumn` of line `endline` in file `filepath`.
121126
* For more information, see
122127
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
123128
*/
124-
predicate hasLocationInfo(
129+
deprecated predicate hasLocationInfo(
125130
string filepath, int startline, int startcolumn, int endline, int endcolumn
126131
) {
127-
this.getFirstNode().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
132+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
128133
}
129134
}
130135

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,31 @@ module ControlFlow {
7777
Root getRoot() { none() }
7878

7979
/** Gets the file to which this node belongs. */
80-
File getFile() { this.hasLocationInfo(result.getAbsolutePath(), _, _, _, _) }
80+
File getFile() { result = this.getLocation().getFile() }
8181

8282
/**
8383
* Gets a textual representation of this control flow node.
8484
*/
8585
string toString() { result = "control-flow node" }
8686

87+
/** Gets the source location for this element. */
88+
Location getLocation() { none() }
89+
8790
/**
91+
* DEPRECATED: Use `getLocation()` instead.
92+
*
8893
* Holds if this element is at the specified location.
8994
* The location spans column `startcolumn` of line `startline` to
9095
* column `endcolumn` of line `endline` in file `filepath`.
9196
* For more information, see
9297
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
9398
*/
94-
predicate hasLocationInfo(
99+
deprecated predicate hasLocationInfo(
95100
string filepath, int startline, int startcolumn, int endline, int endcolumn
96101
) {
102+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
103+
or
104+
not exists(this.getLocation()) and
97105
filepath = "" and
98106
startline = 0 and
99107
startcolumn = 0 and
@@ -244,11 +252,7 @@ module ControlFlow {
244252

245253
override string toString() { result = cond + " is " + outcome }
246254

247-
override predicate hasLocationInfo(
248-
string filepath, int startline, int startcolumn, int endline, int endcolumn
249-
) {
250-
cond.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
251-
}
255+
override Location getLocation() { result = cond.getLocation() }
252256
}
253257

254258
/**

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

+3-15
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,7 @@ class SkipNode extends ControlFlow::Node, MkSkipNode {
418418

419419
override string toString() { result = "skip" }
420420

421-
override predicate hasLocationInfo(
422-
string filepath, int startline, int startcolumn, int endline, int endcolumn
423-
) {
424-
skip.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
425-
}
421+
override Location getLocation() { result = skip.getLocation() }
426422
}
427423

428424
/**
@@ -437,11 +433,7 @@ class EntryNode extends ControlFlow::Node, MkEntryNode {
437433

438434
override string toString() { result = "entry" }
439435

440-
override predicate hasLocationInfo(
441-
string filepath, int startline, int startcolumn, int endline, int endcolumn
442-
) {
443-
root.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
444-
}
436+
override Location getLocation() { result = root.getLocation() }
445437
}
446438

447439
/**
@@ -456,11 +448,7 @@ class ExitNode extends ControlFlow::Node, MkExitNode {
456448

457449
override string toString() { result = "exit" }
458450

459-
override predicate hasLocationInfo(
460-
string filepath, int startline, int startcolumn, int endline, int endcolumn
461-
) {
462-
root.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
463-
}
451+
override Location getLocation() { result = root.getLocation() }
464452
}
465453

466454
/**

0 commit comments

Comments
 (0)