|
| 1 | +private import codeql.files.FileSystem |
| 2 | + |
| 3 | +module LocationImpl { |
| 4 | + newtype TLocation = |
| 5 | + TLocationDefault(@location_default location) or |
| 6 | + TLocationSynth(File file, int beginLine, int beginColumn, int endLine, int endColumn) { |
| 7 | + not locations_default(_, file, beginLine, beginColumn, endLine, endColumn) and none() |
| 8 | + } |
| 9 | + |
| 10 | + /** |
| 11 | + * A location as given by a file, a start line, a start column, |
| 12 | + * an end line, and an end column. |
| 13 | + * |
| 14 | + * For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). |
| 15 | + */ |
| 16 | + abstract class Location extends TLocation { |
| 17 | + /** Gets the file for this location. */ |
| 18 | + File getFile() { this.hasLocationInfo(result.getAbsolutePath(), _, _, _, _) } |
| 19 | + |
| 20 | + /** Gets the 1-based line number (inclusive) where this location starts. */ |
| 21 | + int getStartLine() { this.hasLocationInfo(_, result, _, _, _) } |
| 22 | + |
| 23 | + /** Gets the 1-based column number (inclusive) where this location starts. */ |
| 24 | + int getStartColumn() { this.hasLocationInfo(_, _, result, _, _) } |
| 25 | + |
| 26 | + /** Gets the 1-based line number (inclusive) where this location ends. */ |
| 27 | + int getEndLine() { this.hasLocationInfo(_, _, _, result, _) } |
| 28 | + |
| 29 | + /** Gets the 1-based column number (inclusive) where this location ends. */ |
| 30 | + int getEndColumn() { this.hasLocationInfo(_, _, _, _, result) } |
| 31 | + |
| 32 | + /** Gets the number of lines covered by this location. */ |
| 33 | + int getNumLines() { result = this.getEndLine() - this.getStartLine() + 1 } |
| 34 | + |
| 35 | + /** Gets a textual representation of this element. */ |
| 36 | + bindingset[this] |
| 37 | + pragma[inline_late] |
| 38 | + string toString() { |
| 39 | + exists(string filepath, int startline, int startcolumn, int endline, int endcolumn | |
| 40 | + this.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and |
| 41 | + result = filepath + "@" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Holds if this element is at the specified location. |
| 47 | + * The location spans column `startcolumn` of line `startline` to |
| 48 | + * column `endcolumn` of line `endline` in file `filepath`. |
| 49 | + * For more information, see |
| 50 | + * [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). |
| 51 | + */ |
| 52 | + abstract predicate hasLocationInfo( |
| 53 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 54 | + ); |
| 55 | + |
| 56 | + /** Holds if this location starts strictly before the specified location. */ |
| 57 | + pragma[inline] |
| 58 | + predicate strictlyBefore(Location other) { |
| 59 | + this.getStartLine() < other.getStartLine() |
| 60 | + or |
| 61 | + this.getStartLine() = other.getStartLine() and this.getStartColumn() < other.getStartColumn() |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private class LocationDefault extends Location, TLocationDefault { |
| 66 | + @location_default self; |
| 67 | + |
| 68 | + LocationDefault() { this = TLocationDefault(self) } |
| 69 | + |
| 70 | + override predicate hasLocationInfo( |
| 71 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 72 | + ) { |
| 73 | + exists(File f | |
| 74 | + locations_default(self, f, startline, startcolumn, endline, endcolumn) and |
| 75 | + filepath = f.getAbsolutePath() |
| 76 | + ) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** An entity representing an empty location. */ |
| 81 | + class EmptyLocation extends LocationDefault { |
| 82 | + EmptyLocation() { empty_location(self) } |
| 83 | + } |
| 84 | + |
| 85 | + private class LocationSynth extends Location, TLocationSynth { |
| 86 | + override predicate hasLocationInfo( |
| 87 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 88 | + ) { |
| 89 | + this = |
| 90 | + TLocationSynth(any(File f | f.getAbsolutePath() = filepath), startline, startcolumn, |
| 91 | + endline, endcolumn) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments