From 2b8674b37ed533fba65b82da35a33c77775df278 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 10 Mar 2021 19:43:03 +0100 Subject: Implement builtin `cfg!` macro --- crates/hir_expand/Cargo.toml | 1 + crates/hir_expand/src/builtin_macro.rs | 14 ++++++++++++++ crates/hir_expand/src/name.rs | 1 + crates/hir_expand/src/quote.rs | 5 +++-- 4 files changed, 19 insertions(+), 2 deletions(-) (limited to 'crates') 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" la-arena = { version = "0.2.0", path = "../../lib/arena" } base_db = { path = "../base_db", version = "0.0.0" } +cfg = { path = "../cfg", version = "0.0.0" } syntax = { path = "../syntax", version = "0.0.0" } parser = { path = "../parser", version = "0.0.0" } 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::{ }; use base_db::{AnchoredPath, FileId}; +use cfg::CfgExpr; use either::Either; use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult}; use parser::FragmentKind; @@ -97,6 +98,7 @@ register_builtin! { (format_args_nl, FormatArgsNl) => format_args_expand, (llvm_asm, LlvmAsm) => asm_expand, (asm, Asm) => asm_expand, + (cfg, Cfg) => cfg_expand, EAGER: (compile_error, CompileError) => compile_error_expand, @@ -258,6 +260,18 @@ fn asm_expand( ExpandResult::ok(expanded) } +fn cfg_expand( + db: &dyn AstDatabase, + id: LazyMacroId, + tt: &tt::Subtree, +) -> ExpandResult { + let loc = db.lookup_intern_macro(id); + let expr = CfgExpr::parse(tt); + let enabled = db.crate_graph()[loc.krate].cfg_options.check(&expr) != Some(false); + let expanded = if enabled { quote!(true) } else { quote!(false) }; + ExpandResult::ok(expanded) +} + fn unquote_str(lit: &tt::Literal) -> Option { let lit = ast::make::tokens::literal(&lit.to_string()); 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 { macro_rules, derive, doc, + cfg, cfg_attr, // Components of known path (value or mod name) 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 { impl_to_to_tokentrees! { u32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; - usize => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()}}; - i32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()}}; + usize => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; + i32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; + bool => self { tt::Ident{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; tt::Leaf => self { self }; tt::Literal => self { self }; tt::Ident => self { self }; -- cgit v1.2.3