Sha256: 160a6d6701cabf2514e23570df1bd1b648c909cc27b7c583f21d98fe0c16722e

Contents?: true

Size: 1.47 KB

Versions: 31

Compression:

Stored size: 1.47 KB

Contents

use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{parse2, File};

use crate::BindgenOptions;

mod merge_extern_blocks;
mod sort_semantically;

use merge_extern_blocks::merge_extern_blocks;
use sort_semantically::sort_semantically;

struct PostProcessingPass {
    should_run: fn(&BindgenOptions) -> bool,
    run: fn(&mut File),
}

// TODO: This can be a const fn when mutable references are allowed in const
// context.
macro_rules! pass {
    ($pass:ident) => {
        PostProcessingPass {
            should_run: |options| options.$pass,
            run: |file| $pass(file),
        }
    };
}

const PASSES: &[PostProcessingPass] =
    &[pass!(merge_extern_blocks), pass!(sort_semantically)];

pub(crate) fn postprocessing(
    items: Vec<TokenStream>,
    options: &BindgenOptions,
) -> TokenStream {
    let items = items.into_iter().collect();
    let require_syn = PASSES.iter().any(|pass| (pass.should_run)(options));

    if !require_syn {
        return items;
    }

    // This syn business is a hack, for now. This means that we are re-parsing already
    // generated code using `syn` (as opposed to `quote`) because `syn` provides us more
    // control over the elements.
    // The `unwrap` here is deliberate because bindgen should generate valid rust items at all
    // times.
    let mut file = parse2::<File>(items).unwrap();

    for pass in PASSES {
        if (pass.should_run)(options) {
            (pass.run)(&mut file);
        }
    }

    file.into_token_stream()
}

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/bindgen-0.69.5/codegen/postprocessing/mod.rs
wasmtime-29.0.0 ./ext/cargo-vendor/bindgen-0.69.5/codegen/postprocessing/mod.rs
wasmtime-28.0.0 ./ext/cargo-vendor/bindgen-0.69.5/codegen/postprocessing/mod.rs
wasmtime-27.0.0 ./ext/cargo-vendor/bindgen-0.69.5/codegen/postprocessing/mod.rs
wasmtime-26.0.0 ./ext/cargo-vendor/bindgen-0.69.5/codegen/postprocessing/mod.rs
wasmtime-25.0.2 ./ext/cargo-vendor/bindgen-0.69.4/codegen/postprocessing/mod.rs
wasmtime-25.0.1 ./ext/cargo-vendor/bindgen-0.69.4/codegen/postprocessing/mod.rs
wasmtime-25.0.0 ./ext/cargo-vendor/bindgen-0.69.4/codegen/postprocessing/mod.rs
wasmtime-24.0.0 ./ext/cargo-vendor/bindgen-0.69.4/codegen/postprocessing/mod.rs
wasmtime-23.0.2 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs
wasmtime-22.0.0 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs
wasmtime-21.0.1 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs
wasmtime-20.0.2 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs
wasmtime-20.0.0 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs
wasmtime-18.0.3 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs
wasmtime-17.0.1 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs
wasmtime-17.0.0 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs
wasmtime-16.0.0 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs
wasmtime-15.0.1 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs
wasmtime-15.0.0 ./ext/cargo-vendor/bindgen-0.69.1/codegen/postprocessing/mod.rs