Rust: extract declarations of builtin types

This commit is contained in:
Arthur Baars
2025-04-30 13:21:56 +02:00
parent 254789c89a
commit 53b2e9708c
4 changed files with 59 additions and 11 deletions

View File

@@ -36,6 +36,7 @@ pkg_filegroup(
srcs = [
":tools-arch",
"//rust/tools",
"//rust/tools/builtins",
],
prefix = "tools",
)

View File

@@ -17,6 +17,7 @@ use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use std::{env, fs};
use tracing::{error, info, warn};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
@@ -77,17 +78,19 @@ impl<'a> Extractor<'a> {
}
let no_location = (LineCol { line: 0, col: 0 }, LineCol { line: 0, col: 0 });
if let Err(reason) = semantics_info {
let message = format!("semantic analyzer unavailable ({reason})");
let full_message = format!(
"{message}: macro expansion, call graph, and type inference will be skipped."
);
translator.emit_diagnostic(
trap::DiagnosticSeverity::Warning,
"semantics".to_owned(),
message,
full_message,
no_location,
);
if !reason.is_empty() {
let message = format!("semantic analyzer unavailable ({reason})");
let full_message = format!(
"{message}: macro expansion, call graph, and type inference will be skipped."
);
translator.emit_diagnostic(
trap::DiagnosticSeverity::Warning,
"semantics".to_owned(),
message,
full_message,
no_location,
);
}
}
translator.emit_source_file(ast);
translator.trap.commit().unwrap_or_else(|err| {
@@ -276,5 +279,16 @@ fn main() -> anyhow::Result<()> {
}
}
}
let builtins_dir = env::var("CODEQL_EXTRACTOR_RUST_ROOT")
.map(|path| Path::new(&path).join("tools").join("builtins"))?;
let builtins = fs::read_dir(builtins_dir).context("failed to read builtins directory")?;
for entry in builtins {
let entry = entry.context("failed to read builtins directory")?;
let path = entry.path();
if path.extension().is_some_and(|ext| ext == "rs") {
extractor.extract_without_semantics(&path, "");
}
}
extractor.emit_extraction_diagnostics(start, &cfg)
}

View File

@@ -0,0 +1,8 @@
load("//misc/bazel:pkg.bzl", "codeql_pkg_files")
codeql_pkg_files(
name = "builtins",
srcs = glob(["*.rs"]),
prefix = "builtins",
visibility = ["//rust:__subpackages__"],
)

View File

@@ -0,0 +1,25 @@
// The Language Prelude: https://doc.rust-lang.org/reference/names/preludes.html#language-prelude
// Type namespace
// Boolean type
pub struct bool;
// Textual types
pub struct char;
pub struct str;
// Integer types
pub struct i8;
pub struct i16;
pub struct i32;
pub struct i64;
pub struct i128;
pub struct u8;
pub struct u16;
pub struct u32;
pub struct u64;
pub struct u128;
// Machine-dependent integer types
pub struct usize;
pub struct isize;
// floating-point types
pub struct f32;
pub struct f64;