2
2
3
3
import go
4
4
5
+ cached
6
+ private 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
+
5
17
/**
6
18
* A location as given by a file, a start line, a start column,
7
19
* an end line, and an end column.
8
20
*
9
21
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
10
22
*/
11
- class Location extends @location {
23
+ abstract private class LocationImpl extends TLocation {
12
24
/** Gets the file for this location. */
13
- File getFile ( ) { locations_default ( this , result , _ , _ , _ , _ ) }
25
+ abstract File getFile ( ) ;
14
26
15
27
/** Gets the 1-based line number (inclusive) where this location starts. */
16
- int getStartLine ( ) { locations_default ( this , _ , result , _ , _ , _ ) }
28
+ abstract int getStartLine ( ) ;
17
29
18
30
/** Gets the 1-based column number (inclusive) where this location starts. */
19
- int getStartColumn ( ) { locations_default ( this , _ , _ , result , _ , _ ) }
31
+ abstract int getStartColumn ( ) ;
20
32
21
33
/** Gets the 1-based line number (inclusive) where this location ends. */
22
- int getEndLine ( ) { locations_default ( this , _ , _ , _ , result , _ ) }
34
+ abstract int getEndLine ( ) ;
23
35
24
36
/** Gets the 1-based column number (inclusive) where this location ends. */
25
- int getEndColumn ( ) { locations_default ( this , _ , _ , _ , _ , result ) }
37
+ abstract int getEndColumn ( ) ;
26
38
27
39
/** Gets the number of lines covered by this location. */
28
40
int getNumLines ( ) { result = this .getEndLine ( ) - this .getStartLine ( ) + 1 }
@@ -42,23 +54,127 @@ class Location extends @location {
42
54
* For more information, see
43
55
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
44
56
*/
57
+ abstract predicate hasLocationInfo (
58
+ string filepath , int startline , int startcolumn , int endline , int endcolumn
59
+ ) ;
60
+ }
61
+
62
+ /** A subset of `Location`s that are part of the DB `@location` relation. */
63
+ class DbLocation extends TDbLocation {
64
+ private @location loc ;
65
+
66
+ DbLocation ( ) { this = TDbLocation ( loc ) }
67
+
68
+ /** Gets the file for this location. */
69
+ File getFile ( ) { locations_default ( loc , result , _, _, _, _) }
70
+
71
+ /** Gets the 1-based line number (inclusive) where this location starts. */
72
+ int getStartLine ( ) { locations_default ( loc , _, result , _, _, _) }
73
+
74
+ /** Gets the 1-based column number (inclusive) where this location starts. */
75
+ int getStartColumn ( ) { locations_default ( loc , _, _, result , _, _) }
76
+
77
+ /** Gets the 1-based line number (inclusive) where this location ends. */
78
+ int getEndLine ( ) { locations_default ( loc , _, _, _, result , _) }
79
+
80
+ /** Gets the 1-based column number (inclusive) where this location ends. */
81
+ int getEndColumn ( ) { locations_default ( loc , _, _, _, _, result ) }
82
+
83
+ /** Gets the number of lines covered by this location. */
84
+ int getNumLines ( ) { result = this .getEndLine ( ) - this .getStartLine ( ) + 1 }
85
+
86
+ /** Gets a textual representation of this element. */
87
+ string toString ( ) {
88
+ exists ( string filepath , int startline , int startcolumn , int endline , int endcolumn |
89
+ this .hasLocationInfo ( filepath , startline , startcolumn , endline , endcolumn ) and
90
+ result = filepath + "@" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
91
+ )
92
+ }
93
+
45
94
predicate hasLocationInfo (
46
95
string filepath , int startline , int startcolumn , int endline , int endcolumn
47
96
) {
48
97
exists ( File f |
49
- locations_default ( this , f , startline , startcolumn , endline , endcolumn ) and
98
+ locations_default ( loc , f , startline , startcolumn , endline , endcolumn ) and
50
99
filepath = f .getAbsolutePath ( )
51
100
)
52
101
}
53
102
}
54
103
104
+ private class DbLocationImpl extends LocationImpl instanceof DbLocation {
105
+ private @location loc ;
106
+
107
+ DbLocationImpl ( ) { this = TDbLocation ( loc ) }
108
+
109
+ override File getFile ( ) { result = DbLocation .super .getFile ( ) }
110
+
111
+ override int getStartLine ( ) { result = DbLocation .super .getStartLine ( ) }
112
+
113
+ override int getStartColumn ( ) { result = DbLocation .super .getStartColumn ( ) }
114
+
115
+ override int getEndLine ( ) { result = DbLocation .super .getEndLine ( ) }
116
+
117
+ override int getEndColumn ( ) { result = DbLocation .super .getEndColumn ( ) }
118
+
119
+ override int getNumLines ( ) { result = LocationImpl .super .getNumLines ( ) }
120
+
121
+ override string toString ( ) { result = LocationImpl .super .toString ( ) }
122
+
123
+ override predicate hasLocationInfo (
124
+ string filepath , int startline , int startcolumn , int endline , int endcolumn
125
+ ) {
126
+ DbLocation .super .hasLocationInfo ( filepath , startline , startcolumn , endline , endcolumn )
127
+ }
128
+ }
129
+
130
+ private class DataFlowLocation extends LocationImpl , TDataFlowLocation {
131
+ private string filepath_ ;
132
+ private int startline_ ;
133
+ private int startcolumn_ ;
134
+ private int endline_ ;
135
+ private int endcolumn_ ;
136
+
137
+ DataFlowLocation ( ) {
138
+ this = TDataFlowLocation ( filepath_ , startline_ , startcolumn_ , endline_ , endcolumn_ )
139
+ }
140
+
141
+ override File getFile ( ) { result .getAbsolutePath ( ) = filepath_ }
142
+
143
+ override int getStartLine ( ) { result = startline_ }
144
+
145
+ override int getStartColumn ( ) { result = startcolumn_ }
146
+
147
+ override int getEndLine ( ) { result = endline_ }
148
+
149
+ override int getEndColumn ( ) { result = endcolumn_ }
150
+
151
+ override predicate hasLocationInfo (
152
+ string filepath , int startline , int startcolumn , int endline , int endcolumn
153
+ ) {
154
+ filepath = filepath_ and
155
+ startline = startline_ and
156
+ startcolumn = startcolumn_ and
157
+ endline = endline_ and
158
+ endcolumn = endcolumn_
159
+ }
160
+ }
161
+
162
+ final class Location = LocationImpl ;
163
+
55
164
/** A program element with a location. */
56
165
class Locatable extends @locatable {
57
166
/** Gets the file this program element comes from. */
58
167
File getFile ( ) { result = this .getLocation ( ) .getFile ( ) }
59
168
60
169
/** Gets this element's location. */
61
- Location getLocation ( ) { has_location ( this , result ) }
170
+ final DbLocation getLocation ( ) {
171
+ exists ( @location loc |
172
+ has_location ( this , loc ) or
173
+ xmllocations ( this , loc )
174
+ |
175
+ result = TDbLocation ( loc )
176
+ )
177
+ }
62
178
63
179
/** Gets the number of lines covered by this element. */
64
180
int getNumLines ( ) { result = this .getLocation ( ) .getNumLines ( ) }
0 commit comments