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::HashMap;
14 use ast::{Ident, Name, TokenTree};
16 use ext::base::{ExtCtxt, MacExpr, MacResult, MacItems};
17 use ext::build::AstBuilder;
21 thread_local!(static REGISTERED_DIAGNOSTICS: RefCell<HashMap<Name, Option<Name>>> = {
22 RefCell::new(HashMap::new())
24 thread_local!(static USED_DIAGNOSTICS: RefCell<HashMap<Name, Span>> = {
25 RefCell::new(HashMap::new())
28 fn with_registered_diagnostics<T>(f: |&mut HashMap<Name, Option<Name>>| -> T) -> T {
29 REGISTERED_DIAGNOSTICS.with(|slot| {
30 f(&mut *slot.borrow_mut())
34 fn with_used_diagnostics<T>(f: |&mut HashMap<Name, Span>| -> T) -> T {
35 USED_DIAGNOSTICS.with(|slot| {
36 f(&mut *slot.borrow_mut())
40 pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
42 token_tree: &[TokenTree])
43 -> Box<MacResult+'cx> {
44 let code = match token_tree {
45 [ast::TtToken(_, token::Ident(code, _))] => code,
48 with_registered_diagnostics(|diagnostics| {
49 if !diagnostics.contains_key(&code.name) {
50 ecx.span_err(span, format!(
51 "unknown diagnostic code {}; add to librustc/diagnostics.rs",
52 token::get_ident(code).get()
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).get()
63 ecx.span_note(previous_span, "previous invocation");
69 MacExpr::new(quote_expr!(ecx, ()))
72 pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
74 token_tree: &[TokenTree])
75 -> Box<MacResult+'cx> {
76 let (code, description) = match token_tree {
77 [ast::TtToken(_, token::Ident(ref code, _))] => {
80 [ast::TtToken(_, token::Ident(ref code, _)),
81 ast::TtToken(_, token::Comma),
82 ast::TtToken(_, token::Literal(token::StrRaw(description, _), None))] => {
83 (code, Some(description))
87 with_registered_diagnostics(|diagnostics| {
88 if diagnostics.insert(code.name, description).is_some() {
89 ecx.span_err(span, format!(
90 "diagnostic code {} already registered", token::get_ident(*code).get()
94 let sym = Ident::new(token::gensym((
95 "__register_diagnostic_".to_string() + token::get_ident(*code).get()
97 MacItems::new(vec![quote_item!(ecx, mod $sym {}).unwrap()].into_iter())
100 pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
102 token_tree: &[TokenTree])
103 -> Box<MacResult+'cx> {
104 let name = match token_tree {
105 [ast::TtToken(_, token::Ident(ref name, _))] => name,
109 let (count, expr) = with_used_diagnostics(|diagnostics_in_use| {
110 with_registered_diagnostics(|diagnostics| {
111 let descriptions: Vec<P<ast::Expr>> = diagnostics
112 .iter().filter_map(|(code, description)| {
113 if !diagnostics_in_use.contains_key(code) {
114 ecx.span_warn(span, format!(
115 "diagnostic code {} never used", token::get_name(*code).get()
118 description.map(|description| {
119 ecx.expr_tuple(span, vec![
120 ecx.expr_str(span, token::get_name(*code)),
121 ecx.expr_str(span, token::get_name(description))
125 (descriptions.len(), ecx.expr_vec(span, descriptions))
128 MacItems::new(vec![quote_item!(ecx,
129 pub static $name: [(&'static str, &'static str), ..$count] = $expr;
130 ).unwrap()].into_iter())