]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/diagnostics/plugin.rs
rollup merge of #21413: ahmedcharles/remove-test-features
[rust.git] / src / libsyntax / diagnostics / plugin.rs
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.
4 //
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.
10
11 use std::cell::RefCell;
12 use std::collections::HashMap;
13 use ast;
14 use ast::{Ident, Name, TokenTree};
15 use codemap::Span;
16 use ext::base::{ExtCtxt, MacExpr, MacResult, MacItems};
17 use ext::build::AstBuilder;
18 use parse::token;
19 use ptr::P;
20
21 thread_local! {
22     static REGISTERED_DIAGNOSTICS: RefCell<HashMap<Name, Option<Name>>> = {
23         RefCell::new(HashMap::new())
24     }
25 }
26 thread_local! {
27     static USED_DIAGNOSTICS: RefCell<HashMap<Name, Span>> = {
28         RefCell::new(HashMap::new())
29     }
30 }
31
32 fn with_registered_diagnostics<T, F>(f: F) -> T where
33     F: FnOnce(&mut HashMap<Name, Option<Name>>) -> T,
34 {
35     REGISTERED_DIAGNOSTICS.with(move |slot| {
36         f(&mut *slot.borrow_mut())
37     })
38 }
39
40 fn with_used_diagnostics<T, F>(f: F) -> T where
41     F: FnOnce(&mut HashMap<Name, Span>) -> T,
42 {
43     USED_DIAGNOSTICS.with(move |slot| {
44         f(&mut *slot.borrow_mut())
45     })
46 }
47
48 pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
49                                    span: Span,
50                                    token_tree: &[TokenTree])
51                                    -> Box<MacResult+'cx> {
52     let code = match token_tree {
53         [ast::TtToken(_, token::Ident(code, _))] => code,
54         _ => unreachable!()
55     };
56     with_used_diagnostics(|diagnostics| {
57         match diagnostics.insert(code.name, span) {
58             Some(previous_span) => {
59                 ecx.span_warn(span, &format!(
60                     "diagnostic code {} already used", token::get_ident(code).get()
61                 )[]);
62                 ecx.span_note(previous_span, "previous invocation");
63             },
64             None => ()
65         }
66         ()
67     });
68     with_registered_diagnostics(|diagnostics| {
69         if !diagnostics.contains_key(&code.name) {
70             ecx.span_err(span, &format!(
71                 "used diagnostic code {} not registered", token::get_ident(code).get()
72             )[]);
73         }
74     });
75     MacExpr::new(quote_expr!(ecx, ()))
76 }
77
78 pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
79                                        span: Span,
80                                        token_tree: &[TokenTree])
81                                        -> Box<MacResult+'cx> {
82     let (code, description) = match token_tree {
83         [ast::TtToken(_, token::Ident(ref code, _))] => {
84             (code, None)
85         },
86         [ast::TtToken(_, token::Ident(ref code, _)),
87          ast::TtToken(_, token::Comma),
88          ast::TtToken(_, token::Literal(token::StrRaw(description, _), None))] => {
89             (code, Some(description))
90         }
91         _ => unreachable!()
92     };
93     with_registered_diagnostics(|diagnostics| {
94         if diagnostics.insert(code.name, description).is_some() {
95             ecx.span_err(span, &format!(
96                 "diagnostic code {} already registered", token::get_ident(*code).get()
97             )[]);
98         }
99     });
100     let sym = Ident::new(token::gensym(&(
101         "__register_diagnostic_".to_string() + token::get_ident(*code).get()
102     )[]));
103     MacItems::new(vec![quote_item!(ecx, mod $sym {}).unwrap()].into_iter())
104 }
105
106 pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
107                                           span: Span,
108                                           token_tree: &[TokenTree])
109                                           -> Box<MacResult+'cx> {
110     let name = match token_tree {
111         [ast::TtToken(_, token::Ident(ref name, _))] => name,
112         _ => unreachable!()
113     };
114
115     let (count, expr) =
116         with_registered_diagnostics(|diagnostics| {
117             let descriptions: Vec<P<ast::Expr>> =
118                 diagnostics.iter().filter_map(|(code, description)| {
119                     description.map(|description| {
120                         ecx.expr_tuple(span, vec![
121                             ecx.expr_str(span, token::get_name(*code)),
122                             ecx.expr_str(span, token::get_name(description))])
123                     })
124                 }).collect();
125             (descriptions.len(), ecx.expr_vec(span, descriptions))
126         });
127
128     MacItems::new(vec![quote_item!(ecx,
129         pub static $name: [(&'static str, &'static str); $count] = $expr;
130     ).unwrap()].into_iter())
131 }