Skip to content

Commit d0b6ed4

Browse files
committed
Add ProcMacroClient
1 parent a617f24 commit d0b6ed4

File tree

17 files changed

+222
-43
lines changed

17 files changed

+222
-43
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_db/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ rustc-hash = "1.1.0"
1515
ra_syntax = { path = "../ra_syntax" }
1616
ra_cfg = { path = "../ra_cfg" }
1717
ra_prof = { path = "../ra_prof" }
18+
ra_proc_macro = { path = "../ra_proc_macro" }
1819
test_utils = { path = "../test_utils" }

crates/ra_db/src/fixture.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
7070
meta.cfg,
7171
meta.env,
7272
Default::default(),
73+
Default::default(),
7374
);
7475
crate_graph
7576
} else {
@@ -81,6 +82,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
8182
CfgOptions::default(),
8283
Env::default(),
8384
Default::default(),
85+
Default::default(),
8486
);
8587
crate_graph
8688
};
@@ -130,6 +132,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
130132
meta.cfg,
131133
meta.env,
132134
Default::default(),
135+
Default::default(),
133136
);
134137
let prev = crates.insert(krate.clone(), crate_id);
135138
assert!(prev.is_none());
@@ -167,6 +170,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
167170
CfgOptions::default(),
168171
Env::default(),
169172
Default::default(),
173+
Default::default(),
170174
);
171175
} else {
172176
for (from, to) in crate_deps {

crates/ra_db/src/input.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_hash::FxHashSet;
1919

2020
use crate::{RelativePath, RelativePathBuf};
2121
use fmt::Display;
22+
use ra_proc_macro::ProcMacro;
2223

2324
/// `FileId` is an integer which uniquely identifies a file. File paths are
2425
/// messy and system-dependent, so most of the code should work directly with
@@ -115,6 +116,9 @@ impl Display for CrateName {
115116
}
116117
}
117118

119+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
120+
pub struct ProcMacroId(pub usize);
121+
118122
#[derive(Debug, Clone, PartialEq, Eq)]
119123
pub struct CrateData {
120124
pub root_file_id: FileId,
@@ -127,6 +131,7 @@ pub struct CrateData {
127131
pub env: Env,
128132
pub extern_source: ExternSource,
129133
pub dependencies: Vec<Dependency>,
134+
pub proc_macro: Vec<ProcMacro>,
130135
}
131136

132137
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -166,6 +171,7 @@ impl CrateGraph {
166171
cfg_options: CfgOptions,
167172
env: Env,
168173
extern_source: ExternSource,
174+
proc_macro: Vec<ProcMacro>,
169175
) -> CrateId {
170176
let data = CrateData {
171177
root_file_id: file_id,
@@ -174,6 +180,7 @@ impl CrateGraph {
174180
cfg_options,
175181
env,
176182
extern_source,
183+
proc_macro,
177184
dependencies: Vec::new(),
178185
};
179186
let crate_id = CrateId(self.arena.len() as u32);
@@ -345,6 +352,7 @@ mod tests {
345352
CfgOptions::default(),
346353
Env::default(),
347354
Default::default(),
355+
Default::default(),
348356
);
349357
let crate2 = graph.add_crate_root(
350358
FileId(2u32),
@@ -353,6 +361,7 @@ mod tests {
353361
CfgOptions::default(),
354362
Env::default(),
355363
Default::default(),
364+
Default::default(),
356365
);
357366
let crate3 = graph.add_crate_root(
358367
FileId(3u32),
@@ -361,6 +370,7 @@ mod tests {
361370
CfgOptions::default(),
362371
Env::default(),
363372
Default::default(),
373+
Default::default(),
364374
);
365375
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
366376
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@@ -377,6 +387,7 @@ mod tests {
377387
CfgOptions::default(),
378388
Env::default(),
379389
Default::default(),
390+
Default::default(),
380391
);
381392
let crate2 = graph.add_crate_root(
382393
FileId(2u32),
@@ -385,6 +396,7 @@ mod tests {
385396
CfgOptions::default(),
386397
Env::default(),
387398
Default::default(),
399+
Default::default(),
388400
);
389401
let crate3 = graph.add_crate_root(
390402
FileId(3u32),
@@ -393,6 +405,7 @@ mod tests {
393405
CfgOptions::default(),
394406
Env::default(),
395407
Default::default(),
408+
Default::default(),
396409
);
397410
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
398411
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@@ -408,6 +421,7 @@ mod tests {
408421
CfgOptions::default(),
409422
Env::default(),
410423
Default::default(),
424+
Default::default(),
411425
);
412426
let crate2 = graph.add_crate_root(
413427
FileId(2u32),
@@ -416,6 +430,7 @@ mod tests {
416430
CfgOptions::default(),
417431
Env::default(),
418432
Default::default(),
433+
Default::default(),
419434
);
420435
assert!(graph
421436
.add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)

crates/ra_db/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ pub use crate::{
1212
cancellation::Canceled,
1313
input::{
1414
CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, ExternSourceId,
15-
FileId, SourceRoot, SourceRootId,
15+
FileId, ProcMacroId, SourceRoot, SourceRootId,
1616
},
1717
};
18+
pub use ra_proc_macro::ProcMacro;
1819
pub use relative_path::{RelativePath, RelativePathBuf};
1920
pub use salsa;
2021

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use hir_expand::{
1111
HirFileId, MacroCallId, MacroDefId, MacroDefKind,
1212
};
1313
use ra_cfg::CfgOptions;
14-
use ra_db::{CrateId, FileId};
15-
use ra_syntax::ast;
14+
use ra_db::{CrateId, FileId, ProcMacroId};
15+
use ra_syntax::{ast, SmolStr};
1616
use rustc_hash::FxHashMap;
1717
use test_utils::tested_by;
1818

@@ -53,6 +53,16 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr
5353
}
5454

5555
let cfg_options = &crate_graph[def_map.krate].cfg_options;
56+
let proc_macros = &crate_graph[def_map.krate].proc_macro;
57+
let proc_macros = proc_macros
58+
.iter()
59+
.enumerate()
60+
.map(|(idx, it)| {
61+
// FIXME: a hacky way to create a Name from string.
62+
let name = tt::Ident { text: SmolStr::new(&it.name()), id: tt::TokenId::unspecified() };
63+
(name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx)))
64+
})
65+
.collect();
5666

5767
let mut collector = DefCollector {
5868
db,
@@ -65,9 +75,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr
6575
unexpanded_attribute_macros: Vec::new(),
6676
mod_dirs: FxHashMap::default(),
6777
cfg_options,
68-
69-
// FIXME: pass proc-macro from crate-graph
70-
proc_macros: Default::default(),
78+
proc_macros,
7179
};
7280
collector.collect();
7381
collector.finish()
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
//! Proc Macro Expander stub
22
3-
use crate::{db::AstDatabase, LazyMacroId, MacroCallKind, MacroCallLoc};
4-
use ra_db::CrateId;
3+
use crate::{db::AstDatabase, LazyMacroId};
4+
use ra_db::{CrateId, ProcMacroId};
55

66
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
77
pub struct ProcMacroExpander {
88
krate: CrateId,
9+
proc_macro_id: ProcMacroId,
910
}
1011

1112
impl ProcMacroExpander {
12-
pub fn new(krate: CrateId) -> ProcMacroExpander {
13-
ProcMacroExpander { krate }
13+
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> ProcMacroExpander {
14+
ProcMacroExpander { krate, proc_macro_id }
1415
}
1516

1617
pub fn expand(
1718
&self,
1819
db: &dyn AstDatabase,
19-
id: LazyMacroId,
20-
_tt: &tt::Subtree,
20+
_id: LazyMacroId,
21+
tt: &tt::Subtree,
2122
) -> Result<tt::Subtree, mbe::ExpandError> {
22-
let loc: MacroCallLoc = db.lookup_intern_macro(id);
23-
let name = match loc.kind {
24-
MacroCallKind::FnLike(_) => return Err(mbe::ExpandError::ConversionError),
25-
MacroCallKind::Attr(_, name) => name,
26-
};
27-
28-
log::debug!("Proc-macro-expanding name = {}", name);
29-
30-
// Return nothing for now
31-
return Ok(tt::Subtree::default());
23+
let krate_graph = db.crate_graph();
24+
let proc_macro = krate_graph[self.krate]
25+
.proc_macro
26+
.get(self.proc_macro_id.0)
27+
.clone()
28+
.ok_or_else(|| mbe::ExpandError::ConversionError)?;
29+
proc_macro.custom_derive(tt)
3230
}
3331
}

crates/ra_ide/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ impl Analysis {
213213
cfg_options,
214214
Env::default(),
215215
Default::default(),
216+
Default::default(),
216217
);
217218
change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text));
218219
change.set_crate_graph(crate_graph);

crates/ra_ide/src/mock_analysis.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ impl MockAnalysis {
103103
cfg_options,
104104
Env::default(),
105105
Default::default(),
106+
Default::default(),
106107
));
107108
} else if path.ends_with("/lib.rs") {
108109
let crate_name = path.parent().unwrap().file_name().unwrap();
@@ -113,6 +114,7 @@ impl MockAnalysis {
113114
cfg_options,
114115
Env::default(),
115116
Default::default(),
117+
Default::default(),
116118
);
117119
if let Some(root_crate) = root_crate {
118120
crate_graph

crates/ra_ide/src/parent_module.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ mod tests {
137137
CfgOptions::default(),
138138
Env::default(),
139139
Default::default(),
140+
Default::default(),
140141
);
141142
let mut change = AnalysisChange::new();
142143
change.set_crate_graph(crate_graph);

0 commit comments

Comments
 (0)