Skip to content
Open
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
10 changes: 10 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "proc_macro_state"
version = "0.1.0"
edition = "2021"

[dependencies]
macros = { path = "macros" }

[workspace]
members = ["macros", "busy0", "busy1", "busy2", "busy3", "busy4", "busy5"]
7 changes: 7 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy0/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "busy0"
version = "0.1.0"
edition = "2021"

[dependencies]
macros = { path = "../macros" }
8 changes: 8 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy0/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[macros::slow]
pub fn f0() {}

#[macros::slow]
pub fn f1() {}

#[macros::slow]
pub fn f2() {}
7 changes: 7 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "busy1"
version = "0.1.0"
edition = "2021"

[dependencies]
macros = { path = "../macros" }
17 changes: 17 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[macros::slow]
pub fn f0() {}

#[macros::slow]
pub fn f1() {}

#[macros::slow]
pub fn f2() {}

#[macros::slow]
pub fn f3() {}

#[macros::slow]
pub fn f4() {}

#[macros::slow]
pub fn f5() {}
7 changes: 7 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "busy2"
version = "0.1.0"
edition = "2021"

[dependencies]
macros = { path = "../macros" }
26 changes: 26 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[macros::slow]
pub fn f0() {}

#[macros::slow]
pub fn f1() {}

#[macros::slow]
pub fn f2() {}

#[macros::slow]
pub fn f3() {}

#[macros::slow]
pub fn f4() {}

#[macros::slow]
pub fn f5() {}

#[macros::slow]
pub fn f6() {}

#[macros::slow]
pub fn f7() {}

#[macros::slow]
pub fn f8() {}
7 changes: 7 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy3/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "busy3"
version = "0.1.0"
edition = "2021"

[dependencies]
macros = { path = "../macros" }
35 changes: 35 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#[macros::slow]
pub fn f0() {}

#[macros::slow]
pub fn f1() {}

#[macros::slow]
pub fn f2() {}

#[macros::slow]
pub fn f3() {}

#[macros::slow]
pub fn f4() {}

#[macros::slow]
pub fn f5() {}

#[macros::slow]
pub fn f6() {}

#[macros::slow]
pub fn f7() {}

#[macros::slow]
pub fn f8() {}

#[macros::slow]
pub fn f9() {}

#[macros::slow]
pub fn f10() {}

#[macros::slow]
pub fn f11() {}
7 changes: 7 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy4/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "busy4"
version = "0.1.0"
edition = "2021"

[dependencies]
macros = { path = "../macros" }
44 changes: 44 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy4/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#[macros::slow]
pub fn f0() {}

#[macros::slow]
pub fn f1() {}

#[macros::slow]
pub fn f2() {}

#[macros::slow]
pub fn f3() {}

#[macros::slow]
pub fn f4() {}

#[macros::slow]
pub fn f5() {}

#[macros::slow]
pub fn f6() {}

#[macros::slow]
pub fn f7() {}

#[macros::slow]
pub fn f8() {}

#[macros::slow]
pub fn f9() {}

#[macros::slow]
pub fn f10() {}

#[macros::slow]
pub fn f11() {}

#[macros::slow]
pub fn f12() {}

#[macros::slow]
pub fn f13() {}

#[macros::slow]
pub fn f14() {}
7 changes: 7 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy5/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "busy5"
version = "0.1.0"
edition = "2021"

[dependencies]
macros = { path = "../macros" }
53 changes: 53 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/busy5/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#[macros::slow]
pub fn f0() {}

#[macros::slow]
pub fn f1() {}

#[macros::slow]
pub fn f2() {}

#[macros::slow]
pub fn f3() {}

#[macros::slow]
pub fn f4() {}

#[macros::slow]
pub fn f5() {}

#[macros::slow]
pub fn f6() {}

#[macros::slow]
pub fn f7() {}

#[macros::slow]
pub fn f8() {}

#[macros::slow]
pub fn f9() {}

#[macros::slow]
pub fn f10() {}

#[macros::slow]
pub fn f11() {}

#[macros::slow]
pub fn f12() {}

#[macros::slow]
pub fn f13() {}

#[macros::slow]
pub fn f14() {}

#[macros::slow]
pub fn f15() {}

#[macros::slow]
pub fn f16() {}

#[macros::slow]
pub fn f17() {}
8 changes: 8 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/calls.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
| src/lib.rs:122:5:122:12 | e.get0() | <proc_macro_state::E0 as proc_macro_state::Get0>::get0 |
| src/lib.rs:131:5:131:12 | e.get1() | <proc_macro_state::E1 as proc_macro_state::Get1>::get1 |
| src/lib.rs:140:5:140:12 | e.get2() | <proc_macro_state::E2 as proc_macro_state::Get2>::get2 |
| src/lib.rs:149:5:149:12 | e.get3() | <proc_macro_state::E3 as proc_macro_state::Get3>::get3 |
| src/lib.rs:158:5:158:12 | e.get4() | <proc_macro_state::E4 as proc_macro_state::Get4>::get4 |
| src/lib.rs:167:5:167:12 | e.get5() | <proc_macro_state::E5 as proc_macro_state::Get5>::get5 |
| src/lib.rs:176:5:176:12 | e.get6() | <proc_macro_state::E6 as proc_macro_state::Get6>::get6 |
| src/lib.rs:185:5:185:12 | e.get7() | <proc_macro_state::E7 as proc_macro_state::Get7>::get7 |
5 changes: 5 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/calls.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import rust

from MethodCallExpr c
where c.getLocation().getFile().getRelativePath() = "src/lib.rs"
select c, c.getStaticTarget().getCanonicalPath()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "macros"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true
89 changes: 89 additions & 0 deletions rust/ql/integration-tests/proc-macro-state/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//! Mimics `enum_dispatch`, which records trait definitions in process-global state when expanding
//! the trait attribute and reads them back when expanding the enum attribute.
extern crate proc_macro;

use proc_macro::{Delimiter, TokenStream, TokenTree};
use std::collections::BTreeMap;
use std::sync::Mutex;
use std::time::Duration;

/// Maps trait names to their method name and signature.
static TRAIT_METHODS: Mutex<BTreeMap<String, (String, String)>> = Mutex::new(BTreeMap::new());

fn ident_after(tokens: &[TokenTree], keyword: &str) -> Option<String> {
tokens
.windows(2)
.find(|w| matches!(&w[0], TokenTree::Ident(i) if i.to_string() == keyword))
.map(|w| w[1].to_string())
}

fn body(tokens: &[TokenTree]) -> Vec<TokenTree> {
tokens
.iter()
.find_map(|t| match t {
TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => {
Some(g.stream().into_iter().collect())
}
_ => None,
})
.unwrap_or_default()
}

/// Expects a trait with a single `fn name(&self) -> Type;` method.
#[proc_macro_attribute]
pub fn register(_: TokenStream, item: TokenStream) -> TokenStream {
pause();
let tokens: Vec<_> = item.clone().into_iter().collect();
let name = ident_after(&tokens, "trait").unwrap();
let signature: Vec<_> = body(&tokens)
.into_iter()
.take_while(|t| !matches!(t, TokenTree::Punct(p) if p.as_char() == ';'))
.collect();
let method = ident_after(&signature, "fn").unwrap();
let signature = TokenStream::from_iter(signature).to_string();
TRAIT_METHODS
.lock()
.unwrap()
.insert(name, (method, signature));
item
}

/// Implements the registered trait for an enum of single-field tuple variants, if this process
/// has seen the trait.
#[proc_macro_attribute]
pub fn dispatch(attr: TokenStream, item: TokenStream) -> TokenStream {
pause();
let trait_name = attr.to_string();
let Some((method, signature)) = TRAIT_METHODS.lock().unwrap().get(&trait_name).cloned() else {
return item;
};
let tokens: Vec<_> = item.clone().into_iter().collect();
let name = ident_after(&tokens, "enum").unwrap();
let arms: String = body(&tokens)
.iter()
.filter_map(|t| match t {
TokenTree::Ident(variant) => Some(format!(
"{name}::{variant}(x) => {trait_name}::{method}(x),"
)),
_ => None,
})
.collect();
let mut ret = item;
ret.extend(
format!("impl {trait_name} for {name} {{ {signature} {{ match self {{ {arms} }} }} }}")
.parse::<TokenStream>()
.unwrap(),
);
ret
}

/// Makes concurrent expansions overlap, so that a server pool would spread them across servers.
fn pause() {
std::thread::sleep(Duration::from_millis(20));
}

#[proc_macro_attribute]
pub fn slow(_: TokenStream, item: TokenStream) -> TokenStream {
pause();
item
}
Loading
Loading