diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-10 18:46:58 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-10 18:46:58 +0000 |
commit | 0b96f1a0475b0f9a9eb8cfddf129f7565d4bc2b8 (patch) | |
tree | 8293508a0beaea93703d2a8f25ba7476ab3f9bb7 /crates | |
parent | f0e78f2ed6bda5d48caabe35efbb09cb2f3ff5a4 (diff) | |
parent | 2b8674b37ed533fba65b82da35a33c77775df278 (diff) |
Merge #7964
7964: Implement builtin `cfg!` macro r=jonas-schievink a=jonas-schievink
bors r+
Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir_expand/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/hir_expand/src/builtin_macro.rs | 14 | ||||
-rw-r--r-- | crates/hir_expand/src/name.rs | 1 | ||||
-rw-r--r-- | crates/hir_expand/src/quote.rs | 5 |
4 files changed, 19 insertions, 2 deletions
diff --git a/crates/hir_expand/Cargo.toml b/crates/hir_expand/Cargo.toml index 76cb03126..7bb40ba9b 100644 --- a/crates/hir_expand/Cargo.toml +++ b/crates/hir_expand/Cargo.toml | |||
@@ -16,6 +16,7 @@ rustc-hash = "1.0.0" | |||
16 | la-arena = { version = "0.2.0", path = "../../lib/arena" } | 16 | la-arena = { version = "0.2.0", path = "../../lib/arena" } |
17 | 17 | ||
18 | base_db = { path = "../base_db", version = "0.0.0" } | 18 | base_db = { path = "../base_db", version = "0.0.0" } |
19 | cfg = { path = "../cfg", version = "0.0.0" } | ||
19 | syntax = { path = "../syntax", version = "0.0.0" } | 20 | syntax = { path = "../syntax", version = "0.0.0" } |
20 | parser = { path = "../parser", version = "0.0.0" } | 21 | parser = { path = "../parser", version = "0.0.0" } |
21 | profile = { path = "../profile", version = "0.0.0" } | 22 | profile = { path = "../profile", version = "0.0.0" } |
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index eb57ea7d6..b34f1a4f2 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs | |||
@@ -5,6 +5,7 @@ use crate::{ | |||
5 | }; | 5 | }; |
6 | 6 | ||
7 | use base_db::{AnchoredPath, FileId}; | 7 | use base_db::{AnchoredPath, FileId}; |
8 | use cfg::CfgExpr; | ||
8 | use either::Either; | 9 | use either::Either; |
9 | use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult}; | 10 | use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult}; |
10 | use parser::FragmentKind; | 11 | use parser::FragmentKind; |
@@ -97,6 +98,7 @@ register_builtin! { | |||
97 | (format_args_nl, FormatArgsNl) => format_args_expand, | 98 | (format_args_nl, FormatArgsNl) => format_args_expand, |
98 | (llvm_asm, LlvmAsm) => asm_expand, | 99 | (llvm_asm, LlvmAsm) => asm_expand, |
99 | (asm, Asm) => asm_expand, | 100 | (asm, Asm) => asm_expand, |
101 | (cfg, Cfg) => cfg_expand, | ||
100 | 102 | ||
101 | EAGER: | 103 | EAGER: |
102 | (compile_error, CompileError) => compile_error_expand, | 104 | (compile_error, CompileError) => compile_error_expand, |
@@ -258,6 +260,18 @@ fn asm_expand( | |||
258 | ExpandResult::ok(expanded) | 260 | ExpandResult::ok(expanded) |
259 | } | 261 | } |
260 | 262 | ||
263 | fn cfg_expand( | ||
264 | db: &dyn AstDatabase, | ||
265 | id: LazyMacroId, | ||
266 | tt: &tt::Subtree, | ||
267 | ) -> ExpandResult<tt::Subtree> { | ||
268 | let loc = db.lookup_intern_macro(id); | ||
269 | let expr = CfgExpr::parse(tt); | ||
270 | let enabled = db.crate_graph()[loc.krate].cfg_options.check(&expr) != Some(false); | ||
271 | let expanded = if enabled { quote!(true) } else { quote!(false) }; | ||
272 | ExpandResult::ok(expanded) | ||
273 | } | ||
274 | |||
261 | fn unquote_str(lit: &tt::Literal) -> Option<String> { | 275 | fn unquote_str(lit: &tt::Literal) -> Option<String> { |
262 | let lit = ast::make::tokens::literal(&lit.to_string()); | 276 | let lit = ast::make::tokens::literal(&lit.to_string()); |
263 | let token = ast::String::cast(lit)?; | 277 | let token = ast::String::cast(lit)?; |
diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs index c94fb580a..e833e032c 100644 --- a/crates/hir_expand/src/name.rs +++ b/crates/hir_expand/src/name.rs | |||
@@ -154,6 +154,7 @@ pub mod known { | |||
154 | macro_rules, | 154 | macro_rules, |
155 | derive, | 155 | derive, |
156 | doc, | 156 | doc, |
157 | cfg, | ||
157 | cfg_attr, | 158 | cfg_attr, |
158 | // Components of known path (value or mod name) | 159 | // Components of known path (value or mod name) |
159 | std, | 160 | std, |
diff --git a/crates/hir_expand/src/quote.rs b/crates/hir_expand/src/quote.rs index 219bc2097..08bc5aa49 100644 --- a/crates/hir_expand/src/quote.rs +++ b/crates/hir_expand/src/quote.rs | |||
@@ -188,8 +188,9 @@ macro_rules! impl_to_to_tokentrees { | |||
188 | 188 | ||
189 | impl_to_to_tokentrees! { | 189 | impl_to_to_tokentrees! { |
190 | u32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; | 190 | u32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; |
191 | usize => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()}}; | 191 | usize => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; |
192 | i32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()}}; | 192 | i32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; |
193 | bool => self { tt::Ident{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; | ||
193 | tt::Leaf => self { self }; | 194 | tt::Leaf => self { self }; |
194 | tt::Literal => self { self }; | 195 | tt::Literal => self { self }; |
195 | tt::Ident => self { self }; | 196 | tt::Ident => self { self }; |