11/** Provides classes for working with locations. */
22
33import files.FileSystem
4+ import codeql.actions.Ast
45
56bindingset [ loc]
67pragma [ inline_late]
@@ -11,30 +12,57 @@ private string locationToString(Location loc) {
1112 )
1213}
1314
15+ newtype TLocation =
16+ TBaseLocation ( string filepath , int startline , int startcolumn , int endline , int endcolumn ) {
17+ exists ( File file |
18+ file .getAbsolutePath ( ) = filepath and
19+ locations_default ( _, file , startline , startcolumn , endline , endcolumn )
20+ )
21+ or
22+ exists ( ExpressionNode e |
23+ e .hasLocationInfo ( filepath , startline , startcolumn , endline , endcolumn )
24+ )
25+ or
26+ filepath = "" and startline = 0 and startcolumn = 0 and endline = 0 and endcolumn = 0
27+ }
28+
1429/**
1530 * A location as given by a file, a start line, a start column,
1631 * an end line, and an end column.
1732 *
1833 * For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
1934 */
20- class Location extends @location_default {
35+ class Location extends TLocation , TBaseLocation {
36+ string filepath ;
37+ int startline ;
38+ int startcolumn ;
39+ int endline ;
40+ int endcolumn ;
41+
42+ Location ( ) { this = TBaseLocation ( filepath , startline , startcolumn , endline , endcolumn ) }
43+
2144 /** Gets the file for this location. */
22- File getFile ( ) { locations_default ( this , result , _, _, _, _) }
45+ File getFile ( ) {
46+ exists ( File file |
47+ file .getAbsolutePath ( ) = filepath and
48+ result = file
49+ )
50+ }
2351
2452 /** Gets the 1-based line number (inclusive) where this location starts. */
25- int getStartLine ( ) { locations_default ( this , _ , result , _ , _ , _ ) }
53+ int getStartLine ( ) { result = startline }
2654
2755 /** Gets the 1-based column number (inclusive) where this location starts. */
28- int getStartColumn ( ) { locations_default ( this , _ , _ , result , _ , _ ) }
56+ int getStartColumn ( ) { result = startcolumn }
2957
30- /** Gets the 1-based line number (inclusive) where this location ends. */
31- int getEndLine ( ) { locations_default ( this , _ , _ , _ , result , _ ) }
58+ /** Gets the 1-based line number (inclusive) where this.getLocationDefault() location ends. */
59+ int getEndLine ( ) { result = endline }
3260
33- /** Gets the 1-based column number (inclusive) where this location ends. */
34- int getEndColumn ( ) { locations_default ( this , _ , _ , _ , _ , result ) }
61+ /** Gets the 1-based column number (inclusive) where this.getLocationDefault() location ends. */
62+ int getEndColumn ( ) { result = endcolumn }
3563
3664 /** Gets the number of lines covered by this location. */
37- int getNumLines ( ) { result = this . getEndLine ( ) - this . getStartLine ( ) + 1 }
65+ int getNumLines ( ) { result = endline - startline + 1 }
3866
3967 /** Gets a textual representation of this element. */
4068 pragma [ inline]
@@ -47,13 +75,12 @@ class Location extends @location_default {
4775 * For more information, see
4876 * [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
4977 */
50- predicate hasLocationInfo (
51- string filepath , int startline , int startcolumn , int endline , int endcolumn
52- ) {
53- exists ( File f |
54- locations_default ( this , f , startline , startcolumn , endline , endcolumn ) and
55- filepath = f .getAbsolutePath ( )
56- )
78+ predicate hasLocationInfo ( string p , int sl , int sc , int el , int ec ) {
79+ p = filepath and
80+ sl = startline and
81+ sc = startcolumn and
82+ el = endline and
83+ ec = endcolumn
5784 }
5885
5986 /** Holds if this location starts strictly before the specified location. */
0 commit comments