blob: 3549ab343cd863783aa73190c719eca908442d39 [file] [log] [blame]
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
use anyhow::Result;
use ffi_types::{FfiU8Slice, FfiU8SliceBox};
use ir::{self, make_ir_from_parts, Func, Identifier, Item, Record, IR};
use itertools::Itertools;
/// Generates `IR` from a header containing `header_source`.
pub fn ir_from_cc(header_source: &str) -> Result<IR> {
ir_from_cc_dependency(header_source, "// empty header")
}
/// Name of the current target used by `ir_from_cc` and `ir_from_cc_dependency`.
pub const TESTING_TARGET: &str = "//test:testing_target";
/// Create a testing `IR` instance from given items, using mock values for other
/// fields.
pub fn make_ir_from_items(items: impl IntoIterator<Item = Item>) -> Result<IR> {
make_ir_from_parts(
items.into_iter().collect_vec(),
/* used_headers= */ vec![],
/* current_target= */ TESTING_TARGET.into(),
/* top_level_item_ids= */ vec![],
)
}
/// Target of the dependency used by `ir_from_cc_dependency`.
/// Needs to be kept in sync with `kDependencyTarget` in `json_from_cc.cc`.
pub const DEPENDENCY_TARGET: &str = "//test:dependency";
/// Generates `IR` from a header that depends on another header.
///
/// `header_source` of the header will be updated to contain the `#include` line
/// for the header with `dependency_header_source`. The name of the dependency
/// target is exposed as `DEPENDENCY_TARGET`.
pub fn ir_from_cc_dependency(header_source: &str, dependency_header_source: &str) -> Result<IR> {
const DEPENDENCY_HEADER_NAME: &str = "test/dependency_header.h";
extern "C" {
fn json_from_cc_dependency(
header_source: FfiU8Slice,
dependency_header_source: FfiU8Slice,
) -> FfiU8SliceBox;
}
let header_source_with_include =
format!("#include \"{}\"\n\n{}", DEPENDENCY_HEADER_NAME, header_source);
let header_source_with_include_u8 = header_source_with_include.as_bytes();
let dependency_header_source_u8 = dependency_header_source.as_bytes();
let json_utf8 = unsafe {
json_from_cc_dependency(
FfiU8Slice::from_slice(header_source_with_include_u8),
FfiU8Slice::from_slice(dependency_header_source_u8),
)
.into_boxed_slice()
};
ir::deserialize_ir(&*json_utf8)
}
/// Creates an identifier
pub fn ir_id(name: &str) -> Identifier {
Identifier { identifier: name.to_string() }
}
/// Creates a simple `Item::Record` with a given name.
pub fn ir_record(name: &str) -> Record {
let ir = ir_from_cc("struct REPLACEME final {};").unwrap();
for item in ir.take_items() {
if let Item::Record(mut record) = item {
record.rs_name = name.to_string();
record.cc_name = name.to_string();
return record;
}
}
panic!("Test IR doesn't contain a record");
}
/// Retrieves the function with the given name.
/// Panics if no such function could be found.
pub fn retrieve_func<'a>(ir: &'a IR, name: &str) -> &'a Func {
for item in ir.items() {
if let Item::Func(func) = item {
if func.name == ir::UnqualifiedIdentifier::Identifier(ir_id(name)) {
return func;
}
}
}
panic!("Didn't find function with name {}", name);
}