mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 07:04:22 +00:00
Merge #7964
7964: Implement builtin `cfg!` macro r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
commit
0b96f1a047
5 changed files with 20 additions and 2 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -524,6 +524,7 @@ name = "hir_expand"
|
|||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"base_db",
|
||||
"cfg",
|
||||
"either",
|
||||
"la-arena",
|
||||
"log",
|
||||
|
|
|
@ -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" }
|
||||
|
|
|
@ -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<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)?;
|
||||
|
|
|
@ -154,6 +154,7 @@ pub mod known {
|
|||
macro_rules,
|
||||
derive,
|
||||
doc,
|
||||
cfg,
|
||||
cfg_attr,
|
||||
// Components of known path (value or mod name)
|
||||
std,
|
||||
|
|
|
@ -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 };
|
||||
|
|
Loading…
Reference in a new issue