Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit c7e03cb

Browse files
authored
Merge pull request #398 from github/nickrolfe/getFileBySourceArchiveName
Replace getEncodedFile with getFileBySourceArchiveName predicate
2 parents 235b7c0 + 17b6401 commit c7e03cb

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

ql/src/ideContextual.qll

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
import go
77

88
/**
9-
* Gets the `File` with encoded name `name`.
9+
* Returns the `File` matching the given source file name as encoded by the VS
10+
* Code extension.
1011
*/
1112
cached
12-
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }
13+
File getFileBySourceArchiveName(string name) {
14+
// The name provided for a file in the source archive by the VS Code extension
15+
// has some differences from the absolute path in the database:
16+
// 1. colons are replaced by underscores
17+
// 2. there's a leading slash, even for Windows paths: "C:/foo/bar" ->
18+
// "/C_/foo/bar"
19+
// 3. double slashes in UNC prefixes are replaced with a single slash
20+
// We can handle 2 and 3 together by unconditionally adding a leading slash
21+
// before replacing double slashes.
22+
name = ("/" + result.getAbsolutePath().replaceAll(":", "_")).replaceAll("//", "/")
23+
}

ql/src/localDefinitions.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ import ideContextual
1313
external string selectedSourceFile();
1414

1515
from Ident def, Ident use, Entity e
16-
where use.uses(e) and def.declares(e) and use.getFile() = getEncodedFile(selectedSourceFile())
16+
where
17+
use.uses(e) and
18+
def.declares(e) and
19+
use.getFile() = getFileBySourceArchiveName(selectedSourceFile())
1720
select use, def, "V"

ql/src/localReferences.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ import ideContextual
1313
external string selectedSourceFile();
1414

1515
from Ident def, Ident use, Entity e
16-
where use.uses(e) and def.declares(e) and def.getFile() = getEncodedFile(selectedSourceFile())
16+
where
17+
use.uses(e) and
18+
def.declares(e) and
19+
def.getFile() = getFileBySourceArchiveName(selectedSourceFile())
1720
select use, def, "V"

ql/src/printAst.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ external string selectedSourceFile();
2222
class Cfg extends PrintAstConfiguration {
2323
override predicate shouldPrintFunction(FuncDecl func) { shouldPrintFile(func.getFile()) }
2424

25-
override predicate shouldPrintFile(File file) { file = getEncodedFile(selectedSourceFile()) }
25+
override predicate shouldPrintFile(File file) {
26+
file = getFileBySourceArchiveName(selectedSourceFile())
27+
}
2628

2729
override predicate shouldPrintComments(File file) { none() }
2830
}

0 commit comments

Comments
 (0)