Skip to content

Commit 4f7cce9

Browse files
author
Alvaro Muñoz
authored
Merge pull request #27 from GitHubSecurityLab/refactor_astnode
Add Expression nodes and locations
2 parents b3cecfc + 96246f4 commit 4f7cce9

32 files changed

+1247
-749
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
ql/lib/.codeql/
44
ql/src/.codeql/
55
ql/test/.codeql/
6+
db/

ql/lib/codeql/Locations.qll

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** Provides classes for working with locations. */
22

33
import files.FileSystem
4+
import codeql.actions.Ast
45

56
bindingset[loc]
67
pragma[inline_late]
@@ -11,30 +12,57 @@ private string locationToString(Location loc) {
1112
)
1213
}
1314

15+
newtype TLocation =
16+
TBaseLocation(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
17+
exists(File file |
18+
file.getAbsolutePath() = filepath and
19+
locations_default(_, file, startline, startcolumn, endline, endcolumn)
20+
)
21+
or
22+
exists(ExpressionNode e |
23+
e.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
24+
)
25+
or
26+
filepath = "" and startline = 0 and startcolumn = 0 and endline = 0 and endcolumn = 0
27+
}
28+
1429
/**
1530
* A location as given by a file, a start line, a start column,
1631
* an end line, and an end column.
1732
*
1833
* For more information about locations see [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
1934
*/
20-
class Location extends @location_default {
35+
class Location extends TLocation, TBaseLocation {
36+
string filepath;
37+
int startline;
38+
int startcolumn;
39+
int endline;
40+
int endcolumn;
41+
42+
Location() { this = TBaseLocation(filepath, startline, startcolumn, endline, endcolumn) }
43+
2144
/** Gets the file for this location. */
22-
File getFile() { locations_default(this, result, _, _, _, _) }
45+
File getFile() {
46+
exists(File file |
47+
file.getAbsolutePath() = filepath and
48+
result = file
49+
)
50+
}
2351

2452
/** Gets the 1-based line number (inclusive) where this location starts. */
25-
int getStartLine() { locations_default(this, _, result, _, _, _) }
53+
int getStartLine() { result = startline }
2654

2755
/** Gets the 1-based column number (inclusive) where this location starts. */
28-
int getStartColumn() { locations_default(this, _, _, result, _, _) }
56+
int getStartColumn() { result = startcolumn }
2957

30-
/** Gets the 1-based line number (inclusive) where this location ends. */
31-
int getEndLine() { locations_default(this, _, _, _, result, _) }
58+
/** Gets the 1-based line number (inclusive) where this.getLocationDefault() location ends. */
59+
int getEndLine() { result = endline }
3260

33-
/** Gets the 1-based column number (inclusive) where this location ends. */
34-
int getEndColumn() { locations_default(this, _, _, _, _, result) }
61+
/** Gets the 1-based column number (inclusive) where this.getLocationDefault() location ends. */
62+
int getEndColumn() { result = endcolumn }
3563

3664
/** Gets the number of lines covered by this location. */
37-
int getNumLines() { result = this.getEndLine() - this.getStartLine() + 1 }
65+
int getNumLines() { result = endline - startline + 1 }
3866

3967
/** Gets a textual representation of this element. */
4068
pragma[inline]
@@ -47,13 +75,12 @@ class Location extends @location_default {
4775
* For more information, see
4876
* [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
4977
*/
50-
predicate hasLocationInfo(
51-
string filepath, int startline, int startcolumn, int endline, int endcolumn
52-
) {
53-
exists(File f |
54-
locations_default(this, f, startline, startcolumn, endline, endcolumn) and
55-
filepath = f.getAbsolutePath()
56-
)
78+
predicate hasLocationInfo(string p, int sl, int sc, int el, int ec) {
79+
p = filepath and
80+
sl = startline and
81+
sc = startcolumn and
82+
el = endline and
83+
ec = endcolumn
5784
}
5885

5986
/** Holds if this location starts strictly before the specified location. */

0 commit comments

Comments
 (0)