Add jump-to-def support

This commit is contained in:
Joe Farebrother
2021-05-27 15:45:00 +01:00
parent 16005fa6cf
commit 45e6af2d14
2 changed files with 83 additions and 4 deletions

View File

@@ -9,13 +9,53 @@
import ql
import codeql_ql.ast.internal.Module
import codeql_ql.ast.internal.Type
import codeql.IDEContextual
external string selectedSourceFile();
from ModuleRef ref, FileOrModule target, string kind
where
newtype TLoc =
TAst(AstNode n) or
TFileOrModule(FileOrModule m)
class Loc extends TLoc {
string toString() { result = "" }
AstNode asAst() { this = TAst(result) }
FileOrModule asMod() { this = TFileOrModule(result) }
predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
exists(AstNode n | this = TAst(n) |
n.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
)
or
exists(FileOrModule m | this = TFileOrModule(m) |
m.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
)
}
}
predicate resolveModule(ModuleRef ref, FileOrModule target, string kind) {
target = ref.getResolvedModule() and
kind = "module" and
ref.getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile())
}
predicate resolveType(TypeExpr ref, AstNode target, string kind) {
target = ref.getResolvedType().getDeclaration() and
kind = "type" and
ref.getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile())
}
predicate resolve(Loc ref, Loc target, string kind) {
resolveModule(ref.asAst(), target.asMod(), kind)
or
resolveType(ref.asAst(), target.asAst(), kind)
}
from Loc ref, Loc target, string kind
where resolve(ref, target, kind)
select ref, target, kind

View File

@@ -13,9 +13,48 @@ import codeql.IDEContextual
external string selectedSourceFile();
from ModuleRef ref, FileOrModule target, string kind
where
newtype TLoc =
TAst(AstNode n) or
TFileOrModule(FileOrModule m)
class Loc extends TLoc {
string toString() { result = "" }
AstNode asAst() { this = TAst(result) }
FileOrModule asMod() { this = TFileOrModule(result) }
predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
exists(AstNode n | this = TAst(n) |
n.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
)
or
exists(FileOrModule m | this = TFileOrModule(m) |
m.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
)
}
}
predicate resolveModule(ModuleRef ref, FileOrModule target, string kind) {
target = ref.getResolvedModule() and
kind = "module" and
ref.getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile())
}
predicate resolveType(TypeExpr ref, AstNode target, string kind) {
target = ref.getResolvedType().getDeclaration() and
kind = "type" and
ref.getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile())
}
predicate resolve(Loc ref, Loc target, string kind) {
resolveModule(ref.asAst(), target.asMod(), kind)
or
resolveType(ref.asAst(), target.asAst(), kind)
}
from Loc ref, Loc target, string kind
where resolve(ref, target, kind)
select ref, target, kind