Merge branch 'main' into redsun82/rust-expand-attr-macros

This commit is contained in:
Paolo Tranquilli
2025-05-13 16:21:29 +02:00
257 changed files with 38826 additions and 1723 deletions

View File

@@ -1,2 +1,2 @@
mod.rs 4bcb9def847469aae9d8649461546b7c21ec97cf6e63d3cf394e339915ce65d7 4bcb9def847469aae9d8649461546b7c21ec97cf6e63d3cf394e339915ce65d7
top.rs 7fa95af0d85ffc251cfcd543129baa8cb0dde9df310194f3aff1868dd66417f4 7fa95af0d85ffc251cfcd543129baa8cb0dde9df310194f3aff1868dd66417f4
top.rs 7d0c7b324631207a5f0f89649634431b6fe6f27610a31be118f0dc90c17314f7 7d0c7b324631207a5f0f89649634431b6fe6f27610a31be118f0dc90c17314f7

View File

@@ -1515,60 +1515,6 @@ impl From<trap::Label<MacroItems>> for trap::Label<Element> {
}
}
#[derive(Debug)]
pub struct MacroStmts {
pub id: trap::TrapId<MacroStmts>,
pub expr: Option<trap::Label<Expr>>,
pub statements: Vec<trap::Label<Stmt>>,
}
impl trap::TrapEntry for MacroStmts {
fn extract_id(&mut self) -> trap::TrapId<Self> {
std::mem::replace(&mut self.id, trap::TrapId::Star)
}
fn emit(self, id: trap::Label<Self>, out: &mut trap::Writer) {
out.add_tuple("macro_stmts", vec![id.into()]);
if let Some(v) = self.expr {
out.add_tuple("macro_stmts_exprs", vec![id.into(), v.into()]);
}
for (i, v) in self.statements.into_iter().enumerate() {
out.add_tuple("macro_stmts_statements", vec![id.into(), i.into(), v.into()]);
}
}
}
impl trap::TrapClass for MacroStmts {
fn class_name() -> &'static str { "MacroStmts" }
}
impl From<trap::Label<MacroStmts>> for trap::Label<AstNode> {
fn from(value: trap::Label<MacroStmts>) -> Self {
// SAFETY: this is safe because in the dbscheme MacroStmts is a subclass of AstNode
unsafe {
Self::from_untyped(value.as_untyped())
}
}
}
impl From<trap::Label<MacroStmts>> for trap::Label<Locatable> {
fn from(value: trap::Label<MacroStmts>) -> Self {
// SAFETY: this is safe because in the dbscheme MacroStmts is a subclass of Locatable
unsafe {
Self::from_untyped(value.as_untyped())
}
}
}
impl From<trap::Label<MacroStmts>> for trap::Label<Element> {
fn from(value: trap::Label<MacroStmts>) -> Self {
// SAFETY: this is safe because in the dbscheme MacroStmts is a subclass of Element
unsafe {
Self::from_untyped(value.as_untyped())
}
}
}
#[derive(Debug)]
pub struct MatchArm {
pub id: trap::TrapId<MatchArm>,
@@ -6367,6 +6313,69 @@ impl From<trap::Label<LiteralPat>> for trap::Label<Element> {
}
}
#[derive(Debug)]
pub struct MacroBlockExpr {
pub id: trap::TrapId<MacroBlockExpr>,
pub tail_expr: Option<trap::Label<Expr>>,
pub statements: Vec<trap::Label<Stmt>>,
}
impl trap::TrapEntry for MacroBlockExpr {
fn extract_id(&mut self) -> trap::TrapId<Self> {
std::mem::replace(&mut self.id, trap::TrapId::Star)
}
fn emit(self, id: trap::Label<Self>, out: &mut trap::Writer) {
out.add_tuple("macro_block_exprs", vec![id.into()]);
if let Some(v) = self.tail_expr {
out.add_tuple("macro_block_expr_tail_exprs", vec![id.into(), v.into()]);
}
for (i, v) in self.statements.into_iter().enumerate() {
out.add_tuple("macro_block_expr_statements", vec![id.into(), i.into(), v.into()]);
}
}
}
impl trap::TrapClass for MacroBlockExpr {
fn class_name() -> &'static str { "MacroBlockExpr" }
}
impl From<trap::Label<MacroBlockExpr>> for trap::Label<Expr> {
fn from(value: trap::Label<MacroBlockExpr>) -> Self {
// SAFETY: this is safe because in the dbscheme MacroBlockExpr is a subclass of Expr
unsafe {
Self::from_untyped(value.as_untyped())
}
}
}
impl From<trap::Label<MacroBlockExpr>> for trap::Label<AstNode> {
fn from(value: trap::Label<MacroBlockExpr>) -> Self {
// SAFETY: this is safe because in the dbscheme MacroBlockExpr is a subclass of AstNode
unsafe {
Self::from_untyped(value.as_untyped())
}
}
}
impl From<trap::Label<MacroBlockExpr>> for trap::Label<Locatable> {
fn from(value: trap::Label<MacroBlockExpr>) -> Self {
// SAFETY: this is safe because in the dbscheme MacroBlockExpr is a subclass of Locatable
unsafe {
Self::from_untyped(value.as_untyped())
}
}
}
impl From<trap::Label<MacroBlockExpr>> for trap::Label<Element> {
fn from(value: trap::Label<MacroBlockExpr>) -> Self {
// SAFETY: this is safe because in the dbscheme MacroBlockExpr is a subclass of Element
unsafe {
Self::from_untyped(value.as_untyped())
}
}
}
#[derive(Debug)]
pub struct MacroExpr {
pub id: trap::TrapId<MacroExpr>,

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

@@ -1644,19 +1644,19 @@ impl Translator<'_> {
pub(crate) fn emit_macro_stmts(
&mut self,
node: &ast::MacroStmts,
) -> Option<Label<generated::MacroStmts>> {
let expr = node.expr().and_then(|x| self.emit_expr(&x));
) -> Option<Label<generated::MacroBlockExpr>> {
let tail_expr = node.expr().and_then(|x| self.emit_expr(&x));
let statements = node
.statements()
.filter_map(|x| self.emit_stmt(&x))
.collect();
let label = self.trap.emit(generated::MacroStmts {
let label = self.trap.emit(generated::MacroBlockExpr {
id: TrapId::Star,
expr,
tail_expr,
statements,
});
self.emit_location(label, node);
emit_detached!(MacroStmts, self, node, label);
emit_detached!(MacroBlockExpr, self, node, label);
self.emit_tokens(node, label.into(), node.syntax().children_with_tokens());
Some(label)
}