diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..fec1e57 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,97 @@ +name: CI + +on: + push: + pull_request: + workflow_dispatch: + schedule: [cron: "40 1 * * *"] + +permissions: + contents: read + +env: + RUSTFLAGS: -Dwarnings + +jobs: + pre_ci: + uses: dtolnay/.github/.github/workflows/pre_ci.yml@master + + test: + name: Rust ${{matrix.rust}} + needs: pre_ci + if: needs.pre_ci.outputs.continue + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + rust: [nightly, beta, stable, 1.56.0] + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{matrix.rust}} + - name: Enable type layout randomization + run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout >> $GITHUB_ENV + if: matrix.rust == 'nightly' + - run: cargo test + + minimal: + name: Minimal versions + needs: pre_ci + if: needs.pre_ci.outputs.continue + runs-on: ubuntu-latest + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly + - run: cargo generate-lockfile -Z minimal-versions + - run: cargo check --locked + + doc: + name: Documentation + needs: pre_ci + if: needs.pre_ci.outputs.continue + runs-on: ubuntu-latest + timeout-minutes: 45 + env: + RUSTDOCFLAGS: -Dwarnings + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly + - uses: dtolnay/install@cargo-docs-rs + - run: cargo docs-rs + + clippy: + name: Clippy + runs-on: ubuntu-latest + if: github.event_name != 'pull_request' + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@clippy + - run: cargo clippy --tests -- -Dclippy::all -Dclippy::pedantic + + miri: + name: Miri + needs: pre_ci + if: needs.pre_ci.outputs.continue + runs-on: ubuntu-latest + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@miri + - run: cargo miri setup + - run: cargo miri test + env: + MIRIFLAGS: -Zmiri-strict-provenance + + outdated: + name: Outdated + runs-on: ubuntu-latest + if: github.event_name != 'pull_request' + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/install@cargo-outdated + - run: cargo outdated --workspace --exit-code 1 diff --git a/or-rs-macros/src/lib.rs b/or-rs-macros/src/lib.rs index 9c05bef..eb8c672 100644 --- a/or-rs-macros/src/lib.rs +++ b/or-rs-macros/src/lib.rs @@ -58,3 +58,56 @@ use proc_macro::TokenStream; pub fn or_gen(_attr: TokenStream, item: TokenStream) -> TokenStream { parser::MacroParser::parse(item) } + +#[proc_macro] +pub fn my_first_proc_macro(item: TokenStream) -> TokenStream { + item +} + +use proc_macro::TokenStream; +use quote::quote; +use syn::*; + +#[proc_macro_attribute] +pub fn add_print(_attr: TokenStream, item: TokenStream) -> TokenStream { + // 入力をItemFn(関数を表現する構文木データ型に変換) + let input: ItemFn = parse_macro_input!(item as ItemFn); + // 関数名を取得 + let name = &input.sig.ident; + // 関数のブロックを取得 + let block = &input.block; + + // quoteマクロでproc_macro2::TokenStreamを生成 + let expanded: proc_macro2::TokenStream = quote! { + fn #name() { + println!("Function {} is called", stringify!(#name)); + #block + } + }; + + // proc_macro2::TokenStreamからTokenStreamに変換 + TokenStream::from(expanded) +} + +use proc_macro::{TokenStream, TokenTree}; + +#[proc_macro_attribute] +pub fn log(attr: TokenStream, item: TokenStream) -> TokenStream { + let mut iter = item.into_iter(); + + if let Some(TokenTree::Ident(ident)) = iter.next() { + let func_name = ident.to_string(); + let rest_of_stream: TokenStream = iter.collect(); + + let new_stream = format!( + "fn {}() {{ println!(\"Function '{}' called\"); {} }}", + func_name, func_name, rest_of_stream + ); + new_stream.parse().unwrap() + } else { + // トークンストリームが関数宣言でない場合はエラー + "compile_error!(\"Expected function declaration\")" + .parse() + .unwrap() + } +} diff --git a/tests/tests/integration_test.rs b/tests/tests/integration_test.rs index 76ceeff..6155a32 100644 --- a/tests/tests/integration_test.rs +++ b/tests/tests/integration_test.rs @@ -1,6 +1,7 @@ #![feature(proc_macro_hygiene)] // for now, you have to add this unstable feature flag use or_rs::enums::*; +use or_rs_macros::my_first_proc_macro; use or_rs_macros::or_gen; fn main() { diff --git a/tests/tests/macro_test.rs b/tests/tests/macro_test.rs index b2c9cb8..a756ed1 100644 --- a/tests/tests/macro_test.rs +++ b/tests/tests/macro_test.rs @@ -1,6 +1,8 @@ #![feature(proc_macro_hygiene)] #![allow(unused_variables)] +use or_rs_macros::add_print; +use or_rs_macros::my_first_proc_macro; use or_rs_macros::or_gen; #[test] @@ -56,3 +58,11 @@ fn test_compile() { _ => "hello".to_string(), }; } + +// fn foo() { +// my_first_proc_macro!(42); +// } + +#[test] +#[add_print] +fn your_function() {}