Skip to content

Commit a9ae583

Browse files
committed
Rust: introduce File::hasSemantics and File::isSkippedByCompilation
1 parent e120e5c commit a9ae583

File tree

8 files changed

+45
-9
lines changed

8 files changed

+45
-9
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added `ExtractedFile::hasSemantics` and `ExtractedFile::isSkippedByCompilation` predicates.

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ class File extends Container, Impl::File {
7171
*/
7272
class ExtractedFile extends File {
7373
ExtractedFile() { this.fromSource() }
74+
75+
private Diagnostic getNoSemanticsDiagnostic() {
76+
result.getTag() = "semantics" and result.getLocation().getFile() = this
77+
}
78+
79+
/**
80+
* Holds if we have semantical information about this file, which means
81+
* we should be able to
82+
* * expand any macros
83+
* * skip any blocks that are conditionally compiled out
84+
*/
85+
predicate hasSemantics() { not exists(Diagnostic d | d = this.getNoSemanticsDiagnostic()) }
86+
87+
/**
88+
* Holds if we know this file was skipped by conditional compilation.
89+
* This is not the same as `not this.hasSemantics()`, as a file
90+
* might not have semantics because of some error.
91+
*/
92+
predicate isSkippedByCompilation() { this.getNoSemanticsDiagnostic().getSeverityText() = "Info" }
7493
}
7594

7695
/**
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
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 |
5-
| nested.rs:0:0:0:0 | nested.rs | fromSource: yes |
6-
| nested/file.rs:0:0:0:0 | nested/file.rs | fromSource: yes |
1+
| Cargo.toml:0:0:0:0 | Cargo.toml | fromSource: no | hasSemantics: no | isSkippedByCompilation: no |
2+
| a_file.rs:0:0:0:0 | a_file.rs | fromSource: yes | hasSemantics: yes | isSkippedByCompilation: no |
3+
| another_file.rs:0:0:0:0 | another_file.rs | fromSource: yes | hasSemantics: yes | isSkippedByCompilation: no |
4+
| bad_cargo/Cargo.toml:0:0:0:0 | bad_cargo/Cargo.toml | fromSource: no | hasSemantics: no | isSkippedByCompilation: no |
5+
| bad_cargo/src/no_semantics.rs:0:0:0:0 | bad_cargo/src/no_semantics.rs | fromSource: yes | hasSemantics: no | isSkippedByCompilation: no |
6+
| lib.rs:0:0:0:0 | lib.rs | fromSource: yes | hasSemantics: yes | isSkippedByCompilation: no |
7+
| nested.rs:0:0:0:0 | nested.rs | fromSource: yes | hasSemantics: yes | isSkippedByCompilation: no |
8+
| nested/file.rs:0:0:0:0 | nested/file.rs | fromSource: yes | hasSemantics: yes | isSkippedByCompilation: no |
9+
| nested/not_compiled.rs:0:0:0:0 | nested/not_compiled.rs | fromSource: yes | hasSemantics: no | isSkippedByCompilation: yes |
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import rust
22

3-
from File f, string fromSource
3+
from File f, string fromSource, string hasSemantics, string isSkippedByCompilation
44
where
55
exists(f.getRelativePath()) and
6-
if f.fromSource() then fromSource = "fromSource: yes" else fromSource = "fromSource: no"
7-
select f, fromSource
6+
(if f.fromSource() then fromSource = "fromSource: yes" else fromSource = "fromSource: no") and
7+
(
8+
if f.(ExtractedFile).hasSemantics()
9+
then hasSemantics = "hasSemantics: yes"
10+
else hasSemantics = "hasSemantics: no"
11+
) and
12+
if f.(ExtractedFile).isSkippedByCompilation()
13+
then isSkippedByCompilation = "isSkippedByCompilation: yes"
14+
else isSkippedByCompilation = "isSkippedByCompilation: no"
15+
select f, fromSource, hasSemantics, isSkippedByCompilation
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!/Cargo.toml
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
wrong

‎rust/ql/test/extractor-tests/File/bad_cargo/src/no_semantics.rs‎

Whitespace-only changes.

‎rust/ql/test/extractor-tests/File/nested/not_compiled.rs‎

Whitespace-only changes.

0 commit comments

Comments
 (0)