11/** Provides classes for working with locations and program elements that have locations. */
22
33import 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. */
9196class 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.
0 commit comments