Skip to content

Commit

Permalink
Implement procedural macro diagnostics
Browse files Browse the repository at this point in the history
commit-id:f06e9653
  • Loading branch information
maciektr committed Mar 7, 2024
1 parent a72b77b commit 24ec6d5
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 52 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion plugins/cairo-lang-macro-attributes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ repository.workspace = true
proc-macro = true

[dependencies]
camino.workspace = true
quote.workspace = true
syn = { workspace = true, features = ["full", "extra-traits"] }
45 changes: 45 additions & 0 deletions plugins/cairo-lang-macro-stable/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/// This struct encapsulates a stable ABI representation of struct.
///
/// Please be aware that the memory management of values passing the FFI-barrier is tricky.
/// The memory must be freed on the same side of the barrier, where the allocation was made.
#[repr(C)]
#[derive(Debug)]
#[doc(hidden)]
pub struct StableSlice<T> {
ptr: *mut T,
len: usize,
}

impl<T> Copy for StableSlice<T> {}

impl<T> Clone for StableSlice<T> {
fn clone(&self) -> Self {
*self
}
}

impl<T> StableSlice<T> {
/// Create a new `StableSlice` from a Vector.
/// Note that the vector will not be deallocated automatically.
/// Please make sure to use `into_owned` afterward, to free the memory.
pub fn new(mut x: Vec<T>) -> Self {
x.shrink_to_fit();
assert_eq!(x.len(), x.capacity());
let ptr = x.as_mut_ptr();
let len = x.len();
std::mem::forget(x);
Self { ptr, len }
}

/// Convert to owned vector.
pub fn into_owned(self) -> Vec<T> {
unsafe { Vec::from_raw_parts(self.ptr, self.len, self.len) }
}

/// Returns raw pointer and length.
/// Can be used to construct a slice.
/// No ownership is transferred.
pub fn into_raw_parts(self) -> (*mut T, usize) {
(self.ptr, self.len)
}
}
38 changes: 36 additions & 2 deletions plugins/cairo-lang-macro-stable/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::ffi::StableSlice;
use std::ffi::{CStr, CString};
use std::os::raw::c_char;

pub mod ffi;

/// Token stream.
///
/// This struct implements FFI-safe stable ABI.
Expand All @@ -15,21 +18,52 @@ pub enum StableAuxData {
Some(*mut c_char),
}

/// Diagnostic returned by the procedural macro.
///
/// This struct implements FFI-safe stable ABI.
#[repr(C)]
#[derive(Debug)]
pub struct StableDiagnostic {
pub message: *mut c_char,
pub severity: StableSeverity,
}

/// The severity of a diagnostic.
///
/// This struct implements FFI-safe stable ABI.
#[repr(C)]
#[derive(Debug)]
pub enum StableSeverity {
Error,
Warning,
}

/// Procedural macro result.
///
/// This struct implements FFI-safe stable ABI.
#[repr(C)]
#[derive(Debug, Clone)]
pub enum StableProcMacroResult {
/// Plugin has not taken any action.
Leave,
Leave {
diagnostics: StableSlice<StableDiagnostic>,
// diagnostics: *mut StableDiagnostic,
// diagnostics_n: usize,
},
/// Plugin generated [`StableTokenStream`] replacement.
Replace {
diagnostics: StableSlice<StableDiagnostic>,
token_stream: StableTokenStream,
aux_data: StableAuxData,
// diagnostics: *mut StableDiagnostic,
// diagnostics_n: usize,
},
/// Plugin ordered item removal.
Remove,
Remove {
diagnostics: StableSlice<StableDiagnostic>,
// diagnostics: *mut StableDiagnostic,
// diagnostics_n: usize,
},
}

impl StableTokenStream {
Expand Down
Loading

0 comments on commit 24ec6d5

Please sign in to comment.