Skip to content

Commit

Permalink
update to use annotate-snippets 0.11.4
Browse files Browse the repository at this point in the history
- re-export annotate-snippets::Level in diagnostics.rs
- require title is always set in Diagnostic
  • Loading branch information
zh-jq authored and pvdrz committed Sep 3, 2024
1 parent 2d9273c commit 82ffe40
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 67 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ name = "bindgen"
path = "lib.rs"

[dependencies]
annotate-snippets = { version = "0.10.2", optional = true }
annotate-snippets = { version = "0.11.4", optional = true }
bitflags = "2.2.1"
cexpr = "0.6"
clang-sys = { version = "1", features = ["clang_11_0"] }
Expand Down
6 changes: 3 additions & 3 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4619,11 +4619,11 @@ fn unsupported_abi_diagnostic(
fn_name,
error
),
Level::Warn,
Level::Warning,
)
.add_annotation(
"No code will be generated for this function.",
Level::Warn,
Level::Warning,
)
.add_annotation(
format!(
Expand Down Expand Up @@ -4667,7 +4667,7 @@ fn variadic_fn_diagnostic(

let mut diag = Diagnostic::default();

diag.with_title(format!("Cannot generate wrapper for the static function `{}`.", fn_name), Level::Warn)
diag.with_title(format!("Cannot generate wrapper for the static function `{}`.", fn_name), Level::Warning)
.add_annotation("The `--wrap-static-fns` feature does not support variadic functions.", Level::Note)
.add_annotation("No code will be generated for this function.", Level::Note);

Expand Down
72 changes: 18 additions & 54 deletions bindgen/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,9 @@ use std::fmt::Write;
use std::io::{self, BufRead, BufReader};
use std::{borrow::Cow, fs::File};

use annotate_snippets::{
Annotation, AnnotationType, Renderer, Slice as ExtSlice, Snippet,
};

#[derive(Clone, Copy, Debug)]
pub(crate) enum Level {
Error,
Warn,
Info,
Note,
Help,
}
use annotate_snippets::{Renderer, Snippet};

impl From<Level> for AnnotationType {
fn from(level: Level) -> Self {
match level {
Level::Error => Self::Error,
Level::Warn => Self::Warning,
Level::Info => Self::Info,
Level::Note => Self::Note,
Level::Help => Self::Help,
}
}
}
pub(crate) use annotate_snippets::Level;

/// A `bindgen` diagnostic.
#[derive(Default)]
Expand Down Expand Up @@ -75,52 +54,37 @@ impl<'a> Diagnostic<'a> {
static INVOKED_BY_BUILD_SCRIPT: bool = std::env::var_os("CARGO_CFG_TARGET_ARCH").is_some();
}

let mut title = None;
let mut footer = vec![];
let mut slices = vec![];
if let Some((msg, level)) = &self.title {
title = Some(Annotation {
id: Some("bindgen"),
label: Some(msg.as_ref()),
annotation_type: (*level).into(),
})
}
let snippet = if let Some((msg, level)) = &self.title {
(*level).title(msg)
} else {
return;
};

for (msg, level) in &self.footer {
footer.push(Annotation {
id: None,
label: Some(msg.as_ref()),
annotation_type: (*level).into(),
});
footer.push((*level).title(msg));
}

// add additional info that this is generated by bindgen
// so as to not confuse with rustc warnings
footer.push(Annotation {
id: None,
label: Some("This diagnostic was generated by bindgen."),
annotation_type: AnnotationType::Info,
});
footer.push(
Level::Info.title("This diagnostic was generated by bindgen."),
);

for slice in &self.slices {
if let Some(source) = &slice.source {
slices.push(ExtSlice {
source: source.as_ref(),
line_start: slice.line.unwrap_or_default(),
origin: slice.filename.as_deref(),
annotations: vec![],
fold: false,
})
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 snippet = Snippet {
title,
footer,
slices,
};
let renderer = Renderer::styled();
let dl = renderer.render(snippet);
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
Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3144,7 +3144,7 @@ fn unused_regex_diagnostic(item: &str, name: &str, _ctx: &BindgenContext) {
Diagnostic::default()
.with_title(
format!("Unused regular expression: `{}`.", item),
Level::Warn,
Level::Warning,
)
.add_annotation(
format!("This regular expression was passed to `{}`.", name),
Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ fn duplicated_macro_diagnostic(
slice.with_source(source);

Diagnostic::default()
.with_title("Duplicated macro definition.", Level::Warn)
.with_title("Duplicated macro definition.", Level::Warning)
.add_slice(slice)
.add_annotation("This macro had a duplicate.", Level::Note)
.display();
Expand Down
4 changes: 2 additions & 2 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ fn deprecated_target_diagnostic(target: RustTarget, _options: &BindgenOptions) {
let mut diagnostic = Diagnostic::default();
diagnostic.with_title(
format!("The {} Rust target is deprecated.", target),
Level::Warn,
Level::Warning,
);
diagnostic.add_annotation(
"This Rust target was passed to `--rust-target`",
Expand Down Expand Up @@ -1057,7 +1057,7 @@ fn rustfmt_non_fatal_error_diagnostic(msg: &str, _options: &BindgenOptions) {
use crate::diagnostics::{Diagnostic, Level};

Diagnostic::default()
.with_title(msg, Level::Warn)
.with_title(msg, Level::Warning)
.add_annotation(
"The bindings will be generated but not formatted.",
Level::Note,
Expand Down
6 changes: 3 additions & 3 deletions bindgen/regex_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ fn invalid_regex_warning(

diagnostic.with_title(
"Error while parsing a regular expression.",
Level::Warn,
Level::Warning,
);
} else {
diagnostic.with_title(string, Level::Warn);
diagnostic.with_title(string, Level::Warning);
}
}
err => {
let err = err.to_string();
diagnostic.with_title(err, Level::Warn);
diagnostic.with_title(err, Level::Warning);
}
}

Expand Down

0 comments on commit 82ffe40

Please sign in to comment.