1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
//! Types and function used to emit pretty diagnostics for `bindgen`.
//!
//! The entry point of this module is the [`Diagnostic`] type.
use std::fmt::Write;
use std::io::{self, BufRead, BufReader};
use std::{borrow::Cow, fs::File};
use annotate_snippets::{Renderer, Snippet};
pub(crate) use annotate_snippets::Level;
/// A `bindgen` diagnostic.
#[derive(Default)]
pub(crate) struct Diagnostic<'a> {
title: Option<(Cow<'a, str>, Level)>,
slices: Vec<Slice<'a>>,
footer: Vec<(Cow<'a, str>, Level)>,
}
impl<'a> Diagnostic<'a> {
/// Add a title to the diagnostic and set its type.
pub(crate) fn with_title(
&mut self,
title: impl Into<Cow<'a, str>>,
level: Level,
) -> &mut Self {
self.title = Some((title.into(), level));
self
}
/// Add a slice of source code to the diagnostic.
pub(crate) fn add_slice(&mut self, slice: Slice<'a>) -> &mut Self {
self.slices.push(slice);
self
}
/// Add a footer annotation to the diagnostic. This annotation will have its own type.
pub(crate) fn add_annotation(
&mut self,
msg: impl Into<Cow<'a, str>>,
level: Level,
) -> &mut Self {
self.footer.push((msg.into(), level));
self
}
/// Print this diagnostic.
///
/// The diagnostic is printed using `cargo:warning` if `bindgen` is being invoked by a build
/// script or using `eprintln` otherwise.
pub(crate) fn display(&self) {
std::thread_local! {
static INVOKED_BY_BUILD_SCRIPT: bool = std::env::var_os("CARGO_CFG_TARGET_ARCH").is_some();
}
let mut footer = vec![];
let mut slices = vec![];
let snippet = if let Some((msg, level)) = &self.title {
(*level).title(msg)
} else {
return;
};
for (msg, level) in &self.footer {
footer.push((*level).title(msg));
}
// add additional info that this is generated by bindgen
// so as to not confuse with rustc warnings
footer.push(
Level::Info.title("This diagnostic was generated by bindgen."),
);
for slice in &self.slices {
if let Some(source) = &slice.source {
let mut snippet = Snippet::source(source)
.line_start(slice.line.unwrap_or_default());
if let Some(origin) = &slice.filename {
snippet = snippet.origin(origin);
}
slices.push(snippet);
}
}
let renderer = Renderer::styled();
let dl = renderer.render(snippet.snippets(slices).footers(footer));
if INVOKED_BY_BUILD_SCRIPT.with(Clone::clone) {
// This is just a hack which hides the `warning:` added by cargo at the beginning of
// every line. This should be fine as our diagnostics already have a colorful title.
// FIXME (pvdrz): Could it be that this doesn't work in other languages?
let hide_warning = "\r \r";
let string = dl.to_string();
for line in string.lines() {
println!("cargo:warning={hide_warning}{line}");
}
} else {
eprintln!("{dl}\n");
}
}
}
/// A slice of source code.
#[derive(Default)]
pub(crate) struct Slice<'a> {
source: Option<Cow<'a, str>>,
filename: Option<String>,
line: Option<usize>,
}
impl<'a> Slice<'a> {
/// Set the source code.
pub(crate) fn with_source(
&mut self,
source: impl Into<Cow<'a, str>>,
) -> &mut Self {
self.source = Some(source.into());
self
}
/// Set the file, line and column.
pub(crate) fn with_location(
&mut self,
mut name: String,
line: usize,
col: usize,
) -> &mut Self {
write!(name, ":{line}:{col}").expect("Writing to a string cannot fail");
self.filename = Some(name);
self.line = Some(line);
self
}
}
pub(crate) fn get_line(
filename: &str,
line: usize,
) -> io::Result<Option<String>> {
let file = BufReader::new(File::open(filename)?);
if let Some(line) = file.lines().nth(line.wrapping_sub(1)) {
return line.map(Some);
}
Ok(None)
}
|