1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::cell::RefCell;
12 use std::collections::BTreeMap;
15 use ast::{Ident, Name, TokenTree};
17 use ext::base::{ExtCtxt, MacExpr, MacResult, MacItems};
18 use ext::build::AstBuilder;
23 static REGISTERED_DIAGNOSTICS: RefCell<BTreeMap<Name, Option<Name>>> = {
24 RefCell::new(BTreeMap::new())
28 static USED_DIAGNOSTICS: RefCell<BTreeMap<Name, Span>> = {
29 RefCell::new(BTreeMap::new())
33 fn with_registered_diagnostics<T, F>(f: F) -> T where
34 F: FnOnce(&mut BTreeMap<Name, Option<Name>>) -> T,
36 REGISTERED_DIAGNOSTICS.with(move |slot| {
37 f(&mut *slot.borrow_mut())
41 fn with_used_diagnostics<T, F>(f: F) -> T where
42 F: FnOnce(&mut BTreeMap<Name, Span>) -> T,
44 USED_DIAGNOSTICS.with(move |slot| {
45 f(&mut *slot.borrow_mut())
49 pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
51 token_tree: &[TokenTree])
52 -> Box<MacResult+'cx> {
53 let code = match token_tree {
54 [ast::TtToken(_, token::Ident(code, _))] => code,
57 with_used_diagnostics(|diagnostics| {
58 match diagnostics.insert(code.name, span) {
59 Some(previous_span) => {
60 ecx.span_warn(span, &format!(
61 "diagnostic code {} already used", &token::get_ident(code)
63 ecx.span_note(previous_span, "previous invocation");
69 with_registered_diagnostics(|diagnostics| {
70 if !diagnostics.contains_key(&code.name) {
71 ecx.span_err(span, &format!(
72 "used diagnostic code {} not registered", &token::get_ident(code)
76 MacExpr::new(quote_expr!(ecx, ()))
79 pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
81 token_tree: &[TokenTree])
82 -> Box<MacResult+'cx> {
83 let (code, description) = match token_tree {
84 [ast::TtToken(_, token::Ident(ref code, _))] => {
87 [ast::TtToken(_, token::Ident(ref code, _)),
88 ast::TtToken(_, token::Comma),
89 ast::TtToken(_, token::Literal(token::StrRaw(description, _), None))] => {
90 (code, Some(description))
94 with_registered_diagnostics(|diagnostics| {
95 if diagnostics.insert(code.name, description).is_some() {
96 ecx.span_err(span, &format!(
97 "diagnostic code {} already registered", &token::get_ident(*code)
101 let sym = Ident::new(token::gensym(&(
102 "__register_diagnostic_".to_string() + &token::get_ident(*code)
104 MacItems::new(vec![quote_item!(ecx, mod $sym {}).unwrap()].into_iter())
107 pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
109 token_tree: &[TokenTree])
110 -> Box<MacResult+'cx> {
111 let name = match token_tree {
112 [ast::TtToken(_, token::Ident(ref name, _))] => name,
117 with_registered_diagnostics(|diagnostics| {
118 let descriptions: Vec<P<ast::Expr>> =
119 diagnostics.iter().filter_map(|(code, description)| {
120 description.map(|description| {
121 ecx.expr_tuple(span, vec![
122 ecx.expr_str(span, token::get_name(*code)),
123 ecx.expr_str(span, token::get_name(description))])
126 (descriptions.len(), ecx.expr_vec(span, descriptions))
129 MacItems::new(vec![quote_item!(ecx,
130 pub static $name: [(&'static str, &'static str); $count] = $expr;
131 ).unwrap()].into_iter())