Skip to content

Commit 50c917d

Browse files
author
Paolo Tranquilli
committed
Rust: restrict extracted files queries
1 parent 4e71155 commit 50c917d

File tree

10 files changed

+37
-19
lines changed

10 files changed

+37
-19
lines changed

rust/ql/lib/codeql/files/FileSystem.qll

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ private import codeql.rust.elements.SourceFile
66
private import codeql.rust.elements.AstNode
77
private import codeql.rust.elements.Comment
88
private import codeql.rust.Diagnostics
9+
private import codeql.rust.internal.ExtractorStep
910

1011
private module Input implements InputSig {
1112
abstract class ContainerBase extends @container {
@@ -36,7 +37,9 @@ class Folder = Impl::Folder;
3637
/** A file. */
3738
class File extends Container, Impl::File {
3839
/** Holds if this file was extracted from ordinary source code. */
39-
predicate fromSource() { any() }
40+
predicate fromSource() {
41+
exists(ExtractorStep s | s.getAction() = "Extract" and s.getFile() = this)
42+
}
4043

4144
/**
4245
* Gets the number of lines containing code in this file. This value
@@ -58,11 +61,20 @@ class File extends Container, Impl::File {
5861
}
5962
}
6063

64+
/**
65+
* A source file that was extracted.
66+
*
67+
* TODO: rename `SourceFile` from the generated AST to give that name to this class.
68+
*/
69+
class ExtractedFile extends File {
70+
ExtractedFile() { this.fromSource() }
71+
}
72+
6173
/**
6274
* A successfully extracted file, that is, a file that was extracted and
6375
* contains no extraction errors or warnings.
6476
*/
65-
class SuccessfullyExtractedFile extends File {
77+
class SuccessfullyExtractedFile extends ExtractedFile {
6678
SuccessfullyExtractedFile() {
6779
not exists(Diagnostic d |
6880
d.getLocation().getFile() = this and

rust/ql/lib/codeql/rust/internal/ExtractorStep.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import codeql.files.FileSystem
77
*/
88
class ExtractorStep extends @extractor_step {
99
/**
10-
* The string representation of this extractor step.
10+
* Gets the string representation of this extractor step.
1111
*/
1212
string toString() {
1313
exists(File file, string action |
@@ -17,17 +17,17 @@ class ExtractorStep extends @extractor_step {
1717
}
1818

1919
/**
20-
* The action this extractor step carried out.
20+
* Gets the action this extractor step carried out.
2121
*/
2222
string getAction() { extractor_steps(this, result, _, _) }
2323

2424
/**
25-
* The file the extractor step was carried out on.
25+
* Gets the file the extractor step was carried out on.
2626
*/
2727
File getFile() { extractor_steps(this, _, result, _) }
2828

2929
/**
30-
* The duration of the extractor step in milliseconds.
30+
* Gets the duration of the extractor step in milliseconds.
3131
*/
3232
int getDurationMs() { extractor_steps(this, _, _, result) }
3333

rust/ql/src/queries/diagnostics/ExtractedFiles.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88

99
import rust
1010

11-
from File f
11+
from ExtractedFile f
1212
where exists(f.getRelativePath())
1313
select f, "File successfully extracted."

rust/ql/src/queries/diagnostics/ExtractionErrors.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import codeql.files.FileSystem
1111
/** Gets the SARIF severity to associate with an error. */
1212
int getSeverity() { result = 2 }
1313

14-
from ExtractionError error, File f
14+
from ExtractionError error, ExtractedFile f
1515
where
1616
f = error.getLocation().getFile() and
1717
exists(f.getRelativePath())

rust/ql/src/queries/summary/SummaryStats.ql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ where
2121
or
2222
key = "Extraction warnings" and value = count(ExtractionWarning w)
2323
or
24-
key = "Files extracted - total" and value = count(File f | exists(f.getRelativePath()))
24+
key = "Files extracted - total" and value = count(ExtractedFile f | exists(f.getRelativePath()))
2525
or
2626
key = "Files extracted - with errors" and
27-
value = count(File f | exists(f.getRelativePath()) and not f instanceof SuccessfullyExtractedFile)
27+
value =
28+
count(ExtractedFile f |
29+
exists(f.getRelativePath()) and not f instanceof SuccessfullyExtractedFile
30+
)
2831
or
2932
key = "Files extracted - without errors" and
3033
value = count(SuccessfullyExtractedFile f | exists(f.getRelativePath()))
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
| a_file.rs:0:0:0:0 | a_file.rs |
2-
| another_file.rs:0:0:0:0 | another_file.rs |
3-
| lib.rs:0:0:0:0 | lib.rs |
1+
| Cargo.toml:0:0:0:0 | Cargo.toml | fromSource: no |
2+
| a_file.rs:0:0:0:0 | a_file.rs | fromSource: yes |
3+
| another_file.rs:0:0:0:0 | another_file.rs | fromSource: yes |
4+
| lib.rs:0:0:0:0 | lib.rs | fromSource: yes |
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import rust
22

3-
from File f
4-
where exists(f.getRelativePath())
5-
select f
3+
from File f, string fromSource
4+
where
5+
exists(f.getRelativePath()) and
6+
if f.fromSource() then fromSource = "fromSource: yes" else fromSource = "fromSource: no"
7+
select f, fromSource

rust/ql/test/query-tests/diagnostics/ExtractedFiles.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55
| main.rs:0:0:0:0 | main.rs | File successfully extracted. |
66
| my_macro.rs:0:0:0:0 | my_macro.rs | File successfully extracted. |
77
| my_struct.rs:0:0:0:0 | my_struct.rs | File successfully extracted. |
8-
| options.yml:0:0:0:0 | options.yml | File successfully extracted. |

rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
| lib.rs:0:0:0:0 | lib.rs | 5 |
66
| does_not_compile.rs:0:0:0:0 | does_not_compile.rs | 3 |
77
| error.rs:0:0:0:0 | error.rs | 3 |
8+
| Cargo.toml:0:0:0:0 | Cargo.toml | 0 |
89
| options.yml:0:0:0:0 | options.yml | 0 |

rust/ql/test/query-tests/diagnostics/SummaryStats.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
| Elements unextracted | 0 |
33
| Extraction errors | 0 |
44
| Extraction warnings | 7 |
5-
| Files extracted - total | 8 |
5+
| Files extracted - total | 7 |
66
| Files extracted - with errors | 3 |
7-
| Files extracted - without errors | 5 |
7+
| Files extracted - without errors | 4 |
88
| Inconsistencies - AST | 0 |
99
| Inconsistencies - CFG | 0 |
1010
| Inconsistencies - data flow | 0 |

0 commit comments

Comments
 (0)