Skip to content

Commit 82ffe40

Browse files
zh-jqpvdrz
authored andcommitted
update to use annotate-snippets 0.11.4
- re-export annotate-snippets::Level in diagnostics.rs - require title is always set in Diagnostic
1 parent 2d9273c commit 82ffe40

File tree

8 files changed

+31
-67
lines changed

8 files changed

+31
-67
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ name = "bindgen"
2525
path = "lib.rs"
2626

2727
[dependencies]
28-
annotate-snippets = { version = "0.10.2", optional = true }
28+
annotate-snippets = { version = "0.11.4", optional = true }
2929
bitflags = "2.2.1"
3030
cexpr = "0.6"
3131
clang-sys = { version = "1", features = ["clang_11_0"] }

bindgen/codegen/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4619,11 +4619,11 @@ fn unsupported_abi_diagnostic(
46194619
fn_name,
46204620
error
46214621
),
4622-
Level::Warn,
4622+
Level::Warning,
46234623
)
46244624
.add_annotation(
46254625
"No code will be generated for this function.",
4626-
Level::Warn,
4626+
Level::Warning,
46274627
)
46284628
.add_annotation(
46294629
format!(
@@ -4667,7 +4667,7 @@ fn variadic_fn_diagnostic(
46674667

46684668
let mut diag = Diagnostic::default();
46694669

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

bindgen/diagnostics.rs

+18-54
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,9 @@ use std::fmt::Write;
66
use std::io::{self, BufRead, BufReader};
77
use std::{borrow::Cow, fs::File};
88

9-
use annotate_snippets::{
10-
Annotation, AnnotationType, Renderer, Slice as ExtSlice, Snippet,
11-
};
12-
13-
#[derive(Clone, Copy, Debug)]
14-
pub(crate) enum Level {
15-
Error,
16-
Warn,
17-
Info,
18-
Note,
19-
Help,
20-
}
9+
use annotate_snippets::{Renderer, Snippet};
2110

22-
impl From<Level> for AnnotationType {
23-
fn from(level: Level) -> Self {
24-
match level {
25-
Level::Error => Self::Error,
26-
Level::Warn => Self::Warning,
27-
Level::Info => Self::Info,
28-
Level::Note => Self::Note,
29-
Level::Help => Self::Help,
30-
}
31-
}
32-
}
11+
pub(crate) use annotate_snippets::Level;
3312

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

78-
let mut title = None;
7957
let mut footer = vec![];
8058
let mut slices = vec![];
81-
if let Some((msg, level)) = &self.title {
82-
title = Some(Annotation {
83-
id: Some("bindgen"),
84-
label: Some(msg.as_ref()),
85-
annotation_type: (*level).into(),
86-
})
87-
}
59+
let snippet = if let Some((msg, level)) = &self.title {
60+
(*level).title(msg)
61+
} else {
62+
return;
63+
};
8864

8965
for (msg, level) in &self.footer {
90-
footer.push(Annotation {
91-
id: None,
92-
label: Some(msg.as_ref()),
93-
annotation_type: (*level).into(),
94-
});
66+
footer.push((*level).title(msg));
9567
}
9668

9769
// add additional info that this is generated by bindgen
9870
// so as to not confuse with rustc warnings
99-
footer.push(Annotation {
100-
id: None,
101-
label: Some("This diagnostic was generated by bindgen."),
102-
annotation_type: AnnotationType::Info,
103-
});
71+
footer.push(
72+
Level::Info.title("This diagnostic was generated by bindgen."),
73+
);
10474

10575
for slice in &self.slices {
10676
if let Some(source) = &slice.source {
107-
slices.push(ExtSlice {
108-
source: source.as_ref(),
109-
line_start: slice.line.unwrap_or_default(),
110-
origin: slice.filename.as_deref(),
111-
annotations: vec![],
112-
fold: false,
113-
})
77+
let mut snippet = Snippet::source(source)
78+
.line_start(slice.line.unwrap_or_default());
79+
if let Some(origin) = &slice.filename {
80+
snippet = snippet.origin(origin);
81+
}
82+
slices.push(snippet);
11483
}
11584
}
11685

117-
let snippet = Snippet {
118-
title,
119-
footer,
120-
slices,
121-
};
12286
let renderer = Renderer::styled();
123-
let dl = renderer.render(snippet);
87+
let dl = renderer.render(snippet.snippets(slices).footers(footer));
12488

12589
if INVOKED_BY_BUILD_SCRIPT.with(Clone::clone) {
12690
// This is just a hack which hides the `warning:` added by cargo at the beginning of

bindgen/ir/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3144,7 +3144,7 @@ fn unused_regex_diagnostic(item: &str, name: &str, _ctx: &BindgenContext) {
31443144
Diagnostic::default()
31453145
.with_title(
31463146
format!("Unused regular expression: `{}`.", item),
3147-
Level::Warn,
3147+
Level::Warning,
31483148
)
31493149
.add_annotation(
31503150
format!("This regular expression was passed to `{}`.", name),

bindgen/ir/var.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ fn duplicated_macro_diagnostic(
519519
slice.with_source(source);
520520

521521
Diagnostic::default()
522-
.with_title("Duplicated macro definition.", Level::Warn)
522+
.with_title("Duplicated macro definition.", Level::Warning)
523523
.add_slice(slice)
524524
.add_annotation("This macro had a duplicate.", Level::Note)
525525
.display();

bindgen/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ fn deprecated_target_diagnostic(target: RustTarget, _options: &BindgenOptions) {
596596
let mut diagnostic = Diagnostic::default();
597597
diagnostic.with_title(
598598
format!("The {} Rust target is deprecated.", target),
599-
Level::Warn,
599+
Level::Warning,
600600
);
601601
diagnostic.add_annotation(
602602
"This Rust target was passed to `--rust-target`",
@@ -1057,7 +1057,7 @@ fn rustfmt_non_fatal_error_diagnostic(msg: &str, _options: &BindgenOptions) {
10571057
use crate::diagnostics::{Diagnostic, Level};
10581058

10591059
Diagnostic::default()
1060-
.with_title(msg, Level::Warn)
1060+
.with_title(msg, Level::Warning)
10611061
.add_annotation(
10621062
"The bindings will be generated but not formatted.",
10631063
Level::Note,

bindgen/regex_set.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ fn invalid_regex_warning(
180180

181181
diagnostic.with_title(
182182
"Error while parsing a regular expression.",
183-
Level::Warn,
183+
Level::Warning,
184184
);
185185
} else {
186-
diagnostic.with_title(string, Level::Warn);
186+
diagnostic.with_title(string, Level::Warning);
187187
}
188188
}
189189
err => {
190190
let err = err.to_string();
191-
diagnostic.with_title(err, Level::Warn);
191+
diagnostic.with_title(err, Level::Warning);
192192
}
193193
}
194194

0 commit comments

Comments
 (0)