Skip to content

Commit 0d0eaa2

Browse files
authored
Merge pull request #20302 from asgerf/js/simpler-locations
JS: Remove synthetic locations
2 parents aa60442 + cc8fe10 commit 0d0eaa2

File tree

89 files changed

+3423
-3610
lines changed

Some content is hidden

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

89 files changed

+3423
-3610
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: 30 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.
@@ -144,3 +149,8 @@ class Locatable extends @locatable {
144149
*/
145150
string getAPrimaryQlClass() { result = "???" }
146151
}
152+
153+
/**
154+
* DEPRECATED. Use `Location` instead.
155+
*/
156+
deprecated class DbLocation = Location;

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: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ private module Internal {
108108
*/
109109
cached
110110
newtype TSsaDefinition =
111-
TExplicitDef(ReachableBasicBlock bb, int i, VarDef d, SsaSourceVariable v) {
112-
bb.defAt(i, v, d) and
111+
TExplicitDef(ReachableBasicBlock bb, int i, VarDef d, SsaSourceVariable v, VarRef lhs) {
112+
bb.defAt(i, v, d, lhs) and
113113
(
114114
liveAfterDef(bb, i, v) or
115115
v.isCaptured()
@@ -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() }
@@ -505,36 +509,34 @@ class SsaDefinition extends TSsaDefinition {
505509
*/
506510
class SsaExplicitDefinition extends SsaDefinition, TExplicitDef {
507511
override predicate definesAt(ReachableBasicBlock bb, int i, SsaSourceVariable v) {
508-
this = TExplicitDef(bb, i, _, v)
512+
this = TExplicitDef(bb, i, _, v, _)
509513
}
510514

511515
/** This SSA definition corresponds to the definition of `v` at `def`. */
512-
predicate defines(VarDef def, SsaSourceVariable v) { this = TExplicitDef(_, _, def, v) }
516+
predicate defines(VarDef def, SsaSourceVariable v) { this = TExplicitDef(_, _, def, v, _) }
513517

514518
/** Gets the variable definition wrapped by this SSA definition. */
515-
VarDef getDef() { this = TExplicitDef(_, _, result, _) }
519+
VarDef getDef() { this = TExplicitDef(_, _, result, _, _) }
520+
521+
/** Gets the variable reference appearing on the left-hand side of this assignment. */
522+
VarRef getLhs() { this = TExplicitDef(_, _, _, _, result) }
516523

517524
/** Gets the basic block to which this definition belongs. */
518525
override ReachableBasicBlock getBasicBlock() { this.definesAt(result, _, _) }
519526

520-
override SsaSourceVariable getSourceVariable() { this = TExplicitDef(_, _, _, result) }
527+
override SsaSourceVariable getSourceVariable() { this = TExplicitDef(_, _, _, result, _) }
521528

522529
override VarDef getAContributingVarDef() { result = this.getDef() }
523530

524531
override string prettyPrintRef() {
525-
exists(int l, int c | this.hasLocationInfo(_, l, c, _, _) | result = "def@" + l + ":" + c)
532+
exists(int l, int c | this.getLocation().hasLocationInfo(_, l, c, _, _) |
533+
result = "def@" + l + ":" + c
534+
)
526535
}
527536

528537
override string prettyPrintDef() { result = this.getDef().toString() }
529538

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-
}
539+
override Location getLocation() { result = this.getLhs().getLocation() }
538540

539541
/**
540542
* Gets the data flow node representing the incoming value assigned at this definition,
@@ -557,21 +559,10 @@ abstract class SsaImplicitDefinition extends SsaDefinition {
557559
abstract string getKind();
558560

559561
override string prettyPrintRef() {
560-
exists(int l, int c | this.hasLocationInfo(_, l, c, _, _) |
562+
exists(int l, int c | this.getLocation().hasLocationInfo(_, l, c, _, _) |
561563
result = this.getKind() + "@" + l + ":" + c
562564
)
563565
}
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-
}
575566
}
576567

577568
/**
@@ -617,16 +608,6 @@ class SsaVariableCapture extends SsaImplicitDefinition, TCapture {
617608
override string getKind() { result = "capture" }
618609

619610
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-
}
630611
}
631612

632613
/**
@@ -747,13 +728,7 @@ class SsaRefinementNode extends SsaPseudoDefinition, TRefinement {
747728
this.getSourceVariable() + " = refine[" + this.getGuard() + "](" + this.ppInputs() + ")"
748729
}
749730

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-
}
731+
override Location getLocation() { result = this.getGuard().getLocation() }
757732
}
758733

759734
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

0 commit comments

Comments
 (0)