Skip to content

Commit

Permalink
Merge pull request #102 from Wodann/test/incremental-compilation
Browse files Browse the repository at this point in the history
test(code_gen): add incremental compilation test
  • Loading branch information
baszalmstra authored Mar 25, 2020
2 parents 098eede + 52c31f1 commit 93ad9b8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/mun_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ features = ["llvm7-0"]

[dev-dependencies]
insta = "0.12.0"
parking_lot = "0.10"

[build-dependencies]
semver = "0.9.0"
Expand Down
30 changes: 30 additions & 0 deletions crates/mun_codegen/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{IrDatabase, OptimizationLevel};
use hir::{FileId, RelativePathBuf, SourceDatabase, SourceRoot, SourceRootId};
use parking_lot::Mutex;
use std::sync::Arc;

/// A mock implementation of the IR database. It can be used to set up a simple test case.
Expand All @@ -12,12 +13,20 @@ use std::sync::Arc;
#[derive(Default, Debug)]
pub(crate) struct MockDatabase {
runtime: salsa::Runtime<MockDatabase>,
events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>,
}

impl salsa::Database for MockDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<MockDatabase> {
&self.runtime
}

fn salsa_event(&self, event: impl Fn() -> salsa::Event<MockDatabase>) {
let mut events = self.events.lock();
if let Some(events) = &mut *events {
events.push(event());
}
}
}

impl MockDatabase {
Expand All @@ -43,4 +52,25 @@ impl MockDatabase {
db.set_context(Arc::new(context));
(db, file_id)
}

pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<MockDatabase>> {
*self.events.lock() = Some(Vec::new());
f();
self.events.lock().take().unwrap()
}

pub fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
let events = self.log(f);
events
.into_iter()
.filter_map(|e| match e.kind {
// This pretty horrible, but `Debug` is the only way to inspect
// QueryDescriptor at the moment.
salsa::EventKind::WillExecute { database_key } => {
Some(format!("{:?}", database_key))
}
_ => None,
})
.collect()
}
}
45 changes: 45 additions & 0 deletions crates/mun_codegen/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,51 @@ fn extern_fn() {
)
}

#[test]
fn incremental_compilation() {
let (mut db, file_id) = MockDatabase::with_single_file(
r#"
struct Foo(int);
pub fn foo(foo: Foo):int {
foo.0
}
"#,
);
db.set_optimization_lvl(OptimizationLevel::Default);
db.set_target(Target::host_target().unwrap());

{
let events = db.log_executed(|| {
db.file_ir(file_id);
});
assert!(
format!("{:?}", events).contains("group_ir"),
"{:#?}",
events
);
assert!(format!("{:?}", events).contains("file_ir"), "{:#?}", events);
}

db.set_optimization_lvl(OptimizationLevel::Aggressive);

{
let events = db.log_executed(|| {
db.file_ir(file_id);
});
println!("events: {:?}", events);
assert!(
!format!("{:?}", events).contains("group_ir"),
"{:#?}",
events
);
assert!(format!("{:?}", events).contains("file_ir"), "{:#?}", events);
}

// TODO: Try to disconnect `group_ir` and `file_ir`
// TODO: Add support for multiple files in a group
}

fn test_snapshot(text: &str) {
test_snapshot_with_optimization(text, OptimizationLevel::Default);
}
Expand Down

0 comments on commit 93ad9b8

Please sign in to comment.