Skip to content

Commit fc764db

Browse files
authored
Merge pull request github#4643 from nickrolfe/getFileBySourceArchiveName
Replace getEncodedFile with shared getFileBySourceArchiveName predicate
2 parents 8845546 + ac4a1f1 commit fc764db

26 files changed

+141
-55
lines changed

config/identical-files.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,5 +409,12 @@
409409
"java/ql/src/Metrics/Files/CommentedOutCodeReferences.qhelp",
410410
"javascript/ql/src/Comments/CommentedOutCodeReferences.qhelp",
411411
"python/ql/src/Lexical/CommentedOutCodeReferences.qhelp"
412+
],
413+
"IDE Contextual Queries": [
414+
"cpp/ql/src/IDEContextual.qll",
415+
"csharp/ql/src/IDEContextual.qll",
416+
"java/ql/src/IDEContextual.qll",
417+
"javascript/ql/src/IDEContextual.qll",
418+
"python/ql/src/analysis/IDEContextual.qll"
412419
]
413420
}

cpp/ql/src/IDEContextual.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Provides shared predicates related to contextual queries in the code viewer.
3+
*/
4+
5+
import semmle.files.FileSystem
6+
7+
/**
8+
* Returns the `File` matching the given source file name as encoded by the VS
9+
* Code extension.
10+
*/
11+
cached
12+
File getFileBySourceArchiveName(string name) {
13+
// The name provided for a file in the source archive by the VS Code extension
14+
// has some differences from the absolute path in the database:
15+
// 1. colons are replaced by underscores
16+
// 2. there's a leading slash, even for Windows paths: "C:/foo/bar" ->
17+
// "/C_/foo/bar"
18+
// 3. double slashes in UNC prefixes are replaced with a single slash
19+
// We can handle 2 and 3 together by unconditionally adding a leading slash
20+
// before replacing double slashes.
21+
name = ("/" + result.getAbsolutePath().replaceAll(":", "_")).replaceAll("//", "/")
22+
}

cpp/ql/src/definitions.qll

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import cpp
7+
import IDEContextual
78

89
/**
910
* Any element that might be the source or target of a jump-to-definition
@@ -207,11 +208,3 @@ Top definitionOf(Top e, string kind) {
207208
// later on.
208209
strictcount(result.getLocation()) < 10
209210
}
210-
211-
/**
212-
* Returns an appropriately encoded version of a filename `name`
213-
* passed by the VS Code extension in order to coincide with the
214-
* output of `.getFile()` on locatable entities.
215-
*/
216-
cached
217-
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }

cpp/ql/src/localDefinitions.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ import definitions
1212
external string selectedSourceFile();
1313

1414
from Top e, Top def, string kind
15-
where def = definitionOf(e, kind) and e.getFile() = getEncodedFile(selectedSourceFile())
15+
where def = definitionOf(e, kind) and e.getFile() = getFileBySourceArchiveName(selectedSourceFile())
1616
select e, def, kind

cpp/ql/src/localReferences.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ import definitions
1212
external string selectedSourceFile();
1313

1414
from Top e, Top def, string kind
15-
where def = definitionOf(e, kind) and def.getFile() = getEncodedFile(selectedSourceFile())
15+
where
16+
def = definitionOf(e, kind) and def.getFile() = getFileBySourceArchiveName(selectedSourceFile())
1617
select e, def, kind

cpp/ql/src/printAst.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ class Cfg extends PrintASTConfiguration {
2222
* Print All functions from the selected file.
2323
*/
2424
override predicate shouldPrintFunction(Function func) {
25-
func.getFile() = getEncodedFile(selectedSourceFile())
25+
func.getFile() = getFileBySourceArchiveName(selectedSourceFile())
2626
}
2727
}

csharp/ql/src/IDEContextual.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Provides shared predicates related to contextual queries in the code viewer.
3+
*/
4+
5+
import semmle.files.FileSystem
6+
7+
/**
8+
* Returns the `File` matching the given source file name as encoded by the VS
9+
* Code extension.
10+
*/
11+
cached
12+
File getFileBySourceArchiveName(string name) {
13+
// The name provided for a file in the source archive by the VS Code extension
14+
// has some differences from the absolute path in the database:
15+
// 1. colons are replaced by underscores
16+
// 2. there's a leading slash, even for Windows paths: "C:/foo/bar" ->
17+
// "/C_/foo/bar"
18+
// 3. double slashes in UNC prefixes are replaced with a single slash
19+
// We can handle 2 and 3 together by unconditionally adding a leading slash
20+
// before replacing double slashes.
21+
name = ("/" + result.getAbsolutePath().replaceAll(":", "_")).replaceAll("//", "/")
22+
}

csharp/ql/src/definitions.qll

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import csharp
7+
import IDEContextual
78

89
/** An element with an associated definition. */
910
abstract class Use extends @type_mention_parent {
@@ -188,11 +189,3 @@ Declaration definitionOf(Use use, string kind) {
188189
result.fromSource() and
189190
kind = use.getUseType()
190191
}
191-
192-
/**
193-
* Returns an appropriately encoded version of a filename `name`
194-
* passed by the VS Code extension in order to coincide with the
195-
* output of `.getFile()` on locatable entities.
196-
*/
197-
cached
198-
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }

csharp/ql/src/localDefinitions.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ from Use e, Declaration def, string kind, string filepath
1515
where
1616
def = definitionOf(e, kind) and
1717
e.hasLocationInfo(filepath, _, _, _, _) and
18-
filepath = getEncodedFile(selectedSourceFile()).getAbsolutePath()
18+
filepath = getFileBySourceArchiveName(selectedSourceFile()).getAbsolutePath()
1919
select e, def, kind

csharp/ql/src/localReferences.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ import definitions
1212
external string selectedSourceFile();
1313

1414
from Use e, Declaration def, string kind
15-
where def = definitionOf(e, kind) and def.getFile() = getEncodedFile(selectedSourceFile())
15+
where
16+
def = definitionOf(e, kind) and def.getFile() = getFileBySourceArchiveName(selectedSourceFile())
1617
select e, def, kind

0 commit comments

Comments
 (0)