Skip to content

Commit bd19661

Browse files
committed
Address comments
1 parent a7628e7 commit bd19661

File tree

6 files changed

+47
-55
lines changed

6 files changed

+47
-55
lines changed

rust/ql/lib/codeql/Locations.qll

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
import files.FileSystem
44
private import codeql.rust.elements.internal.LocationImpl
55

6-
/**
7-
* A location as given by a file, a start line, a start column,
8-
* an end line, and an end column.
9-
*
10-
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
11-
*/
126
final class Location = LocationImpl::Location;
137

14-
/** An entity representing an empty location. */
158
final class EmptyLocation = LocationImpl::EmptyLocation;

rust/ql/lib/codeql/rust/elements/internal/FormatArgumentImpl.qll

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ private import codeql.rust.elements.internal.generated.FormatArgument
88
private import codeql.rust.elements.internal.generated.Raw
99
private import codeql.rust.elements.internal.generated.Synth
1010
private import codeql.rust.elements.internal.FormatArgumentConstructor
11+
private import codeql.rust.elements.internal.LocatableImpl::Impl as LocatableImpl
12+
private import codeql.files.FileSystem
1113

1214
/**
1315
* INTERNAL: This module contains the customizable definition of `FormatArgument` and should not
@@ -30,27 +32,29 @@ module Impl {
3032
int index;
3133
int kind;
3234
string name;
33-
private int offset;
35+
int offset;
3436

3537
FormatArgument() { this = Synth::TFormatArgument(parent, index, kind, name, _, offset) }
3638

3739
override string toString() { result = name }
3840

39-
override predicate hasLocationInfo(
40-
string filepath, int startline, int startcolumn, int endline, int endcolumn
41+
override Format getParent() { result = Synth::TFormat(parent, index, _, _) }
42+
}
43+
44+
private class FormatSynthLocationImpl extends FormatArgument, LocatableImpl::SynthLocatable {
45+
override predicate hasSynthLocationInfo(
46+
File file, int startline, int startcolumn, int endline, int endcolumn
4147
) {
4248
// TODO: handle locations in multi-line comments
4349
// TODO: handle the case where the template is from a nested macro call
4450
Synth::convertFormatArgsExprFromRaw(parent)
4551
.(FormatArgsExpr)
4652
.getTemplate()
4753
.getLocation()
48-
.hasLocationInfo(filepath, startline, startcolumn - offset, _, _) and
54+
.hasLocationInfo(file.getAbsolutePath(), startline, startcolumn - offset, _, _) and
4955
endline = startline and
5056
endcolumn = startcolumn + name.length() - 1
5157
}
52-
53-
override Format getParent() { result = Synth::TFormat(parent, index, _, _) }
5458
}
5559

5660
/**

rust/ql/lib/codeql/rust/elements/internal/FormatImpl.qll

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ private import codeql.rust.elements.internal.generated.Format
88
private import codeql.rust.elements.internal.generated.Raw
99
private import codeql.rust.elements.internal.generated.Synth
1010
private import codeql.rust.elements.internal.FormatConstructor
11+
private import codeql.rust.elements.internal.LocatableImpl::Impl as LocatableImpl
12+
private import codeql.files.FileSystem
1113
import codeql.rust.elements.FormatArgument
1214

1315
/**
@@ -24,25 +26,14 @@ module Impl {
2426
*/
2527
class Format extends Generated::Format {
2628
private Raw::FormatArgsExpr parent;
27-
private string text;
29+
string text;
2830
private int index;
29-
private int offset;
31+
int offset;
3032

3133
Format() { this = Synth::TFormat(parent, index, text, offset) }
3234

3335
override string toString() { result = text }
3436

35-
override predicate hasLocationInfo(
36-
string filepath, int startline, int startcolumn, int endline, int endcolumn
37-
) {
38-
this.getParent()
39-
.getTemplate()
40-
.getLocation()
41-
.hasLocationInfo(filepath, startline, startcolumn - offset, _, _) and
42-
endline = startline and
43-
endcolumn = startcolumn + text.length() - 1
44-
}
45-
4637
override FormatArgsExpr getParent() { result = Synth::convertFormatArgsExprFromRaw(parent) }
4738

4839
override int getIndex() { result = index }
@@ -83,4 +74,17 @@ module Impl {
8374
result.getParent() = this and result = Synth::TFormatArgument(_, _, 2, _, _, _)
8475
}
8576
}
77+
78+
private class FormatSynthLocationImpl extends Format, LocatableImpl::SynthLocatable {
79+
override predicate hasSynthLocationInfo(
80+
File file, int startline, int startcolumn, int endline, int endcolumn
81+
) {
82+
this.getParent()
83+
.getTemplate()
84+
.getLocation()
85+
.hasLocationInfo(file.getAbsolutePath(), startline, startcolumn - offset, _, _) and
86+
endline = startline and
87+
endcolumn = startcolumn + text.length() - 1
88+
}
89+
}
8690
}

rust/ql/lib/codeql/rust/elements/internal/FormatTemplateVariableAccessImpl.qll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ private import codeql.rust.elements.internal.generated.Raw
1010
private import codeql.rust.elements.internal.generated.Synth
1111
private import codeql.rust.elements.Format
1212
private import codeql.rust.elements.NamedFormatArgument
13+
private import codeql.Locations
1314

1415
/**
1516
* INTERNAL: This module contains the customizable definition of `FormatTemplateVariableAccess` and should not
@@ -26,11 +27,7 @@ module Impl {
2627
)
2728
}
2829

29-
override predicate hasLocationInfo(
30-
string filepath, int startline, int startcolumn, int endline, int endcolumn
31-
) {
32-
argument.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
33-
}
30+
override Location getLocation() { result = argument.getLocation() }
3431

3532
override string toString() { result = this.getName() }
3633

rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ private import codeql.rust.elements.internal.generated.Raw
1515
* be referenced directly.
1616
*/
1717
module Impl {
18-
class Locatable extends Generated::Locatable {
19-
pragma[nomagic]
20-
final Location getLocation() {
21-
exists(@location_default location |
22-
result = LocationImpl::TLocationDefault(location) and
23-
locatable_locations(Synth::convertLocatableToRaw(this), location)
24-
)
25-
or
18+
abstract class SynthLocatable extends Locatable {
19+
abstract predicate hasSynthLocationInfo(
20+
File file, int startline, int startcolumn, int endline, int endcolumn
21+
);
22+
23+
final override Location getLocation() {
2624
not locatable_locations(Synth::convertLocatableToRaw(this), _) and
2725
exists(File file, int beginLine, int beginColumn, int endLine, int endColumn |
28-
this.hasLocationInfo(file.getAbsolutePath(), beginLine, beginColumn, endLine, endColumn)
26+
this.hasSynthLocationInfo(file, beginLine, beginColumn, endLine, endColumn)
2927
|
3028
result = LocationImpl::TLocationSynth(file, beginLine, beginColumn, endLine, endColumn)
3129
or
@@ -35,18 +33,15 @@ module Impl {
3533
)
3634
)
3735
}
36+
}
3837

39-
/**
40-
* Holds if this element is at the specified location.
41-
* The location spans column `startcolumn` of line `startline` to
42-
* column `endcolumn` of line `endline` in file `filepath`.
43-
* For more information, see
44-
* [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
45-
*/
46-
predicate hasLocationInfo(
47-
string filepath, int startline, int startcolumn, int endline, int endcolumn
48-
) {
49-
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
38+
class Locatable extends Generated::Locatable {
39+
pragma[nomagic]
40+
Location getLocation() {
41+
exists(@location_default location |
42+
result = LocationImpl::TLocationDefault(location) and
43+
locatable_locations(Synth::convertLocatableToRaw(this), location)
44+
)
5045
}
5146

5247
/**

rust/ql/lib/codeql/rust/elements/internal/LocationImpl.qll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
private import codeql.files.FileSystem
2+
private import codeql.rust.elements.internal.LocatableImpl::Impl as LocatableImpl
23
private import codeql.rust.elements.Locatable
34
private import codeql.rust.elements.Format
45
private import codeql.rust.elements.FormatArgument
@@ -8,10 +9,8 @@ module LocationImpl {
89
TLocationDefault(@location_default location) or
910
TLocationSynth(File file, int beginLine, int beginColumn, int endLine, int endColumn) {
1011
not locations_default(_, file, beginLine, beginColumn, endLine, endColumn) and
11-
exists(string filePath | file.getAbsolutePath() = filePath |
12-
any(Format f).hasLocationInfo(filePath, beginLine, beginColumn, endLine, endColumn) or
13-
any(FormatArgument f).hasLocationInfo(filePath, beginLine, beginColumn, endLine, endColumn)
14-
)
12+
any(LocatableImpl::SynthLocatable l)
13+
.hasSynthLocationInfo(file, beginLine, beginColumn, endLine, endColumn)
1514
}
1615

1716
/**

0 commit comments

Comments
 (0)