|
| 1 | +/** Provides classes for working with locations. */ |
| 2 | + |
| 3 | +import files.FileSystem |
| 4 | +import codeql.actions.ast.internal.Ast |
| 5 | + |
| 6 | +bindingset[loc] |
| 7 | +pragma[inline_late] |
| 8 | +private string locationToString(Location loc) { |
| 9 | + exists(string filepath, int startline, int startcolumn, int endline, int endcolumn | |
| 10 | + loc.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and |
| 11 | + result = filepath + "@" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn |
| 12 | + ) |
| 13 | +} |
| 14 | + |
| 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(ExpressionImpl 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 | + |
| 29 | +/** |
| 30 | + * A location as given by a file, a start line, a start column, |
| 31 | + * an end line, and an end column. |
| 32 | + * |
| 33 | + * For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). |
| 34 | + */ |
| 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 | + |
| 44 | + /** Gets the file for this location. */ |
| 45 | + File getFile() { |
| 46 | + exists(File file | |
| 47 | + file.getAbsolutePath() = filepath and |
| 48 | + result = file |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + /** Gets the 1-based line number (inclusive) where this location starts. */ |
| 53 | + int getStartLine() { result = startline } |
| 54 | + |
| 55 | + /** Gets the 1-based column number (inclusive) where this location starts. */ |
| 56 | + int getStartColumn() { result = startcolumn } |
| 57 | + |
| 58 | + /** Gets the 1-based line number (inclusive) where this.getLocationDefault() location ends. */ |
| 59 | + int getEndLine() { result = endline } |
| 60 | + |
| 61 | + /** Gets the 1-based column number (inclusive) where this.getLocationDefault() location ends. */ |
| 62 | + int getEndColumn() { result = endcolumn } |
| 63 | + |
| 64 | + /** Gets the number of lines covered by this location. */ |
| 65 | + int getNumLines() { result = endline - startline + 1 } |
| 66 | + |
| 67 | + /** Gets a textual representation of this element. */ |
| 68 | + pragma[inline] |
| 69 | + string toString() { result = locationToString(this) } |
| 70 | + |
| 71 | + /** |
| 72 | + * Holds if this element is at the specified location. |
| 73 | + * The location spans column `startcolumn` of line `startline` to |
| 74 | + * column `endcolumn` of line `endline` in file `filepath`. |
| 75 | + * For more information, see |
| 76 | + * [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). |
| 77 | + */ |
| 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 |
| 84 | + } |
| 85 | + |
| 86 | + /** Holds if this location starts strictly before the specified location. */ |
| 87 | + pragma[inline] |
| 88 | + predicate strictlyBefore(Location other) { |
| 89 | + this.getStartLine() < other.getStartLine() |
| 90 | + or |
| 91 | + this.getStartLine() = other.getStartLine() and this.getStartColumn() < other.getStartColumn() |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** An entity representing an empty location. */ |
| 96 | +class EmptyLocation extends Location { |
| 97 | + EmptyLocation() { this.hasLocationInfo("", 0, 0, 0, 0) } |
| 98 | +} |
0 commit comments