]> git.lizzy.rs Git - rust.git/commitdiff
Implement builtin `cfg!` macro
authorJonas Schievink <jonasschievink@gmail.com>
Wed, 10 Mar 2021 18:43:03 +0000 (19:43 +0100)
committerJonas Schievink <jonasschievink@gmail.com>
Wed, 10 Mar 2021 18:43:03 +0000 (19:43 +0100)
Cargo.lock
crates/hir_expand/Cargo.toml
crates/hir_expand/src/builtin_macro.rs
crates/hir_expand/src/name.rs
crates/hir_expand/src/quote.rs

index 87cf1bf27f1e85e667eaff8dccf02cdeb78411f7..8059a8809da37ee76e226ccc59236f8d92fcd681 100644 (file)
@@ -524,6 +524,7 @@ name = "hir_expand"
 version = "0.0.0"
 dependencies = [
  "base_db",
+ "cfg",
  "either",
  "la-arena",
  "log",
index 76cb03126e51098742b296525b7ffc017817ff23..7bb40ba9b665e6b4a31490502271f23be309105d 100644 (file)
@@ -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" }
index eb57ea7d61d677333537d1bdd8b446065c538bb3..b34f1a4f2228f276ca5901d969d676e64b8a2ba7 100644 (file)
@@ -5,6 +5,7 @@
 };
 
 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 @@ pub fn find_builtin_macro(
     (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<tt::Subtree> {
+    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<String> {
     let lit = ast::make::tokens::literal(&lit.to_string());
     let token = ast::String::cast(lit)?;
index c94fb580a239645ab9a0b24ecf69bd212f21e495..e833e032c08582288d48e0194d17438becca16b0 100644 (file)
@@ -154,6 +154,7 @@ macro_rules! known_names {
         macro_rules,
         derive,
         doc,
+        cfg,
         cfg_attr,
         // Components of known path (value or mod name)
         std,
index 219bc20978e5c66c79c156bf3591c248beacc2ae..08bc5aa49f2a902fc9fd966834663d9af7e0c519 100644 (file)
@@ -188,8 +188,9 @@ fn to_token($this) -> tt::TokenTree {
 
 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 };