|
| 1 | +// Copyright 2017 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 Span; |
| 12 | + |
| 13 | +use rustc_errors as rustc; |
| 14 | + |
| 15 | +/// An enum representing a diagnostic level. |
| 16 | +#[unstable(feature = "proc_macro", issue = "38356")] |
| 17 | +#[derive(Copy, Clone, Debug)] |
| 18 | +pub enum Level { |
| 19 | + /// An error. |
| 20 | + Error, |
| 21 | + /// A warning. |
| 22 | + Warning, |
| 23 | + /// A note. |
| 24 | + Note, |
| 25 | + /// A help message. |
| 26 | + Help, |
| 27 | + #[doc(hidden)] |
| 28 | + __Nonexhaustive, |
| 29 | +} |
| 30 | + |
| 31 | +/// A structure representing a diagnostic message and associated children |
| 32 | +/// messages. |
| 33 | +#[unstable(feature = "proc_macro", issue = "38356")] |
| 34 | +#[derive(Clone, Debug)] |
| 35 | +pub struct Diagnostic { |
| 36 | + level: Level, |
| 37 | + message: String, |
| 38 | + span: Option<Span>, |
| 39 | + children: Vec<Diagnostic> |
| 40 | +} |
| 41 | + |
| 42 | +macro_rules! diagnostic_child_methods { |
| 43 | + ($spanned:ident, $regular:ident, $level:expr) => ( |
| 44 | + /// Add a new child diagnostic message to `self` with the level |
| 45 | + /// identified by this methods name with the given `span` and `message`. |
| 46 | + #[unstable(feature = "proc_macro", issue = "38356")] |
| 47 | + pub fn $spanned<T: Into<String>>(mut self, span: Span, message: T) -> Diagnostic { |
| 48 | + self.children.push(Diagnostic::spanned(span, $level, message)); |
| 49 | + self |
| 50 | + } |
| 51 | + |
| 52 | + /// Add a new child diagnostic message to `self` with the level |
| 53 | + /// identified by this method's name with the given `message`. |
| 54 | + #[unstable(feature = "proc_macro", issue = "38356")] |
| 55 | + pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic { |
| 56 | + self.children.push(Diagnostic::new($level, message)); |
| 57 | + self |
| 58 | + } |
| 59 | + ) |
| 60 | +} |
| 61 | + |
| 62 | +impl Diagnostic { |
| 63 | + /// Create a new diagnostic with the given `level` and `message`. |
| 64 | + #[unstable(feature = "proc_macro", issue = "38356")] |
| 65 | + pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic { |
| 66 | + Diagnostic { |
| 67 | + level: level, |
| 68 | + message: message.into(), |
| 69 | + span: None, |
| 70 | + children: vec![] |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// Create a new diagnostic with the given `level` and `message` pointing to |
| 75 | + /// the given `span`. |
| 76 | + #[unstable(feature = "proc_macro", issue = "38356")] |
| 77 | + pub fn spanned<T: Into<String>>(span: Span, level: Level, message: T) -> Diagnostic { |
| 78 | + Diagnostic { |
| 79 | + level: level, |
| 80 | + message: message.into(), |
| 81 | + span: Some(span), |
| 82 | + children: vec![] |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + diagnostic_child_methods!(span_error, error, Level::Error); |
| 87 | + diagnostic_child_methods!(span_warning, warning, Level::Warning); |
| 88 | + diagnostic_child_methods!(span_note, note, Level::Note); |
| 89 | + diagnostic_child_methods!(span_help, help, Level::Help); |
| 90 | + |
| 91 | + /// Returns the diagnostic `level` for `self`. |
| 92 | + #[unstable(feature = "proc_macro", issue = "38356")] |
| 93 | + pub fn level(&self) -> Level { |
| 94 | + self.level |
| 95 | + } |
| 96 | + |
| 97 | + /// Emit the diagnostic. |
| 98 | + #[unstable(feature = "proc_macro", issue = "38356")] |
| 99 | + pub fn emit(self) { |
| 100 | + ::__internal::with_sess(move |(sess, _)| { |
| 101 | + let handler = &sess.span_diagnostic; |
| 102 | + let level = __internal::level_to_internal_level(self.level); |
| 103 | + let mut diag = rustc::DiagnosticBuilder::new(handler, level, &*self.message); |
| 104 | + |
| 105 | + if let Some(span) = self.span { |
| 106 | + diag.set_span(span.0); |
| 107 | + } |
| 108 | + |
| 109 | + for child in self.children { |
| 110 | + let span = child.span.map(|s| s.0); |
| 111 | + let level = __internal::level_to_internal_level(child.level); |
| 112 | + diag.sub(level, &*child.message, span); |
| 113 | + } |
| 114 | + |
| 115 | + diag.emit(); |
| 116 | + }); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +#[unstable(feature = "proc_macro_internals", issue = "27812")] |
| 121 | +#[doc(hidden)] |
| 122 | +pub mod __internal { |
| 123 | + use super::{Level, rustc}; |
| 124 | + |
| 125 | + pub fn level_to_internal_level(level: Level) -> rustc::Level { |
| 126 | + match level { |
| 127 | + Level::Error => rustc::Level::Error, |
| 128 | + Level::Warning => rustc::Level::Warning, |
| 129 | + Level::Note => rustc::Level::Note, |
| 130 | + Level::Help => rustc::Level::Help, |
| 131 | + Level::__Nonexhaustive => unreachable!("Level::__Nonexhaustive") |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments