|
| 1 | +/** Provides classes for working with locations and program elements that have locations. */ |
| 2 | + |
| 3 | +import go |
| 4 | + |
| 5 | +cached |
| 6 | +newtype TLocation = |
| 7 | + TDbLocation(@location loc) or |
| 8 | + TDataFlowLocation(string filepath, int startline, int startcolumn, int endline, int endcolumn) { |
| 9 | + any(DataFlow::Node n).hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and |
| 10 | + // avoid overlap with existing DB locations |
| 11 | + not exists(File f | |
| 12 | + locations_default(_, f, startline, startcolumn, endline, endcolumn) and |
| 13 | + f.getAbsolutePath() = filepath |
| 14 | + ) |
| 15 | + } |
| 16 | + |
| 17 | +/** |
| 18 | + * A location as given by a file, a start line, a start column, |
| 19 | + * an end line, and an end column. |
| 20 | + * |
| 21 | + * For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). |
| 22 | + */ |
| 23 | +abstract class LocationImpl extends TLocation { |
| 24 | + /** Gets the file for this location. */ |
| 25 | + abstract File getFile(); |
| 26 | + |
| 27 | + /** Gets the 1-based line number (inclusive) where this location starts. */ |
| 28 | + abstract int getStartLine(); |
| 29 | + |
| 30 | + /** Gets the 1-based column number (inclusive) where this location starts. */ |
| 31 | + abstract int getStartColumn(); |
| 32 | + |
| 33 | + /** Gets the 1-based line number (inclusive) where this location ends. */ |
| 34 | + abstract int getEndLine(); |
| 35 | + |
| 36 | + /** Gets the 1-based column number (inclusive) where this location ends. */ |
| 37 | + abstract int getEndColumn(); |
| 38 | + |
| 39 | + /** Gets the number of lines covered by this location. */ |
| 40 | + int getNumLines() { result = this.getEndLine() - this.getStartLine() + 1 } |
| 41 | + |
| 42 | + /** Gets a textual representation of this element. */ |
| 43 | + string toString() { |
| 44 | + exists(string filepath, int startline, int startcolumn, int endline, int endcolumn | |
| 45 | + this.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and |
| 46 | + result = filepath + "@" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Holds if this element is at the specified location. |
| 52 | + * The location spans column `startcolumn` of line `startline` to |
| 53 | + * column `endcolumn` of line `endline` in file `filepath`. |
| 54 | + * For more information, see |
| 55 | + * [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). |
| 56 | + */ |
| 57 | + abstract predicate hasLocationInfo( |
| 58 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +class DbLocationImpl extends LocationImpl instanceof DbLocation { |
| 63 | + private @location loc; |
| 64 | + |
| 65 | + DbLocationImpl() { this = TDbLocation(loc) } |
| 66 | + |
| 67 | + override File getFile() { result = DbLocation.super.getFile() } |
| 68 | + |
| 69 | + override int getStartLine() { result = DbLocation.super.getStartLine() } |
| 70 | + |
| 71 | + override int getStartColumn() { result = DbLocation.super.getStartColumn() } |
| 72 | + |
| 73 | + override int getEndLine() { result = DbLocation.super.getEndLine() } |
| 74 | + |
| 75 | + override int getEndColumn() { result = DbLocation.super.getEndColumn() } |
| 76 | + |
| 77 | + override predicate hasLocationInfo( |
| 78 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 79 | + ) { |
| 80 | + DbLocation.super.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +class DataFlowLocationImpl extends LocationImpl, TDataFlowLocation { |
| 85 | + private string filepath_; |
| 86 | + private int startline_; |
| 87 | + private int startcolumn_; |
| 88 | + private int endline_; |
| 89 | + private int endcolumn_; |
| 90 | + |
| 91 | + DataFlowLocationImpl() { |
| 92 | + this = TDataFlowLocation(filepath_, startline_, startcolumn_, endline_, endcolumn_) |
| 93 | + } |
| 94 | + |
| 95 | + override File getFile() { result.getAbsolutePath() = filepath_ } |
| 96 | + |
| 97 | + override int getStartLine() { result = startline_ } |
| 98 | + |
| 99 | + override int getStartColumn() { result = startcolumn_ } |
| 100 | + |
| 101 | + override int getEndLine() { result = endline_ } |
| 102 | + |
| 103 | + override int getEndColumn() { result = endcolumn_ } |
| 104 | + |
| 105 | + override predicate hasLocationInfo( |
| 106 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 107 | + ) { |
| 108 | + filepath = filepath_ and |
| 109 | + startline = startline_ and |
| 110 | + startcolumn = startcolumn_ and |
| 111 | + endline = endline_ and |
| 112 | + endcolumn = endcolumn_ |
| 113 | + } |
| 114 | +} |
0 commit comments