From f27fe97089b5c124dae4afbbbfeb66a3b44579d5 Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Thu, 14 Nov 2019 08:02:21 -0500 Subject: Warn rather than panic on unknown namespace prefix When a #defined token was used before a namespace, like so (#1676): #define nssv_inline_ns inline nssv_inline_ns namespace literals {} bindgen would crash when encountering the unknown token preceding the namespace token. This is because we don't get to see "past" the ifdef to the underlying token. The true fix to this is to find a way to extract ifdef info through clang, but for the time being we simply change the panic into a warning when such a token is encountered, and then proceed as if it were empty. Fixes #1676. --- src/ir/context.rs | 23 +++++++++++++++++++--- .../tests/issue-1676-macro-namespace-prefix.rs | 8 ++++++++ .../headers/issue-1676-macro-namespace-prefix.hpp | 2 ++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/expectations/tests/issue-1676-macro-namespace-prefix.rs create mode 100644 tests/headers/issue-1676-macro-namespace-prefix.hpp diff --git a/src/ir/context.rs b/src/ir/context.rs index 127c0a24..384edb95 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -2166,10 +2166,27 @@ If you encounter an error missing from this list, please file an issue or a PR!" } break; } - _ => { + spelling if !found_namespace_keyword => { + // This is _likely_, but not certainly, a macro that's been placed just before + // the namespace keyword. Unfortunately, clang tokens don't let us easily see + // through the ifdef tokens, so we don't know what this token should really be. + // Instead of panicking though, we warn the user that we assumed the token was + // blank, and then move on. + // + // See also https://github.com/rust-lang/rust-bindgen/issues/1676. + warn!( + "Ignored unknown namespace prefix '{}' at {:?} in {:?}", + String::from_utf8_lossy(spelling), + token, + cursor + ); + } + spelling => { panic!( - "Unknown token while processing namespace: {:?}", - token + "Unknown token '{}' while processing namespace at {:?} in {:?}", + String::from_utf8_lossy(spelling), + token, + cursor ); } } diff --git a/tests/expectations/tests/issue-1676-macro-namespace-prefix.rs b/tests/expectations/tests/issue-1676-macro-namespace-prefix.rs new file mode 100644 index 00000000..d6776794 --- /dev/null +++ b/tests/expectations/tests/issue-1676-macro-namespace-prefix.rs @@ -0,0 +1,8 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] diff --git a/tests/headers/issue-1676-macro-namespace-prefix.hpp b/tests/headers/issue-1676-macro-namespace-prefix.hpp new file mode 100644 index 00000000..297927b6 --- /dev/null +++ b/tests/headers/issue-1676-macro-namespace-prefix.hpp @@ -0,0 +1,2 @@ +#define nssv_inline_ns inline +nssv_inline_ns namespace literals {} -- cgit v1.2.3