Skip to content

Commit

Permalink
Merge pull request #18883 from owen-mc/go/get-location
Browse files Browse the repository at this point in the history
Go: Use `getLocation` instead of `hasLocationInfo`
  • Loading branch information
owen-mc authored Feb 27, 2025
2 parents f7d95e4 + a94854b commit 76ad107
Show file tree
Hide file tree
Showing 105 changed files with 430 additions and 823 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: deprecated
---
* The member predicate `hasLocationInfo` has been deprecated on the following classes: `BasicBlock`, `Callable`, `Content`, `ContentSet`, `ControlFlow::Node`, `DataFlowCallable`, `DataFlow::Node`, `Entity`, `GVN`, `HtmlTemplate::TemplateStmt`, `IR:WriteTarget`, `SourceSinkInterpretationInput::SourceOrSinkElement`, `SourceSinkInterpretationInput::InterpretNode`, `SsaVariable`, `SsaDefinition`, `SsaWithFields`, `StringOps::ConcatenationElement`, `Type`, and `VariableWithFields`. Use `getLocation()` instead.
17 changes: 4 additions & 13 deletions go/ql/lib/semmle/go/DiagnosticsReporting.qll
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -20,18 +19,10 @@ private class Diagnostic extends @diagnostic {
string getMessage() { diagnostics(this, _, _, result, _, _) }

/** Gets the file that this error is associated with, if any. */
File getFile() { this.hasLocationInfo(result.getAbsolutePath(), _, _, _, _) }
File getFile() { result = this.getLocation().getFile() }

/**
* 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/).
*/
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
getDiagnosticLocation(this).hasLocationInfo(path, sl, sc, el, ec)
}
/** Gets the location for this error. */
Location getLocation() { diagnostics(this, _, _, _, _, result) }

string toString() { result = this.getMessage() }
}
Expand Down Expand Up @@ -68,7 +59,7 @@ predicate reportableDiagnostics(Diagnostic d, string msg, int sev) {
exists(File f | f = d.getFile() |
exists(f.getAChild()) and
msg =
"Extraction failed in " + d.getFile().getRelativePath() + " with error " +
"Extraction failed in " + f.getRelativePath() + " with error " +
removeAbsolutePaths(d.getMessage())
)
or
Expand Down
23 changes: 11 additions & 12 deletions go/ql/lib/semmle/go/Locations.qll
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 }
Expand All @@ -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() }
Expand Down
48 changes: 26 additions & 22 deletions go/ql/lib/semmle/go/Scopes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -144,36 +144,34 @@ class Entity extends @object {
/** Gets a textual representation of this entity. */
string toString() { result = this.getName() }

private predicate hasRealLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
// take the location of the declaration if there is one
this.getDeclaration().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) or
any(CaseClause cc | this = cc.getImplicitlyDeclaredVariable())
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
/** Gets the location of this entity. */
Location getLocation() {
result = this.getDeclaration().getLocation()
or
result = any(CaseClause cc | this = cc.getImplicitlyDeclaredVariable()).getLocation()
}

/**
* DEPRECATED: Use `getLocation()` instead.
*
* 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/).
*/
predicate hasLocationInfo(
deprecated predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
// take the location of the declaration if there is one
if this.hasRealLocationInfo(_, _, _, _, _)
then this.hasRealLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
else (
// otherwise fall back on dummy location
filepath = "" and
startline = 0 and
startcolumn = 0 and
endline = 0 and
endcolumn = 0
)
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
or
// otherwise fall back on dummy location
not exists(this.getLocation()) and
filepath = "" and
startline = 0 and
startcolumn = 0 and
endline = 0 and
endcolumn = 0
}
}

Expand Down Expand Up @@ -680,16 +678,22 @@ class Callable extends TCallable {
result = this.asFuncLit().getName()
}

/** Gets the location of this callable. */
Location getLocation() {
result = this.asFunction().getLocation() or result = this.asFuncLit().getLocation()
}

/**
* DEPRECATED: Use `getLocation()` instead.
*
* Holds if this element is at the specified location.
* The location spans column `sc` of line `sl` to
* column `ec` of line `el` in file `fp`.
* For more information, see
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
*/
predicate hasLocationInfo(string fp, int sl, int sc, int el, int ec) {
this.asFunction().hasLocationInfo(fp, sl, sc, el, ec) or
this.asFuncLit().hasLocationInfo(fp, sl, sc, el, ec)
deprecated predicate hasLocationInfo(string fp, int sl, int sc, int el, int ec) {
this.getLocation().hasLocationInfo(fp, sl, sc, el, ec)
}
}

Expand Down
11 changes: 8 additions & 3 deletions go/ql/lib/semmle/go/StringOps.qll
Original file line number Diff line number Diff line change
Expand Up @@ -548,20 +548,25 @@ module StringOps {
else result = "concatenation element"
}

/** Gets the location of this element. */
Location getLocation() { result = this.asNode().getLocation() }

/**
* DEPRECATED: Use `getLocation()` instead.
*
* 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/).
*/
predicate hasLocationInfo(
deprecated predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
this.asNode().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
or
// use dummy location for elements that don't have a corresponding node
not exists(this.asNode()) and
not exists(this.getLocation()) and
filepath = "" and
startline = 0 and
startcolumn = 0 and
Expand Down
11 changes: 8 additions & 3 deletions go/ql/lib/semmle/go/Types.qll
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,24 @@ class Type extends @type {
*/
string toString() { result = this.getName() }

/** Gets the location of this type. */
Location getLocation() { result = this.getEntity().getLocation() }

/**
* DEPRECATED: Use `getLocation()` instead.
*
* 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/).
*/
predicate hasLocationInfo(
deprecated predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
this.getEntity().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
or
not exists(this.getEntity()) and
not exists(this.getLocation()) and
filepath = "" and
startline = 0 and
startcolumn = 0 and
Expand Down
9 changes: 7 additions & 2 deletions go/ql/lib/semmle/go/VariableWithFields.qll
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,21 @@ class VariableWithFields extends TVariableWithFields {
*/
string getElement() { this = TVariableElementStep(_, result) }

/** Gets the location of this variable with fields. */
Location getLocation() { result = this.getBaseVariable().getLocation() }

/**
* DEPRECATED: Use `getLocation()` instead.
*
* 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/).
*/
predicate hasLocationInfo(
deprecated predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
this.getBaseVariable().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}
}
9 changes: 7 additions & 2 deletions go/ql/lib/semmle/go/controlflow/BasicBlocks.qll
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,22 @@ class BasicBlock extends TControlFlowNode {
/** Gets a textual representation of this basic block. */
string toString() { result = "basic block" }

/** Gets the source location for this element. */
Location getLocation() { result = this.getFirstNode().getLocation() }

/**
* DEPRECATED: Use `getLocation()` instead.
*
* Holds if this basic block 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/).
*/
predicate hasLocationInfo(
deprecated predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
this.getFirstNode().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}
}

Expand Down
18 changes: 11 additions & 7 deletions go/ql/lib/semmle/go/controlflow/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,31 @@ module ControlFlow {
Root getRoot() { none() }

/** Gets the file to which this node belongs. */
File getFile() { this.hasLocationInfo(result.getAbsolutePath(), _, _, _, _) }
File getFile() { result = this.getLocation().getFile() }

/**
* Gets a textual representation of this control flow node.
*/
string toString() { result = "control-flow node" }

/** Gets the source location for this element. */
Location getLocation() { none() }

/**
* DEPRECATED: Use `getLocation()` instead.
*
* 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/).
*/
predicate hasLocationInfo(
deprecated predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
or
not exists(this.getLocation()) and
filepath = "" and
startline = 0 and
startcolumn = 0 and
Expand Down Expand Up @@ -244,11 +252,7 @@ module ControlFlow {

override string toString() { result = cond + " is " + outcome }

override predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
cond.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}
override Location getLocation() { result = cond.getLocation() }
}

/**
Expand Down
18 changes: 3 additions & 15 deletions go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,7 @@ class SkipNode extends ControlFlow::Node, MkSkipNode {

override string toString() { result = "skip" }

override predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
skip.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}
override Location getLocation() { result = skip.getLocation() }
}

/**
Expand All @@ -437,11 +433,7 @@ class EntryNode extends ControlFlow::Node, MkEntryNode {

override string toString() { result = "entry" }

override predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
root.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}
override Location getLocation() { result = root.getLocation() }
}

/**
Expand All @@ -456,11 +448,7 @@ class ExitNode extends ControlFlow::Node, MkExitNode {

override string toString() { result = "exit" }

override predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
root.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}
override Location getLocation() { result = root.getLocation() }
}

/**
Expand Down
Loading

0 comments on commit 76ad107

Please sign in to comment.