Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update annotate-snippets to get rid of winapi #2904

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 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.9.1", features = ["color"], 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
80 changes: 19 additions & 61 deletions bindgen/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,9 @@ use std::fmt::Write;
use std::io::{self, BufRead, BufReader};
use std::{borrow::Cow, fs::File};

use annotate_snippets::{
display_list::{DisplayList, FormatOptions},
snippet::{Annotation, Slice as ExtSlice, Snippet},
};

use annotate_snippets::snippet::AnnotationType;

#[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 @@ -78,55 +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,
opt: FormatOptions {
color: true,
..Default::default()
},
};
let dl = DisplayList::from(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
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