Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support gz compressed AST input #852

Merged
merged 2 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ categories = ["development-tools::ffi"]
proc-macro = true

[features]
experimental = ["clang-ast", "memmap", "serde", "serde_json"]
experimental = ["clang-ast", "flate2", "memmap", "serde", "serde_json"]

[dependencies]
proc-macro2 = "1.0"
Expand All @@ -24,6 +24,7 @@ syn = { version = "1.0.70", features = ["full"] }

# optional dependencies
clang-ast = { version = "0.1", optional = true }
flate2 = { version = "1.0", optional = true }
memmap = { version = "0.7", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }
serde_json = { version = "1.0", optional = true }
Expand Down
31 changes: 23 additions & 8 deletions macro/src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use crate::syntax::attrs::OtherAttrs;
use crate::syntax::namespace::Namespace;
use crate::syntax::report::Errors;
use crate::syntax::{Api, Discriminant, Doc, Enum, EnumRepr, ForeignName, Pair, Variant};
use flate2::write::GzDecoder;
use memmap::Mmap;
use proc_macro2::{Delimiter, Group, Ident, TokenStream};
use quote::{format_ident, quote, quote_spanned};
use serde::Deserialize;
use std::env;
use std::fmt::{self, Display};
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::str::FromStr;
use syn::{parse_quote, Path};
Expand Down Expand Up @@ -91,16 +93,29 @@ pub fn load(cx: &mut Errors, apis: &mut [Api]) {
}
};

let ast_dump_bytes =
match File::open(&ast_dump_path).and_then(|file| unsafe { Mmap::map(&file) }) {
Ok(memmap) => memmap,
Err(error) => {
let msg = format!("failed to read {}: {}", ast_dump_path.display(), error);
return cx.error(span, msg);
let memmap = File::open(&ast_dump_path).and_then(|file| unsafe { Mmap::map(&file) });
let mut gunzipped;
let ast_dump_bytes = match match memmap {
Ok(ref memmap) => {
let is_gzipped = memmap.get(..2) == Some(b"\x1f\x8b");
if is_gzipped {
gunzipped = Vec::new();
let decode_result = GzDecoder::new(&mut gunzipped).write_all(&memmap);
decode_result.map(|_| gunzipped.as_slice())
} else {
Ok(&memmap as &[u8])
}
};
}
Err(error) => Err(error),
} {
Ok(bytes) => bytes,
Err(error) => {
let msg = format!("failed to read {}: {}", ast_dump_path.display(), error);
return cx.error(span, msg);
}
};

let ref root: Node = match serde_json::from_slice(&ast_dump_bytes) {
let ref root: Node = match serde_json::from_slice(ast_dump_bytes) {
Ok(root) => root,
Err(error) => {
let msg = format!("failed to read {}: {}", ast_dump_path.display(), error);
Expand Down
50 changes: 50 additions & 0 deletions third-party/Cargo.lock

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