Skip to content

Commit dcf63fc

Browse files
committed
JS: Remove synthetic locations
1 parent f1ca0ec commit dcf63fc

File tree

13 files changed

+63
-264
lines changed

13 files changed

+63
-264
lines changed

javascript/ql/lib/semmle/javascript/AST.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class AstNode extends @ast_node, NodeInStmtContainer {
3131

3232
/** Gets the first token belonging to this element. */
3333
Token getFirstToken() {
34-
exists(DbLocation l1, DbLocation l2, string filepath, int startline, int startcolumn |
34+
exists(Location l1, Location l2, string filepath, int startline, int startcolumn |
3535
l1 = this.getLocation() and
3636
l2 = result.getLocation() and
3737
l1.hasLocationInfo(filepath, startline, startcolumn, _, _) and
@@ -41,7 +41,7 @@ class AstNode extends @ast_node, NodeInStmtContainer {
4141

4242
/** Gets the last token belonging to this element. */
4343
Token getLastToken() {
44-
exists(DbLocation l1, DbLocation l2, string filepath, int endline, int endcolumn |
44+
exists(Location l1, Location l2, string filepath, int endline, int endcolumn |
4545
l1 = this.getLocation() and
4646
l2 = result.getLocation() and
4747
l1.hasLocationInfo(filepath, _, _, endline, endcolumn) and

javascript/ql/lib/semmle/javascript/Files.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import javascript
44
private import NodeModuleResolutionImpl
55
private import codeql.util.FileSystem
6-
private import internal.Locations
76

87
private module FsInput implements InputSig {
98
abstract class ContainerBase extends @container {
@@ -99,7 +98,7 @@ class File extends Container, Impl::File {
9998
*
10099
* Note that files have special locations starting and ending at line zero, column zero.
101100
*/
102-
DbLocation getLocation() { result = getLocatableLocation(this) }
101+
Location getLocation() { hasLocation(this, result) }
103102

104103
/** Gets the number of lines in this file. */
105104
int getNumberOfLines() { result = sum(int loc | numlines(this, loc, _, _) | loc) }

javascript/ql/lib/semmle/javascript/JSON.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44

55
import javascript
6-
private import semmle.javascript.internal.Locations
76

87
/**
98
* A JSON-encoded value, which may be a primitive value, an array or an object.
@@ -33,7 +32,7 @@ class JsonValue extends @json_value, Locatable {
3332
override string toString() { json(this, _, _, _, result) }
3433

3534
/** Gets the JSON file containing this value. */
36-
File getJsonFile() { result = getLocatableLocation(this).getFile() }
35+
File getJsonFile() { exists(Location loc | json_locations(this, loc) and result = loc.getFile()) }
3736

3837
/** If this is an object, gets the value of property `name`. */
3938
JsonValue getPropValue(string name) { json_properties(this, name, result) }

javascript/ql/lib/semmle/javascript/Locations.qll

Lines changed: 25 additions & 20 deletions
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 javascript
4-
private import internal.Locations
54

65
/**
76
* A location as given by a file, a start line, a start column,
@@ -11,31 +10,31 @@ 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+
final class Location extends @location_default {
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 }
3231

3332
/** Holds if this location starts before location `that`. */
3433
pragma[inline]
35-
predicate startsBefore(DbLocation that) {
36-
exists(File f, int sl1, int sc1, int sl2, int sc2 |
37-
dbLocationInfo(this, f, sl1, sc1, _, _) and
38-
dbLocationInfo(that, f, sl2, sc2, _, _)
34+
predicate startsBefore(Location that) {
35+
exists(string f, int sl1, int sc1, int sl2, int sc2 |
36+
this.hasLocationInfo(f, sl1, sc1, _, _) and
37+
that.hasLocationInfo(f, sl2, sc2, _, _)
3938
|
4039
sl1 < sl2
4140
or
@@ -45,10 +44,10 @@ class DbLocation extends TDbLocation {
4544

4645
/** Holds if this location ends after location `that`. */
4746
pragma[inline]
48-
predicate endsAfter(DbLocation that) {
49-
exists(File f, int el1, int ec1, int el2, int ec2 |
50-
dbLocationInfo(this, f, _, _, el1, ec1) and
51-
dbLocationInfo(that, f, _, _, el2, ec2)
47+
predicate endsAfter(Location that) {
48+
exists(string f, int el1, int ec1, int el2, int ec2 |
49+
this.hasLocationInfo(f, _, _, el1, ec1) and
50+
that.hasLocationInfo(f, _, _, el2, ec2)
5251
|
5352
el1 > el2
5453
or
@@ -60,10 +59,10 @@ class DbLocation extends TDbLocation {
6059
* Holds if this location contains location `that`, meaning that it starts
6160
* before and ends after it.
6261
*/
63-
predicate contains(DbLocation that) { this.startsBefore(that) and this.endsAfter(that) }
62+
predicate contains(Location that) { this.startsBefore(that) and this.endsAfter(that) }
6463

6564
/** Holds if this location is empty. */
66-
predicate isEmpty() { exists(int l, int c | dbLocationInfo(this, _, l, c, l, c - 1)) }
65+
predicate isEmpty() { exists(int l, int c | this.hasLocationInfo(_, l, c, l, c - 1)) }
6766

6867
/** Gets a textual representation of this element. */
6968
string toString() { result = this.getFile().getBaseName() + ":" + this.getStartLine().toString() }
@@ -79,21 +78,27 @@ class DbLocation extends TDbLocation {
7978
string filepath, int startline, int startcolumn, int endline, int endcolumn
8079
) {
8180
exists(File f |
82-
dbLocationInfo(this, f, startline, startcolumn, endline, endcolumn) and
81+
locations_default(this, f, startline, startcolumn, endline, endcolumn) and
8382
filepath = f.getAbsolutePath()
8483
)
8584
}
8685
}
8786

88-
final class Location = LocationImpl;
87+
cached
88+
private Location getLocatableLocation(@locatable l) {
89+
hasLocation(l, result) or
90+
xmllocations(l, result) or
91+
json_locations(l, result) or
92+
yaml_locations(l, result)
93+
}
8994

9095
/** A program element with a location. */
9196
class Locatable extends @locatable {
9297
/** Gets the file this program element comes from. */
9398
File getFile() { result = this.getLocation().getFile() }
9499

95100
/** Gets this element's location. */
96-
final DbLocation getLocation() { result = getLocatableLocation(this) }
101+
final Location getLocation() { result = getLocatableLocation(this) }
97102

98103
/**
99104
* Gets the line on which this element starts.

javascript/ql/lib/semmle/javascript/RestrictedLocations.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class FirstLineOf extends Locatable {
2626
then endcolumn = xc
2727
else
2828
endcolumn =
29-
max(int c | any(DbLocation l).hasLocationInfo(filepath, startline, _, startline, c))
29+
max(int c | any(Location l).hasLocationInfo(filepath, startline, _, startline, c))
3030
)
3131
}
3232
}

javascript/ql/lib/semmle/javascript/SSA.qll

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -412,17 +412,22 @@ class SsaVariable extends TSsaDefinition {
412412
/** Gets a textual representation of this element. */
413413
string toString() { result = this.getDefinition().prettyPrintRef() }
414414

415+
/** Gets the location of this SSA variable. */
416+
Location getLocation() { result = this.getDefinition().getLocation() }
417+
415418
/**
419+
* DEPRECATED. Use `getLocation().hasLocationInfo()` instead.
420+
*
416421
* Holds if this element is at the specified location.
417422
* The location spans column `startcolumn` of line `startline` to
418423
* column `endcolumn` of line `endline` in file `filepath`.
419424
* For more information, see
420425
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
421426
*/
422-
predicate hasLocationInfo(
427+
deprecated predicate hasLocationInfo(
423428
string filepath, int startline, int startcolumn, int endline, int endcolumn
424429
) {
425-
this.getDefinition().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
430+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
426431
}
427432
}
428433

@@ -478,23 +483,22 @@ class SsaDefinition extends TSsaDefinition {
478483
string toString() { result = this.prettyPrintDef() }
479484

480485
/**
486+
* DEPRECATED. Use `getLocation().hasLocationInfo()` instead.
487+
*
481488
* Holds if this element is at the specified location.
482489
* The location spans column `startcolumn` of line `startline` to
483490
* column `endcolumn` of line `endline` in file `filepath`.
484491
* For more information, see
485492
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
486493
*/
487-
abstract predicate hasLocationInfo(
494+
deprecated predicate hasLocationInfo(
488495
string filepath, int startline, int startcolumn, int endline, int endcolumn
489-
);
496+
) {
497+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
498+
}
490499

491500
/** Gets the location of this element. */
492-
final Location getLocation() {
493-
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
494-
this.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
495-
result.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
496-
)
497-
}
501+
Location getLocation() { result = this.getBasicBlock().getLocation() }
498502

499503
/** Gets the function or toplevel to which this definition belongs. */
500504
StmtContainer getContainer() { result = this.getBasicBlock().getContainer() }
@@ -522,20 +526,13 @@ class SsaExplicitDefinition extends SsaDefinition, TExplicitDef {
522526
override VarDef getAContributingVarDef() { result = this.getDef() }
523527

524528
override string prettyPrintRef() {
525-
exists(int l, int c | this.hasLocationInfo(_, l, c, _, _) | result = "def@" + l + ":" + c)
529+
exists(int l, int c | this.getLocation().hasLocationInfo(_, l, c, _, _) |
530+
result = "def@" + l + ":" + c
531+
)
526532
}
527533

528534
override string prettyPrintDef() { result = this.getDef().toString() }
529535

530-
override predicate hasLocationInfo(
531-
string filepath, int startline, int startcolumn, int endline, int endcolumn
532-
) {
533-
exists(Location loc |
534-
pragma[only_bind_into](loc) = pragma[only_bind_into](this.getDef()).getLocation() and
535-
loc.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
536-
)
537-
}
538-
539536
/**
540537
* Gets the data flow node representing the incoming value assigned at this definition,
541538
* if any.
@@ -557,21 +554,10 @@ abstract class SsaImplicitDefinition extends SsaDefinition {
557554
abstract string getKind();
558555

559556
override string prettyPrintRef() {
560-
exists(int l, int c | this.hasLocationInfo(_, l, c, _, _) |
557+
exists(int l, int c | this.getLocation().hasLocationInfo(_, l, c, _, _) |
561558
result = this.getKind() + "@" + l + ":" + c
562559
)
563560
}
564-
565-
override predicate hasLocationInfo(
566-
string filepath, int startline, int startcolumn, int endline, int endcolumn
567-
) {
568-
endline = startline and
569-
endcolumn = startcolumn and
570-
exists(Location loc |
571-
pragma[only_bind_into](loc) = pragma[only_bind_into](this.getBasicBlock()).getLocation() and
572-
loc.hasLocationInfo(filepath, startline, startcolumn, _, _)
573-
)
574-
}
575561
}
576562

577563
/**
@@ -617,16 +603,6 @@ class SsaVariableCapture extends SsaImplicitDefinition, TCapture {
617603
override string getKind() { result = "capture" }
618604

619605
override string prettyPrintDef() { result = "capture variable " + this.getSourceVariable() }
620-
621-
override predicate hasLocationInfo(
622-
string filepath, int startline, int startcolumn, int endline, int endcolumn
623-
) {
624-
exists(ReachableBasicBlock bb, int i | this.definesAt(bb, i, _) |
625-
bb.getNode(i)
626-
.getLocation()
627-
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
628-
)
629-
}
630606
}
631607

632608
/**
@@ -747,13 +723,7 @@ class SsaRefinementNode extends SsaPseudoDefinition, TRefinement {
747723
this.getSourceVariable() + " = refine[" + this.getGuard() + "](" + this.ppInputs() + ")"
748724
}
749725

750-
override predicate hasLocationInfo(
751-
string filepath, int startline, int startcolumn, int endline, int endcolumn
752-
) {
753-
this.getGuard()
754-
.getLocation()
755-
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
756-
}
726+
override Location getLocation() { result = this.getGuard().getLocation() }
757727
}
758728

759729
module Ssa {

javascript/ql/lib/semmle/javascript/Variables.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ class LocalVariable extends Variable {
353353
* If the variable has one or more declarations, the location of the first declaration is used.
354354
* If the variable has no declaration, the entry point of its declaring container is used.
355355
*/
356-
DbLocation getLocation() {
356+
Location getLocation() {
357357
result =
358-
min(DbLocation loc |
358+
min(Location loc |
359359
loc = this.getADeclaration().getLocation()
360360
|
361361
loc order by loc.getStartLine(), loc.getStartColumn()

javascript/ql/lib/semmle/javascript/XML.qll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
*/
44

55
import semmle.files.FileSystem
6-
private import semmle.javascript.internal.Locations
76
private import codeql.xml.Xml
87

9-
private module Input implements InputSig<File, DbLocation> {
8+
private module Input implements InputSig<File, Location> {
109
class XmlLocatableBase = @xmllocatable or @xmlnamespaceable;
1110

12-
predicate xmllocations_(XmlLocatableBase e, DbLocation loc) { loc = getLocatableLocation(e) }
11+
predicate xmllocations_(XmlLocatableBase e, Location loc) { xmllocations(e, loc) }
1312

1413
class XmlParentBase = @xmlparent;
1514

@@ -67,4 +66,4 @@ private module Input implements InputSig<File, DbLocation> {
6766
}
6867
}
6968

70-
import Make<File, DbLocation, Input>
69+
import Make<File, Location, Input>

javascript/ql/lib/semmle/javascript/YAML.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import javascript
99
private import codeql.yaml.Yaml as LibYaml
1010

1111
private module YamlSig implements LibYaml::InputSig {
12-
class Location = DbLocation;
13-
1412
class LocatableBase extends @yaml_locatable, Locatable { }
1513

1614
import javascript

javascript/ql/lib/semmle/javascript/dataflow/internal/VariableCapture.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ private import semmle.javascript.dataflow.internal.VariableOrThis
44
private import codeql.dataflow.VariableCapture
55
private import semmle.javascript.dataflow.internal.sharedlib.DataFlowImplCommon as DataFlowImplCommon
66

7-
module VariableCaptureConfig implements InputSig<js::DbLocation> {
7+
module VariableCaptureConfig implements InputSig<js::Location> {
88
private js::Function getLambdaFromVariable(js::LocalVariable variable) {
99
result.getVariable() = variable
1010
or
@@ -168,7 +168,7 @@ module VariableCaptureConfig implements InputSig<js::DbLocation> {
168168

169169
string toString() { none() } // Overridden in subclass
170170

171-
js::DbLocation getLocation() { none() } // Overridden in subclass
171+
js::Location getLocation() { none() } // Overridden in subclass
172172

173173
predicate hasCfgNode(BasicBlock bb, int i) { none() } // Overridden in subclass
174174

@@ -186,7 +186,7 @@ module VariableCaptureConfig implements InputSig<js::DbLocation> {
186186
override string toString() { result = pattern.toString() }
187187

188188
/** Gets the location of this write. */
189-
override js::DbLocation getLocation() { result = pattern.getLocation() }
189+
override js::Location getLocation() { result = pattern.getLocation() }
190190

191191
override js::DataFlow::Node getSource() {
192192
// Note: there is not always an expression corresponding to the RHS of the assignment.
@@ -222,7 +222,7 @@ module VariableCaptureConfig implements InputSig<js::DbLocation> {
222222

223223
override string toString() { result = "[implicit init] " + variable }
224224

225-
override js::DbLocation getLocation() { result = variable.getLocation() }
225+
override js::Location getLocation() { result = variable.getLocation() }
226226

227227
override CapturedVariable getVariable() { result = variable }
228228

@@ -242,7 +242,7 @@ module VariableCaptureConfig implements InputSig<js::DbLocation> {
242242
predicate entryBlock(BasicBlock bb) { bb instanceof js::EntryBasicBlock }
243243
}
244244

245-
module VariableCaptureOutput = Flow<js::DbLocation, VariableCaptureConfig>;
245+
module VariableCaptureOutput = Flow<js::Location, VariableCaptureConfig>;
246246

247247
js::DataFlow::Node getNodeFromClosureNode(VariableCaptureOutput::ClosureNode node) {
248248
result = TValueNode(node.(VariableCaptureOutput::ExprNode).getExpr())

0 commit comments

Comments
 (0)