Replace DbLocation with Location

This commit is contained in:
Owen Mansel-Chan
2025-02-26 15:00:56 +00:00
parent f0951823ad
commit ca0b363be3
5 changed files with 16 additions and 172 deletions

View File

@@ -1,7 +1,6 @@
/** Provides classes for working with errors and warnings recorded during extraction. */
import go
private import semmle.go.internal.Locations
/** Gets the SARIF severity level that indicates an error. */
private int getErrorSeverity() { result = 2 }
@@ -30,7 +29,9 @@ private class Diagnostic extends @diagnostic {
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
*/
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
getDiagnosticLocation(this).hasLocationInfo(path, sl, sc, el, ec)
exists(Location loc | diagnostics(this, _, _, _, _, loc) |
loc.hasLocationInfo(path, sl, sc, el, ec)
)
}
string toString() { result = this.getMessage() }

View File

@@ -1,7 +1,6 @@
/** Provides classes for working with locations and program elements that have locations. */
import go
private import internal.Locations
/**
* A location as given by a file, a start line, a start column,
@@ -11,21 +10,21 @@ private import internal.Locations
*
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
*/
class DbLocation extends TDbLocation {
class Location extends @location {
/** Gets the file for this location. */
File getFile() { dbLocationInfo(this, result, _, _, _, _) }
File getFile() { locations_default(this, result, _, _, _, _) }
/** Gets the 1-based line number (inclusive) where this location starts. */
int getStartLine() { dbLocationInfo(this, _, result, _, _, _) }
int getStartLine() { locations_default(this, _, result, _, _, _) }
/** Gets the 1-based column number (inclusive) where this location starts. */
int getStartColumn() { dbLocationInfo(this, _, _, result, _, _) }
int getStartColumn() { locations_default(this, _, _, result, _, _) }
/** Gets the 1-based line number (inclusive) where this location ends. */
int getEndLine() { dbLocationInfo(this, _, _, _, result, _) }
int getEndLine() { locations_default(this, _, _, _, result, _) }
/** Gets the 1-based column number (inclusive) where this location ends. */
int getEndColumn() { dbLocationInfo(this, _, _, _, _, result) }
int getEndColumn() { locations_default(this, _, _, _, _, result) }
/** Gets the number of lines covered by this location. */
int getNumLines() { result = this.getEndLine() - this.getStartLine() + 1 }
@@ -48,22 +47,22 @@ class DbLocation extends TDbLocation {
predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
exists(File f |
dbLocationInfo(this, f, startline, startcolumn, endline, endcolumn) and
exists(File f | locations_default(this, f, startline, startcolumn, endline, endcolumn) |
filepath = f.getAbsolutePath()
)
}
}
final class Location = LocationImpl;
/** A program element with a location. */
class Locatable extends @locatable {
/** Gets the file this program element comes from. */
File getFile() { result = this.getLocation().getFile() }
/** Gets this element's location. */
final DbLocation getLocation() { result = getLocatableLocation(this) }
final Location getLocation() {
has_location(this, result) or
xmllocations(this, result)
}
/** Gets the number of lines covered by this element. */
int getNumLines() { result = this.getLocation().getNumLines() }

View File

@@ -1,150 +0,0 @@
/** Provides classes for working with locations and program elements that have locations. */
import go
// Should _not_ be cached, as that would require the data flow stage to be evaluated
// in order to evaluate the AST stage. Ideally, we would cache each injector separately,
// but that's not possible. Instead, we cache all predicates that need the injectors
// to be tuple numbered.
newtype TLocation =
TDbLocation(@location loc) or
TSynthLocation(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
any(DataFlow::Node n).hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
// avoid overlap with existing DB locations
not existingDBLocation(filepath, startline, startcolumn, endline, endcolumn)
}
pragma[nomagic]
private predicate existingDBLocation(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
exists(File f |
locations_default(_, f, startline, startcolumn, endline, endcolumn) and
f.getAbsolutePath() = filepath
)
}
/**
* A location as given by a file, a start line, a start column,
* an end line, and an end column.
*
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
*/
abstract class LocationImpl extends TLocation {
/** Gets the file for this location. */
abstract File getFile();
/** Gets the 1-based line number (inclusive) where this location starts. */
abstract int getStartLine();
/** Gets the 1-based column number (inclusive) where this location starts. */
abstract int getStartColumn();
/** Gets the 1-based line number (inclusive) where this location ends. */
abstract int getEndLine();
/** Gets the 1-based column number (inclusive) where this location ends. */
abstract int getEndColumn();
/** Gets the number of lines covered by this location. */
int getNumLines() { result = this.getEndLine() - this.getStartLine() + 1 }
/** Gets a textual representation of this element. */
string toString() {
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
this.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
result = filepath + "@" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
)
}
/**
* Holds if this element is at the specified location.
* The location spans column `startcolumn` of line `startline` to
* column `endcolumn` of line `endline` in file `filepath`.
* For more information, see
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
*/
abstract predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
);
}
class DbLocationImpl extends LocationImpl instanceof DbLocation {
private @location loc;
DbLocationImpl() { this = TDbLocation(loc) }
override File getFile() { result = DbLocation.super.getFile() }
override int getStartLine() { result = DbLocation.super.getStartLine() }
override int getStartColumn() { result = DbLocation.super.getStartColumn() }
override int getEndLine() { result = DbLocation.super.getEndLine() }
override int getEndColumn() { result = DbLocation.super.getEndColumn() }
override predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
DbLocation.super.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}
}
class SynthLocationImpl extends LocationImpl, TSynthLocation {
override File getFile() { synthLocationInfo(this, result.getAbsolutePath(), _, _, _, _) }
override int getStartLine() { synthLocationInfo(this, _, result, _, _, _) }
override int getStartColumn() { synthLocationInfo(this, _, _, result, _, _) }
override int getEndLine() { synthLocationInfo(this, _, _, _, result, _) }
override int getEndColumn() { synthLocationInfo(this, _, _, _, _, result) }
override predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
synthLocationInfo(this, filepath, startline, startcolumn, endline, endcolumn)
}
}
cached
private module Cached {
cached
DbLocation getLocatableLocation(@locatable l) {
exists(@location loc |
has_location(l, loc) or
xmllocations(l, loc)
|
result = TDbLocation(loc)
)
}
cached
DbLocation getDiagnosticLocation(@diagnostic d) {
exists(@location loc |
diagnostics(d, _, _, _, _, loc) and
result = TDbLocation(loc)
)
}
cached
predicate dbLocationInfo(
DbLocation l, File f, int startline, int startcolumn, int endline, int endcolumn
) {
exists(@location loc |
l = TDbLocation(loc) and
locations_default(loc, f, startline, startcolumn, endline, endcolumn)
)
}
}
import Cached
cached
private predicate synthLocationInfo(
SynthLocationImpl l, string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
l = TSynthLocation(filepath, startline, startcolumn, endline, endcolumn)
}

View File

@@ -1,5 +1,4 @@
import go
private import semmle.go.internal.Locations
bindingset[path]
string baseName(string path) { result = path.regexpCapture(".*(/|\\\\)([^/\\\\]+)(/|\\\\)?$", 2) }
@@ -31,12 +30,7 @@ class Diagnostic extends @diagnostic {
diagnostic_for(this, c, fileNum, idx)
}
DbLocation getLocation() {
exists(@location loc |
diagnostics(this, _, _, _, _, loc) and
result = TDbLocation(loc)
)
}
Location getLocation() { diagnostics(this, _, _, _, _, result) }
// string getTag() {
// diagnostics(this, _, result, _, _, _)

View File

@@ -5,7 +5,7 @@ int countDecls(Entity e) { result = count(Ident decl | decl = e.getDeclaration()
query predicate entities(string fp, Entity e, int c, Type ty) {
c = countDecls(e) and
ty = e.getType() and
exists(DbLocation loc |
exists(Location loc |
loc = e.getDeclaration().getLocation() and
fp = loc.getFile().getBaseName() and
fp = "aliases.go"