mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Merge pull request #20655 from github/redsun82/rust-file-semantics-predicates
Rust: introduce `ExtractedFile::hasSemantics` and `::isSkippedByCompilation`
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
---
|
||||
category: minorAnalysis
|
||||
---
|
||||
* Added `ExtractedFile::hasSemantics` and `ExtractedFile::isSkippedByCompilation` predicates.
|
||||
@@ -71,6 +71,25 @@ class File extends Container, Impl::File {
|
||||
*/
|
||||
class ExtractedFile extends File {
|
||||
ExtractedFile() { this.fromSource() }
|
||||
|
||||
private Diagnostic getNoSemanticsDiagnostic() {
|
||||
result.getTag() = "semantics" and result.getLocation().getFile() = this
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if we have semantical information about this file, which means
|
||||
* we should be able to
|
||||
* * expand any macros
|
||||
* * skip any blocks that are conditionally compiled out
|
||||
*/
|
||||
predicate hasSemantics() { not exists(this.getNoSemanticsDiagnostic()) }
|
||||
|
||||
/**
|
||||
* Holds if we know this file was skipped by conditional compilation.
|
||||
* This is not the same as `not this.hasSemantics()`, as a file
|
||||
* might not have semantics because of some error.
|
||||
*/
|
||||
predicate isSkippedByCompilation() { this.getNoSemanticsDiagnostic().getSeverityText() = "Info" }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
extractionWarning
|
||||
| bad_cargo/src/no_semantics.rs:1:1:1:1 | semantic analyzer unavailable (unable to load manifest) |
|
||||
@@ -1,6 +1,9 @@
|
||||
| Cargo.toml:0:0:0:0 | Cargo.toml | fromSource: no |
|
||||
| a_file.rs:0:0:0:0 | a_file.rs | fromSource: yes |
|
||||
| another_file.rs:0:0:0:0 | another_file.rs | fromSource: yes |
|
||||
| lib.rs:0:0:0:0 | lib.rs | fromSource: yes |
|
||||
| nested.rs:0:0:0:0 | nested.rs | fromSource: yes |
|
||||
| nested/file.rs:0:0:0:0 | nested/file.rs | fromSource: yes |
|
||||
| Cargo.toml:0:0:0:0 | Cargo.toml | fromSource: no | hasSemantics: no | isSkippedByCompilation: no |
|
||||
| a_file.rs:0:0:0:0 | a_file.rs | fromSource: yes | hasSemantics: yes | isSkippedByCompilation: no |
|
||||
| another_file.rs:0:0:0:0 | another_file.rs | fromSource: yes | hasSemantics: yes | isSkippedByCompilation: no |
|
||||
| bad_cargo/Cargo.toml:0:0:0:0 | bad_cargo/Cargo.toml | fromSource: no | hasSemantics: no | isSkippedByCompilation: no |
|
||||
| bad_cargo/src/no_semantics.rs:0:0:0:0 | bad_cargo/src/no_semantics.rs | fromSource: yes | hasSemantics: no | isSkippedByCompilation: no |
|
||||
| lib.rs:0:0:0:0 | lib.rs | fromSource: yes | hasSemantics: yes | isSkippedByCompilation: no |
|
||||
| nested.rs:0:0:0:0 | nested.rs | fromSource: yes | hasSemantics: yes | isSkippedByCompilation: no |
|
||||
| nested/file.rs:0:0:0:0 | nested/file.rs | fromSource: yes | hasSemantics: yes | isSkippedByCompilation: no |
|
||||
| nested/not_compiled.rs:0:0:0:0 | nested/not_compiled.rs | fromSource: yes | hasSemantics: no | isSkippedByCompilation: yes |
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
import rust
|
||||
|
||||
from File f, string fromSource
|
||||
from File f, string fromSource, string hasSemantics, string isSkippedByCompilation
|
||||
where
|
||||
exists(f.getRelativePath()) and
|
||||
if f.fromSource() then fromSource = "fromSource: yes" else fromSource = "fromSource: no"
|
||||
select f, fromSource
|
||||
(if f.fromSource() then fromSource = "fromSource: yes" else fromSource = "fromSource: no") and
|
||||
(
|
||||
if f.(ExtractedFile).hasSemantics()
|
||||
then hasSemantics = "hasSemantics: yes"
|
||||
else hasSemantics = "hasSemantics: no"
|
||||
) and
|
||||
if f.(ExtractedFile).isSkippedByCompilation()
|
||||
then isSkippedByCompilation = "isSkippedByCompilation: yes"
|
||||
else isSkippedByCompilation = "isSkippedByCompilation: no"
|
||||
select f, fromSource, hasSemantics, isSkippedByCompilation
|
||||
|
||||
1
rust/ql/test/extractor-tests/File/bad_cargo/.gitignore
vendored
Normal file
1
rust/ql/test/extractor-tests/File/bad_cargo/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
!/Cargo.toml
|
||||
1
rust/ql/test/extractor-tests/File/bad_cargo/Cargo.toml
Normal file
1
rust/ql/test/extractor-tests/File/bad_cargo/Cargo.toml
Normal file
@@ -0,0 +1 @@
|
||||
wrong
|
||||
Reference in New Issue
Block a user