55
66use crate :: parser;
77use clap:: ArgMatches ;
8+ use rustc_errors:: {
9+ emitter:: Emitter , emitter:: HumanReadableErrorType , fallback_fluent_bundle, json:: JsonEmitter ,
10+ ColorConfig , Diagnostic ,
11+ } ;
812use std:: panic;
913use std:: str:: FromStr ;
1014use std:: sync:: LazyLock ;
@@ -18,7 +22,7 @@ const LOG_ENV_VAR: &str = "KANI_LOG";
1822const BUG_REPORT_URL : & str =
1923 "https://github.com/model-checking/kani/issues/new?labels=bug&template=bug_report.md" ;
2024
21- // Custom panic hook.
25+ // Custom panic hook when running under user friendly message format .
2226#[ allow( clippy:: type_complexity) ]
2327static PANIC_HOOK : LazyLock < Box < dyn Fn ( & panic:: PanicInfo < ' _ > ) + Sync + Send + ' static > > =
2428 LazyLock :: new ( || {
@@ -30,9 +34,33 @@ static PANIC_HOOK: LazyLock<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 's
3034
3135 // Print the Kani message
3236 eprintln ! ( "Kani unexpectedly panicked during compilation." ) ;
33- eprintln ! (
34- "If you are seeing this message, please file an issue here: {BUG_REPORT_URL}"
37+ eprintln ! ( "Please file an issue here: {BUG_REPORT_URL}" ) ;
38+ } ) ) ;
39+ hook
40+ } ) ;
41+
42+ // Custom panic hook when executing under json error format `--error-format=json`.
43+ #[ allow( clippy:: type_complexity) ]
44+ static JSON_PANIC_HOOK : LazyLock < Box < dyn Fn ( & panic:: PanicInfo < ' _ > ) + Sync + Send + ' static > > =
45+ LazyLock :: new ( || {
46+ let hook = panic:: take_hook ( ) ;
47+ panic:: set_hook ( Box :: new ( |info| {
48+ // Print stack trace.
49+ let msg = format ! ( "Kani unexpectedly panicked at {info}." , ) ;
50+ let fallback_bundle =
51+ fallback_fluent_bundle ( rustc_errors:: DEFAULT_LOCALE_RESOURCES , false ) ;
52+ let mut emitter = JsonEmitter :: basic (
53+ false ,
54+ HumanReadableErrorType :: Default ( ColorConfig :: Never ) ,
55+ None ,
56+ fallback_bundle,
57+ None ,
58+ false ,
59+ false ,
3560 ) ;
61+ let diagnostic = Diagnostic :: new ( rustc_errors:: Level :: Bug , msg) ;
62+ emitter. emit_diagnostic ( & diagnostic) ;
63+ ( * JSON_PANIC_HOOK ) ( info) ;
3664 } ) ) ;
3765 hook
3866 } ) ;
@@ -94,3 +122,8 @@ fn init_panic_hook() {
94122 // Install panic hook
95123 LazyLock :: force ( & PANIC_HOOK ) ; // Install ice hook
96124}
125+
126+ pub fn json_panic_hook ( ) {
127+ // Install panic hook
128+ LazyLock :: force ( & JSON_PANIC_HOOK ) ; // Install ice hook
129+ }
0 commit comments