diff --git a/ql/src/ide-contextual-queries/localDefinitions.ql b/ql/src/ide-contextual-queries/localDefinitions.ql index c9c6b279bea..99049df8305 100644 --- a/ql/src/ide-contextual-queries/localDefinitions.ql +++ b/ql/src/ide-contextual-queries/localDefinitions.ql @@ -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 diff --git a/ql/src/ide-contextual-queries/localReferences.ql b/ql/src/ide-contextual-queries/localReferences.ql index 572a7d4e7a6..ecaed17ef7d 100644 --- a/ql/src/ide-contextual-queries/localReferences.ql +++ b/ql/src/ide-contextual-queries/localReferences.ql @@ -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