Skip to content

Commit 0b96f1a

Browse files
Merge #7964
7964: Implement builtin `cfg!` macro r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents f0e78f2 + 2b8674b commit 0b96f1a

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

Cargo.lock

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

crates/hir_expand/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rustc-hash = "1.0.0"
1616
la-arena = { version = "0.2.0", path = "../../lib/arena" }
1717

1818
base_db = { path = "../base_db", version = "0.0.0" }
19+
cfg = { path = "../cfg", version = "0.0.0" }
1920
syntax = { path = "../syntax", version = "0.0.0" }
2021
parser = { path = "../parser", version = "0.0.0" }
2122
profile = { path = "../profile", version = "0.0.0" }

crates/hir_expand/src/builtin_macro.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
};
66

77
use base_db::{AnchoredPath, FileId};
8+
use cfg::CfgExpr;
89
use either::Either;
910
use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult};
1011
use parser::FragmentKind;
@@ -97,6 +98,7 @@ register_builtin! {
9798
(format_args_nl, FormatArgsNl) => format_args_expand,
9899
(llvm_asm, LlvmAsm) => asm_expand,
99100
(asm, Asm) => asm_expand,
101+
(cfg, Cfg) => cfg_expand,
100102

101103
EAGER:
102104
(compile_error, CompileError) => compile_error_expand,
@@ -258,6 +260,18 @@ fn asm_expand(
258260
ExpandResult::ok(expanded)
259261
}
260262

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+
261275
fn unquote_str(lit: &tt::Literal) -> Option<String> {
262276
let lit = ast::make::tokens::literal(&lit.to_string());
263277
let token = ast::String::cast(lit)?;

crates/hir_expand/src/name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ pub mod known {
154154
macro_rules,
155155
derive,
156156
doc,
157+
cfg,
157158
cfg_attr,
158159
// Components of known path (value or mod name)
159160
std,

crates/hir_expand/src/quote.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,9 @@ macro_rules! impl_to_to_tokentrees {
188188

189189
impl_to_to_tokentrees! {
190190
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()}};
192-
i32 => 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()} };
193+
bool => self { tt::Ident{text: self.to_string().into(), id: tt::TokenId::unspecified()} };
193194
tt::Leaf => self { self };
194195
tt::Literal => self { self };
195196
tt::Ident => self { self };

0 commit comments

Comments
 (0)